Bug 1787199 [wpt PR 35620] - Add tests for `VisibilityStateEntry`, a=testonly
[gecko.git] / mfbt / HashTable.h
blobc1fc716ffec5afad6acc80951e58c5ad458c00be
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 //---------------------------------------------------------------------------
8 // Overview
9 //---------------------------------------------------------------------------
11 // This file defines HashMap<Key, Value> and HashSet<T>, hash tables that are
12 // fast and have a nice API.
14 // Both hash tables have two optional template parameters.
16 // - HashPolicy. This defines the operations for hashing and matching keys. The
17 // default HashPolicy is appropriate when both of the following two
18 // conditions are true.
20 // - The key type stored in the table (|Key| for |HashMap<Key, Value>|, |T|
21 // for |HashSet<T>|) is an integer, pointer, UniquePtr, float, or double.
23 // - The type used for lookups (|Lookup|) is the same as the key type. This
24 // is usually the case, but not always.
26 // There is also a |CStringHasher| policy for |char*| keys. If your keys
27 // don't match any of the above cases, you must provide your own hash policy;
28 // see the "Hash Policy" section below.
30 // - AllocPolicy. This defines how allocations are done by the table.
32 // - |MallocAllocPolicy| is the default and is usually appropriate; note that
33 // operations (such as insertions) that might cause allocations are
34 // fallible and must be checked for OOM. These checks are enforced by the
35 // use of [[nodiscard]].
37 // - |InfallibleAllocPolicy| is another possibility; it allows the
38 // abovementioned OOM checks to be done with MOZ_ALWAYS_TRUE().
40 // Note that entry storage allocation is lazy, and not done until the first
41 // lookupForAdd(), put(), or putNew() is performed.
43 // See AllocPolicy.h for more details.
45 // Documentation on how to use HashMap and HashSet, including examples, is
46 // present within those classes. Search for "class HashMap" and "class
47 // HashSet".
49 // Both HashMap and HashSet are implemented on top of a third class, HashTable.
50 // You only need to look at HashTable if you want to understand the
51 // implementation.
53 // How does mozilla::HashTable (this file) compare with PLDHashTable (and its
54 // subclasses, such as nsTHashtable)?
56 // - mozilla::HashTable is a lot faster, largely because it uses templates
57 // throughout *and* inlines everything. PLDHashTable inlines operations much
58 // less aggressively, and also uses "virtual ops" for operations like hashing
59 // and matching entries that require function calls.
61 // - Correspondingly, mozilla::HashTable use is likely to increase executable
62 // size much more than PLDHashTable.
64 // - mozilla::HashTable has a nicer API, with a proper HashSet vs. HashMap
65 // distinction.
67 // - mozilla::HashTable requires more explicit OOM checking. As mentioned
68 // above, the use of |InfallibleAllocPolicy| can simplify things.
70 // - mozilla::HashTable has a default capacity on creation of 32 and a minimum
71 // capacity of 4. PLDHashTable has a default capacity on creation of 8 and a
72 // minimum capacity of 8.
74 #ifndef mozilla_HashTable_h
75 #define mozilla_HashTable_h
77 #include <utility>
78 #include <type_traits>
80 #include "mozilla/AllocPolicy.h"
81 #include "mozilla/Assertions.h"
82 #include "mozilla/Attributes.h"
83 #include "mozilla/Casting.h"
84 #include "mozilla/HashFunctions.h"
85 #include "mozilla/MathAlgorithms.h"
86 #include "mozilla/Maybe.h"
87 #include "mozilla/MemoryChecking.h"
88 #include "mozilla/MemoryReporting.h"
89 #include "mozilla/Opaque.h"
90 #include "mozilla/OperatorNewExtensions.h"
91 #include "mozilla/ReentrancyGuard.h"
92 #include "mozilla/UniquePtr.h"
93 #include "mozilla/WrappingOperations.h"
95 namespace mozilla {
97 template <class, class = void>
98 struct DefaultHasher;
100 template <class, class>
101 class HashMapEntry;
103 namespace detail {
105 template <typename T>
106 class HashTableEntry;
108 template <class T, class HashPolicy, class AllocPolicy>
109 class HashTable;
111 } // namespace detail
113 // The "generation" of a hash table is an opaque value indicating the state of
114 // modification of the hash table through its lifetime. If the generation of
115 // a hash table compares equal at times T1 and T2, then lookups in the hash
116 // table, pointers to (or into) hash table entries, etc. at time T1 are valid
117 // at time T2. If the generation compares unequal, these computations are all
118 // invalid and must be performed again to be used.
120 // Generations are meaningfully comparable only with respect to a single hash
121 // table. It's always nonsensical to compare the generation of distinct hash
122 // tables H1 and H2.
123 using Generation = Opaque<uint64_t>;
125 //---------------------------------------------------------------------------
126 // HashMap
127 //---------------------------------------------------------------------------
129 // HashMap is a fast hash-based map from keys to values.
131 // Template parameter requirements:
132 // - Key/Value: movable, destructible, assignable.
133 // - HashPolicy: see the "Hash Policy" section below.
134 // - AllocPolicy: see AllocPolicy.h.
136 // Note:
137 // - HashMap is not reentrant: Key/Value/HashPolicy/AllocPolicy members
138 // called by HashMap must not call back into the same HashMap object.
140 template <class Key, class Value, class HashPolicy = DefaultHasher<Key>,
141 class AllocPolicy = MallocAllocPolicy>
142 class HashMap {
143 // -- Implementation details -----------------------------------------------
145 // HashMap is not copyable or assignable.
146 HashMap(const HashMap& hm) = delete;
147 HashMap& operator=(const HashMap& hm) = delete;
149 using TableEntry = HashMapEntry<Key, Value>;
151 struct MapHashPolicy : HashPolicy {
152 using Base = HashPolicy;
153 using KeyType = Key;
155 static const Key& getKey(TableEntry& aEntry) { return aEntry.key(); }
157 static void setKey(TableEntry& aEntry, Key& aKey) {
158 HashPolicy::rekey(aEntry.mutableKey(), aKey);
162 using Impl = detail::HashTable<TableEntry, MapHashPolicy, AllocPolicy>;
163 Impl mImpl;
165 friend class Impl::Enum;
167 public:
168 using Lookup = typename HashPolicy::Lookup;
169 using Entry = TableEntry;
171 // -- Initialization -------------------------------------------------------
173 explicit HashMap(AllocPolicy aAllocPolicy = AllocPolicy(),
174 uint32_t aLen = Impl::sDefaultLen)
175 : mImpl(std::move(aAllocPolicy), aLen) {}
177 explicit HashMap(uint32_t aLen) : mImpl(AllocPolicy(), aLen) {}
179 // HashMap is movable.
180 HashMap(HashMap&& aRhs) = default;
181 HashMap& operator=(HashMap&& aRhs) = default;
183 // -- Status and sizing ----------------------------------------------------
185 // The map's current generation.
186 Generation generation() const { return mImpl.generation(); }
188 // Is the map empty?
189 bool empty() const { return mImpl.empty(); }
191 // Number of keys/values in the map.
192 uint32_t count() const { return mImpl.count(); }
194 // Number of key/value slots in the map. Note: resize will happen well before
195 // count() == capacity().
196 uint32_t capacity() const { return mImpl.capacity(); }
198 // The size of the map's entry storage, in bytes. If the keys/values contain
199 // pointers to other heap blocks, you must iterate over the map and measure
200 // them separately; hence the "shallow" prefix.
201 size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
202 return mImpl.shallowSizeOfExcludingThis(aMallocSizeOf);
204 size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
205 return aMallocSizeOf(this) +
206 mImpl.shallowSizeOfExcludingThis(aMallocSizeOf);
209 // Attempt to minimize the capacity(). If the table is empty, this will free
210 // the empty storage and upon regrowth it will be given the minimum capacity.
211 void compact() { mImpl.compact(); }
213 // Attempt to reserve enough space to fit at least |aLen| elements. Does
214 // nothing if the map already has sufficient capacity.
215 [[nodiscard]] bool reserve(uint32_t aLen) { return mImpl.reserve(aLen); }
217 // -- Lookups --------------------------------------------------------------
219 // Does the map contain a key/value matching |aLookup|?
220 bool has(const Lookup& aLookup) const {
221 return mImpl.lookup(aLookup).found();
224 // Return a Ptr indicating whether a key/value matching |aLookup| is
225 // present in the map. E.g.:
227 // using HM = HashMap<int,char>;
228 // HM h;
229 // if (HM::Ptr p = h.lookup(3)) {
230 // assert(p->key() == 3);
231 // char val = p->value();
232 // }
234 using Ptr = typename Impl::Ptr;
235 MOZ_ALWAYS_INLINE Ptr lookup(const Lookup& aLookup) const {
236 return mImpl.lookup(aLookup);
239 // Like lookup(), but does not assert if two threads call it at the same
240 // time. Only use this method when none of the threads will modify the map.
241 MOZ_ALWAYS_INLINE Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const {
242 return mImpl.readonlyThreadsafeLookup(aLookup);
245 // -- Insertions -----------------------------------------------------------
247 // Overwrite existing value with |aValue|, or add it if not present. Returns
248 // false on OOM.
249 template <typename KeyInput, typename ValueInput>
250 [[nodiscard]] bool put(KeyInput&& aKey, ValueInput&& aValue) {
251 return put(aKey, std::forward<KeyInput>(aKey),
252 std::forward<ValueInput>(aValue));
255 template <typename KeyInput, typename ValueInput>
256 [[nodiscard]] bool put(const Lookup& aLookup, KeyInput&& aKey,
257 ValueInput&& aValue) {
258 AddPtr p = lookupForAdd(aLookup);
259 if (p) {
260 p->value() = std::forward<ValueInput>(aValue);
261 return true;
263 return add(p, std::forward<KeyInput>(aKey),
264 std::forward<ValueInput>(aValue));
267 // Like put(), but slightly faster. Must only be used when the given key is
268 // not already present. (In debug builds, assertions check this.)
269 template <typename KeyInput, typename ValueInput>
270 [[nodiscard]] bool putNew(KeyInput&& aKey, ValueInput&& aValue) {
271 return mImpl.putNew(aKey, std::forward<KeyInput>(aKey),
272 std::forward<ValueInput>(aValue));
275 template <typename KeyInput, typename ValueInput>
276 [[nodiscard]] bool putNew(const Lookup& aLookup, KeyInput&& aKey,
277 ValueInput&& aValue) {
278 return mImpl.putNew(aLookup, std::forward<KeyInput>(aKey),
279 std::forward<ValueInput>(aValue));
282 // Like putNew(), but should be only used when the table is known to be big
283 // enough for the insertion, and hashing cannot fail. Typically this is used
284 // to populate an empty map with known-unique keys after reserving space with
285 // reserve(), e.g.
287 // using HM = HashMap<int,char>;
288 // HM h;
289 // if (!h.reserve(3)) {
290 // MOZ_CRASH("OOM");
291 // }
292 // h.putNewInfallible(1, 'a'); // unique key
293 // h.putNewInfallible(2, 'b'); // unique key
294 // h.putNewInfallible(3, 'c'); // unique key
296 template <typename KeyInput, typename ValueInput>
297 void putNewInfallible(KeyInput&& aKey, ValueInput&& aValue) {
298 mImpl.putNewInfallible(aKey, std::forward<KeyInput>(aKey),
299 std::forward<ValueInput>(aValue));
302 // Like |lookup(l)|, but on miss, |p = lookupForAdd(l)| allows efficient
303 // insertion of Key |k| (where |HashPolicy::match(k,l) == true|) using
304 // |add(p,k,v)|. After |add(p,k,v)|, |p| points to the new key/value. E.g.:
306 // using HM = HashMap<int,char>;
307 // HM h;
308 // HM::AddPtr p = h.lookupForAdd(3);
309 // if (!p) {
310 // if (!h.add(p, 3, 'a')) {
311 // return false;
312 // }
313 // }
314 // assert(p->key() == 3);
315 // char val = p->value();
317 // N.B. The caller must ensure that no mutating hash table operations occur
318 // between a pair of lookupForAdd() and add() calls. To avoid looking up the
319 // key a second time, the caller may use the more efficient relookupOrAdd()
320 // method. This method reuses part of the hashing computation to more
321 // efficiently insert the key if it has not been added. For example, a
322 // mutation-handling version of the previous example:
324 // HM::AddPtr p = h.lookupForAdd(3);
325 // if (!p) {
326 // call_that_may_mutate_h();
327 // if (!h.relookupOrAdd(p, 3, 'a')) {
328 // return false;
329 // }
330 // }
331 // assert(p->key() == 3);
332 // char val = p->value();
334 using AddPtr = typename Impl::AddPtr;
335 MOZ_ALWAYS_INLINE AddPtr lookupForAdd(const Lookup& aLookup) {
336 return mImpl.lookupForAdd(aLookup);
339 // Add a key/value. Returns false on OOM.
340 template <typename KeyInput, typename ValueInput>
341 [[nodiscard]] bool add(AddPtr& aPtr, KeyInput&& aKey, ValueInput&& aValue) {
342 return mImpl.add(aPtr, std::forward<KeyInput>(aKey),
343 std::forward<ValueInput>(aValue));
346 // See the comment above lookupForAdd() for details.
347 template <typename KeyInput, typename ValueInput>
348 [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, KeyInput&& aKey,
349 ValueInput&& aValue) {
350 return mImpl.relookupOrAdd(aPtr, aKey, std::forward<KeyInput>(aKey),
351 std::forward<ValueInput>(aValue));
354 // -- Removal --------------------------------------------------------------
356 // Lookup and remove the key/value matching |aLookup|, if present.
357 void remove(const Lookup& aLookup) {
358 if (Ptr p = lookup(aLookup)) {
359 remove(p);
363 // Remove a previously found key/value (assuming aPtr.found()). The map must
364 // not have been mutated in the interim.
365 void remove(Ptr aPtr) { mImpl.remove(aPtr); }
367 // Remove all keys/values without changing the capacity.
368 void clear() { mImpl.clear(); }
370 // Like clear() followed by compact().
371 void clearAndCompact() { mImpl.clearAndCompact(); }
373 // -- Rekeying -------------------------------------------------------------
375 // Infallibly rekey one entry, if necessary. Requires that template
376 // parameters Key and HashPolicy::Lookup are the same type.
377 void rekeyIfMoved(const Key& aOldKey, const Key& aNewKey) {
378 if (aOldKey != aNewKey) {
379 rekeyAs(aOldKey, aNewKey, aNewKey);
383 // Infallibly rekey one entry if present, and return whether that happened.
384 bool rekeyAs(const Lookup& aOldLookup, const Lookup& aNewLookup,
385 const Key& aNewKey) {
386 if (Ptr p = lookup(aOldLookup)) {
387 mImpl.rekeyAndMaybeRehash(p, aNewLookup, aNewKey);
388 return true;
390 return false;
393 // -- Iteration ------------------------------------------------------------
395 // |iter()| returns an Iterator:
397 // HashMap<int, char> h;
398 // for (auto iter = h.iter(); !iter.done(); iter.next()) {
399 // char c = iter.get().value();
400 // }
402 using Iterator = typename Impl::Iterator;
403 Iterator iter() const { return mImpl.iter(); }
405 // |modIter()| returns a ModIterator:
407 // HashMap<int, char> h;
408 // for (auto iter = h.modIter(); !iter.done(); iter.next()) {
409 // if (iter.get().value() == 'l') {
410 // iter.remove();
411 // }
412 // }
414 // Table resize may occur in ModIterator's destructor.
415 using ModIterator = typename Impl::ModIterator;
416 ModIterator modIter() { return mImpl.modIter(); }
418 // These are similar to Iterator/ModIterator/iter(), but use different
419 // terminology.
420 using Range = typename Impl::Range;
421 using Enum = typename Impl::Enum;
422 Range all() const { return mImpl.all(); }
425 //---------------------------------------------------------------------------
426 // HashSet
427 //---------------------------------------------------------------------------
429 // HashSet is a fast hash-based set of values.
431 // Template parameter requirements:
432 // - T: movable, destructible, assignable.
433 // - HashPolicy: see the "Hash Policy" section below.
434 // - AllocPolicy: see AllocPolicy.h
436 // Note:
437 // - HashSet is not reentrant: T/HashPolicy/AllocPolicy members called by
438 // HashSet must not call back into the same HashSet object.
440 template <class T, class HashPolicy = DefaultHasher<T>,
441 class AllocPolicy = MallocAllocPolicy>
442 class HashSet {
443 // -- Implementation details -----------------------------------------------
445 // HashSet is not copyable or assignable.
446 HashSet(const HashSet& hs) = delete;
447 HashSet& operator=(const HashSet& hs) = delete;
449 struct SetHashPolicy : HashPolicy {
450 using Base = HashPolicy;
451 using KeyType = T;
453 static const KeyType& getKey(const T& aT) { return aT; }
455 static void setKey(T& aT, KeyType& aKey) { HashPolicy::rekey(aT, aKey); }
458 using Impl = detail::HashTable<const T, SetHashPolicy, AllocPolicy>;
459 Impl mImpl;
461 friend class Impl::Enum;
463 public:
464 using Lookup = typename HashPolicy::Lookup;
465 using Entry = T;
467 // -- Initialization -------------------------------------------------------
469 explicit HashSet(AllocPolicy aAllocPolicy = AllocPolicy(),
470 uint32_t aLen = Impl::sDefaultLen)
471 : mImpl(std::move(aAllocPolicy), aLen) {}
473 explicit HashSet(uint32_t aLen) : mImpl(AllocPolicy(), aLen) {}
475 // HashSet is movable.
476 HashSet(HashSet&& aRhs) = default;
477 HashSet& operator=(HashSet&& aRhs) = default;
479 // -- Status and sizing ----------------------------------------------------
481 // The set's current generation.
482 Generation generation() const { return mImpl.generation(); }
484 // Is the set empty?
485 bool empty() const { return mImpl.empty(); }
487 // Number of elements in the set.
488 uint32_t count() const { return mImpl.count(); }
490 // Number of element slots in the set. Note: resize will happen well before
491 // count() == capacity().
492 uint32_t capacity() const { return mImpl.capacity(); }
494 // The size of the set's entry storage, in bytes. If the elements contain
495 // pointers to other heap blocks, you must iterate over the set and measure
496 // them separately; hence the "shallow" prefix.
497 size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
498 return mImpl.shallowSizeOfExcludingThis(aMallocSizeOf);
500 size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
501 return aMallocSizeOf(this) +
502 mImpl.shallowSizeOfExcludingThis(aMallocSizeOf);
505 // Attempt to minimize the capacity(). If the table is empty, this will free
506 // the empty storage and upon regrowth it will be given the minimum capacity.
507 void compact() { mImpl.compact(); }
509 // Attempt to reserve enough space to fit at least |aLen| elements. Does
510 // nothing if the map already has sufficient capacity.
511 [[nodiscard]] bool reserve(uint32_t aLen) { return mImpl.reserve(aLen); }
513 // -- Lookups --------------------------------------------------------------
515 // Does the set contain an element matching |aLookup|?
516 bool has(const Lookup& aLookup) const {
517 return mImpl.lookup(aLookup).found();
520 // Return a Ptr indicating whether an element matching |aLookup| is present
521 // in the set. E.g.:
523 // using HS = HashSet<int>;
524 // HS h;
525 // if (HS::Ptr p = h.lookup(3)) {
526 // assert(*p == 3); // p acts like a pointer to int
527 // }
529 using Ptr = typename Impl::Ptr;
530 MOZ_ALWAYS_INLINE Ptr lookup(const Lookup& aLookup) const {
531 return mImpl.lookup(aLookup);
534 // Like lookup(), but does not assert if two threads call it at the same
535 // time. Only use this method when none of the threads will modify the set.
536 MOZ_ALWAYS_INLINE Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const {
537 return mImpl.readonlyThreadsafeLookup(aLookup);
540 // -- Insertions -----------------------------------------------------------
542 // Add |aU| if it is not present already. Returns false on OOM.
543 template <typename U>
544 [[nodiscard]] bool put(U&& aU) {
545 AddPtr p = lookupForAdd(aU);
546 return p ? true : add(p, std::forward<U>(aU));
549 // Like put(), but slightly faster. Must only be used when the given element
550 // is not already present. (In debug builds, assertions check this.)
551 template <typename U>
552 [[nodiscard]] bool putNew(U&& aU) {
553 return mImpl.putNew(aU, std::forward<U>(aU));
556 // Like the other putNew(), but for when |Lookup| is different to |T|.
557 template <typename U>
558 [[nodiscard]] bool putNew(const Lookup& aLookup, U&& aU) {
559 return mImpl.putNew(aLookup, std::forward<U>(aU));
562 // Like putNew(), but should be only used when the table is known to be big
563 // enough for the insertion, and hashing cannot fail. Typically this is used
564 // to populate an empty set with known-unique elements after reserving space
565 // with reserve(), e.g.
567 // using HS = HashMap<int>;
568 // HS h;
569 // if (!h.reserve(3)) {
570 // MOZ_CRASH("OOM");
571 // }
572 // h.putNewInfallible(1); // unique element
573 // h.putNewInfallible(2); // unique element
574 // h.putNewInfallible(3); // unique element
576 template <typename U>
577 void putNewInfallible(const Lookup& aLookup, U&& aU) {
578 mImpl.putNewInfallible(aLookup, std::forward<U>(aU));
581 // Like |lookup(l)|, but on miss, |p = lookupForAdd(l)| allows efficient
582 // insertion of T value |t| (where |HashPolicy::match(t,l) == true|) using
583 // |add(p,t)|. After |add(p,t)|, |p| points to the new element. E.g.:
585 // using HS = HashSet<int>;
586 // HS h;
587 // HS::AddPtr p = h.lookupForAdd(3);
588 // if (!p) {
589 // if (!h.add(p, 3)) {
590 // return false;
591 // }
592 // }
593 // assert(*p == 3); // p acts like a pointer to int
595 // N.B. The caller must ensure that no mutating hash table operations occur
596 // between a pair of lookupForAdd() and add() calls. To avoid looking up the
597 // key a second time, the caller may use the more efficient relookupOrAdd()
598 // method. This method reuses part of the hashing computation to more
599 // efficiently insert the key if it has not been added. For example, a
600 // mutation-handling version of the previous example:
602 // HS::AddPtr p = h.lookupForAdd(3);
603 // if (!p) {
604 // call_that_may_mutate_h();
605 // if (!h.relookupOrAdd(p, 3, 3)) {
606 // return false;
607 // }
608 // }
609 // assert(*p == 3);
611 // Note that relookupOrAdd(p,l,t) performs Lookup using |l| and adds the
612 // entry |t|, where the caller ensures match(l,t).
613 using AddPtr = typename Impl::AddPtr;
614 MOZ_ALWAYS_INLINE AddPtr lookupForAdd(const Lookup& aLookup) {
615 return mImpl.lookupForAdd(aLookup);
618 // Add an element. Returns false on OOM.
619 template <typename U>
620 [[nodiscard]] bool add(AddPtr& aPtr, U&& aU) {
621 return mImpl.add(aPtr, std::forward<U>(aU));
624 // See the comment above lookupForAdd() for details.
625 template <typename U>
626 [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, const Lookup& aLookup,
627 U&& aU) {
628 return mImpl.relookupOrAdd(aPtr, aLookup, std::forward<U>(aU));
631 // -- Removal --------------------------------------------------------------
633 // Lookup and remove the element matching |aLookup|, if present.
634 void remove(const Lookup& aLookup) {
635 if (Ptr p = lookup(aLookup)) {
636 remove(p);
640 // Remove a previously found element (assuming aPtr.found()). The set must
641 // not have been mutated in the interim.
642 void remove(Ptr aPtr) { mImpl.remove(aPtr); }
644 // Remove all keys/values without changing the capacity.
645 void clear() { mImpl.clear(); }
647 // Like clear() followed by compact().
648 void clearAndCompact() { mImpl.clearAndCompact(); }
650 // -- Rekeying -------------------------------------------------------------
652 // Infallibly rekey one entry, if present. Requires that template parameters
653 // T and HashPolicy::Lookup are the same type.
654 void rekeyIfMoved(const Lookup& aOldValue, const T& aNewValue) {
655 if (aOldValue != aNewValue) {
656 rekeyAs(aOldValue, aNewValue, aNewValue);
660 // Infallibly rekey one entry if present, and return whether that happened.
661 bool rekeyAs(const Lookup& aOldLookup, const Lookup& aNewLookup,
662 const T& aNewValue) {
663 if (Ptr p = lookup(aOldLookup)) {
664 mImpl.rekeyAndMaybeRehash(p, aNewLookup, aNewValue);
665 return true;
667 return false;
670 // Infallibly replace the current key at |aPtr| with an equivalent key.
671 // Specifically, both HashPolicy::hash and HashPolicy::match must return
672 // identical results for the new and old key when applied against all
673 // possible matching values.
674 void replaceKey(Ptr aPtr, const Lookup& aLookup, const T& aNewValue) {
675 MOZ_ASSERT(aPtr.found());
676 MOZ_ASSERT(*aPtr != aNewValue);
677 MOZ_ASSERT(HashPolicy::match(*aPtr, aLookup));
678 MOZ_ASSERT(HashPolicy::match(aNewValue, aLookup));
679 const_cast<T&>(*aPtr) = aNewValue;
680 MOZ_ASSERT(*lookup(aLookup) == aNewValue);
682 void replaceKey(Ptr aPtr, const T& aNewValue) {
683 replaceKey(aPtr, aNewValue, aNewValue);
686 // -- Iteration ------------------------------------------------------------
688 // |iter()| returns an Iterator:
690 // HashSet<int> h;
691 // for (auto iter = h.iter(); !iter.done(); iter.next()) {
692 // int i = iter.get();
693 // }
695 using Iterator = typename Impl::Iterator;
696 Iterator iter() const { return mImpl.iter(); }
698 // |modIter()| returns a ModIterator:
700 // HashSet<int> h;
701 // for (auto iter = h.modIter(); !iter.done(); iter.next()) {
702 // if (iter.get() == 42) {
703 // iter.remove();
704 // }
705 // }
707 // Table resize may occur in ModIterator's destructor.
708 using ModIterator = typename Impl::ModIterator;
709 ModIterator modIter() { return mImpl.modIter(); }
711 // These are similar to Iterator/ModIterator/iter(), but use different
712 // terminology.
713 using Range = typename Impl::Range;
714 using Enum = typename Impl::Enum;
715 Range all() const { return mImpl.all(); }
718 //---------------------------------------------------------------------------
719 // Hash Policy
720 //---------------------------------------------------------------------------
722 // A hash policy |HP| for a hash table with key-type |Key| must provide:
724 // - a type |HP::Lookup| to use to lookup table entries;
726 // - a static member function |HP::hash| that hashes lookup values:
728 // static mozilla::HashNumber hash(const Lookup&);
730 // - a static member function |HP::match| that tests equality of key and
731 // lookup values:
733 // static bool match(const Key&, const Lookup&);
735 // Normally, Lookup = Key. In general, though, different values and types of
736 // values can be used to lookup and store. If a Lookup value |l| is not equal
737 // to the added Key value |k|, the user must ensure that |HP::match(k,l)| is
738 // true. E.g.:
740 // mozilla::HashSet<Key, HP>::AddPtr p = h.lookup(l);
741 // if (!p) {
742 // assert(HP::match(k, l)); // must hold
743 // h.add(p, k);
744 // }
746 // A pointer hashing policy that uses HashGeneric() to create good hashes for
747 // pointers. Note that we don't shift out the lowest k bits because we don't
748 // want to assume anything about the alignment of the pointers.
749 template <typename Key>
750 struct PointerHasher {
751 using Lookup = Key;
753 static HashNumber hash(const Lookup& aLookup) {
754 size_t word = reinterpret_cast<size_t>(aLookup);
755 return HashGeneric(word);
758 static bool match(const Key& aKey, const Lookup& aLookup) {
759 return aKey == aLookup;
762 static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; }
765 // The default hash policy, which only works with integers.
766 template <class Key, typename>
767 struct DefaultHasher {
768 using Lookup = Key;
770 static HashNumber hash(const Lookup& aLookup) {
771 // Just convert the integer to a HashNumber and use that as is. (This
772 // discards the high 32-bits of 64-bit integers!) ScrambleHashCode() is
773 // subsequently called on the value to improve the distribution.
774 return aLookup;
777 static bool match(const Key& aKey, const Lookup& aLookup) {
778 // Use builtin or overloaded operator==.
779 return aKey == aLookup;
782 static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; }
785 // A DefaultHasher specialization for enums.
786 template <class T>
787 struct DefaultHasher<T, std::enable_if_t<std::is_enum_v<T>>> {
788 using Key = T;
789 using Lookup = Key;
791 static HashNumber hash(const Lookup& aLookup) { return HashGeneric(aLookup); }
793 static bool match(const Key& aKey, const Lookup& aLookup) {
794 // Use builtin or overloaded operator==.
795 return aKey == static_cast<Key>(aLookup);
798 static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; }
801 // A DefaultHasher specialization for pointers.
802 template <class T>
803 struct DefaultHasher<T*> : PointerHasher<T*> {};
805 // A DefaultHasher specialization for mozilla::UniquePtr.
806 template <class T, class D>
807 struct DefaultHasher<UniquePtr<T, D>> {
808 using Key = UniquePtr<T, D>;
809 using Lookup = Key;
810 using PtrHasher = PointerHasher<T*>;
812 static HashNumber hash(const Lookup& aLookup) {
813 return PtrHasher::hash(aLookup.get());
816 static bool match(const Key& aKey, const Lookup& aLookup) {
817 return PtrHasher::match(aKey.get(), aLookup.get());
820 static void rekey(UniquePtr<T, D>& aKey, UniquePtr<T, D>&& aNewKey) {
821 aKey = std::move(aNewKey);
825 // A DefaultHasher specialization for doubles.
826 template <>
827 struct DefaultHasher<double> {
828 using Key = double;
829 using Lookup = Key;
831 static HashNumber hash(const Lookup& aLookup) {
832 // Just xor the high bits with the low bits, and then treat the bits of the
833 // result as a uint32_t.
834 static_assert(sizeof(HashNumber) == 4,
835 "subsequent code assumes a four-byte hash");
836 uint64_t u = BitwiseCast<uint64_t>(aLookup);
837 return HashNumber(u ^ (u >> 32));
840 static bool match(const Key& aKey, const Lookup& aLookup) {
841 return BitwiseCast<uint64_t>(aKey) == BitwiseCast<uint64_t>(aLookup);
845 // A DefaultHasher specialization for floats.
846 template <>
847 struct DefaultHasher<float> {
848 using Key = float;
849 using Lookup = Key;
851 static HashNumber hash(const Lookup& aLookup) {
852 // Just use the value as if its bits form an integer. ScrambleHashCode() is
853 // subsequently called on the value to improve the distribution.
854 static_assert(sizeof(HashNumber) == 4,
855 "subsequent code assumes a four-byte hash");
856 return HashNumber(BitwiseCast<uint32_t>(aLookup));
859 static bool match(const Key& aKey, const Lookup& aLookup) {
860 return BitwiseCast<uint32_t>(aKey) == BitwiseCast<uint32_t>(aLookup);
864 // A hash policy for C strings.
865 struct CStringHasher {
866 using Key = const char*;
867 using Lookup = const char*;
869 static HashNumber hash(const Lookup& aLookup) { return HashString(aLookup); }
871 static bool match(const Key& aKey, const Lookup& aLookup) {
872 return strcmp(aKey, aLookup) == 0;
876 //---------------------------------------------------------------------------
877 // Fallible Hashing Interface
878 //---------------------------------------------------------------------------
880 // Most of the time generating a hash code is infallible so this class provides
881 // default methods that always succeed. Specialize this class for your own hash
882 // policy to provide fallible hashing.
884 // This is used by MovableCellHasher to handle the fact that generating a unique
885 // ID for cell pointer may fail due to OOM.
886 template <typename HashPolicy>
887 struct FallibleHashMethods {
888 // Return true if a hashcode is already available for its argument. Once
889 // this returns true for a specific argument it must continue to do so.
890 template <typename Lookup>
891 static bool hasHash(Lookup&& aLookup) {
892 return true;
895 // Fallible method to ensure a hashcode exists for its argument and create
896 // one if not. Returns false on error, e.g. out of memory.
897 template <typename Lookup>
898 static bool ensureHash(Lookup&& aLookup) {
899 return true;
903 template <typename HashPolicy, typename Lookup>
904 static bool HasHash(Lookup&& aLookup) {
905 return FallibleHashMethods<typename HashPolicy::Base>::hasHash(
906 std::forward<Lookup>(aLookup));
909 template <typename HashPolicy, typename Lookup>
910 static bool EnsureHash(Lookup&& aLookup) {
911 return FallibleHashMethods<typename HashPolicy::Base>::ensureHash(
912 std::forward<Lookup>(aLookup));
915 //---------------------------------------------------------------------------
916 // Implementation Details (HashMapEntry, HashTableEntry, HashTable)
917 //---------------------------------------------------------------------------
919 // Both HashMap and HashSet are implemented by a single HashTable that is even
920 // more heavily parameterized than the other two. This leaves HashTable gnarly
921 // and extremely coupled to HashMap and HashSet; thus code should not use
922 // HashTable directly.
924 template <class Key, class Value>
925 class HashMapEntry {
926 Key key_;
927 Value value_;
929 template <class, class, class>
930 friend class detail::HashTable;
931 template <class>
932 friend class detail::HashTableEntry;
933 template <class, class, class, class>
934 friend class HashMap;
936 public:
937 template <typename KeyInput, typename ValueInput>
938 HashMapEntry(KeyInput&& aKey, ValueInput&& aValue)
939 : key_(std::forward<KeyInput>(aKey)),
940 value_(std::forward<ValueInput>(aValue)) {}
942 HashMapEntry(HashMapEntry&& aRhs) = default;
943 HashMapEntry& operator=(HashMapEntry&& aRhs) = default;
945 using KeyType = Key;
946 using ValueType = Value;
948 const Key& key() const { return key_; }
950 // Use this method with caution! If the key is changed such that its hash
951 // value also changes, the map will be left in an invalid state.
952 Key& mutableKey() { return key_; }
954 const Value& value() const { return value_; }
955 Value& value() { return value_; }
957 private:
958 HashMapEntry(const HashMapEntry&) = delete;
959 void operator=(const HashMapEntry&) = delete;
962 namespace detail {
964 template <class T, class HashPolicy, class AllocPolicy>
965 class HashTable;
967 template <typename T>
968 class EntrySlot;
970 template <typename T>
971 class HashTableEntry {
972 private:
973 using NonConstT = std::remove_const_t<T>;
975 // Instead of having a hash table entry store that looks like this:
977 // +--------+--------+--------+--------+
978 // | entry0 | entry1 | .... | entryN |
979 // +--------+--------+--------+--------+
981 // where the entries contained their cached hash code, we're going to lay out
982 // the entry store thusly:
984 // +-------+-------+-------+-------+--------+--------+--------+--------+
985 // | hash0 | hash1 | ... | hashN | entry0 | entry1 | .... | entryN |
986 // +-------+-------+-------+-------+--------+--------+--------+--------+
988 // with all the cached hashes prior to the actual entries themselves.
990 // We do this because implementing the first strategy requires us to make
991 // HashTableEntry look roughly like:
993 // template <typename T>
994 // class HashTableEntry {
995 // HashNumber mKeyHash;
996 // T mValue;
997 // };
999 // The problem with this setup is that, depending on the layout of `T`, there
1000 // may be platform ABI-mandated padding between `mKeyHash` and the first
1001 // member of `T`. This ABI-mandated padding is wasted space, and can be
1002 // surprisingly common, e.g. when `T` is a single pointer on 64-bit platforms.
1003 // In such cases, we're throwing away a quarter of our entry store on padding,
1004 // which is undesirable.
1006 // The second layout above, namely:
1008 // +-------+-------+-------+-------+--------+--------+--------+--------+
1009 // | hash0 | hash1 | ... | hashN | entry0 | entry1 | .... | entryN |
1010 // +-------+-------+-------+-------+--------+--------+--------+--------+
1012 // means there is no wasted space between the hashes themselves, and no wasted
1013 // space between the entries themselves. However, we would also like there to
1014 // be no gap between the last hash and the first entry. The memory allocator
1015 // guarantees the alignment of the start of the hashes. The use of a
1016 // power-of-two capacity of at least 4 guarantees that the alignment of the
1017 // *end* of the hash array is no less than the alignment of the start.
1018 // Finally, the static_asserts here guarantee that the entries themselves
1019 // don't need to be any more aligned than the alignment of the entry store
1020 // itself.
1022 // This assertion is safe for 32-bit builds because on both Windows and Linux
1023 // (including Android), the minimum alignment for allocations larger than 8
1024 // bytes is 8 bytes, and the actual data for entries in our entry store is
1025 // guaranteed to have that alignment as well, thanks to the power-of-two
1026 // number of cached hash values stored prior to the entry data.
1028 // The allocation policy must allocate a table with at least this much
1029 // alignment.
1030 static constexpr size_t kMinimumAlignment = 8;
1032 static_assert(alignof(HashNumber) <= kMinimumAlignment,
1033 "[N*2 hashes, N*2 T values] allocation's alignment must be "
1034 "enough to align each hash");
1035 static_assert(alignof(NonConstT) <= 2 * sizeof(HashNumber),
1036 "subsequent N*2 T values must not require more than an even "
1037 "number of HashNumbers provides");
1039 static const HashNumber sFreeKey = 0;
1040 static const HashNumber sRemovedKey = 1;
1041 static const HashNumber sCollisionBit = 1;
1043 alignas(NonConstT) unsigned char mValueData[sizeof(NonConstT)];
1045 private:
1046 template <class, class, class>
1047 friend class HashTable;
1048 template <typename>
1049 friend class EntrySlot;
1051 // Some versions of GCC treat it as a -Wstrict-aliasing violation (ergo a
1052 // -Werror compile error) to reinterpret_cast<> |mValueData| to |T*|, even
1053 // through |void*|. Placing the latter cast in these separate functions
1054 // breaks the chain such that affected GCC versions no longer warn/error.
1055 void* rawValuePtr() { return mValueData; }
1057 static bool isLiveHash(HashNumber hash) { return hash > sRemovedKey; }
1059 HashTableEntry(const HashTableEntry&) = delete;
1060 void operator=(const HashTableEntry&) = delete;
1062 NonConstT* valuePtr() { return reinterpret_cast<NonConstT*>(rawValuePtr()); }
1064 void destroyStoredT() {
1065 NonConstT* ptr = valuePtr();
1066 ptr->~T();
1067 MOZ_MAKE_MEM_UNDEFINED(ptr, sizeof(*ptr));
1070 public:
1071 HashTableEntry() = default;
1073 ~HashTableEntry() { MOZ_MAKE_MEM_UNDEFINED(this, sizeof(*this)); }
1075 void destroy() { destroyStoredT(); }
1077 void swap(HashTableEntry* aOther, bool aIsLive) {
1078 // This allows types to use Argument-Dependent-Lookup, and thus use a custom
1079 // std::swap, which is needed by types like JS::Heap and such.
1080 using std::swap;
1082 if (this == aOther) {
1083 return;
1085 if (aIsLive) {
1086 swap(*valuePtr(), *aOther->valuePtr());
1087 } else {
1088 *aOther->valuePtr() = std::move(*valuePtr());
1089 destroy();
1093 T& get() { return *valuePtr(); }
1095 NonConstT& getMutable() { return *valuePtr(); }
1098 // A slot represents a cached hash value and its associated entry stored
1099 // in the hash table. These two things are not stored in contiguous memory.
1100 template <class T>
1101 class EntrySlot {
1102 using NonConstT = std::remove_const_t<T>;
1104 using Entry = HashTableEntry<T>;
1106 Entry* mEntry;
1107 HashNumber* mKeyHash;
1109 template <class, class, class>
1110 friend class HashTable;
1112 EntrySlot(Entry* aEntry, HashNumber* aKeyHash)
1113 : mEntry(aEntry), mKeyHash(aKeyHash) {}
1115 public:
1116 static bool isLiveHash(HashNumber hash) { return hash > Entry::sRemovedKey; }
1118 EntrySlot(const EntrySlot&) = default;
1119 EntrySlot(EntrySlot&& aOther) = default;
1121 EntrySlot& operator=(const EntrySlot&) = default;
1122 EntrySlot& operator=(EntrySlot&&) = default;
1124 bool operator==(const EntrySlot& aRhs) const { return mEntry == aRhs.mEntry; }
1126 bool operator<(const EntrySlot& aRhs) const { return mEntry < aRhs.mEntry; }
1128 EntrySlot& operator++() {
1129 ++mEntry;
1130 ++mKeyHash;
1131 return *this;
1134 void destroy() { mEntry->destroy(); }
1136 void swap(EntrySlot& aOther) {
1137 mEntry->swap(aOther.mEntry, aOther.isLive());
1138 std::swap(*mKeyHash, *aOther.mKeyHash);
1141 T& get() const { return mEntry->get(); }
1143 NonConstT& getMutable() { return mEntry->getMutable(); }
1145 bool isFree() const { return *mKeyHash == Entry::sFreeKey; }
1147 void clearLive() {
1148 MOZ_ASSERT(isLive());
1149 *mKeyHash = Entry::sFreeKey;
1150 mEntry->destroyStoredT();
1153 void clear() {
1154 if (isLive()) {
1155 mEntry->destroyStoredT();
1157 MOZ_MAKE_MEM_UNDEFINED(mEntry, sizeof(*mEntry));
1158 *mKeyHash = Entry::sFreeKey;
1161 bool isRemoved() const { return *mKeyHash == Entry::sRemovedKey; }
1163 void removeLive() {
1164 MOZ_ASSERT(isLive());
1165 *mKeyHash = Entry::sRemovedKey;
1166 mEntry->destroyStoredT();
1169 bool isLive() const { return isLiveHash(*mKeyHash); }
1171 void setCollision() {
1172 MOZ_ASSERT(isLive());
1173 *mKeyHash |= Entry::sCollisionBit;
1175 void unsetCollision() { *mKeyHash &= ~Entry::sCollisionBit; }
1176 bool hasCollision() const { return *mKeyHash & Entry::sCollisionBit; }
1177 bool matchHash(HashNumber hn) {
1178 return (*mKeyHash & ~Entry::sCollisionBit) == hn;
1180 HashNumber getKeyHash() const { return *mKeyHash & ~Entry::sCollisionBit; }
1182 template <typename... Args>
1183 void setLive(HashNumber aHashNumber, Args&&... aArgs) {
1184 MOZ_ASSERT(!isLive());
1185 *mKeyHash = aHashNumber;
1186 new (KnownNotNull, mEntry->valuePtr()) T(std::forward<Args>(aArgs)...);
1187 MOZ_ASSERT(isLive());
1190 Entry* toEntry() const { return mEntry; }
1193 template <class T, class HashPolicy, class AllocPolicy>
1194 class HashTable : private AllocPolicy {
1195 friend class mozilla::ReentrancyGuard;
1197 using NonConstT = std::remove_const_t<T>;
1198 using Key = typename HashPolicy::KeyType;
1199 using Lookup = typename HashPolicy::Lookup;
1201 public:
1202 using Entry = HashTableEntry<T>;
1203 using Slot = EntrySlot<T>;
1205 template <typename F>
1206 static void forEachSlot(char* aTable, uint32_t aCapacity, F&& f) {
1207 auto hashes = reinterpret_cast<HashNumber*>(aTable);
1208 auto entries = reinterpret_cast<Entry*>(&hashes[aCapacity]);
1209 Slot slot(entries, hashes);
1210 for (size_t i = 0; i < size_t(aCapacity); ++i) {
1211 f(slot);
1212 ++slot;
1216 // A nullable pointer to a hash table element. A Ptr |p| can be tested
1217 // either explicitly |if (p.found()) p->...| or using boolean conversion
1218 // |if (p) p->...|. Ptr objects must not be used after any mutating hash
1219 // table operations unless |generation()| is tested.
1220 class Ptr {
1221 friend class HashTable;
1223 Slot mSlot;
1224 #ifdef DEBUG
1225 const HashTable* mTable;
1226 Generation mGeneration;
1227 #endif
1229 protected:
1230 Ptr(Slot aSlot, const HashTable& aTable)
1231 : mSlot(aSlot)
1232 #ifdef DEBUG
1234 mTable(&aTable),
1235 mGeneration(aTable.generation())
1236 #endif
1240 // This constructor is used only by AddPtr() within lookupForAdd().
1241 explicit Ptr(const HashTable& aTable)
1242 : mSlot(nullptr, nullptr)
1243 #ifdef DEBUG
1245 mTable(&aTable),
1246 mGeneration(aTable.generation())
1247 #endif
1251 bool isValid() const { return !!mSlot.toEntry(); }
1253 public:
1254 Ptr()
1255 : mSlot(nullptr, nullptr)
1256 #ifdef DEBUG
1258 mTable(nullptr),
1259 mGeneration(0)
1260 #endif
1264 bool found() const {
1265 if (!isValid()) {
1266 return false;
1268 #ifdef DEBUG
1269 MOZ_ASSERT(mGeneration == mTable->generation());
1270 #endif
1271 return mSlot.isLive();
1274 explicit operator bool() const { return found(); }
1276 bool operator==(const Ptr& aRhs) const {
1277 MOZ_ASSERT(found() && aRhs.found());
1278 return mSlot == aRhs.mSlot;
1281 bool operator!=(const Ptr& aRhs) const {
1282 #ifdef DEBUG
1283 MOZ_ASSERT(mGeneration == mTable->generation());
1284 #endif
1285 return !(*this == aRhs);
1288 T& operator*() const {
1289 #ifdef DEBUG
1290 MOZ_ASSERT(found());
1291 MOZ_ASSERT(mGeneration == mTable->generation());
1292 #endif
1293 return mSlot.get();
1296 T* operator->() const {
1297 #ifdef DEBUG
1298 MOZ_ASSERT(found());
1299 MOZ_ASSERT(mGeneration == mTable->generation());
1300 #endif
1301 return &mSlot.get();
1305 // A Ptr that can be used to add a key after a failed lookup.
1306 class AddPtr : public Ptr {
1307 friend class HashTable;
1309 HashNumber mKeyHash;
1310 #ifdef DEBUG
1311 uint64_t mMutationCount;
1312 #endif
1314 AddPtr(Slot aSlot, const HashTable& aTable, HashNumber aHashNumber)
1315 : Ptr(aSlot, aTable),
1316 mKeyHash(aHashNumber)
1317 #ifdef DEBUG
1319 mMutationCount(aTable.mMutationCount)
1320 #endif
1324 // This constructor is used when lookupForAdd() is performed on a table
1325 // lacking entry storage; it leaves mSlot null but initializes everything
1326 // else.
1327 AddPtr(const HashTable& aTable, HashNumber aHashNumber)
1328 : Ptr(aTable),
1329 mKeyHash(aHashNumber)
1330 #ifdef DEBUG
1332 mMutationCount(aTable.mMutationCount)
1333 #endif
1335 MOZ_ASSERT(isLive());
1338 bool isLive() const { return isLiveHash(mKeyHash); }
1340 public:
1341 AddPtr() : mKeyHash(0) {}
1344 // A hash table iterator that (mostly) doesn't allow table modifications.
1345 // As with Ptr/AddPtr, Iterator objects must not be used after any mutating
1346 // hash table operation unless the |generation()| is tested.
1347 class Iterator {
1348 void moveToNextLiveEntry() {
1349 while (++mCur < mEnd && !mCur.isLive()) {
1350 continue;
1354 protected:
1355 friend class HashTable;
1357 explicit Iterator(const HashTable& aTable)
1358 : mCur(aTable.slotForIndex(0)),
1359 mEnd(aTable.slotForIndex(aTable.capacity()))
1360 #ifdef DEBUG
1362 mTable(aTable),
1363 mMutationCount(aTable.mMutationCount),
1364 mGeneration(aTable.generation()),
1365 mValidEntry(true)
1366 #endif
1368 if (!done() && !mCur.isLive()) {
1369 moveToNextLiveEntry();
1373 Slot mCur;
1374 Slot mEnd;
1375 #ifdef DEBUG
1376 const HashTable& mTable;
1377 uint64_t mMutationCount;
1378 Generation mGeneration;
1379 bool mValidEntry;
1380 #endif
1382 public:
1383 bool done() const {
1384 MOZ_ASSERT(mGeneration == mTable.generation());
1385 MOZ_ASSERT(mMutationCount == mTable.mMutationCount);
1386 return mCur == mEnd;
1389 T& get() const {
1390 MOZ_ASSERT(!done());
1391 MOZ_ASSERT(mValidEntry);
1392 MOZ_ASSERT(mGeneration == mTable.generation());
1393 MOZ_ASSERT(mMutationCount == mTable.mMutationCount);
1394 return mCur.get();
1397 void next() {
1398 MOZ_ASSERT(!done());
1399 MOZ_ASSERT(mGeneration == mTable.generation());
1400 MOZ_ASSERT(mMutationCount == mTable.mMutationCount);
1401 moveToNextLiveEntry();
1402 #ifdef DEBUG
1403 mValidEntry = true;
1404 #endif
1408 // A hash table iterator that permits modification, removal and rekeying.
1409 // Since rehashing when elements were removed during enumeration would be
1410 // bad, it is postponed until the ModIterator is destructed. Since the
1411 // ModIterator's destructor touches the hash table, the user must ensure
1412 // that the hash table is still alive when the destructor runs.
1413 class ModIterator : public Iterator {
1414 friend class HashTable;
1416 HashTable& mTable;
1417 bool mRekeyed;
1418 bool mRemoved;
1420 // ModIterator is movable but not copyable.
1421 ModIterator(const ModIterator&) = delete;
1422 void operator=(const ModIterator&) = delete;
1424 protected:
1425 explicit ModIterator(HashTable& aTable)
1426 : Iterator(aTable), mTable(aTable), mRekeyed(false), mRemoved(false) {}
1428 public:
1429 MOZ_IMPLICIT ModIterator(ModIterator&& aOther)
1430 : Iterator(aOther),
1431 mTable(aOther.mTable),
1432 mRekeyed(aOther.mRekeyed),
1433 mRemoved(aOther.mRemoved) {
1434 aOther.mRekeyed = false;
1435 aOther.mRemoved = false;
1438 // Removes the current element from the table, leaving |get()|
1439 // invalid until the next call to |next()|.
1440 void remove() {
1441 mTable.remove(this->mCur);
1442 mRemoved = true;
1443 #ifdef DEBUG
1444 this->mValidEntry = false;
1445 this->mMutationCount = mTable.mMutationCount;
1446 #endif
1449 NonConstT& getMutable() {
1450 MOZ_ASSERT(!this->done());
1451 MOZ_ASSERT(this->mValidEntry);
1452 MOZ_ASSERT(this->mGeneration == this->Iterator::mTable.generation());
1453 MOZ_ASSERT(this->mMutationCount == this->Iterator::mTable.mMutationCount);
1454 return this->mCur.getMutable();
1457 // Removes the current element and re-inserts it into the table with
1458 // a new key at the new Lookup position. |get()| is invalid after
1459 // this operation until the next call to |next()|.
1460 void rekey(const Lookup& l, const Key& k) {
1461 MOZ_ASSERT(&k != &HashPolicy::getKey(this->mCur.get()));
1462 Ptr p(this->mCur, mTable);
1463 mTable.rekeyWithoutRehash(p, l, k);
1464 mRekeyed = true;
1465 #ifdef DEBUG
1466 this->mValidEntry = false;
1467 this->mMutationCount = mTable.mMutationCount;
1468 #endif
1471 void rekey(const Key& k) { rekey(k, k); }
1473 // Potentially rehashes the table.
1474 ~ModIterator() {
1475 if (mRekeyed) {
1476 mTable.mGen++;
1477 mTable.infallibleRehashIfOverloaded();
1480 if (mRemoved) {
1481 mTable.compact();
1486 // Range is similar to Iterator, but uses different terminology.
1487 class Range {
1488 friend class HashTable;
1490 Iterator mIter;
1492 protected:
1493 explicit Range(const HashTable& table) : mIter(table) {}
1495 public:
1496 bool empty() const { return mIter.done(); }
1498 T& front() const { return mIter.get(); }
1500 void popFront() { return mIter.next(); }
1503 // Enum is similar to ModIterator, but uses different terminology.
1504 class Enum {
1505 ModIterator mIter;
1507 // Enum is movable but not copyable.
1508 Enum(const Enum&) = delete;
1509 void operator=(const Enum&) = delete;
1511 public:
1512 template <class Map>
1513 explicit Enum(Map& map) : mIter(map.mImpl) {}
1515 MOZ_IMPLICIT Enum(Enum&& other) : mIter(std::move(other.mIter)) {}
1517 bool empty() const { return mIter.done(); }
1519 T& front() const { return mIter.get(); }
1521 void popFront() { return mIter.next(); }
1523 void removeFront() { mIter.remove(); }
1525 NonConstT& mutableFront() { return mIter.getMutable(); }
1527 void rekeyFront(const Lookup& aLookup, const Key& aKey) {
1528 mIter.rekey(aLookup, aKey);
1531 void rekeyFront(const Key& aKey) { mIter.rekey(aKey); }
1534 // HashTable is movable
1535 HashTable(HashTable&& aRhs) : AllocPolicy(std::move(aRhs)) { moveFrom(aRhs); }
1536 HashTable& operator=(HashTable&& aRhs) {
1537 MOZ_ASSERT(this != &aRhs, "self-move assignment is prohibited");
1538 if (mTable) {
1539 destroyTable(*this, mTable, capacity());
1541 AllocPolicy::operator=(std::move(aRhs));
1542 moveFrom(aRhs);
1543 return *this;
1546 private:
1547 void moveFrom(HashTable& aRhs) {
1548 mGen = aRhs.mGen;
1549 mHashShift = aRhs.mHashShift;
1550 mTable = aRhs.mTable;
1551 mEntryCount = aRhs.mEntryCount;
1552 mRemovedCount = aRhs.mRemovedCount;
1553 #ifdef DEBUG
1554 mMutationCount = aRhs.mMutationCount;
1555 mEntered = aRhs.mEntered;
1556 #endif
1557 aRhs.mTable = nullptr;
1558 aRhs.clearAndCompact();
1561 // HashTable is not copyable or assignable
1562 HashTable(const HashTable&) = delete;
1563 void operator=(const HashTable&) = delete;
1565 static const uint32_t CAP_BITS = 30;
1567 public:
1568 uint64_t mGen : 56; // entry storage generation number
1569 uint64_t mHashShift : 8; // multiplicative hash shift
1570 char* mTable; // entry storage
1571 uint32_t mEntryCount; // number of entries in mTable
1572 uint32_t mRemovedCount; // removed entry sentinels in mTable
1574 #ifdef DEBUG
1575 uint64_t mMutationCount;
1576 mutable bool mEntered;
1577 #endif
1579 // The default initial capacity is 32 (enough to hold 16 elements), but it
1580 // can be as low as 4.
1581 static const uint32_t sDefaultLen = 16;
1582 static const uint32_t sMinCapacity = 4;
1583 // See the comments in HashTableEntry about this value.
1584 static_assert(sMinCapacity >= 4, "too-small sMinCapacity breaks assumptions");
1585 static const uint32_t sMaxInit = 1u << (CAP_BITS - 1);
1586 static const uint32_t sMaxCapacity = 1u << CAP_BITS;
1588 // Hash-table alpha is conceptually a fraction, but to avoid floating-point
1589 // math we implement it as a ratio of integers.
1590 static const uint8_t sAlphaDenominator = 4;
1591 static const uint8_t sMinAlphaNumerator = 1; // min alpha: 1/4
1592 static const uint8_t sMaxAlphaNumerator = 3; // max alpha: 3/4
1594 static const HashNumber sFreeKey = Entry::sFreeKey;
1595 static const HashNumber sRemovedKey = Entry::sRemovedKey;
1596 static const HashNumber sCollisionBit = Entry::sCollisionBit;
1598 static uint32_t bestCapacity(uint32_t aLen) {
1599 static_assert(
1600 (sMaxInit * sAlphaDenominator) / sAlphaDenominator == sMaxInit,
1601 "multiplication in numerator below could overflow");
1602 static_assert(
1603 sMaxInit * sAlphaDenominator <= UINT32_MAX - sMaxAlphaNumerator,
1604 "numerator calculation below could potentially overflow");
1606 // Callers should ensure this is true.
1607 MOZ_ASSERT(aLen <= sMaxInit);
1609 // Compute the smallest capacity allowing |aLen| elements to be
1610 // inserted without rehashing: ceil(aLen / max-alpha). (Ceiling
1611 // integral division: <http://stackoverflow.com/a/2745086>.)
1612 uint32_t capacity = (aLen * sAlphaDenominator + sMaxAlphaNumerator - 1) /
1613 sMaxAlphaNumerator;
1614 capacity = (capacity < sMinCapacity) ? sMinCapacity : RoundUpPow2(capacity);
1616 MOZ_ASSERT(capacity >= aLen);
1617 MOZ_ASSERT(capacity <= sMaxCapacity);
1619 return capacity;
1622 static uint32_t hashShift(uint32_t aLen) {
1623 // Reject all lengths whose initial computed capacity would exceed
1624 // sMaxCapacity. Round that maximum aLen down to the nearest power of two
1625 // for speedier code.
1626 if (MOZ_UNLIKELY(aLen > sMaxInit)) {
1627 MOZ_CRASH("initial length is too large");
1630 return kHashNumberBits - mozilla::CeilingLog2(bestCapacity(aLen));
1633 static bool isLiveHash(HashNumber aHash) { return Entry::isLiveHash(aHash); }
1635 static HashNumber prepareHash(const Lookup& aLookup) {
1636 HashNumber keyHash = ScrambleHashCode(HashPolicy::hash(aLookup));
1638 // Avoid reserved hash codes.
1639 if (!isLiveHash(keyHash)) {
1640 keyHash -= (sRemovedKey + 1);
1642 return keyHash & ~sCollisionBit;
1645 enum FailureBehavior { DontReportFailure = false, ReportFailure = true };
1647 // Fake a struct that we're going to alloc. See the comments in
1648 // HashTableEntry about how the table is laid out, and why it's safe.
1649 struct FakeSlot {
1650 unsigned char c[sizeof(HashNumber) + sizeof(typename Entry::NonConstT)];
1653 static char* createTable(AllocPolicy& aAllocPolicy, uint32_t aCapacity,
1654 FailureBehavior aReportFailure = ReportFailure) {
1655 FakeSlot* fake =
1656 aReportFailure
1657 ? aAllocPolicy.template pod_malloc<FakeSlot>(aCapacity)
1658 : aAllocPolicy.template maybe_pod_malloc<FakeSlot>(aCapacity);
1660 MOZ_ASSERT((reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) ==
1663 char* table = reinterpret_cast<char*>(fake);
1664 if (table) {
1665 forEachSlot(table, aCapacity, [&](Slot& slot) {
1666 *slot.mKeyHash = sFreeKey;
1667 new (KnownNotNull, slot.toEntry()) Entry();
1670 return table;
1673 static void destroyTable(AllocPolicy& aAllocPolicy, char* aOldTable,
1674 uint32_t aCapacity) {
1675 forEachSlot(aOldTable, aCapacity, [&](const Slot& slot) {
1676 if (slot.isLive()) {
1677 slot.toEntry()->destroyStoredT();
1680 freeTable(aAllocPolicy, aOldTable, aCapacity);
1683 static void freeTable(AllocPolicy& aAllocPolicy, char* aOldTable,
1684 uint32_t aCapacity) {
1685 FakeSlot* fake = reinterpret_cast<FakeSlot*>(aOldTable);
1686 aAllocPolicy.free_(fake, aCapacity);
1689 public:
1690 HashTable(AllocPolicy aAllocPolicy, uint32_t aLen)
1691 : AllocPolicy(std::move(aAllocPolicy)),
1692 mGen(0),
1693 mHashShift(hashShift(aLen)),
1694 mTable(nullptr),
1695 mEntryCount(0),
1696 mRemovedCount(0)
1697 #ifdef DEBUG
1699 mMutationCount(0),
1700 mEntered(false)
1701 #endif
1705 explicit HashTable(AllocPolicy aAllocPolicy)
1706 : HashTable(aAllocPolicy, sDefaultLen) {}
1708 ~HashTable() {
1709 if (mTable) {
1710 destroyTable(*this, mTable, capacity());
1714 private:
1715 HashNumber hash1(HashNumber aHash0) const { return aHash0 >> mHashShift; }
1717 struct DoubleHash {
1718 HashNumber mHash2;
1719 HashNumber mSizeMask;
1722 DoubleHash hash2(HashNumber aCurKeyHash) const {
1723 uint32_t sizeLog2 = kHashNumberBits - mHashShift;
1724 DoubleHash dh = {((aCurKeyHash << sizeLog2) >> mHashShift) | 1,
1725 (HashNumber(1) << sizeLog2) - 1};
1726 return dh;
1729 static HashNumber applyDoubleHash(HashNumber aHash1,
1730 const DoubleHash& aDoubleHash) {
1731 return WrappingSubtract(aHash1, aDoubleHash.mHash2) & aDoubleHash.mSizeMask;
1734 static MOZ_ALWAYS_INLINE bool match(T& aEntry, const Lookup& aLookup) {
1735 return HashPolicy::match(HashPolicy::getKey(aEntry), aLookup);
1738 enum LookupReason { ForNonAdd, ForAdd };
1740 Slot slotForIndex(HashNumber aIndex) const {
1741 auto hashes = reinterpret_cast<HashNumber*>(mTable);
1742 auto entries = reinterpret_cast<Entry*>(&hashes[capacity()]);
1743 return Slot(&entries[aIndex], &hashes[aIndex]);
1746 // Warning: in order for readonlyThreadsafeLookup() to be safe this
1747 // function must not modify the table in any way when Reason==ForNonAdd.
1748 template <LookupReason Reason>
1749 MOZ_ALWAYS_INLINE Slot lookup(const Lookup& aLookup,
1750 HashNumber aKeyHash) const {
1751 MOZ_ASSERT(isLiveHash(aKeyHash));
1752 MOZ_ASSERT(!(aKeyHash & sCollisionBit));
1753 MOZ_ASSERT(mTable);
1755 // Compute the primary hash address.
1756 HashNumber h1 = hash1(aKeyHash);
1757 Slot slot = slotForIndex(h1);
1759 // Miss: return space for a new entry.
1760 if (slot.isFree()) {
1761 return slot;
1764 // Hit: return entry.
1765 if (slot.matchHash(aKeyHash) && match(slot.get(), aLookup)) {
1766 return slot;
1769 // Collision: double hash.
1770 DoubleHash dh = hash2(aKeyHash);
1772 // Save the first removed entry pointer so we can recycle later.
1773 Maybe<Slot> firstRemoved;
1775 while (true) {
1776 if (Reason == ForAdd && !firstRemoved) {
1777 if (MOZ_UNLIKELY(slot.isRemoved())) {
1778 firstRemoved.emplace(slot);
1779 } else {
1780 slot.setCollision();
1784 h1 = applyDoubleHash(h1, dh);
1786 slot = slotForIndex(h1);
1787 if (slot.isFree()) {
1788 return firstRemoved.refOr(slot);
1791 if (slot.matchHash(aKeyHash) && match(slot.get(), aLookup)) {
1792 return slot;
1797 // This is a copy of lookup() hardcoded to the assumptions:
1798 // 1. the lookup is for an add;
1799 // 2. the key, whose |keyHash| has been passed, is not in the table.
1800 Slot findNonLiveSlot(HashNumber aKeyHash) {
1801 MOZ_ASSERT(!(aKeyHash & sCollisionBit));
1802 MOZ_ASSERT(mTable);
1804 // We assume 'aKeyHash' has already been distributed.
1806 // Compute the primary hash address.
1807 HashNumber h1 = hash1(aKeyHash);
1808 Slot slot = slotForIndex(h1);
1810 // Miss: return space for a new entry.
1811 if (!slot.isLive()) {
1812 return slot;
1815 // Collision: double hash.
1816 DoubleHash dh = hash2(aKeyHash);
1818 while (true) {
1819 slot.setCollision();
1821 h1 = applyDoubleHash(h1, dh);
1823 slot = slotForIndex(h1);
1824 if (!slot.isLive()) {
1825 return slot;
1830 enum RebuildStatus { NotOverloaded, Rehashed, RehashFailed };
1832 RebuildStatus changeTableSize(
1833 uint32_t newCapacity, FailureBehavior aReportFailure = ReportFailure) {
1834 MOZ_ASSERT(IsPowerOfTwo(newCapacity));
1835 MOZ_ASSERT(!!mTable == !!capacity());
1837 // Look, but don't touch, until we succeed in getting new entry store.
1838 char* oldTable = mTable;
1839 uint32_t oldCapacity = capacity();
1840 uint32_t newLog2 = mozilla::CeilingLog2(newCapacity);
1842 if (MOZ_UNLIKELY(newCapacity > sMaxCapacity)) {
1843 if (aReportFailure) {
1844 this->reportAllocOverflow();
1846 return RehashFailed;
1849 char* newTable = createTable(*this, newCapacity, aReportFailure);
1850 if (!newTable) {
1851 return RehashFailed;
1854 // We can't fail from here on, so update table parameters.
1855 mHashShift = kHashNumberBits - newLog2;
1856 mRemovedCount = 0;
1857 mGen++;
1858 mTable = newTable;
1860 // Copy only live entries, leaving removed ones behind.
1861 forEachSlot(oldTable, oldCapacity, [&](Slot& slot) {
1862 if (slot.isLive()) {
1863 HashNumber hn = slot.getKeyHash();
1864 findNonLiveSlot(hn).setLive(
1865 hn, std::move(const_cast<typename Entry::NonConstT&>(slot.get())));
1868 slot.clear();
1871 // All entries have been destroyed, no need to destroyTable.
1872 freeTable(*this, oldTable, oldCapacity);
1873 return Rehashed;
1876 RebuildStatus rehashIfOverloaded(
1877 FailureBehavior aReportFailure = ReportFailure) {
1878 static_assert(sMaxCapacity <= UINT32_MAX / sMaxAlphaNumerator,
1879 "multiplication below could overflow");
1881 // Note: if capacity() is zero, this will always succeed, which is
1882 // what we want.
1883 bool overloaded = mEntryCount + mRemovedCount >=
1884 capacity() * sMaxAlphaNumerator / sAlphaDenominator;
1886 if (!overloaded) {
1887 return NotOverloaded;
1890 // Succeed if a quarter or more of all entries are removed. Note that this
1891 // always succeeds if capacity() == 0 (i.e. entry storage has not been
1892 // allocated), which is what we want, because it means changeTableSize()
1893 // will allocate the requested capacity rather than doubling it.
1894 bool manyRemoved = mRemovedCount >= (capacity() >> 2);
1895 uint32_t newCapacity = manyRemoved ? rawCapacity() : rawCapacity() * 2;
1896 return changeTableSize(newCapacity, aReportFailure);
1899 void infallibleRehashIfOverloaded() {
1900 if (rehashIfOverloaded(DontReportFailure) == RehashFailed) {
1901 rehashTableInPlace();
1905 void remove(Slot& aSlot) {
1906 MOZ_ASSERT(mTable);
1908 if (aSlot.hasCollision()) {
1909 aSlot.removeLive();
1910 mRemovedCount++;
1911 } else {
1912 aSlot.clearLive();
1914 mEntryCount--;
1915 #ifdef DEBUG
1916 mMutationCount++;
1917 #endif
1920 void shrinkIfUnderloaded() {
1921 static_assert(sMaxCapacity <= UINT32_MAX / sMinAlphaNumerator,
1922 "multiplication below could overflow");
1923 bool underloaded =
1924 capacity() > sMinCapacity &&
1925 mEntryCount <= capacity() * sMinAlphaNumerator / sAlphaDenominator;
1927 if (underloaded) {
1928 (void)changeTableSize(capacity() / 2, DontReportFailure);
1932 // This is identical to changeTableSize(currentSize), but without requiring
1933 // a second table. We do this by recycling the collision bits to tell us if
1934 // the element is already inserted or still waiting to be inserted. Since
1935 // already-inserted elements win any conflicts, we get the same table as we
1936 // would have gotten through random insertion order.
1937 void rehashTableInPlace() {
1938 mRemovedCount = 0;
1939 mGen++;
1940 forEachSlot(mTable, capacity(), [&](Slot& slot) { slot.unsetCollision(); });
1941 for (uint32_t i = 0; i < capacity();) {
1942 Slot src = slotForIndex(i);
1944 if (!src.isLive() || src.hasCollision()) {
1945 ++i;
1946 continue;
1949 HashNumber keyHash = src.getKeyHash();
1950 HashNumber h1 = hash1(keyHash);
1951 DoubleHash dh = hash2(keyHash);
1952 Slot tgt = slotForIndex(h1);
1953 while (true) {
1954 if (!tgt.hasCollision()) {
1955 src.swap(tgt);
1956 tgt.setCollision();
1957 break;
1960 h1 = applyDoubleHash(h1, dh);
1961 tgt = slotForIndex(h1);
1965 // TODO: this algorithm leaves collision bits on *all* elements, even if
1966 // they are on no collision path. We have the option of setting the
1967 // collision bits correctly on a subsequent pass or skipping the rehash
1968 // unless we are totally filled with tombstones: benchmark to find out
1969 // which approach is best.
1972 // Note: |aLookup| may be a reference to a piece of |u|, so this function
1973 // must take care not to use |aLookup| after moving |u|.
1975 // Prefer to use putNewInfallible; this function does not check
1976 // invariants.
1977 template <typename... Args>
1978 void putNewInfallibleInternal(const Lookup& aLookup, Args&&... aArgs) {
1979 MOZ_ASSERT(mTable);
1981 HashNumber keyHash = prepareHash(aLookup);
1982 Slot slot = findNonLiveSlot(keyHash);
1984 if (slot.isRemoved()) {
1985 mRemovedCount--;
1986 keyHash |= sCollisionBit;
1989 slot.setLive(keyHash, std::forward<Args>(aArgs)...);
1990 mEntryCount++;
1991 #ifdef DEBUG
1992 mMutationCount++;
1993 #endif
1996 public:
1997 void clear() {
1998 forEachSlot(mTable, capacity(), [&](Slot& slot) { slot.clear(); });
1999 mRemovedCount = 0;
2000 mEntryCount = 0;
2001 #ifdef DEBUG
2002 mMutationCount++;
2003 #endif
2006 // Resize the table down to the smallest capacity that doesn't overload the
2007 // table. Since we call shrinkIfUnderloaded() on every remove, you only need
2008 // to call this after a bulk removal of items done without calling remove().
2009 void compact() {
2010 if (empty()) {
2011 // Free the entry storage.
2012 freeTable(*this, mTable, capacity());
2013 mGen++;
2014 mHashShift = hashShift(0); // gives minimum capacity on regrowth
2015 mTable = nullptr;
2016 mRemovedCount = 0;
2017 return;
2020 uint32_t bestCapacity = this->bestCapacity(mEntryCount);
2021 MOZ_ASSERT(bestCapacity <= capacity());
2023 if (bestCapacity < capacity()) {
2024 (void)changeTableSize(bestCapacity, DontReportFailure);
2028 void clearAndCompact() {
2029 clear();
2030 compact();
2033 [[nodiscard]] bool reserve(uint32_t aLen) {
2034 if (aLen == 0) {
2035 return true;
2038 if (MOZ_UNLIKELY(aLen > sMaxInit)) {
2039 return false;
2042 uint32_t bestCapacity = this->bestCapacity(aLen);
2043 if (bestCapacity <= capacity()) {
2044 return true; // Capacity is already sufficient.
2047 RebuildStatus status = changeTableSize(bestCapacity, ReportFailure);
2048 MOZ_ASSERT(status != NotOverloaded);
2049 return status != RehashFailed;
2052 Iterator iter() const { return Iterator(*this); }
2054 ModIterator modIter() { return ModIterator(*this); }
2056 Range all() const { return Range(*this); }
2058 bool empty() const { return mEntryCount == 0; }
2060 uint32_t count() const { return mEntryCount; }
2062 uint32_t rawCapacity() const { return 1u << (kHashNumberBits - mHashShift); }
2064 uint32_t capacity() const { return mTable ? rawCapacity() : 0; }
2066 Generation generation() const { return Generation(mGen); }
2068 size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
2069 return aMallocSizeOf(mTable);
2072 size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
2073 return aMallocSizeOf(this) + shallowSizeOfExcludingThis(aMallocSizeOf);
2076 MOZ_ALWAYS_INLINE Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const {
2077 if (empty() || !HasHash<HashPolicy>(aLookup)) {
2078 return Ptr();
2080 HashNumber keyHash = prepareHash(aLookup);
2081 return Ptr(lookup<ForNonAdd>(aLookup, keyHash), *this);
2084 MOZ_ALWAYS_INLINE Ptr lookup(const Lookup& aLookup) const {
2085 ReentrancyGuard g(*this);
2086 return readonlyThreadsafeLookup(aLookup);
2089 MOZ_ALWAYS_INLINE AddPtr lookupForAdd(const Lookup& aLookup) {
2090 ReentrancyGuard g(*this);
2091 if (!EnsureHash<HashPolicy>(aLookup)) {
2092 return AddPtr();
2095 HashNumber keyHash = prepareHash(aLookup);
2097 if (!mTable) {
2098 return AddPtr(*this, keyHash);
2101 // Directly call the constructor in the return statement to avoid
2102 // excess copying when building with Visual Studio 2017.
2103 // See bug 1385181.
2104 return AddPtr(lookup<ForAdd>(aLookup, keyHash), *this, keyHash);
2107 template <typename... Args>
2108 [[nodiscard]] bool add(AddPtr& aPtr, Args&&... aArgs) {
2109 ReentrancyGuard g(*this);
2110 MOZ_ASSERT_IF(aPtr.isValid(), mTable);
2111 MOZ_ASSERT_IF(aPtr.isValid(), aPtr.mTable == this);
2112 MOZ_ASSERT(!aPtr.found());
2113 MOZ_ASSERT(!(aPtr.mKeyHash & sCollisionBit));
2115 // Check for error from ensureHash() here.
2116 if (!aPtr.isLive()) {
2117 return false;
2120 MOZ_ASSERT(aPtr.mGeneration == generation());
2121 #ifdef DEBUG
2122 MOZ_ASSERT(aPtr.mMutationCount == mMutationCount);
2123 #endif
2125 if (!aPtr.isValid()) {
2126 MOZ_ASSERT(!mTable && mEntryCount == 0);
2127 uint32_t newCapacity = rawCapacity();
2128 RebuildStatus status = changeTableSize(newCapacity, ReportFailure);
2129 MOZ_ASSERT(status != NotOverloaded);
2130 if (status == RehashFailed) {
2131 return false;
2133 aPtr.mSlot = findNonLiveSlot(aPtr.mKeyHash);
2135 } else if (aPtr.mSlot.isRemoved()) {
2136 // Changing an entry from removed to live does not affect whether we are
2137 // overloaded and can be handled separately.
2138 if (!this->checkSimulatedOOM()) {
2139 return false;
2141 mRemovedCount--;
2142 aPtr.mKeyHash |= sCollisionBit;
2144 } else {
2145 // Preserve the validity of |aPtr.mSlot|.
2146 RebuildStatus status = rehashIfOverloaded();
2147 if (status == RehashFailed) {
2148 return false;
2150 if (status == NotOverloaded && !this->checkSimulatedOOM()) {
2151 return false;
2153 if (status == Rehashed) {
2154 aPtr.mSlot = findNonLiveSlot(aPtr.mKeyHash);
2158 aPtr.mSlot.setLive(aPtr.mKeyHash, std::forward<Args>(aArgs)...);
2159 mEntryCount++;
2160 #ifdef DEBUG
2161 mMutationCount++;
2162 aPtr.mGeneration = generation();
2163 aPtr.mMutationCount = mMutationCount;
2164 #endif
2165 return true;
2168 // Note: |aLookup| may be a reference to a piece of |u|, so this function
2169 // must take care not to use |aLookup| after moving |u|.
2170 template <typename... Args>
2171 void putNewInfallible(const Lookup& aLookup, Args&&... aArgs) {
2172 MOZ_ASSERT(!lookup(aLookup).found());
2173 ReentrancyGuard g(*this);
2174 putNewInfallibleInternal(aLookup, std::forward<Args>(aArgs)...);
2177 // Note: |aLookup| may be alias arguments in |aArgs|, so this function must
2178 // take care not to use |aLookup| after moving |aArgs|.
2179 template <typename... Args>
2180 [[nodiscard]] bool putNew(const Lookup& aLookup, Args&&... aArgs) {
2181 if (!this->checkSimulatedOOM()) {
2182 return false;
2184 if (!EnsureHash<HashPolicy>(aLookup)) {
2185 return false;
2187 if (rehashIfOverloaded() == RehashFailed) {
2188 return false;
2190 putNewInfallible(aLookup, std::forward<Args>(aArgs)...);
2191 return true;
2194 // Note: |aLookup| may be a reference to a piece of |u|, so this function
2195 // must take care not to use |aLookup| after moving |u|.
2196 template <typename... Args>
2197 [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, const Lookup& aLookup,
2198 Args&&... aArgs) {
2199 // Check for error from ensureHash() here.
2200 if (!aPtr.isLive()) {
2201 return false;
2203 #ifdef DEBUG
2204 aPtr.mGeneration = generation();
2205 aPtr.mMutationCount = mMutationCount;
2206 #endif
2207 if (mTable) {
2208 ReentrancyGuard g(*this);
2209 // Check that aLookup has not been destroyed.
2210 MOZ_ASSERT(prepareHash(aLookup) == aPtr.mKeyHash);
2211 aPtr.mSlot = lookup<ForAdd>(aLookup, aPtr.mKeyHash);
2212 if (aPtr.found()) {
2213 return true;
2215 } else {
2216 // Clear aPtr so it's invalid; add() will allocate storage and redo the
2217 // lookup.
2218 aPtr.mSlot = Slot(nullptr, nullptr);
2220 return add(aPtr, std::forward<Args>(aArgs)...);
2223 void remove(Ptr aPtr) {
2224 MOZ_ASSERT(mTable);
2225 ReentrancyGuard g(*this);
2226 MOZ_ASSERT(aPtr.found());
2227 MOZ_ASSERT(aPtr.mGeneration == generation());
2228 remove(aPtr.mSlot);
2229 shrinkIfUnderloaded();
2232 void rekeyWithoutRehash(Ptr aPtr, const Lookup& aLookup, const Key& aKey) {
2233 MOZ_ASSERT(mTable);
2234 ReentrancyGuard g(*this);
2235 MOZ_ASSERT(aPtr.found());
2236 MOZ_ASSERT(aPtr.mGeneration == generation());
2237 typename HashTableEntry<T>::NonConstT t(std::move(*aPtr));
2238 HashPolicy::setKey(t, const_cast<Key&>(aKey));
2239 remove(aPtr.mSlot);
2240 putNewInfallibleInternal(aLookup, std::move(t));
2243 void rekeyAndMaybeRehash(Ptr aPtr, const Lookup& aLookup, const Key& aKey) {
2244 rekeyWithoutRehash(aPtr, aLookup, aKey);
2245 infallibleRehashIfOverloaded();
2249 } // namespace detail
2250 } // namespace mozilla
2252 #endif /* mozilla_HashTable_h */