Bug 1415799 - Inline and remove PREF_Init() and PREF_CleanupPrefs(). r=glandium draft
authorNicholas Nethercote <nnethercote@mozilla.com>
Thu, 09 Nov 2017 17:58:35 +1100
changeset 695471 6936c54990bd01388508c429186aa6d3e16ee63e
parent 695470 cdc081daa3eda689c7f0cc2a02587b858167dbef
child 695472 21d2abca1c7720d55d02d8bff7dfe8e6608ecdcb
push id88417
push usernnethercote@mozilla.com
push dateThu, 09 Nov 2017 07:02:38 +0000
reviewersglandium
bugs1415799
milestone58.0a1
Bug 1415799 - Inline and remove PREF_Init() and PREF_CleanupPrefs(). r=glandium They both have two callsites, but in one of those they appear together, where much of the code can be replaced with a ClearAndPrepareForLength() call. MozReview-Commit-ID: 1A771gsHWan
modules/libpref/Preferences.cpp
--- a/modules/libpref/Preferences.cpp
+++ b/modules/libpref/Preferences.cpp
@@ -362,38 +362,16 @@ enum
 static nsresult
 pref_HashPref(const char* aKey,
               PrefValue aValue,
               PrefType aType,
               uint32_t aFlags);
 
 #define PREF_HASHTABLE_INITIAL_LENGTH 1024
 
-// The Init function initializes the preference context and creates the
-// preference hashtable.
-static void
-PREF_Init()
-{
-  if (!gHashTable) {
-    gHashTable = new PLDHashTable(
-      &pref_HashTableOps, sizeof(PrefHashEntry), PREF_HASHTABLE_INITIAL_LENGTH);
-  }
-}
-
-// Frees up all the objects except the callback list.
-static void
-PREF_CleanupPrefs()
-{
-  if (gHashTable) {
-    delete gHashTable;
-    gHashTable = nullptr;
-    gPrefNameArena.Clear();
-  }
-}
-
 // Assign to aResult a quoted, escaped copy of aOriginal.
 static void
 StrEscape(const char* aOriginal, nsCString& aResult)
 {
   if (aOriginal == nullptr) {
     aResult.AssignLiteral("\"\"");
     return;
   }
@@ -3669,17 +3647,19 @@ Preferences::~Preferences()
   while (node) {
     CallbackNode* next_node = node->mNext;
     free(const_cast<char*>(node->mDomain));
     free(node);
     node = next_node;
   }
   gLastPriorityNode = gFirstCallback = nullptr;
 
-  PREF_CleanupPrefs();
+  delete gHashTable;
+  gHashTable = nullptr;
+  gPrefNameArena.Clear();
 }
 
 //
 // nsISupports Implementation
 //
 
 NS_IMPL_ADDREF(Preferences)
 NS_IMPL_RELEASE(Preferences)
@@ -3702,17 +3682,19 @@ static InfallibleTArray<Preferences::Pre
 Preferences::SetInitPreferences(nsTArray<PrefSetting>* aPrefs)
 {
   gInitPrefs = new InfallibleTArray<PrefSetting>(mozilla::Move(*aPrefs));
 }
 
 Result<Ok, const char*>
 Preferences::Init()
 {
-  PREF_Init();
+  MOZ_ASSERT(!gHashTable);
+  gHashTable = new PLDHashTable(
+    &pref_HashTableOps, sizeof(PrefHashEntry), PREF_HASHTABLE_INITIAL_LENGTH);
 
   MOZ_TRY(pref_InitInitialObjects());
 
   if (XRE_IsContentProcess()) {
     MOZ_ASSERT(gInitPrefs);
     for (unsigned int i = 0; i < gInitPrefs->Length(); i++) {
       Preferences::SetPreference(gInitPrefs->ElementAt(i));
     }
@@ -3844,19 +3826,19 @@ NS_IMETHODIMP
 Preferences::ResetPrefs()
 {
   if (MOZ_UNLIKELY(!XRE_IsParentProcess())) {
     NS_ERROR("must reset prefs from parent process");
     return NS_ERROR_NOT_AVAILABLE;
   }
 
   NotifyServiceObservers(NS_PREFSERVICE_RESET_TOPIC_ID);
-  PREF_CleanupPrefs();
-
-  PREF_Init();
+
+  gHashTable->ClearAndPrepareForLength(PREF_HASHTABLE_INITIAL_LENGTH);
+  gPrefNameArena.Clear();
 
   return pref_InitInitialObjects().isOk() ? NS_OK : NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP
 Preferences::ResetUserPrefs()
 {
   if (MOZ_UNLIKELY(!XRE_IsParentProcess())) {