Bug 1550009 - Make HashTable call templated alloc/free methods with the same type r=froydnj?
authorJon Coppeard <jcoppeard@mozilla.com>
Wed, 08 May 2019 16:42:06 +0000
changeset 531908 685e0cd74c2e8854e87ad8b0555a38a0f05bd605
parent 531907 8a953fd10269eda5ba13be8579ea49b9e157be8f
child 531909 6986b4b5c3db2c68d9b8d694d942c01a265a1afd
push id11265
push userffxbld-merge
push dateMon, 13 May 2019 10:53:39 +0000
treeherdermozilla-beta@77e0fe8dbdd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1550009
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1550009 - Make HashTable call templated alloc/free methods with the same type r=froydnj? Differential Revision: https://phabricator.services.mozilla.com/D30371
mfbt/HashTable.h
--- a/mfbt/HashTable.h
+++ b/mfbt/HashTable.h
@@ -1605,23 +1605,24 @@ class HashTable : private AllocPolicy {
     if (!isLiveHash(keyHash)) {
       keyHash -= (sRemovedKey + 1);
     }
     return keyHash & ~sCollisionBit;
   }
 
   enum FailureBehavior { DontReportFailure = false, ReportFailure = true };
 
+  // Fake a struct that we're going to alloc. See the comments in
+  // HashTableEntry about how the table is laid out, and why it's safe.
+  struct FakeSlot {
+    unsigned char c[sizeof(HashNumber) + sizeof(typename Entry::NonConstT)];
+  };
+
   static char* createTable(AllocPolicy& aAllocPolicy, uint32_t aCapacity,
                            FailureBehavior aReportFailure = ReportFailure) {
-    // Fake a struct that we're going to alloc. See the comments in
-    // HashTableEntry about how the table is laid out, and why it's safe.
-    struct FakeSlot {
-      unsigned char c[sizeof(HashNumber) + sizeof(typename Entry::NonConstT)];
-    };
 
     FakeSlot* fake =
         aReportFailure
             ? aAllocPolicy.template pod_malloc<FakeSlot>(aCapacity)
             : aAllocPolicy.template maybe_pod_malloc<FakeSlot>(aCapacity);
     char* table = reinterpret_cast<char*>(fake);
     if (table) {
       forEachSlot(table, aCapacity, [&](Slot& slot) {
@@ -1634,17 +1635,23 @@ class HashTable : private AllocPolicy {
 
   static void destroyTable(AllocPolicy& aAllocPolicy, char* aOldTable,
                            uint32_t aCapacity) {
     forEachSlot(aOldTable, aCapacity, [&](const Slot& slot) {
       if (slot.isLive()) {
         slot.toEntry()->destroyStoredT();
       }
     });
-    aAllocPolicy.free_(aOldTable, aCapacity);
+    freeTable(aAllocPolicy, aOldTable, aCapacity);
+  }
+
+  static void freeTable(AllocPolicy& aAllocPolicy, char* aOldTable,
+                        uint32_t aCapacity) {
+    FakeSlot* fake = reinterpret_cast<FakeSlot*>(aOldTable);
+    aAllocPolicy.free_(fake, aCapacity);
   }
 
  public:
   HashTable(AllocPolicy aAllocPolicy, uint32_t aLen)
       : AllocPolicy(aAllocPolicy),
         mGen(0),
         mHashShift(hashShift(aLen)),
         mTable(nullptr),
@@ -1820,17 +1827,17 @@ class HashTable : private AllocPolicy {
         findNonLiveSlot(hn).setLive(
             hn, std::move(const_cast<typename Entry::NonConstT&>(slot.get())));
       }
 
       slot.clear();
     });
 
     // All entries have been destroyed, no need to destroyTable.
-    this->free_(oldTable, oldCapacity);
+    freeTable(*this, oldTable, oldCapacity);
     return Rehashed;
   }
 
   RebuildStatus rehashIfOverloaded(
       FailureBehavior aReportFailure = ReportFailure) {
     static_assert(sMaxCapacity <= UINT32_MAX / sMaxAlphaNumerator,
                   "multiplication below could overflow");
 
@@ -1960,17 +1967,17 @@ class HashTable : private AllocPolicy {
   }
 
   // Resize the table down to the smallest capacity that doesn't overload the
   // table. Since we call shrinkIfUnderloaded() on every remove, you only need
   // to call this after a bulk removal of items done without calling remove().
   void compact() {
     if (empty()) {
       // Free the entry storage.
-      this->free_(mTable, capacity());
+      freeTable(*this, mTable, capacity());
       mGen++;
       mHashShift = hashShift(0);  // gives minimum capacity on regrowth
       mTable = nullptr;
       mRemovedCount = 0;
       return;
     }
 
     uint32_t bestCapacity = this->bestCapacity(mEntryCount);