Bug 1431441 - Part 6 - Start middleman WebReplay process sandbox later r=Alex_Gaynor
[gecko.git] / mfbt / HashTable.h
blobdd4890dc4e07bc8187460836f99ed7d432a1beea
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 MOZ_MUST_USE.
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 "mozilla/AllocPolicy.h"
78 #include "mozilla/Assertions.h"
79 #include "mozilla/Attributes.h"
80 #include "mozilla/Casting.h"
81 #include "mozilla/HashFunctions.h"
82 #include "mozilla/MathAlgorithms.h"
83 #include "mozilla/MemoryChecking.h"
84 #include "mozilla/MemoryReporting.h"
85 #include "mozilla/Move.h"
86 #include "mozilla/Opaque.h"
87 #include "mozilla/PodOperations.h"
88 #include "mozilla/ReentrancyGuard.h"
89 #include "mozilla/TypeTraits.h"
90 #include "mozilla/UniquePtr.h"
92 namespace mozilla {
94 template<class>
95 struct DefaultHasher;
97 template<class, class>
98 class HashMapEntry;
100 namespace detail {
102 template<typename T>
103 class HashTableEntry;
105 template<class T, class HashPolicy, class AllocPolicy>
106 class HashTable;
108 } // namespace detail
110 // The "generation" of a hash table is an opaque value indicating the state of
111 // modification of the hash table through its lifetime. If the generation of
112 // a hash table compares equal at times T1 and T2, then lookups in the hash
113 // table, pointers to (or into) hash table entries, etc. at time T1 are valid
114 // at time T2. If the generation compares unequal, these computations are all
115 // invalid and must be performed again to be used.
117 // Generations are meaningfully comparable only with respect to a single hash
118 // table. It's always nonsensical to compare the generation of distinct hash
119 // tables H1 and H2.
120 using Generation = Opaque<uint64_t>;
122 //---------------------------------------------------------------------------
123 // HashMap
124 //---------------------------------------------------------------------------
126 // HashMap is a fast hash-based map from keys to values.
128 // Template parameter requirements:
129 // - Key/Value: movable, destructible, assignable.
130 // - HashPolicy: see the "Hash Policy" section below.
131 // - AllocPolicy: see AllocPolicy.h.
133 // Note:
134 // - HashMap is not reentrant: Key/Value/HashPolicy/AllocPolicy members
135 // called by HashMap must not call back into the same HashMap object.
137 template<class Key,
138 class Value,
139 class HashPolicy = DefaultHasher<Key>,
140 class AllocPolicy = MallocAllocPolicy>
141 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
153 using Base = HashPolicy;
154 using KeyType = Key;
156 static const Key& getKey(TableEntry& aEntry) { return aEntry.key(); }
158 static void setKey(TableEntry& aEntry, Key& aKey)
160 HashPolicy::rekey(aEntry.mutableKey(), aKey);
164 using Impl = detail::HashTable<TableEntry, MapHashPolicy, AllocPolicy>;
165 Impl mImpl;
167 friend class Impl::Enum;
169 public:
170 using Lookup = typename HashPolicy::Lookup;
171 using Entry = TableEntry;
173 // -- Initialization -------------------------------------------------------
175 explicit HashMap(AllocPolicy aAllocPolicy = AllocPolicy(),
176 uint32_t aLen = Impl::sDefaultLen)
177 : mImpl(aAllocPolicy, aLen)
181 explicit HashMap(uint32_t aLen)
182 : mImpl(AllocPolicy(), aLen)
186 // HashMap is movable.
187 HashMap(HashMap&& aRhs)
188 : mImpl(std::move(aRhs.mImpl))
191 void operator=(HashMap&& aRhs)
193 MOZ_ASSERT(this != &aRhs, "self-move assignment is prohibited");
194 mImpl = std::move(aRhs.mImpl);
197 // -- Status and sizing ----------------------------------------------------
199 // The map's current generation.
200 Generation generation() const { return mImpl.generation(); }
202 // Is the map empty?
203 bool empty() const { return mImpl.empty(); }
205 // Number of keys/values in the map.
206 uint32_t count() const { return mImpl.count(); }
208 // Number of key/value slots in the map. Note: resize will happen well before
209 // count() == capacity().
210 uint32_t capacity() const { return mImpl.capacity(); }
212 // The size of the map's entry storage, in bytes. If the keys/values contain
213 // pointers to other heap blocks, you must iterate over the map and measure
214 // them separately; hence the "shallow" prefix.
215 size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
217 return mImpl.shallowSizeOfExcludingThis(aMallocSizeOf);
219 size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
221 return aMallocSizeOf(this) +
222 mImpl.shallowSizeOfExcludingThis(aMallocSizeOf);
225 // Attempt to minimize the capacity(). If the table is empty, this will free
226 // the empty storage and upon regrowth it will be given the minimum capacity.
227 void compact() { mImpl.compact(); }
229 // Attempt to reserve enough space to fit at least |aLen| elements. Does
230 // nothing if the map already has sufficient capacity.
231 MOZ_MUST_USE bool reserve(uint32_t aLen) { return mImpl.reserve(aLen); }
233 // -- Lookups --------------------------------------------------------------
235 // Does the map contain a key/value matching |aLookup|?
236 bool has(const Lookup& aLookup) const
238 return mImpl.lookup(aLookup).found();
241 // Return a Ptr indicating whether a key/value matching |aLookup| is
242 // present in the map. E.g.:
244 // using HM = HashMap<int,char>;
245 // HM h;
246 // if (HM::Ptr p = h.lookup(3)) {
247 // assert(p->key() == 3);
248 // char val = p->value();
249 // }
251 using Ptr = typename Impl::Ptr;
252 MOZ_ALWAYS_INLINE Ptr lookup(const Lookup& aLookup) const
254 return mImpl.lookup(aLookup);
257 // Like lookup(), but does not assert if two threads call it at the same
258 // time. Only use this method when none of the threads will modify the map.
259 MOZ_ALWAYS_INLINE Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const
261 return mImpl.readonlyThreadsafeLookup(aLookup);
264 // -- Insertions -----------------------------------------------------------
266 // Overwrite existing value with |aValue|, or add it if not present. Returns
267 // false on OOM.
268 template<typename KeyInput, typename ValueInput>
269 MOZ_MUST_USE bool put(KeyInput&& aKey, ValueInput&& aValue)
271 AddPtr p = lookupForAdd(aKey);
272 if (p) {
273 p->value() = std::forward<ValueInput>(aValue);
274 return true;
276 return add(
277 p, std::forward<KeyInput>(aKey), std::forward<ValueInput>(aValue));
280 // Like put(), but slightly faster. Must only be used when the given key is
281 // not already present. (In debug builds, assertions check this.)
282 template<typename KeyInput, typename ValueInput>
283 MOZ_MUST_USE bool putNew(KeyInput&& aKey, ValueInput&& aValue)
285 return mImpl.putNew(
286 aKey, std::forward<KeyInput>(aKey), std::forward<ValueInput>(aValue));
289 // Like putNew(), but should be only used when the table is known to be big
290 // enough for the insertion, and hashing cannot fail. Typically this is used
291 // to populate an empty map with known-unique keys after reserving space with
292 // reserve(), e.g.
294 // using HM = HashMap<int,char>;
295 // HM h;
296 // if (!h.reserve(3)) {
297 // MOZ_CRASH("OOM");
298 // }
299 // h.putNewInfallible(1, 'a'); // unique key
300 // h.putNewInfallible(2, 'b'); // unique key
301 // h.putNewInfallible(3, 'c'); // unique key
303 template<typename KeyInput, typename ValueInput>
304 void putNewInfallible(KeyInput&& aKey, ValueInput&& aValue)
306 mImpl.putNewInfallible(
307 aKey, std::forward<KeyInput>(aKey), std::forward<ValueInput>(aValue));
310 // Like |lookup(l)|, but on miss, |p = lookupForAdd(l)| allows efficient
311 // insertion of Key |k| (where |HashPolicy::match(k,l) == true|) using
312 // |add(p,k,v)|. After |add(p,k,v)|, |p| points to the new key/value. E.g.:
314 // using HM = HashMap<int,char>;
315 // HM h;
316 // HM::AddPtr p = h.lookupForAdd(3);
317 // if (!p) {
318 // if (!h.add(p, 3, 'a')) {
319 // return false;
320 // }
321 // }
322 // assert(p->key() == 3);
323 // char val = p->value();
325 // N.B. The caller must ensure that no mutating hash table operations occur
326 // between a pair of lookupForAdd() and add() calls. To avoid looking up the
327 // key a second time, the caller may use the more efficient relookupOrAdd()
328 // method. This method reuses part of the hashing computation to more
329 // efficiently insert the key if it has not been added. For example, a
330 // mutation-handling version of the previous example:
332 // HM::AddPtr p = h.lookupForAdd(3);
333 // if (!p) {
334 // call_that_may_mutate_h();
335 // if (!h.relookupOrAdd(p, 3, 'a')) {
336 // return false;
337 // }
338 // }
339 // assert(p->key() == 3);
340 // char val = p->value();
342 using AddPtr = typename Impl::AddPtr;
343 MOZ_ALWAYS_INLINE AddPtr lookupForAdd(const Lookup& aLookup)
345 return mImpl.lookupForAdd(aLookup);
348 // Add a key/value. Returns false on OOM.
349 template<typename KeyInput, typename ValueInput>
350 MOZ_MUST_USE bool add(AddPtr& aPtr, KeyInput&& aKey, ValueInput&& aValue)
352 return mImpl.add(
353 aPtr, std::forward<KeyInput>(aKey), std::forward<ValueInput>(aValue));
356 // See the comment above lookupForAdd() for details.
357 template<typename KeyInput, typename ValueInput>
358 MOZ_MUST_USE bool relookupOrAdd(AddPtr& aPtr,
359 KeyInput&& aKey,
360 ValueInput&& aValue)
362 return mImpl.relookupOrAdd(aPtr,
363 aKey,
364 std::forward<KeyInput>(aKey),
365 std::forward<ValueInput>(aValue));
368 // -- Removal --------------------------------------------------------------
370 // Lookup and remove the key/value matching |aLookup|, if present.
371 void remove(const Lookup& aLookup)
373 if (Ptr p = lookup(aLookup)) {
374 remove(p);
378 // Remove a previously found key/value (assuming aPtr.found()). The map must
379 // not have been mutated in the interim.
380 void remove(Ptr aPtr) { mImpl.remove(aPtr); }
382 // Remove all keys/values without changing the capacity.
383 void clear() { mImpl.clear(); }
385 // Like clear() followed by compact().
386 void clearAndCompact() { mImpl.clearAndCompact(); }
388 // -- Rekeying -------------------------------------------------------------
390 // Infallibly rekey one entry, if necessary. Requires that template
391 // parameters Key and HashPolicy::Lookup are the same type.
392 void rekeyIfMoved(const Key& aOldKey, const Key& aNewKey)
394 if (aOldKey != aNewKey) {
395 rekeyAs(aOldKey, aNewKey, aNewKey);
399 // Infallibly rekey one entry if present, and return whether that happened.
400 bool rekeyAs(const Lookup& aOldLookup,
401 const Lookup& aNewLookup,
402 const Key& aNewKey)
404 if (Ptr p = lookup(aOldLookup)) {
405 mImpl.rekeyAndMaybeRehash(p, aNewLookup, aNewKey);
406 return true;
408 return false;
411 // -- Iteration ------------------------------------------------------------
413 // |iter()| returns an Iterator:
415 // HashMap<int, char> h;
416 // for (auto iter = h.iter(); !iter.done(); iter.next()) {
417 // char c = iter.get().value();
418 // }
420 using Iterator = typename Impl::Iterator;
421 Iterator iter() const { return mImpl.iter(); }
423 // |modIter()| returns a ModIterator:
425 // HashMap<int, char> h;
426 // for (auto iter = h.modIter(); !iter.done(); iter.next()) {
427 // if (iter.get().value() == 'l') {
428 // iter.remove();
429 // }
430 // }
432 // Table resize may occur in ModIterator's destructor.
433 using ModIterator = typename Impl::ModIterator;
434 ModIterator modIter() { return mImpl.modIter(); }
436 // These are similar to Iterator/ModIterator/iter(), but use different
437 // terminology.
438 using Range = typename Impl::Range;
439 using Enum = typename Impl::Enum;
440 Range all() const { return mImpl.all(); }
443 //---------------------------------------------------------------------------
444 // HashSet
445 //---------------------------------------------------------------------------
447 // HashSet is a fast hash-based set of values.
449 // Template parameter requirements:
450 // - T: movable, destructible, assignable.
451 // - HashPolicy: see the "Hash Policy" section below.
452 // - AllocPolicy: see AllocPolicy.h
454 // Note:
455 // - HashSet is not reentrant: T/HashPolicy/AllocPolicy members called by
456 // HashSet must not call back into the same HashSet object.
458 template<class T,
459 class HashPolicy = DefaultHasher<T>,
460 class AllocPolicy = MallocAllocPolicy>
461 class HashSet
463 // -- Implementation details -----------------------------------------------
465 // HashSet is not copyable or assignable.
466 HashSet(const HashSet& hs) = delete;
467 HashSet& operator=(const HashSet& hs) = delete;
469 struct SetHashPolicy : HashPolicy
471 using Base = HashPolicy;
472 using KeyType = T;
474 static const KeyType& getKey(const T& aT) { return aT; }
476 static void setKey(T& aT, KeyType& aKey) { HashPolicy::rekey(aT, aKey); }
479 using Impl = detail::HashTable<const T, SetHashPolicy, AllocPolicy>;
480 Impl mImpl;
482 friend class Impl::Enum;
484 public:
485 using Lookup = typename HashPolicy::Lookup;
486 using Entry = T;
488 // -- Initialization -------------------------------------------------------
490 explicit HashSet(AllocPolicy aAllocPolicy = AllocPolicy(),
491 uint32_t aLen = Impl::sDefaultLen)
492 : mImpl(aAllocPolicy, aLen)
496 explicit HashSet(uint32_t aLen)
497 : mImpl(AllocPolicy(), aLen)
501 // HashSet is movable.
502 HashSet(HashSet&& aRhs)
503 : mImpl(std::move(aRhs.mImpl))
506 void operator=(HashSet&& aRhs)
508 MOZ_ASSERT(this != &aRhs, "self-move assignment is prohibited");
509 mImpl = std::move(aRhs.mImpl);
512 // -- Status and sizing ----------------------------------------------------
514 // The set's current generation.
515 Generation generation() const { return mImpl.generation(); }
517 // Is the set empty?
518 bool empty() const { return mImpl.empty(); }
520 // Number of elements in the set.
521 uint32_t count() const { return mImpl.count(); }
523 // Number of element slots in the set. Note: resize will happen well before
524 // count() == capacity().
525 uint32_t capacity() const { return mImpl.capacity(); }
527 // The size of the set's entry storage, in bytes. If the elements contain
528 // pointers to other heap blocks, you must iterate over the set and measure
529 // them separately; hence the "shallow" prefix.
530 size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
532 return mImpl.shallowSizeOfExcludingThis(aMallocSizeOf);
534 size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
536 return aMallocSizeOf(this) +
537 mImpl.shallowSizeOfExcludingThis(aMallocSizeOf);
540 // Attempt to minimize the capacity(). If the table is empty, this will free
541 // the empty storage and upon regrowth it will be given the minimum capacity.
542 void compact() { mImpl.compact(); }
544 // Attempt to reserve enough space to fit at least |aLen| elements. Does
545 // nothing if the map already has sufficient capacity.
546 MOZ_MUST_USE bool reserve(uint32_t aLen) { return mImpl.reserve(aLen); }
548 // -- Lookups --------------------------------------------------------------
550 // Does the set contain an element matching |aLookup|?
551 bool has(const Lookup& aLookup) const
553 return mImpl.lookup(aLookup).found();
556 // Return a Ptr indicating whether an element matching |aLookup| is present
557 // in the set. E.g.:
559 // using HS = HashSet<int>;
560 // HS h;
561 // if (HS::Ptr p = h.lookup(3)) {
562 // assert(*p == 3); // p acts like a pointer to int
563 // }
565 using Ptr = typename Impl::Ptr;
566 MOZ_ALWAYS_INLINE Ptr lookup(const Lookup& aLookup) const
568 return mImpl.lookup(aLookup);
571 // Like lookup(), but does not assert if two threads call it at the same
572 // time. Only use this method when none of the threads will modify the set.
573 MOZ_ALWAYS_INLINE Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const
575 return mImpl.readonlyThreadsafeLookup(aLookup);
578 // -- Insertions -----------------------------------------------------------
580 // Add |aU| if it is not present already. Returns false on OOM.
581 template<typename U>
582 MOZ_MUST_USE bool put(U&& aU)
584 AddPtr p = lookupForAdd(aU);
585 return p ? true : add(p, std::forward<U>(aU));
588 // Like put(), but slightly faster. Must only be used when the given element
589 // is not already present. (In debug builds, assertions check this.)
590 template<typename U>
591 MOZ_MUST_USE bool putNew(U&& aU)
593 return mImpl.putNew(aU, std::forward<U>(aU));
596 // Like the other putNew(), but for when |Lookup| is different to |T|.
597 template<typename U>
598 MOZ_MUST_USE bool putNew(const Lookup& aLookup, U&& aU)
600 return mImpl.putNew(aLookup, std::forward<U>(aU));
603 // Like putNew(), but should be only used when the table is known to be big
604 // enough for the insertion, and hashing cannot fail. Typically this is used
605 // to populate an empty set with known-unique elements after reserving space
606 // with reserve(), e.g.
608 // using HS = HashMap<int>;
609 // HS h;
610 // if (!h.reserve(3)) {
611 // MOZ_CRASH("OOM");
612 // }
613 // h.putNewInfallible(1); // unique element
614 // h.putNewInfallible(2); // unique element
615 // h.putNewInfallible(3); // unique element
617 template<typename U>
618 void putNewInfallible(const Lookup& aLookup, U&& aU)
620 mImpl.putNewInfallible(aLookup, std::forward<U>(aU));
623 // Like |lookup(l)|, but on miss, |p = lookupForAdd(l)| allows efficient
624 // insertion of T value |t| (where |HashPolicy::match(t,l) == true|) using
625 // |add(p,t)|. After |add(p,t)|, |p| points to the new element. E.g.:
627 // using HS = HashSet<int>;
628 // HS h;
629 // HS::AddPtr p = h.lookupForAdd(3);
630 // if (!p) {
631 // if (!h.add(p, 3)) {
632 // return false;
633 // }
634 // }
635 // assert(*p == 3); // p acts like a pointer to int
637 // N.B. The caller must ensure that no mutating hash table operations occur
638 // between a pair of lookupForAdd() and add() calls. To avoid looking up the
639 // key a second time, the caller may use the more efficient relookupOrAdd()
640 // method. This method reuses part of the hashing computation to more
641 // efficiently insert the key if it has not been added. For example, a
642 // mutation-handling version of the previous example:
644 // HS::AddPtr p = h.lookupForAdd(3);
645 // if (!p) {
646 // call_that_may_mutate_h();
647 // if (!h.relookupOrAdd(p, 3, 3)) {
648 // return false;
649 // }
650 // }
651 // assert(*p == 3);
653 // Note that relookupOrAdd(p,l,t) performs Lookup using |l| and adds the
654 // entry |t|, where the caller ensures match(l,t).
655 using AddPtr = typename Impl::AddPtr;
656 MOZ_ALWAYS_INLINE AddPtr lookupForAdd(const Lookup& aLookup)
658 return mImpl.lookupForAdd(aLookup);
661 // Add an element. Returns false on OOM.
662 template<typename U>
663 MOZ_MUST_USE bool add(AddPtr& aPtr, U&& aU)
665 return mImpl.add(aPtr, std::forward<U>(aU));
668 // See the comment above lookupForAdd() for details.
669 template<typename U>
670 MOZ_MUST_USE bool relookupOrAdd(AddPtr& aPtr, const Lookup& aLookup, U&& aU)
672 return mImpl.relookupOrAdd(aPtr, aLookup, std::forward<U>(aU));
675 // -- Removal --------------------------------------------------------------
677 // Lookup and remove the element matching |aLookup|, if present.
678 void remove(const Lookup& aLookup)
680 if (Ptr p = lookup(aLookup)) {
681 remove(p);
685 // Remove a previously found element (assuming aPtr.found()). The set must
686 // not have been mutated in the interim.
687 void remove(Ptr aPtr) { mImpl.remove(aPtr); }
689 // Remove all keys/values without changing the capacity.
690 void clear() { mImpl.clear(); }
692 // Like clear() followed by compact().
693 void clearAndCompact() { mImpl.clearAndCompact(); }
695 // -- Rekeying -------------------------------------------------------------
697 // Infallibly rekey one entry, if present. Requires that template parameters
698 // T and HashPolicy::Lookup are the same type.
699 void rekeyIfMoved(const Lookup& aOldValue, const T& aNewValue)
701 if (aOldValue != aNewValue) {
702 rekeyAs(aOldValue, aNewValue, aNewValue);
706 // Infallibly rekey one entry if present, and return whether that happened.
707 bool rekeyAs(const Lookup& aOldLookup,
708 const Lookup& aNewLookup,
709 const T& aNewValue)
711 if (Ptr p = lookup(aOldLookup)) {
712 mImpl.rekeyAndMaybeRehash(p, aNewLookup, aNewValue);
713 return true;
715 return false;
718 // Infallibly replace the current key at |aPtr| with an equivalent key.
719 // Specifically, both HashPolicy::hash and HashPolicy::match must return
720 // identical results for the new and old key when applied against all
721 // possible matching values.
722 void replaceKey(Ptr aPtr, const T& aNewValue)
724 MOZ_ASSERT(aPtr.found());
725 MOZ_ASSERT(*aPtr != aNewValue);
726 MOZ_ASSERT(HashPolicy::hash(*aPtr) == HashPolicy::hash(aNewValue));
727 MOZ_ASSERT(HashPolicy::match(*aPtr, aNewValue));
728 const_cast<T&>(*aPtr) = aNewValue;
731 // -- Iteration ------------------------------------------------------------
733 // |iter()| returns an Iterator:
735 // HashSet<int> h;
736 // for (auto iter = h.iter(); !iter.done(); iter.next()) {
737 // int i = iter.get();
738 // }
740 using Iterator = typename Impl::Iterator;
741 Iterator iter() const { return mImpl.iter(); }
743 // |modIter()| returns a ModIterator:
745 // HashSet<int> h;
746 // for (auto iter = h.modIter(); !iter.done(); iter.next()) {
747 // if (iter.get() == 42) {
748 // iter.remove();
749 // }
750 // }
752 // Table resize may occur in ModIterator's destructor.
753 using ModIterator = typename Impl::ModIterator;
754 ModIterator modIter() { return mImpl.modIter(); }
756 // These are similar to Iterator/ModIterator/iter(), but use different
757 // terminology.
758 using Range = typename Impl::Range;
759 using Enum = typename Impl::Enum;
760 Range all() const { return mImpl.all(); }
763 //---------------------------------------------------------------------------
764 // Hash Policy
765 //---------------------------------------------------------------------------
767 // A hash policy |HP| for a hash table with key-type |Key| must provide:
769 // - a type |HP::Lookup| to use to lookup table entries;
771 // - a static member function |HP::hash| that hashes lookup values:
773 // static mozilla::HashNumber hash(const Lookup&);
775 // - a static member function |HP::match| that tests equality of key and
776 // lookup values:
778 // static bool match(const Key&, const Lookup&);
780 // Normally, Lookup = Key. In general, though, different values and types of
781 // values can be used to lookup and store. If a Lookup value |l| is not equal
782 // to the added Key value |k|, the user must ensure that |HP::match(k,l)| is
783 // true. E.g.:
785 // mozilla::HashSet<Key, HP>::AddPtr p = h.lookup(l);
786 // if (!p) {
787 // assert(HP::match(k, l)); // must hold
788 // h.add(p, k);
789 // }
791 // A pointer hashing policy that uses HashGeneric() to create good hashes for
792 // pointers. Note that we don't shift out the lowest k bits because we don't
793 // want to assume anything about the alignment of the pointers.
794 template<typename Key>
795 struct PointerHasher
797 using Lookup = Key;
799 static HashNumber hash(const Lookup& aLookup)
801 size_t word = reinterpret_cast<size_t>(aLookup);
802 return HashGeneric(word);
805 static bool match(const Key& aKey, const Lookup& aLookup)
807 return aKey == aLookup;
810 static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; }
813 // The default hash policy, which only works with integers.
814 template<class Key>
815 struct DefaultHasher
817 using Lookup = Key;
819 static HashNumber hash(const Lookup& aLookup)
821 // Just convert the integer to a HashNumber and use that as is. (This
822 // discards the high 32-bits of 64-bit integers!) ScrambleHashCode() is
823 // subsequently called on the value to improve the distribution.
824 return aLookup;
827 static bool match(const Key& aKey, const Lookup& aLookup)
829 // Use builtin or overloaded operator==.
830 return aKey == aLookup;
833 static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; }
836 // A DefaultHasher specialization for pointers.
837 template<class T>
838 struct DefaultHasher<T*> : PointerHasher<T*>
842 // A DefaultHasher specialization for mozilla::UniquePtr.
843 template<class T, class D>
844 struct DefaultHasher<UniquePtr<T, D>>
846 using Key = UniquePtr<T, D>;
847 using Lookup = Key;
848 using PtrHasher = PointerHasher<T*>;
850 static HashNumber hash(const Lookup& aLookup)
852 return PtrHasher::hash(aLookup.get());
855 static bool match(const Key& aKey, const Lookup& aLookup)
857 return PtrHasher::match(aKey.get(), aLookup.get());
860 static void rekey(UniquePtr<T, D>& aKey, UniquePtr<T, D>&& aNewKey)
862 aKey = std::move(aNewKey);
866 // A DefaultHasher specialization for doubles.
867 template<>
868 struct DefaultHasher<double>
870 using Key = double;
871 using Lookup = Key;
873 static HashNumber hash(const Lookup& aLookup)
875 // Just xor the high bits with the low bits, and then treat the bits of the
876 // result as a uint32_t.
877 static_assert(sizeof(HashNumber) == 4,
878 "subsequent code assumes a four-byte hash");
879 uint64_t u = BitwiseCast<uint64_t>(aLookup);
880 return HashNumber(u ^ (u >> 32));
883 static bool match(const Key& aKey, const Lookup& aLookup)
885 return BitwiseCast<uint64_t>(aKey) == BitwiseCast<uint64_t>(aLookup);
889 // A DefaultHasher specialization for floats.
890 template<>
891 struct DefaultHasher<float>
893 using Key = float;
894 using Lookup = Key;
896 static HashNumber hash(const Lookup& aLookup)
898 // Just use the value as if its bits form an integer. ScrambleHashCode() is
899 // subsequently called on the value to improve the distribution.
900 static_assert(sizeof(HashNumber) == 4,
901 "subsequent code assumes a four-byte hash");
902 return HashNumber(BitwiseCast<uint32_t>(aLookup));
905 static bool match(const Key& aKey, const Lookup& aLookup)
907 return BitwiseCast<uint32_t>(aKey) == BitwiseCast<uint32_t>(aLookup);
911 // A hash policy for C strings.
912 struct CStringHasher
914 using Key = const char*;
915 using Lookup = const char*;
917 static HashNumber hash(const Lookup& aLookup) { return HashString(aLookup); }
919 static bool match(const Key& aKey, const Lookup& aLookup)
921 return strcmp(aKey, aLookup) == 0;
925 //---------------------------------------------------------------------------
926 // Fallible Hashing Interface
927 //---------------------------------------------------------------------------
929 // Most of the time generating a hash code is infallible so this class provides
930 // default methods that always succeed. Specialize this class for your own hash
931 // policy to provide fallible hashing.
933 // This is used by MovableCellHasher to handle the fact that generating a unique
934 // ID for cell pointer may fail due to OOM.
935 template<typename HashPolicy>
936 struct FallibleHashMethods
938 // Return true if a hashcode is already available for its argument. Once
939 // this returns true for a specific argument it must continue to do so.
940 template<typename Lookup>
941 static bool hasHash(Lookup&& aLookup)
943 return true;
946 // Fallible method to ensure a hashcode exists for its argument and create
947 // one if not. Returns false on error, e.g. out of memory.
948 template<typename Lookup>
949 static bool ensureHash(Lookup&& aLookup)
951 return true;
955 template<typename HashPolicy, typename Lookup>
956 static bool
957 HasHash(Lookup&& aLookup)
959 return FallibleHashMethods<typename HashPolicy::Base>::hasHash(
960 std::forward<Lookup>(aLookup));
963 template<typename HashPolicy, typename Lookup>
964 static bool
965 EnsureHash(Lookup&& aLookup)
967 return FallibleHashMethods<typename HashPolicy::Base>::ensureHash(
968 std::forward<Lookup>(aLookup));
971 //---------------------------------------------------------------------------
972 // Implementation Details (HashMapEntry, HashTableEntry, HashTable)
973 //---------------------------------------------------------------------------
975 // Both HashMap and HashSet are implemented by a single HashTable that is even
976 // more heavily parameterized than the other two. This leaves HashTable gnarly
977 // and extremely coupled to HashMap and HashSet; thus code should not use
978 // HashTable directly.
980 template<class Key, class Value>
981 class HashMapEntry
983 Key key_;
984 Value value_;
986 template<class, class, class>
987 friend class detail::HashTable;
988 template<class>
989 friend class detail::HashTableEntry;
990 template<class, class, class, class>
991 friend class HashMap;
993 public:
994 template<typename KeyInput, typename ValueInput>
995 HashMapEntry(KeyInput&& aKey, ValueInput&& aValue)
996 : key_(std::forward<KeyInput>(aKey))
997 , value_(std::forward<ValueInput>(aValue))
1001 HashMapEntry(HashMapEntry&& aRhs)
1002 : key_(std::move(aRhs.key_))
1003 , value_(std::move(aRhs.value_))
1007 void operator=(HashMapEntry&& aRhs)
1009 key_ = std::move(aRhs.key_);
1010 value_ = std::move(aRhs.value_);
1013 using KeyType = Key;
1014 using ValueType = Value;
1016 const Key& key() const { return key_; }
1018 // Use this method with caution! If the key is changed such that its hash
1019 // value also changes, the map will be left in an invalid state.
1020 Key& mutableKey() { return key_; }
1022 const Value& value() const { return value_; }
1023 Value& value() { return value_; }
1025 private:
1026 HashMapEntry(const HashMapEntry&) = delete;
1027 void operator=(const HashMapEntry&) = delete;
1030 template<typename K, typename V>
1031 struct IsPod<HashMapEntry<K, V>>
1032 : IntegralConstant<bool, IsPod<K>::value && IsPod<V>::value>
1036 namespace detail {
1038 template<class T, class HashPolicy, class AllocPolicy>
1039 class HashTable;
1041 template<typename T>
1042 class HashTableEntry
1044 private:
1045 using NonConstT = typename RemoveConst<T>::Type;
1047 static const HashNumber sFreeKey = 0;
1048 static const HashNumber sRemovedKey = 1;
1049 static const HashNumber sCollisionBit = 1;
1051 HashNumber mKeyHash = sFreeKey;
1052 alignas(NonConstT) unsigned char mValueData[sizeof(NonConstT)];
1054 private:
1055 template<class, class, class>
1056 friend class HashTable;
1058 // Some versions of GCC treat it as a -Wstrict-aliasing violation (ergo a
1059 // -Werror compile error) to reinterpret_cast<> |mValueData| to |T*|, even
1060 // through |void*|. Placing the latter cast in these separate functions
1061 // breaks the chain such that affected GCC versions no longer warn/error.
1062 void* rawValuePtr() { return mValueData; }
1064 static bool isLiveHash(HashNumber hash) { return hash > sRemovedKey; }
1066 HashTableEntry(const HashTableEntry&) = delete;
1067 void operator=(const HashTableEntry&) = delete;
1069 NonConstT* valuePtr() { return reinterpret_cast<NonConstT*>(rawValuePtr()); }
1071 void destroyStoredT()
1073 NonConstT* ptr = valuePtr();
1074 ptr->~T();
1075 MOZ_MAKE_MEM_UNDEFINED(ptr, sizeof(*ptr));
1078 public:
1079 HashTableEntry() = default;
1081 ~HashTableEntry()
1083 if (isLive()) {
1084 destroyStoredT();
1087 MOZ_MAKE_MEM_UNDEFINED(this, sizeof(*this));
1090 void destroy()
1092 MOZ_ASSERT(isLive());
1093 destroyStoredT();
1096 void swap(HashTableEntry* aOther)
1098 if (this == aOther) {
1099 return;
1101 MOZ_ASSERT(isLive());
1102 if (aOther->isLive()) {
1103 Swap(*valuePtr(), *aOther->valuePtr());
1104 } else {
1105 *aOther->valuePtr() = std::move(*valuePtr());
1106 destroy();
1108 Swap(mKeyHash, aOther->mKeyHash);
1111 T& get()
1113 MOZ_ASSERT(isLive());
1114 return *valuePtr();
1117 NonConstT& getMutable()
1119 MOZ_ASSERT(isLive());
1120 return *valuePtr();
1123 bool isFree() const { return mKeyHash == sFreeKey; }
1125 void clearLive()
1127 MOZ_ASSERT(isLive());
1128 mKeyHash = sFreeKey;
1129 destroyStoredT();
1132 void clear()
1134 if (isLive()) {
1135 destroyStoredT();
1137 MOZ_MAKE_MEM_UNDEFINED(this, sizeof(*this));
1138 mKeyHash = sFreeKey;
1141 bool isRemoved() const { return mKeyHash == sRemovedKey; }
1143 void removeLive()
1145 MOZ_ASSERT(isLive());
1146 mKeyHash = sRemovedKey;
1147 destroyStoredT();
1150 bool isLive() const { return isLiveHash(mKeyHash); }
1152 void setCollision()
1154 MOZ_ASSERT(isLive());
1155 mKeyHash |= sCollisionBit;
1158 void unsetCollision() { mKeyHash &= ~sCollisionBit; }
1160 bool hasCollision() const { return mKeyHash & sCollisionBit; }
1162 bool matchHash(HashNumber hn) { return (mKeyHash & ~sCollisionBit) == hn; }
1164 HashNumber getKeyHash() const { return mKeyHash & ~sCollisionBit; }
1166 template<typename... Args>
1167 void setLive(HashNumber aHashNumber, Args&&... aArgs)
1169 MOZ_ASSERT(!isLive());
1170 mKeyHash = aHashNumber;
1171 new (valuePtr()) T(std::forward<Args>(aArgs)...);
1172 MOZ_ASSERT(isLive());
1176 template<class T, class HashPolicy, class AllocPolicy>
1177 class HashTable : private AllocPolicy
1179 friend class mozilla::ReentrancyGuard;
1181 using NonConstT = typename RemoveConst<T>::Type;
1182 using Key = typename HashPolicy::KeyType;
1183 using Lookup = typename HashPolicy::Lookup;
1185 public:
1186 using Entry = HashTableEntry<T>;
1188 // A nullable pointer to a hash table element. A Ptr |p| can be tested
1189 // either explicitly |if (p.found()) p->...| or using boolean conversion
1190 // |if (p) p->...|. Ptr objects must not be used after any mutating hash
1191 // table operations unless |generation()| is tested.
1192 class Ptr
1194 friend class HashTable;
1196 Entry* mEntry;
1197 #ifdef DEBUG
1198 const HashTable* mTable;
1199 Generation mGeneration;
1200 #endif
1202 protected:
1203 Ptr(Entry& aEntry, const HashTable& aTable)
1204 : mEntry(&aEntry)
1205 #ifdef DEBUG
1206 , mTable(&aTable)
1207 , mGeneration(aTable.generation())
1208 #endif
1212 // This constructor is used only by AddPtr() within lookupForAdd().
1213 explicit Ptr(const HashTable& aTable)
1214 : mEntry(nullptr)
1215 #ifdef DEBUG
1216 , mTable(&aTable)
1217 , mGeneration(aTable.generation())
1218 #endif
1222 bool isValid() const { return !!mEntry; }
1224 public:
1225 Ptr()
1226 : mEntry(nullptr)
1227 #ifdef DEBUG
1228 , mTable(nullptr)
1229 , mGeneration(0)
1230 #endif
1234 bool found() const
1236 if (!isValid()) {
1237 return false;
1239 #ifdef DEBUG
1240 MOZ_ASSERT(mGeneration == mTable->generation());
1241 #endif
1242 return mEntry->isLive();
1245 explicit operator bool() const { return found(); }
1247 bool operator==(const Ptr& aRhs) const
1249 MOZ_ASSERT(found() && aRhs.found());
1250 return mEntry == aRhs.mEntry;
1253 bool operator!=(const Ptr& aRhs) const
1255 #ifdef DEBUG
1256 MOZ_ASSERT(mGeneration == mTable->generation());
1257 #endif
1258 return !(*this == aRhs);
1261 T& operator*() const
1263 #ifdef DEBUG
1264 MOZ_ASSERT(found());
1265 MOZ_ASSERT(mGeneration == mTable->generation());
1266 #endif
1267 return mEntry->get();
1270 T* operator->() const
1272 #ifdef DEBUG
1273 MOZ_ASSERT(found());
1274 MOZ_ASSERT(mGeneration == mTable->generation());
1275 #endif
1276 return &mEntry->get();
1280 // A Ptr that can be used to add a key after a failed lookup.
1281 class AddPtr : public Ptr
1283 friend class HashTable;
1285 HashNumber mKeyHash;
1286 #ifdef DEBUG
1287 uint64_t mMutationCount;
1288 #endif
1290 AddPtr(Entry& aEntry, const HashTable& aTable, HashNumber aHashNumber)
1291 : Ptr(aEntry, aTable)
1292 , mKeyHash(aHashNumber)
1293 #ifdef DEBUG
1294 , mMutationCount(aTable.mMutationCount)
1295 #endif
1299 // This constructor is used when lookupForAdd() is performed on a table
1300 // lacking entry storage; it leaves mEntry null but initializes everything
1301 // else.
1302 AddPtr(const HashTable& aTable, HashNumber aHashNumber)
1303 : Ptr(aTable)
1304 , mKeyHash(aHashNumber)
1305 #ifdef DEBUG
1306 , mMutationCount(aTable.mMutationCount)
1307 #endif
1309 MOZ_ASSERT(isLive());
1312 bool isLive() const { return isLiveHash(mKeyHash); }
1314 public:
1315 AddPtr()
1316 : mKeyHash(0)
1321 // A hash table iterator that (mostly) doesn't allow table modifications.
1322 // As with Ptr/AddPtr, Iterator objects must not be used after any mutating
1323 // hash table operation unless the |generation()| is tested.
1324 class Iterator
1326 protected:
1327 friend class HashTable;
1329 explicit Iterator(const HashTable& aTable)
1330 : mCur(aTable.mTable)
1331 , mEnd(aTable.mTable + aTable.capacity())
1332 #ifdef DEBUG
1333 , mTable(aTable)
1334 , mMutationCount(aTable.mMutationCount)
1335 , mGeneration(aTable.generation())
1336 , mValidEntry(true)
1337 #endif
1339 while (mCur < mEnd && !mCur->isLive()) {
1340 ++mCur;
1344 Entry* mCur;
1345 Entry* mEnd;
1346 #ifdef DEBUG
1347 const HashTable& mTable;
1348 uint64_t mMutationCount;
1349 Generation mGeneration;
1350 bool mValidEntry;
1351 #endif
1353 public:
1354 bool done() const
1356 #ifdef DEBUG
1357 MOZ_ASSERT(mGeneration == mTable.generation());
1358 MOZ_ASSERT(mMutationCount == mTable.mMutationCount);
1359 #endif
1360 return mCur == mEnd;
1363 T& get() const
1365 MOZ_ASSERT(!done());
1366 #ifdef DEBUG
1367 MOZ_ASSERT(mValidEntry);
1368 MOZ_ASSERT(mGeneration == mTable.generation());
1369 MOZ_ASSERT(mMutationCount == mTable.mMutationCount);
1370 #endif
1371 return mCur->get();
1374 void next()
1376 MOZ_ASSERT(!done());
1377 #ifdef DEBUG
1378 MOZ_ASSERT(mGeneration == mTable.generation());
1379 MOZ_ASSERT(mMutationCount == mTable.mMutationCount);
1380 #endif
1381 while (++mCur < mEnd && !mCur->isLive()) {
1382 continue;
1384 #ifdef DEBUG
1385 mValidEntry = true;
1386 #endif
1390 // A hash table iterator that permits modification, removal and rekeying.
1391 // Since rehashing when elements were removed during enumeration would be
1392 // bad, it is postponed until the ModIterator is destructed. Since the
1393 // ModIterator's destructor touches the hash table, the user must ensure
1394 // that the hash table is still alive when the destructor runs.
1395 class ModIterator : public Iterator
1397 friend class HashTable;
1399 HashTable& mTable;
1400 bool mRekeyed;
1401 bool mRemoved;
1403 // ModIterator is movable but not copyable.
1404 ModIterator(const ModIterator&) = delete;
1405 void operator=(const ModIterator&) = delete;
1407 protected:
1408 explicit ModIterator(HashTable& aTable)
1409 : Iterator(aTable)
1410 , mTable(aTable)
1411 , mRekeyed(false)
1412 , mRemoved(false)
1416 public:
1417 MOZ_IMPLICIT ModIterator(ModIterator&& aOther)
1418 : Iterator(aOther)
1419 , mTable(aOther.mTable)
1420 , mRekeyed(aOther.mRekeyed)
1421 , mRemoved(aOther.mRemoved)
1423 aOther.mRekeyed = false;
1424 aOther.mRemoved = false;
1427 // Removes the current element from the table, leaving |get()|
1428 // invalid until the next call to |next()|.
1429 void remove()
1431 mTable.remove(*this->mCur);
1432 mRemoved = true;
1433 #ifdef DEBUG
1434 this->mValidEntry = false;
1435 this->mMutationCount = mTable.mMutationCount;
1436 #endif
1439 NonConstT& getMutable()
1441 MOZ_ASSERT(!this->done());
1442 #ifdef DEBUG
1443 MOZ_ASSERT(this->mValidEntry);
1444 MOZ_ASSERT(this->mGeneration == this->Iterator::mTable.generation());
1445 MOZ_ASSERT(this->mMutationCount == this->Iterator::mTable.mMutationCount);
1446 #endif
1447 return this->mCur->getMutable();
1450 // Removes the current element and re-inserts it into the table with
1451 // a new key at the new Lookup position. |get()| is invalid after
1452 // this operation until the next call to |next()|.
1453 void rekey(const Lookup& l, const Key& k)
1455 MOZ_ASSERT(&k != &HashPolicy::getKey(this->mCur->get()));
1456 Ptr p(*this->mCur, mTable);
1457 mTable.rekeyWithoutRehash(p, l, k);
1458 mRekeyed = true;
1459 #ifdef DEBUG
1460 this->mValidEntry = false;
1461 this->mMutationCount = mTable.mMutationCount;
1462 #endif
1465 void rekey(const Key& k) { rekey(k, k); }
1467 // Potentially rehashes the table.
1468 ~ModIterator()
1470 if (mRekeyed) {
1471 mTable.mGen++;
1472 mTable.infallibleRehashIfOverloaded();
1475 if (mRemoved) {
1476 mTable.compact();
1481 // Range is similar to Iterator, but uses different terminology.
1482 class Range
1484 friend class HashTable;
1486 Iterator mIter;
1488 protected:
1489 explicit Range(const HashTable& table)
1490 : mIter(table)
1494 public:
1495 bool empty() const { return mIter.done(); }
1497 T& front() const { return mIter.get(); }
1499 void popFront() { return mIter.next(); }
1502 // Enum is similar to ModIterator, but uses different terminology.
1503 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)
1514 : mIter(map.mImpl)
1518 MOZ_IMPLICIT Enum(Enum&& other)
1519 : mIter(std::move(other.mIter))
1523 bool empty() const { return mIter.done(); }
1525 T& front() const { return mIter.get(); }
1527 void popFront() { return mIter.next(); }
1529 void removeFront() { mIter.remove(); }
1531 NonConstT& mutableFront() { return mIter.getMutable(); }
1533 void rekeyFront(const Lookup& aLookup, const Key& aKey)
1535 mIter.rekey(aLookup, aKey);
1538 void rekeyFront(const Key& aKey) { mIter.rekey(aKey); }
1541 // HashTable is movable
1542 HashTable(HashTable&& aRhs)
1543 : AllocPolicy(aRhs)
1545 PodAssign(this, &aRhs);
1546 aRhs.mTable = nullptr;
1548 void operator=(HashTable&& aRhs)
1550 MOZ_ASSERT(this != &aRhs, "self-move assignment is prohibited");
1551 if (mTable) {
1552 destroyTable(*this, mTable, capacity());
1554 PodAssign(this, &aRhs);
1555 aRhs.mTable = nullptr;
1558 private:
1559 // HashTable is not copyable or assignable
1560 HashTable(const HashTable&) = delete;
1561 void operator=(const HashTable&) = delete;
1563 static const uint32_t CAP_BITS = 30;
1565 public:
1566 uint64_t mGen : 56; // entry storage generation number
1567 uint64_t mHashShift : 8; // multiplicative hash shift
1568 Entry* mTable; // entry storage
1569 uint32_t mEntryCount; // number of entries in mTable
1570 uint32_t mRemovedCount; // removed entry sentinels in mTable
1572 #ifdef DEBUG
1573 uint64_t mMutationCount;
1574 mutable bool mEntered;
1575 #endif
1577 // The default initial capacity is 32 (enough to hold 16 elements), but it
1578 // can be as low as 4.
1579 static const uint32_t sDefaultLen = 16;
1580 static const uint32_t sMinCapacity = 4;
1581 static const uint32_t sMaxInit = 1u << (CAP_BITS - 1);
1582 static const uint32_t sMaxCapacity = 1u << CAP_BITS;
1584 // Hash-table alpha is conceptually a fraction, but to avoid floating-point
1585 // math we implement it as a ratio of integers.
1586 static const uint8_t sAlphaDenominator = 4;
1587 static const uint8_t sMinAlphaNumerator = 1; // min alpha: 1/4
1588 static const uint8_t sMaxAlphaNumerator = 3; // max alpha: 3/4
1590 static const HashNumber sFreeKey = Entry::sFreeKey;
1591 static const HashNumber sRemovedKey = Entry::sRemovedKey;
1592 static const HashNumber sCollisionBit = Entry::sCollisionBit;
1594 static uint32_t bestCapacity(uint32_t aLen)
1596 static_assert((sMaxInit * sAlphaDenominator) / sAlphaDenominator ==
1597 sMaxInit,
1598 "multiplication in numerator below could overflow");
1599 static_assert(sMaxInit * sAlphaDenominator <=
1600 UINT32_MAX - sMaxAlphaNumerator,
1601 "numerator calculation below could potentially overflow");
1603 // Compute the smallest capacity allowing |aLen| elements to be
1604 // inserted without rehashing: ceil(aLen / max-alpha). (Ceiling
1605 // integral division: <http://stackoverflow.com/a/2745086>.)
1606 uint32_t capacity =
1607 (aLen * sAlphaDenominator + sMaxAlphaNumerator - 1) / sMaxAlphaNumerator;
1608 capacity = (capacity < sMinCapacity)
1609 ? sMinCapacity
1610 : RoundUpPow2(capacity);
1612 MOZ_ASSERT(capacity >= aLen);
1613 MOZ_ASSERT(capacity <= sMaxCapacity);
1615 return capacity;
1618 static uint32_t hashShift(uint32_t aLen)
1620 // Reject all lengths whose initial computed capacity would exceed
1621 // sMaxCapacity. Round that maximum aLen down to the nearest power of two
1622 // for speedier code.
1623 if (MOZ_UNLIKELY(aLen > sMaxInit)) {
1624 MOZ_CRASH("initial length is too large");
1627 return kHashNumberBits - mozilla::CeilingLog2(bestCapacity(aLen));
1630 static bool isLiveHash(HashNumber aHash) { return Entry::isLiveHash(aHash); }
1632 static HashNumber prepareHash(const Lookup& aLookup)
1634 HashNumber keyHash = ScrambleHashCode(HashPolicy::hash(aLookup));
1636 // Avoid reserved hash codes.
1637 if (!isLiveHash(keyHash)) {
1638 keyHash -= (sRemovedKey + 1);
1640 return keyHash & ~sCollisionBit;
1643 enum FailureBehavior
1645 DontReportFailure = false,
1646 ReportFailure = true
1649 static Entry* createTable(AllocPolicy& aAllocPolicy,
1650 uint32_t aCapacity,
1651 FailureBehavior aReportFailure = ReportFailure)
1653 Entry* table = aReportFailure
1654 ? aAllocPolicy.template pod_malloc<Entry>(aCapacity)
1655 : aAllocPolicy.template maybe_pod_malloc<Entry>(aCapacity);
1656 if (table) {
1657 for (uint32_t i = 0; i < aCapacity; i++) {
1658 new (&table[i]) Entry();
1661 return table;
1664 static void destroyTable(AllocPolicy& aAllocPolicy,
1665 Entry* aOldTable,
1666 uint32_t aCapacity)
1668 Entry* end = aOldTable + aCapacity;
1669 for (Entry* e = aOldTable; e < end; ++e) {
1670 e->~Entry();
1672 aAllocPolicy.free_(aOldTable, aCapacity);
1675 public:
1676 HashTable(AllocPolicy aAllocPolicy, uint32_t aLen)
1677 : AllocPolicy(aAllocPolicy)
1678 , mGen(0)
1679 , mHashShift(hashShift(aLen))
1680 , mTable(nullptr)
1681 , mEntryCount(0)
1682 , mRemovedCount(0)
1683 #ifdef DEBUG
1684 , mMutationCount(0)
1685 , mEntered(false)
1686 #endif
1690 explicit HashTable(AllocPolicy aAllocPolicy)
1691 : HashTable(aAllocPolicy, sDefaultLen)
1695 ~HashTable()
1697 if (mTable) {
1698 destroyTable(*this, mTable, capacity());
1702 private:
1703 HashNumber hash1(HashNumber aHash0) const { return aHash0 >> mHashShift; }
1705 struct DoubleHash
1707 HashNumber mHash2;
1708 HashNumber mSizeMask;
1711 DoubleHash hash2(HashNumber aCurKeyHash) const
1713 uint32_t sizeLog2 = kHashNumberBits - mHashShift;
1714 DoubleHash dh = { ((aCurKeyHash << sizeLog2) >> mHashShift) | 1,
1715 (HashNumber(1) << sizeLog2) - 1 };
1716 return dh;
1719 static HashNumber applyDoubleHash(HashNumber aHash1,
1720 const DoubleHash& aDoubleHash)
1722 return (aHash1 - aDoubleHash.mHash2) & aDoubleHash.mSizeMask;
1725 static MOZ_ALWAYS_INLINE bool match(Entry& aEntry, const Lookup& aLookup)
1727 return HashPolicy::match(HashPolicy::getKey(aEntry.get()), aLookup);
1730 enum LookupReason
1732 ForNonAdd,
1733 ForAdd
1736 // Warning: in order for readonlyThreadsafeLookup() to be safe this
1737 // function must not modify the table in any way when Reason==ForNonAdd.
1738 template<LookupReason Reason>
1739 MOZ_ALWAYS_INLINE Entry& lookup(const Lookup& aLookup,
1740 HashNumber aKeyHash) const
1742 MOZ_ASSERT(isLiveHash(aKeyHash));
1743 MOZ_ASSERT(!(aKeyHash & sCollisionBit));
1744 MOZ_ASSERT(mTable);
1746 // Compute the primary hash address.
1747 HashNumber h1 = hash1(aKeyHash);
1748 Entry* entry = &mTable[h1];
1750 // Miss: return space for a new entry.
1751 if (entry->isFree()) {
1752 return *entry;
1755 // Hit: return entry.
1756 if (entry->matchHash(aKeyHash) && match(*entry, aLookup)) {
1757 return *entry;
1760 // Collision: double hash.
1761 DoubleHash dh = hash2(aKeyHash);
1763 // Save the first removed entry pointer so we can recycle later.
1764 Entry* firstRemoved = nullptr;
1766 while (true) {
1767 if (Reason == ForAdd && !firstRemoved) {
1768 if (MOZ_UNLIKELY(entry->isRemoved())) {
1769 firstRemoved = entry;
1770 } else {
1771 entry->setCollision();
1775 h1 = applyDoubleHash(h1, dh);
1777 entry = &mTable[h1];
1778 if (entry->isFree()) {
1779 return firstRemoved ? *firstRemoved : *entry;
1782 if (entry->matchHash(aKeyHash) && match(*entry, aLookup)) {
1783 return *entry;
1788 // This is a copy of lookup() hardcoded to the assumptions:
1789 // 1. the lookup is for an add;
1790 // 2. the key, whose |keyHash| has been passed, is not in the table.
1791 Entry& findNonLiveEntry(HashNumber aKeyHash)
1793 MOZ_ASSERT(!(aKeyHash & sCollisionBit));
1794 MOZ_ASSERT(mTable);
1796 // We assume 'aKeyHash' has already been distributed.
1798 // Compute the primary hash address.
1799 HashNumber h1 = hash1(aKeyHash);
1800 Entry* entry = &mTable[h1];
1802 // Miss: return space for a new entry.
1803 if (!entry->isLive()) {
1804 return *entry;
1807 // Collision: double hash.
1808 DoubleHash dh = hash2(aKeyHash);
1810 while (true) {
1811 entry->setCollision();
1813 h1 = applyDoubleHash(h1, dh);
1815 entry = &mTable[h1];
1816 if (!entry->isLive()) {
1817 return *entry;
1822 enum RebuildStatus
1824 NotOverloaded,
1825 Rehashed,
1826 RehashFailed
1829 RebuildStatus changeTableSize(uint32_t newCapacity,
1830 FailureBehavior aReportFailure = ReportFailure)
1832 MOZ_ASSERT(IsPowerOfTwo(newCapacity));
1833 MOZ_ASSERT(!!mTable == !!capacity());
1835 // Look, but don't touch, until we succeed in getting new entry store.
1836 Entry* oldTable = mTable;
1837 uint32_t oldCapacity = capacity();
1838 uint32_t newLog2 = mozilla::CeilingLog2(newCapacity);
1840 if (MOZ_UNLIKELY(newCapacity > sMaxCapacity)) {
1841 if (aReportFailure) {
1842 this->reportAllocOverflow();
1844 return RehashFailed;
1847 Entry* newTable = createTable(*this, newCapacity, aReportFailure);
1848 if (!newTable) {
1849 return RehashFailed;
1852 // We can't fail from here on, so update table parameters.
1853 mHashShift = kHashNumberBits - newLog2;
1854 mRemovedCount = 0;
1855 mGen++;
1856 mTable = newTable;
1858 // Copy only live entries, leaving removed ones behind.
1859 Entry* end = oldTable + oldCapacity;
1860 for (Entry* src = oldTable; src < end; ++src) {
1861 if (src->isLive()) {
1862 HashNumber hn = src->getKeyHash();
1863 findNonLiveEntry(hn).setLive(
1864 hn, std::move(const_cast<typename Entry::NonConstT&>(src->get())));
1867 src->~Entry();
1870 // All entries have been destroyed, no need to destroyTable.
1871 this->free_(oldTable, oldCapacity);
1872 return Rehashed;
1875 RebuildStatus rehashIfOverloaded(
1876 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()
1901 if (rehashIfOverloaded(DontReportFailure) == RehashFailed) {
1902 rehashTableInPlace();
1906 void remove(Entry& aEntry)
1908 MOZ_ASSERT(mTable);
1910 if (aEntry.hasCollision()) {
1911 aEntry.removeLive();
1912 mRemovedCount++;
1913 } else {
1914 aEntry.clearLive();
1916 mEntryCount--;
1917 #ifdef DEBUG
1918 mMutationCount++;
1919 #endif
1922 void shrinkIfUnderloaded()
1924 static_assert(sMaxCapacity <= UINT32_MAX / sMinAlphaNumerator,
1925 "multiplication below could overflow");
1926 bool underloaded =
1927 capacity() > sMinCapacity &&
1928 mEntryCount <= capacity() * sMinAlphaNumerator / sAlphaDenominator;
1930 if (underloaded) {
1931 (void)changeTableSize(capacity() / 2, DontReportFailure);
1935 // This is identical to changeTableSize(currentSize), but without requiring
1936 // a second table. We do this by recycling the collision bits to tell us if
1937 // the element is already inserted or still waiting to be inserted. Since
1938 // already-inserted elements win any conflicts, we get the same table as we
1939 // would have gotten through random insertion order.
1940 void rehashTableInPlace()
1942 mRemovedCount = 0;
1943 mGen++;
1944 for (uint32_t i = 0; i < capacity(); ++i) {
1945 mTable[i].unsetCollision();
1947 for (uint32_t i = 0; i < capacity();) {
1948 Entry* src = &mTable[i];
1950 if (!src->isLive() || src->hasCollision()) {
1951 ++i;
1952 continue;
1955 HashNumber keyHash = src->getKeyHash();
1956 HashNumber h1 = hash1(keyHash);
1957 DoubleHash dh = hash2(keyHash);
1958 Entry* tgt = &mTable[h1];
1959 while (true) {
1960 if (!tgt->hasCollision()) {
1961 src->swap(tgt);
1962 tgt->setCollision();
1963 break;
1966 h1 = applyDoubleHash(h1, dh);
1967 tgt = &mTable[h1];
1971 // TODO: this algorithm leaves collision bits on *all* elements, even if
1972 // they are on no collision path. We have the option of setting the
1973 // collision bits correctly on a subsequent pass or skipping the rehash
1974 // unless we are totally filled with tombstones: benchmark to find out
1975 // which approach is best.
1978 // Note: |aLookup| may be a reference to a piece of |u|, so this function
1979 // must take care not to use |aLookup| after moving |u|.
1981 // Prefer to use putNewInfallible; this function does not check
1982 // invariants.
1983 template<typename... Args>
1984 void putNewInfallibleInternal(const Lookup& aLookup, Args&&... aArgs)
1986 MOZ_ASSERT(mTable);
1988 HashNumber keyHash = prepareHash(aLookup);
1989 Entry* entry = &findNonLiveEntry(keyHash);
1990 MOZ_ASSERT(entry);
1992 if (entry->isRemoved()) {
1993 mRemovedCount--;
1994 keyHash |= sCollisionBit;
1997 entry->setLive(keyHash, std::forward<Args>(aArgs)...);
1998 mEntryCount++;
1999 #ifdef DEBUG
2000 mMutationCount++;
2001 #endif
2004 public:
2005 void clear()
2007 Entry* end = mTable + capacity();
2008 for (Entry* e = mTable; e < end; ++e) {
2009 e->clear();
2011 mRemovedCount = 0;
2012 mEntryCount = 0;
2013 #ifdef DEBUG
2014 mMutationCount++;
2015 #endif
2018 // Resize the table down to the smallest capacity that doesn't overload the
2019 // table. Since we call shrinkIfUnderloaded() on every remove, you only need
2020 // to call this after a bulk removal of items done without calling remove().
2021 void compact()
2023 if (empty()) {
2024 // Free the entry storage.
2025 this->free_(mTable, capacity());
2026 mGen++;
2027 mHashShift = hashShift(0); // gives minimum capacity on regrowth
2028 mTable = nullptr;
2029 mRemovedCount = 0;
2030 return;
2033 uint32_t bestCapacity = this->bestCapacity(mEntryCount);
2034 MOZ_ASSERT(bestCapacity <= capacity());
2036 if (bestCapacity < capacity()) {
2037 (void)changeTableSize(bestCapacity, DontReportFailure);
2041 void clearAndCompact()
2043 clear();
2044 compact();
2047 MOZ_MUST_USE bool reserve(uint32_t aLen)
2049 if (aLen == 0) {
2050 return true;
2053 uint32_t bestCapacity = this->bestCapacity(aLen);
2054 if (bestCapacity <= capacity()) {
2055 return true; // Capacity is already sufficient.
2058 RebuildStatus status = changeTableSize(bestCapacity, ReportFailure);
2059 MOZ_ASSERT(status != NotOverloaded);
2060 return status != RehashFailed;
2063 Iterator iter() const
2065 return Iterator(*this);
2068 ModIterator modIter()
2070 return ModIterator(*this);
2073 Range all() const
2075 return Range(*this);
2078 bool empty() const
2080 return mEntryCount == 0;
2083 uint32_t count() const
2085 return mEntryCount;
2088 uint32_t rawCapacity() const
2090 return 1u << (kHashNumberBits - mHashShift);
2093 uint32_t capacity() const
2095 return mTable ? rawCapacity() : 0;
2098 Generation generation() const
2100 return Generation(mGen);
2103 size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
2105 return aMallocSizeOf(mTable);
2108 size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
2110 return aMallocSizeOf(this) + shallowSizeOfExcludingThis(aMallocSizeOf);
2113 MOZ_ALWAYS_INLINE Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const
2115 if (!mTable || !HasHash<HashPolicy>(aLookup)) {
2116 return Ptr();
2118 HashNumber keyHash = prepareHash(aLookup);
2119 return Ptr(lookup<ForNonAdd>(aLookup, keyHash), *this);
2122 MOZ_ALWAYS_INLINE Ptr lookup(const Lookup& aLookup) const
2124 ReentrancyGuard g(*this);
2125 return readonlyThreadsafeLookup(aLookup);
2128 MOZ_ALWAYS_INLINE AddPtr lookupForAdd(const Lookup& aLookup)
2130 ReentrancyGuard g(*this);
2131 if (!EnsureHash<HashPolicy>(aLookup)) {
2132 return AddPtr();
2135 HashNumber keyHash = prepareHash(aLookup);
2137 if (!mTable) {
2138 return AddPtr(*this, keyHash);
2141 // Directly call the constructor in the return statement to avoid
2142 // excess copying when building with Visual Studio 2017.
2143 // See bug 1385181.
2144 return AddPtr(lookup<ForAdd>(aLookup, keyHash), *this, keyHash);
2147 template<typename... Args>
2148 MOZ_MUST_USE bool add(AddPtr& aPtr, Args&&... aArgs)
2150 ReentrancyGuard g(*this);
2151 MOZ_ASSERT_IF(aPtr.isValid(), mTable);
2152 MOZ_ASSERT_IF(aPtr.isValid(), aPtr.mTable == this);
2153 MOZ_ASSERT(!aPtr.found());
2154 MOZ_ASSERT(!(aPtr.mKeyHash & sCollisionBit));
2156 // Check for error from ensureHash() here.
2157 if (!aPtr.isLive()) {
2158 return false;
2161 MOZ_ASSERT(aPtr.mGeneration == generation());
2162 #ifdef DEBUG
2163 MOZ_ASSERT(aPtr.mMutationCount == mMutationCount);
2164 #endif
2166 if (!aPtr.isValid()) {
2167 MOZ_ASSERT(!mTable && mEntryCount == 0);
2168 uint32_t newCapacity = rawCapacity();
2169 RebuildStatus status = changeTableSize(newCapacity, ReportFailure);
2170 MOZ_ASSERT(status != NotOverloaded);
2171 if (status == RehashFailed) {
2172 return false;
2174 aPtr.mEntry = &findNonLiveEntry(aPtr.mKeyHash);
2176 } else if (aPtr.mEntry->isRemoved()) {
2177 // Changing an entry from removed to live does not affect whether we are
2178 // overloaded and can be handled separately.
2179 if (!this->checkSimulatedOOM()) {
2180 return false;
2182 mRemovedCount--;
2183 aPtr.mKeyHash |= sCollisionBit;
2185 } else {
2186 // Preserve the validity of |aPtr.mEntry|.
2187 RebuildStatus status = rehashIfOverloaded();
2188 if (status == RehashFailed) {
2189 return false;
2191 if (status == NotOverloaded && !this->checkSimulatedOOM()) {
2192 return false;
2194 if (status == Rehashed) {
2195 aPtr.mEntry = &findNonLiveEntry(aPtr.mKeyHash);
2199 aPtr.mEntry->setLive(aPtr.mKeyHash, std::forward<Args>(aArgs)...);
2200 mEntryCount++;
2201 #ifdef DEBUG
2202 mMutationCount++;
2203 aPtr.mGeneration = generation();
2204 aPtr.mMutationCount = mMutationCount;
2205 #endif
2206 return true;
2209 // Note: |aLookup| may be a reference to a piece of |u|, so this function
2210 // must take care not to use |aLookup| after moving |u|.
2211 template<typename... Args>
2212 void putNewInfallible(const Lookup& aLookup, Args&&... aArgs)
2214 MOZ_ASSERT(!lookup(aLookup).found());
2215 ReentrancyGuard g(*this);
2216 putNewInfallibleInternal(aLookup, std::forward<Args>(aArgs)...);
2219 // Note: |aLookup| may be alias arguments in |aArgs|, so this function must
2220 // take care not to use |aLookup| after moving |aArgs|.
2221 template<typename... Args>
2222 MOZ_MUST_USE bool putNew(const Lookup& aLookup, Args&&... aArgs)
2224 if (!this->checkSimulatedOOM()) {
2225 return false;
2227 if (!EnsureHash<HashPolicy>(aLookup)) {
2228 return false;
2230 if (rehashIfOverloaded() == RehashFailed) {
2231 return false;
2233 putNewInfallible(aLookup, std::forward<Args>(aArgs)...);
2234 return true;
2237 // Note: |aLookup| may be a reference to a piece of |u|, so this function
2238 // must take care not to use |aLookup| after moving |u|.
2239 template<typename... Args>
2240 MOZ_MUST_USE bool relookupOrAdd(AddPtr& aPtr,
2241 const Lookup& aLookup,
2242 Args&&... aArgs)
2244 // Check for error from ensureHash() here.
2245 if (!aPtr.isLive()) {
2246 return false;
2248 #ifdef DEBUG
2249 aPtr.mGeneration = generation();
2250 aPtr.mMutationCount = mMutationCount;
2251 #endif
2252 if (mTable) {
2253 ReentrancyGuard g(*this);
2254 // Check that aLookup has not been destroyed.
2255 MOZ_ASSERT(prepareHash(aLookup) == aPtr.mKeyHash);
2256 aPtr.mEntry = &lookup<ForAdd>(aLookup, aPtr.mKeyHash);
2257 if (aPtr.found()) {
2258 return true;
2260 } else {
2261 // Clear aPtr so it's invalid; add() will allocate storage and redo the
2262 // lookup.
2263 aPtr.mEntry = nullptr;
2265 return add(aPtr, std::forward<Args>(aArgs)...);
2268 void remove(Ptr aPtr)
2270 MOZ_ASSERT(mTable);
2271 ReentrancyGuard g(*this);
2272 MOZ_ASSERT(aPtr.found());
2273 MOZ_ASSERT(aPtr.mGeneration == generation());
2274 remove(*aPtr.mEntry);
2275 shrinkIfUnderloaded();
2278 void rekeyWithoutRehash(Ptr aPtr, const Lookup& aLookup, const Key& aKey)
2280 MOZ_ASSERT(mTable);
2281 ReentrancyGuard g(*this);
2282 MOZ_ASSERT(aPtr.found());
2283 MOZ_ASSERT(aPtr.mGeneration == generation());
2284 typename HashTableEntry<T>::NonConstT t(std::move(*aPtr));
2285 HashPolicy::setKey(t, const_cast<Key&>(aKey));
2286 remove(*aPtr.mEntry);
2287 putNewInfallibleInternal(aLookup, std::move(t));
2290 void rekeyAndMaybeRehash(Ptr aPtr, const Lookup& aLookup, const Key& aKey)
2292 rekeyWithoutRehash(aPtr, aLookup, aKey);
2293 infallibleRehashIfOverloaded();
2297 } // namespace detail
2298 } // namespace mozilla
2300 #endif /* mozilla_HashTable_h */