Bug 1477579: Part 1 - Use literal strings for statically registered contract ID keys. r?froydnj draft
authorKris Maglione <maglione.k@gmail.com>
Sun, 22 Jul 2018 10:54:56 -0700
changeset 821291 0e901ce6af2a825f56ff714bc3340b15ac612fff
parent 821284 b9d40a7b26f470ae24b843a1e8e5edadd321d070
child 821292 055b89d30b88312c0f50e771b46b35c1774ccc8f
push id117051
push usermaglione.k@gmail.com
push dateSun, 22 Jul 2018 18:01:56 +0000
reviewersfroydnj
bugs1477579
milestone63.0a1
Bug 1477579: Part 1 - Use literal strings for statically registered contract ID keys. r?froydnj Most of our components are static, and registered using literal C strings. For those components, we currently use a nsDependentCString as a key when creating a hash entry, which leads to an unnecessary duplication. Using literal CStrings instead avoids the duplication. MozReview-Commit-ID: 5DOUF8ZQMlh
xpcom/components/nsComponentManager.cpp
--- a/xpcom/components/nsComponentManager.cpp
+++ b/xpcom/components/nsComponentManager.cpp
@@ -52,16 +52,20 @@
 
 #include <new>     // for placement new
 
 #include "mozilla/Omnijar.h"
 
 #include "mozilla/Logging.h"
 #include "LogModulePrefWatcher.h"
 
+#ifdef MOZ_MEMORY
+#include "mozmemory.h"
+#endif
+
 using namespace mozilla;
 
 static LazyLogModule nsComponentManagerLog("nsComponentManager");
 
 #if 0
  #define SHOW_DENIED_ON_SHUTDOWN
  #define SHOW_CI_ON_EXISTING_SERVICE
 #endif
@@ -463,16 +467,49 @@ ProcessSelectorMatches(Module::ProcessSe
   if (aSelector & Module::CONTENT_PROCESS_ONLY) {
     return type == GeckoProcessType_Content;
   }
   return true;
 }
 
 static const int kModuleVersionWithSelector = 51;
 
+template<typename T>
+static void
+AssertNotMallocAllocated(T* aPtr)
+{
+#if defined(DEBUG) && defined(MOZ_MEMORY)
+  jemalloc_ptr_info_t info;
+  jemalloc_ptr_info((void*)aPtr, &info);
+  MOZ_ASSERT(info.tag == TagUnknown);
+#endif
+}
+
+template<typename T>
+static void
+AssertNotStackAllocated(T* aPtr)
+{
+  // The main thread's stack should be allocated at the top of our address
+  // space. Anything stack allocated should be above us on the stack, and
+  // therefore above our first argument pointer.
+  MOZ_ASSERT(NS_IsMainThread());
+  MOZ_ASSERT((char*)aPtr < (char*)&aPtr);
+}
+
+static inline nsCString
+AsLiteralCString(const char* aStr)
+{
+  AssertNotMallocAllocated(aStr);
+  AssertNotStackAllocated(aStr);
+
+  nsCString str;
+  str.AssignLiteral(aStr, strlen(aStr));
+  return str;
+}
+
 void
 nsComponentManagerImpl::RegisterModule(const mozilla::Module* aModule,
                                        FileLocation* aFile)
 {
   mLock.AssertNotCurrentThreadOwns();
 
   if (aModule->mVersion >= kModuleVersionWithSelector &&
       !ProcessSelectorMatches(aModule->selector))
@@ -578,17 +615,17 @@ nsComponentManagerImpl::RegisterContract
     SafeMutexAutoUnlock unlock(mLock);
     LogMessage("Could not map contract ID '%s' to CID %s because no implementation of the CID is registered.",
                aEntry->contractid,
                idstr);
 
     return;
   }
 
-  mContractIDs.Put(nsDependentCString(aEntry->contractid), f);
+  mContractIDs.Put(AsLiteralCString(aEntry->contractid), f);
 }
 
 static void
 CutExtension(nsCString& aPath)
 {
   int32_t dotPos = aPath.RFindChar('.');
   if (kNotFound == dotPos) {
     aPath.Truncate();