Bug 1471132 - Change how static xpcom components are initialized. r?froydnj draft
authorMike Hommey <mh+mozilla@glandium.org>
Tue, 26 Jun 2018 16:10:30 +0900
changeset 810593 5f1d71046c36f44f441aff6e3cb52896a72df1a2
parent 810592 f2109d8f3747b6f0e21c46ccb013213d8d1b8cd1
child 810594 d22d84880927dfd328d1e2d829c8e7e624587bec
push id114044
push userbmo:mh+mozilla@glandium.org
push dateTue, 26 Jun 2018 07:19:25 +0000
reviewersfroydnj
bugs1471132
milestone63.0a1
Bug 1471132 - Change how static xpcom components are initialized. r?froydnj The sStaticModules list is, practically speaking, a copy of the list of components we already have in libxul, augmented at runtime with a few other components for tests (for gtest and xpcshell). We don't actually need to keep that copy in memory. We can instead just use the pointers in libxul directly to register them to the component manager, and use a separate list, only for those extra components when they need to be registered.
xpcom/components/nsComponentManager.cpp
xpcom/components/nsComponentManager.h
--- a/xpcom/components/nsComponentManager.cpp
+++ b/xpcom/components/nsComponentManager.cpp
@@ -240,17 +240,17 @@ static const int CONTRACTID_HASHTABLE_IN
 nsComponentManagerImpl::nsComponentManagerImpl()
   : mFactories(CONTRACTID_HASHTABLE_INITIAL_LENGTH)
   , mContractIDs(CONTRACTID_HASHTABLE_INITIAL_LENGTH)
   , mLock("nsComponentManagerImpl.mLock")
   , mStatus(NOT_INITIALIZED)
 {
 }
 
-nsTArray<const mozilla::Module*>* nsComponentManagerImpl::sStaticModules;
+static nsTArray<const mozilla::Module*>* sExtraStaticModules;
 
 /* NSMODULE_DEFN places NSModules in specific sections, as per Module.h.
  * The linker will group them all together, and we use tricks below to
  * find the start and end of the grouped list of NSModules.
  *
  * On Windows, all the symbols in the .kPStaticModules* sections are
  * grouped together, by lexical order of the section names. The NSModules
  * themselves are in .kPStaticModules$M. We use the section name
@@ -321,24 +321,21 @@ mozilla::Module const* const* begin(AllS
 
 mozilla::Module const* const* end(AllStaticModules& _) {
     return &__stop_kPStaticModules;
 }
 
 /* static */ void
 nsComponentManagerImpl::InitializeStaticModules()
 {
-  if (sStaticModules) {
+  if (sExtraStaticModules) {
     return;
   }
 
-  sStaticModules = new nsTArray<const mozilla::Module*>;
-  for (auto module : AllStaticModules()) {
-    sStaticModules->AppendElement(module);
-  }
+  sExtraStaticModules = new nsTArray<const mozilla::Module*>;
 }
 
 nsTArray<nsComponentManagerImpl::ComponentLocation>*
 nsComponentManagerImpl::sModuleLocations;
 
 /* static */ void
 nsComponentManagerImpl::InitializeModuleLocations()
 {
@@ -360,18 +357,22 @@ nsComponentManagerImpl::Init()
     GetLocationFromDirectoryService(NS_XPCOM_CURRENT_PROCESS_DIR);
 
   InitializeStaticModules();
 
   nsCategoryManager::GetSingleton()->SuppressNotifications(true);
 
   RegisterModule(&kXPCOMModule, nullptr);
 
-  for (uint32_t i = 0; i < sStaticModules->Length(); ++i) {
-    RegisterModule((*sStaticModules)[i], nullptr);
+  for (auto module : AllStaticModules()) {
+    RegisterModule(module, nullptr);
+  }
+
+  for (uint32_t i = 0; i < sExtraStaticModules->Length(); ++i) {
+    RegisterModule((*sExtraStaticModules)[i], nullptr);
   }
 
   bool loadChromeManifests = (XRE_GetProcessType() != GeckoProcessType_GPU);
   if (loadChromeManifests) {
     // The overall order in which chrome.manifests are expected to be treated
     // is the following:
     // - greDir
     // - greDir's omni.ja
@@ -824,17 +825,17 @@ nsresult nsComponentManagerImpl::Shutdow
 
   // Release all cached factories
   mContractIDs.Clear();
   mFactories.Clear(); // XXX release the objects, don't just clear
   mLoaderMap.Clear();
   mKnownModules.Clear();
   mKnownStaticModules.Clear();
 
-  delete sStaticModules;
+  delete sExtraStaticModules;
   delete sModuleLocations;
 
   mStatus = SHUTDOWN_COMPLETE;
 
   MOZ_LOG(nsComponentManagerLog, LogLevel::Debug,
          ("nsComponentManager: Shutdown complete."));
 
   return NS_OK;
@@ -1736,33 +1737,32 @@ nsComponentManagerImpl::SizeOfIncludingT
 
   n += mContractIDs.ShallowSizeOfExcludingThis(aMallocSizeOf);
   for (auto iter = mContractIDs.ConstIter(); !iter.Done(); iter.Next()) {
     // We don't measure the nsFactoryEntry data because it's owned by
     // mFactories (which is measured above).
     n += iter.Key().SizeOfExcludingThisIfUnshared(aMallocSizeOf);
   }
 
-  n += sStaticModules->ShallowSizeOfIncludingThis(aMallocSizeOf);
+  n += sExtraStaticModules->ShallowSizeOfIncludingThis(aMallocSizeOf);
   if (sModuleLocations) {
     n += sModuleLocations->ShallowSizeOfIncludingThis(aMallocSizeOf);
   }
 
   n += mKnownStaticModules.ShallowSizeOfExcludingThis(aMallocSizeOf);
   n += mKnownModules.ShallowSizeOfExcludingThis(aMallocSizeOf);
 
   n += mArena.SizeOfExcludingThis(aMallocSizeOf);
 
   n += mPendingServices.ShallowSizeOfExcludingThis(aMallocSizeOf);
 
   // Measurement of the following members may be added later if DMD finds it is
   // worthwhile:
   // - mLoaderMap's keys and values
   // - mMon
-  // - sStaticModules' entries
   // - sModuleLocations' entries
   // - mKnownStaticModules' entries?
   // - mKnownModules' keys and values?
 
   return n;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -1891,17 +1891,17 @@ NS_GetComponentRegistrar(nsIComponentReg
   NS_ADDREF(*aResult = nsComponentManagerImpl::gComponentManager);
   return NS_OK;
 }
 
 EXPORT_XPCOM_API(nsresult)
 XRE_AddStaticComponent(const mozilla::Module* aComponent)
 {
   nsComponentManagerImpl::InitializeStaticModules();
-  nsComponentManagerImpl::sStaticModules->AppendElement(aComponent);
+  sExtraStaticModules->AppendElement(aComponent);
 
   if (nsComponentManagerImpl::gComponentManager &&
       nsComponentManagerImpl::NORMAL ==
         nsComponentManagerImpl::gComponentManager->mStatus) {
     nsComponentManagerImpl::gComponentManager->RegisterModule(aComponent,
                                                               nullptr);
   }
 
--- a/xpcom/components/nsComponentManager.h
+++ b/xpcom/components/nsComponentManager.h
@@ -185,17 +185,16 @@ public:
   {
   public:
     bool Equals(const ComponentLocation& aA, const ComponentLocation& aB) const
     {
       return (aA.type == aB.type && aA.location.Equals(aB.location));
     }
   };
 
-  static nsTArray<const mozilla::Module*>* sStaticModules;
   static nsTArray<ComponentLocation>* sModuleLocations;
 
   class KnownModule
   {
   public:
     /**
      * Static or binary module.
      */