summaryrefslogtreecommitdiffstats
path: root/xpcom/ds/PLDHashTable.cpp
blob: 75a512ba7ab932e567b62167f2023dfdc3be3672 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <new>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "PLDHashTable.h"
#include "nsDebug.h"
#include "mozilla/HashFunctions.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/OperatorNewExtensions.h"
#include "mozilla/ScopeExit.h"
#include "nsAlgorithm.h"
#include "nsPointerHashKeys.h"
#include "mozilla/Likely.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Maybe.h"
#include "mozilla/ChaosMode.h"

using namespace mozilla;

#ifdef MOZ_HASH_TABLE_CHECKS_ENABLED

class AutoReadOp {
  Checker& mChk;

 public:
  explicit AutoReadOp(Checker& aChk) : mChk(aChk) { mChk.StartReadOp(); }
  ~AutoReadOp() { mChk.EndReadOp(); }
};

class AutoWriteOp {
  Checker& mChk;

 public:
  explicit AutoWriteOp(Checker& aChk) : mChk(aChk) { mChk.StartWriteOp(); }
  ~AutoWriteOp() { mChk.EndWriteOp(); }
};

class AutoIteratorRemovalOp {
  Checker& mChk;

 public:
  explicit AutoIteratorRemovalOp(Checker& aChk) : mChk(aChk) {
    mChk.StartIteratorRemovalOp();
  }
  ~AutoIteratorRemovalOp() { mChk.EndIteratorRemovalOp(); }
};

class AutoDestructorOp {
  Checker& mChk;

 public:
  explicit AutoDestructorOp(Checker& aChk) : mChk(aChk) {
    mChk.StartDestructorOp();
  }
  ~AutoDestructorOp() { mChk.EndDestructorOp(); }
};

#endif

/* static */
PLDHashNumber PLDHashTable::HashStringKey(const void* aKey) {
  return HashString(static_cast<const char*>(aKey));
}

/* static */
PLDHashNumber PLDHashTable::HashVoidPtrKeyStub(const void* aKey) {
  return nsPtrHashKey<void>::HashKey(aKey);
}

/* static */
bool PLDHashTable::MatchEntryStub(const PLDHashEntryHdr* aEntry,
                                  const void* aKey) {
  const PLDHashEntryStub* stub = (const PLDHashEntryStub*)aEntry;

  return stub->key == aKey;
}

/* static */
bool PLDHashTable::MatchStringKey(const PLDHashEntryHdr* aEntry,
                                  const void* aKey) {
  const PLDHashEntryStub* stub = (const PLDHashEntryStub*)aEntry;

  // XXX tolerate null keys on account of sloppy Mozilla callers.
  return stub->key == aKey ||
         (stub->key && aKey &&
          strcmp((const char*)stub->key, (const char*)aKey) == 0);
}

/* static */
void PLDHashTable::MoveEntryStub(PLDHashTable* aTable,
                                 const PLDHashEntryHdr* aFrom,
                                 PLDHashEntryHdr* aTo) {
  memcpy(aTo, aFrom, aTable->mEntrySize);
}

/* static */
void PLDHashTable::ClearEntryStub(PLDHashTable* aTable,
                                  PLDHashEntryHdr* aEntry) {
  memset(aEntry, 0, aTable->mEntrySize);
}

static const PLDHashTableOps gStubOps = {
    PLDHashTable::HashVoidPtrKeyStub, PLDHashTable::MatchEntryStub,
    PLDHashTable::MoveEntryStub, PLDHashTable::ClearEntryStub, nullptr};

/* static */ const PLDHashTableOps* PLDHashTable::StubOps() {
  return &gStubOps;
}

static bool SizeOfEntryStore(uint32_t aCapacity, uint32_t aEntrySize,
                             uint32_t* aNbytes) {
  uint32_t slotSize = aEntrySize + sizeof(PLDHashNumber);
  uint64_t nbytes64 = uint64_t(aCapacity) * uint64_t(slotSize);
  *aNbytes = aCapacity * slotSize;
  return uint64_t(*aNbytes) == nbytes64;  // returns false on overflow
}

// Compute max and min load numbers (entry counts). We have a secondary max
// that allows us to overload a table reasonably if it cannot be grown further
// (i.e. if ChangeTable() fails). The table slows down drastically if the
// secondary max is too close to 1, but 0.96875 gives only a slight slowdown
// while allowing 1.3x more elements.
static inline uint32_t MaxLoad(uint32_t aCapacity) {
  return aCapacity - (aCapacity >> 2);  // == aCapacity * 0.75
}
static inline uint32_t MaxLoadOnGrowthFailure(uint32_t aCapacity) {
  return aCapacity - (aCapacity >> 5);  // == aCapacity * 0.96875
}
static inline uint32_t MinLoad(uint32_t aCapacity) {
  return aCapacity >> 2;  // == aCapacity * 0.25
}

// Compute the minimum capacity (and the Log2 of that capacity) for a table
// containing |aLength| elements while respecting the following contraints:
// - table must be at most 75% full;
// - capacity must be a power of two;
// - capacity cannot be too small.
static inline void BestCapacity(uint32_t aLength, uint32_t* aCapacityOut,
                                uint32_t* aLog2CapacityOut) {
  // Callers should ensure this is true.
  MOZ_ASSERT(aLength <= PLDHashTable::kMaxInitialLength);

  // Compute the smallest capacity allowing |aLength| elements to be inserted
  // without rehashing.
  uint32_t capacity = (aLength * 4 + (3 - 1)) / 3;  // == ceil(aLength * 4 / 3)
  if (capacity < PLDHashTable::kMinCapacity) {
    capacity = PLDHashTable::kMinCapacity;
  }

  // Round up capacity to next power-of-two.
  uint32_t log2 = CeilingLog2(capacity);
  capacity = 1u << log2;
  MOZ_ASSERT(capacity <= PLDHashTable::kMaxCapacity);

  *aCapacityOut = capacity;
  *aLog2CapacityOut = log2;
}

/* static */ MOZ_ALWAYS_INLINE uint32_t
PLDHashTable::HashShift(uint32_t aEntrySize, uint32_t aLength) {
  if (aLength > kMaxInitialLength) {
    MOZ_CRASH("Initial length is too large");
  }

  uint32_t capacity, log2;
  BestCapacity(aLength, &capacity, &log2);

  uint32_t nbytes;
  if (!SizeOfEntryStore(capacity, aEntrySize, &nbytes)) {
    MOZ_CRASH("Initial entry store size is too large");
  }

  // Compute the hashShift value.
  return kPLDHashNumberBits - log2;
}

PLDHashTable::PLDHashTable(const PLDHashTableOps* aOps, uint32_t aEntrySize,
                           uint32_t aLength)
    : mOps(aOps),
      mGeneration(0),
      mHashShift(HashShift(aEntrySize, aLength)),
      mEntrySize(aEntrySize),
      mEntryCount(0),
      mRemovedCount(0) {
  // An entry size greater than 0xff is unlikely, but let's check anyway. If
  // you hit this, your hashtable would waste lots of space for unused entries
  // and you should change your hash table's entries to pointers.
  if (aEntrySize != uint32_t(mEntrySize)) {
    MOZ_CRASH("Entry size is too large");
  }
}

PLDHashTable& PLDHashTable::operator=(PLDHashTable&& aOther) {
  if (this == &aOther) {
    return *this;
  }

  // |mOps| and |mEntrySize| are required to stay the same, they're
  // conceptually part of the type -- indeed, if PLDHashTable was a templated
  // type like nsTHashtable, they *would* be part of the type -- so it only
  // makes sense to assign in cases where they match.
  MOZ_RELEASE_ASSERT(mOps == aOther.mOps || !mOps);
  MOZ_RELEASE_ASSERT(mEntrySize == aOther.mEntrySize || !mEntrySize);

  // Reconstruct |this|.
  const PLDHashTableOps* ops = aOther.mOps;
  this->~PLDHashTable();
  new (KnownNotNull, this) PLDHashTable(ops, aOther.mEntrySize, 0);

  // Move non-const pieces over.
  mHashShift = std::move(aOther.mHashShift);
  mEntryCount = std::move(aOther.mEntryCount);
  mRemovedCount = std::move(aOther.mRemovedCount);
  mEntryStore.Set(aOther.mEntryStore.Get(), &mGeneration);
#ifdef MOZ_HASH_TABLE_CHECKS_ENABLED
  mChecker = std::move(aOther.mChecker);
#endif

  // Clear up |aOther| so its destruction will be a no-op and it reports being
  // empty.
  {
#ifdef MOZ_HASH_TABLE_CHECKS_ENABLED
    AutoDestructorOp op(mChecker);
#endif
    aOther.mEntryCount = 0;
    aOther.mEntryStore.Set(nullptr, &aOther.mGeneration);
  }

  return *this;
}

PLDHashNumber PLDHashTable::Hash1(PLDHashNumber aHash0) const {
  return aHash0 >> mHashShift;
}

void PLDHashTable::Hash2(PLDHashNumber aHash0, uint32_t& aHash2Out,
                         uint32_t& aSizeMaskOut) const {
  uint32_t sizeLog2 = kPLDHashNumberBits - mHashShift;
  uint32_t sizeMask = (PLDHashNumber(1) << sizeLog2) - 1;
  aSizeMaskOut = sizeMask;

  // The incoming aHash0 always has the low bit unset (since we leave it
  // free for the collision flag), and should have reasonably random
  // data in the other 31 bits.  We used the high bits of aHash0 for
  // Hash1, so we use the low bits here.  If the table size is large,
  // the bits we use may overlap, but that's still more random than
  // filling with 0s.
  //
  // Double hashing needs the second hash code to be relatively prime to table
  // size, so we simply make hash2 odd.
  //
  // This also conveniently covers up the fact that we have the low bit
  // unset since aHash0 has the low bit unset.
  aHash2Out = (aHash0 & sizeMask) | 1;
}

// Reserve mKeyHash 0 for free entries and 1 for removed-entry sentinels. Note
// that a removed-entry sentinel need be stored only if the removed entry had
// a colliding entry added after it. Therefore we can use 1 as the collision
// flag in addition to the removed-entry sentinel value. Multiplicative hash
// uses the high order bits of mKeyHash, so this least-significant reservation
// should not hurt the hash function's effectiveness much.

// Match an entry's mKeyHash against an unstored one computed from a key.
/* static */
bool PLDHashTable::MatchSlotKeyhash(Slot& aSlot, const PLDHashNumber aKeyHash) {
  return (aSlot.KeyHash() & ~kCollisionFlag) == aKeyHash;
}

// Compute the address of the indexed entry in table.
auto PLDHashTable::SlotForIndex(uint32_t aIndex) const -> Slot {
  return mEntryStore.SlotForIndex(aIndex, mEntrySize, CapacityFromHashShift());
}

PLDHashTable::~PLDHashTable() {
#ifdef MOZ_HASH_TABLE_CHECKS_ENABLED
  AutoDestructorOp op(mChecker);
#endif

  if (!mEntryStore.IsAllocated()) {
    return;
  }

  // Clear any remaining live entries (if not trivially destructible).
  if (mOps->clearEntry) {
    mEntryStore.ForEachSlot(Capacity(), mEntrySize, [&](const Slot& aSlot) {
      if (aSlot.IsLive()) {
        mOps->clearEntry(this, aSlot.ToEntry());
      }
    });
  }

  // Entry storage is freed last, by ~EntryStore().
}

void PLDHashTable::ClearAndPrepareForLength(uint32_t aLength) {
  // Get these values before the destructor clobbers them.
  const PLDHashTableOps* ops = mOps;
  uint32_t entrySize = mEntrySize;

  this->~PLDHashTable();
  new (KnownNotNull, this) PLDHashTable(ops, entrySize, aLength);
}

void PLDHashTable::Clear() { ClearAndPrepareForLength(kDefaultInitialLength); }

// If |Reason| is |ForAdd|, the return value is always non-null and it may be
// a previously-removed entry. If |Reason| is |ForSearchOrRemove|, the return
// value is null on a miss, and will never be a previously-removed entry on a
// hit. This distinction is a bit grotty but this function is hot enough that
// these differences are worthwhile. (It's also hot enough that
// MOZ_ALWAYS_INLINE makes a significant difference.)
template <PLDHashTable::SearchReason Reason, typename Success, typename Failure>
MOZ_ALWAYS_INLINE auto PLDHashTable::SearchTable(const void* aKey,
                                                 PLDHashNumber aKeyHash,
                                                 Success&& aSuccess,
                                                 Failure&& aFailure) const {
  MOZ_ASSERT(mEntryStore.IsAllocated());
  NS_ASSERTION(!(aKeyHash & kCollisionFlag), "!(aKeyHash & kCollisionFlag)");

  // Compute the primary hash address.
  PLDHashNumber hash1 = Hash1(aKeyHash);
  Slot slot = SlotForIndex(hash1);

  // Miss: return space for a new entry.
  if (slot.IsFree()) {
    return (Reason == ForAdd) ? aSuccess(slot) : aFailure();
  }

  // Hit: return entry.
  PLDHashMatchEntry matchEntry = mOps->matchEntry;
  if (MatchSlotKeyhash(slot, aKeyHash)) {
    PLDHashEntryHdr* e = slot.ToEntry();
    if (matchEntry(e, aKey)) {
      return aSuccess(slot);
    }
  }

  // Collision: double hash.
  PLDHashNumber hash2;
  uint32_t sizeMask;
  Hash2(aKeyHash, hash2, sizeMask);

  // Save the first removed entry slot so Add() can recycle it. (Only used
  // if Reason==ForAdd.)
  Maybe<Slot> firstRemoved;

  for (;;) {
    if (Reason == ForAdd && !firstRemoved) {
      if (MOZ_UNLIKELY(slot.IsRemoved())) {
        firstRemoved.emplace(slot);
      } else {
        slot.MarkColliding();
      }
    }

    hash1 -= hash2;
    hash1 &= sizeMask;

    slot = SlotForIndex(hash1);
    if (slot.IsFree()) {
      if (Reason != ForAdd) {
        return aFailure();
      }
      return aSuccess(firstRemoved.refOr(slot));
    }

    if (MatchSlotKeyhash(slot, aKeyHash)) {
      PLDHashEntryHdr* e = slot.ToEntry();
      if (matchEntry(e, aKey)) {
        return aSuccess(slot);
      }
    }
  }

  // NOTREACHED
  return aFailure();
}

// This is a copy of SearchTable(), used by ChangeTable(), hardcoded to
//   1. assume |Reason| is |ForAdd|,
//   2. assume that |aKey| will never match an existing entry, and
//   3. assume that no entries have been removed from the current table
//      structure.
// Avoiding the need for |aKey| means we can avoid needing a way to map entries
// to keys, which means callers can use complex key types more easily.
MOZ_ALWAYS_INLINE auto PLDHashTable::FindFreeSlot(PLDHashNumber aKeyHash) const
    -> Slot {
  MOZ_ASSERT(mEntryStore.IsAllocated());
  NS_ASSERTION(!(aKeyHash & kCollisionFlag), "!(aKeyHash & kCollisionFlag)");

  // Compute the primary hash address.
  PLDHashNumber hash1 = Hash1(aKeyHash);
  Slot slot = SlotForIndex(hash1);

  // Miss: return space for a new entry.
  if (slot.IsFree()) {
    return slot;
  }

  // Collision: double hash.
  PLDHashNumber hash2;
  uint32_t sizeMask;
  Hash2(aKeyHash, hash2, sizeMask);

  for (;;) {
    MOZ_ASSERT(!slot.IsRemoved());
    slot.MarkColliding();

    hash1 -= hash2;
    hash1 &= sizeMask;

    slot = SlotForIndex(hash1);
    if (slot.IsFree()) {
      return slot;
    }
  }

  // NOTREACHED
}

bool PLDHashTable::ChangeTable(int32_t aDeltaLog2) {
  MOZ_ASSERT(mEntryStore.IsAllocated());

  // Look, but don't touch, until we succeed in getting new entry store.
  int32_t oldLog2 = kPLDHashNumberBits - mHashShift;
  int32_t newLog2 = oldLog2 + aDeltaLog2;
  uint32_t newCapacity = 1u << newLog2;
  if (newCapacity > kMaxCapacity) {
    return false;
  }

  uint32_t nbytes;
  if (!SizeOfEntryStore(newCapacity, mEntrySize, &nbytes)) {
    return false;  // overflowed
  }

  char* newEntryStore = (char*)calloc(1, nbytes);
  if (!newEntryStore) {
    return false;
  }

  // We can't fail from here on, so update table parameters.
  mHashShift = kPLDHashNumberBits - newLog2;
  mRemovedCount = 0;

  // Assign the new entry store to table.
  char* oldEntryStore = mEntryStore.Get();
  mEntryStore.Set(newEntryStore, &mGeneration);
  PLDHashMoveEntry moveEntry = mOps->moveEntry;

  // Copy only live entries, leaving removed ones behind.
  uint32_t oldCapacity = 1u << oldLog2;
  EntryStore::ForEachSlot(
      oldEntryStore, oldCapacity, mEntrySize, [&](const Slot& slot) {
        if (slot.IsLive()) {
          const PLDHashNumber key = slot.KeyHash() & ~kCollisionFlag;
          Slot newSlot = FindFreeSlot(key);
          MOZ_ASSERT(newSlot.IsFree());
          moveEntry(this, slot.ToEntry(), newSlot.ToEntry());
          newSlot.SetKeyHash(key);
        }
      });

  free(oldEntryStore);
  return true;
}

MOZ_ALWAYS_INLINE PLDHashNumber
PLDHashTable::ComputeKeyHash(const void* aKey) const {
  MOZ_ASSERT(mEntryStore.IsAllocated());

  PLDHashNumber keyHash = mozilla::ScrambleHashCode(mOps->hashKey(aKey));

  // Avoid 0 and 1 hash codes, they indicate free and removed entries.
  if (keyHash < 2) {
    keyHash -= 2;
  }
  keyHash &= ~kCollisionFlag;

  return keyHash;
}

PLDHashEntryHdr* PLDHashTable::Search(const void* aKey) const {
#ifdef MOZ_HASH_TABLE_CHECKS_ENABLED
  AutoReadOp op(mChecker);
#endif

  if (!mEntryStore.IsAllocated()) {
    return nullptr;
  }

  return SearchTable<ForSearchOrRemove>(
      aKey, ComputeKeyHash(aKey),
      [&](Slot& slot) -> PLDHashEntryHdr* { return slot.ToEntry(); },
      [&]() -> PLDHashEntryHdr* { return nullptr; });
}

PLDHashEntryHdr* PLDHashTable::Add(const void* aKey,
                                   const mozilla::fallible_t& aFallible) {
  auto maybeEntryHandle = MakeEntryHandle(aKey, aFallible);
  if (!maybeEntryHandle) {
    return nullptr;
  }
  return maybeEntryHandle->OrInsert([&aKey, this](PLDHashEntryHdr* entry) {
    if (mOps->initEntry) {
      mOps->initEntry(entry, aKey);
    }
  });
}

PLDHashEntryHdr* PLDHashTable::Add(const void* aKey) {
  return MakeEntryHandle(aKey).OrInsert([&aKey, this](PLDHashEntryHdr* entry) {
    if (mOps->initEntry) {
      mOps->initEntry(entry, aKey);
    }
  });
}

void PLDHashTable::Remove(const void* aKey) {
#ifdef MOZ_HASH_TABLE_CHECKS_ENABLED
  AutoWriteOp op(mChecker);
#endif

  if (!mEntryStore.IsAllocated()) {
    return;
  }

  PLDHashNumber keyHash = ComputeKeyHash(aKey);
  SearchTable<ForSearchOrRemove>(
      aKey, keyHash,
      [&](Slot& slot) {
        RawRemove(slot);
        ShrinkIfAppropriate();
      },
      [&]() {
        // Do nothing.
      });
}

void PLDHashTable::RemoveEntry(PLDHashEntryHdr* aEntry) {
#ifdef MOZ_HASH_TABLE_CHECKS_ENABLED
  AutoWriteOp op(mChecker);
#endif

  RawRemove(aEntry);
  ShrinkIfAppropriate();
}

void PLDHashTable::RawRemove(PLDHashEntryHdr* aEntry) {
  Slot slot(mEntryStore.SlotForPLDHashEntry(aEntry, Capacity(), mEntrySize));
  RawRemove(slot);
}

void PLDHashTable::RawRemove(Slot& aSlot) {
  // Unfortunately, we can only do weak checking here. That's because
  // RawRemove() can be called legitimately while an Enumerate() call is
  // active, which doesn't fit well into how Checker's mState variable works.
  MOZ_ASSERT(mChecker.IsWritable());

  MOZ_ASSERT(mEntryStore.IsAllocated());

  MOZ_ASSERT(aSlot.IsLive());

  // Load keyHash first in case clearEntry() goofs it.
  PLDHashNumber keyHash = aSlot.KeyHash();
  if (mOps->clearEntry) {
    PLDHashEntryHdr* entry = aSlot.ToEntry();
    mOps->clearEntry(this, entry);
  }
  if (keyHash & kCollisionFlag) {
    aSlot.MarkRemoved();
    mRemovedCount++;
  } else {
    aSlot.MarkFree();
  }
  mEntryCount--;
}

// Shrink or compress if a quarter or more of all entries are removed, or if the
// table is underloaded according to the minimum alpha, and is not minimal-size
// already.
void PLDHashTable::ShrinkIfAppropriate() {
  uint32_t capacity = Capacity();
  if (mRemovedCount >= capacity >> 2 ||
      (capacity > kMinCapacity && mEntryCount <= MinLoad(capacity))) {
    uint32_t log2;
    BestCapacity(mEntryCount, &capacity, &log2);

    int32_t deltaLog2 = log2 - (kPLDHashNumberBits - mHashShift);
    MOZ_ASSERT(deltaLog2 <= 0);

    (void)ChangeTable(deltaLog2);
  }
}

size_t PLDHashTable::ShallowSizeOfExcludingThis(
    MallocSizeOf aMallocSizeOf) const {
#ifdef MOZ_HASH_TABLE_CHECKS_ENABLED
  AutoReadOp op(mChecker);
#endif

  return aMallocSizeOf(mEntryStore.Get());
}

size_t PLDHashTable::ShallowSizeOfIncludingThis(
    MallocSizeOf aMallocSizeOf) const {
  return aMallocSizeOf(this) + ShallowSizeOfExcludingThis(aMallocSizeOf);
}

mozilla::Maybe<PLDHashTable::EntryHandle> PLDHashTable::MakeEntryHandle(
    const void* aKey, const mozilla::fallible_t&) {
#ifdef MOZ_HASH_TABLE_CHECKS_ENABLED
  mChecker.StartWriteOp();
  auto endWriteOp = MakeScopeExit([&] { mChecker.EndWriteOp(); });
#endif

  // Allocate the entry storage if it hasn't already been allocated.
  if (!mEntryStore.IsAllocated()) {
    uint32_t nbytes;
    // We already checked this in the constructor, so it must still be true.
    MOZ_RELEASE_ASSERT(
        SizeOfEntryStore(CapacityFromHashShift(), mEntrySize, &nbytes));
    mEntryStore.Set((char*)calloc(1, nbytes), &mGeneration);
    if (!mEntryStore.IsAllocated()) {
      return Nothing();
    }
  }

  // If alpha is >= .75, grow or compress the table. If aKey is already in the
  // table, we may grow once more than necessary, but only if we are on the
  // edge of being overloaded.
  uint32_t capacity = Capacity();
  if (mEntryCount + mRemovedCount >= MaxLoad(capacity)) {
    // Compress if a quarter or more of all entries are removed.
    int deltaLog2 = 1;
    if (mRemovedCount >= capacity >> 2) {
      deltaLog2 = 0;
    }

    // Grow or compress the table. If ChangeTable() fails, allow overloading up
    // to the secondary max. Once we hit the secondary max, return null.
    if (!ChangeTable(deltaLog2) &&
        mEntryCount + mRemovedCount >= MaxLoadOnGrowthFailure(capacity)) {
      return Nothing();
    }
  }

  // Look for entry after possibly growing, so we don't have to add it,
  // then skip it while growing the table and re-add it after.
  PLDHashNumber keyHash = ComputeKeyHash(aKey);
  Slot slot = SearchTable<ForAdd>(
      aKey, keyHash, [](Slot& found) -> Slot { return found; },
      []() -> Slot {
        MOZ_CRASH("Nope");
        return Slot(nullptr, nullptr);
      });

  // The `EntryHandle` will handle ending the write op when it is destroyed.
#ifdef MOZ_HASH_TABLE_CHECKS_ENABLED
  endWriteOp.release();
#endif

  return Some(EntryHandle{this, keyHash, slot});
}

PLDHashTable::EntryHandle PLDHashTable::MakeEntryHandle(const void* aKey) {
  auto res = MakeEntryHandle(aKey, fallible);
  if (!res) {
    if (!mEntryStore.IsAllocated()) {
      // We OOM'd while allocating the initial entry storage.
      uint32_t nbytes;
      (void)SizeOfEntryStore(CapacityFromHashShift(), mEntrySize, &nbytes);
      NS_ABORT_OOM(nbytes);
    } else {
      // We failed to resize the existing entry storage, either due to OOM or
      // because we exceeded the maximum table capacity or size; report it as
      // an OOM. The multiplication by 2 gets us the size we tried to allocate,
      // which is double the current size.
      NS_ABORT_OOM(2 * EntrySize() * EntryCount());
    }
  }
  return res.extract();
}

PLDHashTable::EntryHandle::EntryHandle(PLDHashTable* aTable,
                                       PLDHashNumber aKeyHash, Slot aSlot)
    : mTable(aTable), mKeyHash(aKeyHash), mSlot(aSlot) {}

PLDHashTable::EntryHandle::EntryHandle(EntryHandle&& aOther) noexcept
    : mTable(std::exchange(aOther.mTable, nullptr)),
      mKeyHash(aOther.mKeyHash),
      mSlot(aOther.mSlot) {}

#ifdef MOZ_HASH_TABLE_CHECKS_ENABLED
PLDHashTable::EntryHandle::~EntryHandle() {
  if (!mTable) {
    return;
  }

  mTable->mChecker.EndWriteOp();
}
#endif

void PLDHashTable::EntryHandle::Remove() {
  MOZ_ASSERT(HasEntry());

  mTable->RawRemove(mSlot);
}

void PLDHashTable::EntryHandle::OrRemove() {
  if (HasEntry()) {
    Remove();
  }
}

void PLDHashTable::EntryHandle::OccupySlot() {
  MOZ_ASSERT(!HasEntry());

  PLDHashNumber keyHash = mKeyHash;
  if (mSlot.IsRemoved()) {
    mTable->mRemovedCount--;
    keyHash |= kCollisionFlag;
  }
  mSlot.SetKeyHash(keyHash);
  mTable->mEntryCount++;
}

PLDHashTable::Iterator::Iterator(Iterator&& aOther)
    : mTable(aOther.mTable),
      mCurrent(aOther.mCurrent),
      mNexts(aOther.mNexts),
      mNextsLimit(aOther.mNextsLimit),
      mHaveRemoved(aOther.mHaveRemoved),
      mEntrySize(aOther.mEntrySize) {
  // No need to change |mChecker| here.
  aOther.mTable = nullptr;
  // We don't really have the concept of a null slot, so leave mCurrent.
  aOther.mNexts = 0;
  aOther.mNextsLimit = 0;
  aOther.mHaveRemoved = false;
  aOther.mEntrySize = 0;
}

PLDHashTable::Iterator::Iterator(PLDHashTable* aTable)
    : mTable(aTable),
      mCurrent(mTable->mEntryStore.SlotForIndex(0, mTable->mEntrySize,
                                                mTable->Capacity())),
      mNexts(0),
      mNextsLimit(mTable->EntryCount()),
      mHaveRemoved(false),
      mEntrySize(aTable->mEntrySize) {
#ifdef MOZ_HASH_TABLE_CHECKS_ENABLED
  mTable->mChecker.StartReadOp();
#endif

  if (ChaosMode::isActive(ChaosFeature::HashTableIteration) &&
      mTable->Capacity() > 0) {
    // Start iterating at a random entry. It would be even more chaotic to
    // iterate in fully random order, but that's harder.
    uint32_t capacity = mTable->CapacityFromHashShift();
    uint32_t i = ChaosMode::randomUint32LessThan(capacity);
    mCurrent =
        mTable->mEntryStore.SlotForIndex(i, mTable->mEntrySize, capacity);
  }

  // Advance to the first live entry, if there is one.
  if (!Done() && IsOnNonLiveEntry()) {
    MoveToNextLiveEntry();
  }
}

PLDHashTable::Iterator::Iterator(PLDHashTable* aTable, EndIteratorTag aTag)
    : mTable(aTable),
      mCurrent(mTable->mEntryStore.SlotForIndex(0, mTable->mEntrySize,
                                                mTable->Capacity())),
      mNexts(mTable->EntryCount()),
      mNextsLimit(mTable->EntryCount()),
      mHaveRemoved(false),
      mEntrySize(aTable->mEntrySize) {
#ifdef MOZ_HASH_TABLE_CHECKS_ENABLED
  mTable->mChecker.StartReadOp();
#endif

  MOZ_ASSERT(Done());
}

PLDHashTable::Iterator::Iterator(const Iterator& aOther)
    : mTable(aOther.mTable),
      mCurrent(aOther.mCurrent),
      mNexts(aOther.mNexts),
      mNextsLimit(aOther.mNextsLimit),
      mHaveRemoved(aOther.mHaveRemoved),
      mEntrySize(aOther.mEntrySize) {
  // TODO: Is this necessary?
  MOZ_ASSERT(!mHaveRemoved);

#ifdef MOZ_HASH_TABLE_CHECKS_ENABLED
  mTable->mChecker.StartReadOp();
#endif
}

PLDHashTable::Iterator::~Iterator() {
  if (mTable) {
    if (mHaveRemoved) {
      mTable->ShrinkIfAppropriate();
    }
#ifdef MOZ_HASH_TABLE_CHECKS_ENABLED
    mTable->mChecker.EndReadOp();
#endif
  }
}

MOZ_ALWAYS_INLINE bool PLDHashTable::Iterator::IsOnNonLiveEntry() const {
  MOZ_ASSERT(!Done());
  return !mCurrent.IsLive();
}

void PLDHashTable::Iterator::Next() {
  MOZ_ASSERT(!Done());

  mNexts++;

  // Advance to the next live entry, if there is one.
  if (!Done()) {
    MoveToNextLiveEntry();
  }
}

MOZ_ALWAYS_INLINE void PLDHashTable::Iterator::MoveToNextLiveEntry() {
  // Chaos mode requires wraparound to cover all possible entries, so we can't
  // simply move to the next live entry and stop when we hit the end of the
  // entry store. But we don't want to introduce extra branches into our inner
  // loop. So we are going to exploit the structure of the entry store in this
  // method to implement an efficient inner loop.
  //
  // The idea is that since we are really only iterating through the stored
  // hashes and because we know that there are a power-of-two number of
  // hashes, we can use masking to implement the wraparound for us. This
  // method does have the downside of needing to recalculate where the
  // associated entry is once we've found it, but that seems OK.

  // Our current slot and its associated hash.
  Slot slot = mCurrent;
  PLDHashNumber* p = slot.HashPtr();
  const uint32_t capacity = mTable->CapacityFromHashShift();
  const uint32_t mask = capacity - 1;
  auto hashes = reinterpret_cast<PLDHashNumber*>(mTable->mEntryStore.Get());
  uint32_t slotIndex = p - hashes;

  do {
    slotIndex = (slotIndex + 1) & mask;
  } while (!Slot::IsLiveHash(hashes[slotIndex]));

  // slotIndex now indicates where a live slot is. Rematerialize the entry
  // and the slot.
  mCurrent = mTable->mEntryStore.SlotForIndex(slotIndex, mEntrySize, capacity);
}

void PLDHashTable::Iterator::Remove() {
  mTable->RawRemove(mCurrent);
  mHaveRemoved = true;
}