| 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/. */ |
| 6 |
|
| 7 |
#ifndef builtin_OrderedHashTableObject_h |
| 8 |
#define builtin_OrderedHashTableObject_h |
| 9 |
|
| 10 |
/* |
| 11 |
* This file defines js::OrderedHashMapObject (a base class of js::MapObject) |
| 12 |
* and js::OrderedHashSetObject (a base class of js::SetObject). |
| 13 |
* |
| 14 |
* It also defines two templates, js::OrderedHashMapImpl and |
| 15 |
* js::OrderedHashSetImpl, that operate on these objects and implement the |
| 16 |
* ordered hash table algorithm. These templates are defined separately from |
| 17 |
* the JS object types because it lets us switch between different template |
| 18 |
* instantiations to enable or disable GC barriers. |
| 19 |
* |
| 20 |
* The implemented hash table algorithm is also different from HashMap and |
| 21 |
* HashSet: |
| 22 |
* |
| 23 |
* - Iterating over an Ordered hash table visits the entries in the order in |
| 24 |
* which they were inserted. This means that unlike a HashMap, the behavior |
| 25 |
* of an OrderedHashMapImpl is deterministic (as long as the HashPolicy |
| 26 |
* methods are effect-free and consistent); the hashing is a pure |
| 27 |
* performance optimization. |
| 28 |
* |
| 29 |
* - Iterator objects remain valid even when entries are added or removed or |
| 30 |
* the table is resized. |
| 31 |
* |
| 32 |
* Hash policies |
| 33 |
* |
| 34 |
* See the comment about "Hash policy" in HashTable.h for general features that |
| 35 |
* hash policy classes must provide. Hash policies for OrderedHashMapImpl and |
| 36 |
* Sets differ in that the hash() method takes an extra argument: |
| 37 |
* |
| 38 |
* static js::HashNumber hash(Lookup, const HashCodeScrambler&); |
| 39 |
* |
| 40 |
* They must additionally provide a distinguished "empty" key value and the |
| 41 |
* following static member functions: |
| 42 |
* |
| 43 |
* bool isEmpty(const Key&); |
| 44 |
* void makeEmpty(Key*); |
| 45 |
*/ |
| 46 |
|
| 47 |
#include "mozilla/CheckedInt.h" |
| 48 |
#include "mozilla/HashFunctions.h" |
| 49 |
#include "mozilla/Likely.h" |
| 50 |
#include "mozilla/Maybe.h" |
| 51 |
#include "mozilla/MemoryReporting.h" |
| 52 |
#include "mozilla/TemplateLib.h" |
| 53 |
|
| 54 |
#include <memory> |
| 55 |
#include <tuple> |
| 56 |
#include <utility> |
| 57 |
|
| 58 |
#include "builtin/SelfHostingDefines.h" |
| 59 |
#include "gc/Barrier.h" |
| 60 |
#include "gc/Zone.h" |
| 61 |
#include "js/GCPolicyAPI.h" |
| 62 |
#include "js/HashTable.h" |
| 63 |
#include "vm/JSContext.h" |
| 64 |
#include "vm/Realm.h" |
| 65 |
|
| 66 |
class JSTracer; |
| 67 |
|
| 68 |
namespace js { |
| 69 |
|
| 70 |
class MapIteratorObject; |
| 71 |
class SetIteratorObject; |
| 72 |
|
| 73 |
namespace detail { |
| 74 |
|
| 75 |
template <class T, class Ops> |
| 76 |
class OrderedHashTableImpl; |
| 77 |
|
| 78 |
// Base class for OrderedHashMapObject and OrderedHashSetObject. |
| 79 |
class OrderedHashTableObject : public NativeObject { |
| 80 |
// Use a friend class to avoid exposing these slot definitions directly to |
| 81 |
// MapObject and SetObject. |
| 82 |
template <class T, class Ops> |
| 83 |
friend class OrderedHashTableImpl; |
| 84 |
|
| 85 |
enum Slots { |
| 86 |
HashTableSlot, |
| 87 |
DataSlot, |
| 88 |
DataLengthSlot, |
| 89 |
DataCapacitySlot, |
| 90 |
LiveCountSlot, |
| 91 |
HashShiftSlot, |
| 92 |
TenuredIteratorsSlot, |
| 93 |
NurseryIteratorsSlot, |
| 94 |
HashCodeScramblerSlot, |
| 95 |
SlotCount |
| 96 |
}; |
| 97 |
|
| 98 |
inline void* allocateCellBuffer(JSContext* cx, size_t numBytes); |
| 99 |
|
| 100 |
public: |
| 101 |
static constexpr size_t offsetOfDataLength() { |
| 102 |
return getFixedSlotOffset(DataLengthSlot); |
| 103 |
} |
| 104 |
static constexpr size_t offsetOfData() { |
| 105 |
return getFixedSlotOffset(DataSlot); |
| 106 |
} |
| 107 |
static constexpr size_t offsetOfHashTable() { |
| 108 |
return getFixedSlotOffset(HashTableSlot); |
| 109 |
} |
| 110 |
static constexpr size_t offsetOfHashShift() { |
| 111 |
return getFixedSlotOffset(HashShiftSlot); |
| 112 |
} |
| 113 |
static constexpr size_t offsetOfLiveCount() { |
| 114 |
return getFixedSlotOffset(LiveCountSlot); |
| 115 |
} |
| 116 |
static constexpr size_t offsetOfHashCodeScrambler() { |
| 117 |
return getFixedSlotOffset(HashCodeScramblerSlot); |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 121 |
} // namespace detail |
| 122 |
|
| 123 |
// Base class for MapIteratorObject and SetIteratorObject. |
| 124 |
// |
| 125 |
// Iterators remain valid for the lifetime of the OrderedHashTableObject, even |
| 126 |
// if entries are added or removed or if the table is resized. Hash table |
| 127 |
// objects have a doubly linked list of all active iterator objects and this |
| 128 |
// lets us update the iterators when needed. |
| 129 |
// |
| 130 |
// An "active" iterator object has a target object and must be part of its |
| 131 |
// linked list of iterators. When the iterator is finished, the target slot is |
| 132 |
// cleared and the iterator is removed from the linked list. |
| 133 |
// |
| 134 |
// Note: the iterator list stores pointers to iterators as PrivateValue instead |
| 135 |
// of ObjectValue because we want these links to be weak pointers: an iterator |
| 136 |
// must not keep all other iterators alive. |
| 137 |
class TableIteratorObject : public NativeObject { |
| 138 |
template <class T, class Ops> |
| 139 |
friend class detail::OrderedHashTableImpl; |
| 140 |
|
| 141 |
public: |
| 142 |
enum Slots { |
| 143 |
TargetSlot, |
| 144 |
KindSlot, |
| 145 |
IndexSlot, |
| 146 |
CountSlot, |
| 147 |
PrevPtrSlot, |
| 148 |
NextSlot, |
| 149 |
SlotCount |
| 150 |
}; |
| 151 |
enum class Kind { Keys, Values, Entries }; |
| 152 |
|
| 153 |
// Slot numbers and Kind values must match constants used in self-hosted code. |
| 154 |
static_assert(TargetSlot == ITERATOR_SLOT_TARGET); |
| 155 |
static_assert(KindSlot == MAP_SET_ITERATOR_SLOT_ITEM_KIND); |
| 156 |
static_assert(int32_t(Kind::Keys) == ITEM_KIND_KEY); |
| 157 |
static_assert(int32_t(Kind::Values) == ITEM_KIND_VALUE); |
| 158 |
static_assert(int32_t(Kind::Entries) == ITEM_KIND_KEY_AND_VALUE); |
| 159 |
|
| 160 |
private: |
| 161 |
// The index of the current entry within the table's data array. |
| 162 |
uint32_t getIndex() const { |
| 163 |
return getReservedSlot(IndexSlot).toPrivateUint32(); |
| 164 |
} |
| 165 |
void setIndex(uint32_t i) { |
| 166 |
MOZ_ASSERT(isActive()); |
| 167 |
setReservedSlotPrivateUint32Unbarriered(IndexSlot, i); |
| 168 |
} |
| 169 |
|
| 170 |
// The number of nonempty entries in the data array to the left of |index|. |
| 171 |
// This is used when the table is resized or compacted. |
| 172 |
uint32_t getCount() const { |
| 173 |
return getReservedSlot(CountSlot).toPrivateUint32(); |
| 174 |
} |
| 175 |
void setCount(uint32_t i) { |
| 176 |
MOZ_ASSERT(isActive()); |
| 177 |
setReservedSlotPrivateUint32Unbarriered(CountSlot, i); |
| 178 |
} |
| 179 |
|
| 180 |
// Links in the doubly-linked list of active iterator objects on the |
| 181 |
// OrderedHashTableObject. |
| 182 |
// |
| 183 |
// The PrevPtr slot points to either the previous iterator's NextSlot or to |
| 184 |
// the table's TenuredIteratorsSlot or NurseryIteratorsSlot if this is the |
| 185 |
// first iterator in the list. |
| 186 |
// |
| 187 |
// The Next slot points to the next iterator object, or is nullptr if this is |
| 188 |
// the last iterator in the list. |
| 189 |
// |
| 190 |
// Invariant: *getPrevPtr() == this. |
| 191 |
TableIteratorObject** getPrevPtr() const { |
| 192 |
MOZ_ASSERT(isActive()); |
| 193 |
Value v = getReservedSlot(PrevPtrSlot); |
| 194 |
return static_cast<TableIteratorObject**>(v.toPrivate()); |
| 195 |
} |
| 196 |
void setPrevPtr(TableIteratorObject** p) { |
| 197 |
MOZ_ASSERT(isActive()); |
| 198 |
setReservedSlotPrivateUnbarriered(PrevPtrSlot, p); |
| 199 |
} |
| 200 |
TableIteratorObject* getNext() const { |
| 201 |
MOZ_ASSERT(isActive()); |
| 202 |
Value v = getReservedSlot(NextSlot); |
| 203 |
return static_cast<TableIteratorObject*>(v.toPrivate()); |
| 204 |
} |
| 205 |
TableIteratorObject** addressOfNext() { |
| 206 |
MOZ_ASSERT(isActive()); |
| 207 |
return addressOfFixedSlotPrivatePtr<TableIteratorObject>(NextSlot); |
| 208 |
} |
| 209 |
void setNext(TableIteratorObject* p) { |
| 210 |
MOZ_ASSERT(isActive()); |
| 211 |
setReservedSlotPrivateUnbarriered(NextSlot, p); |
| 212 |
} |
| 213 |
|
| 214 |
void link(TableIteratorObject** listp) { |
| 215 |
MOZ_ASSERT(isActive()); |
| 216 |
TableIteratorObject* next = *listp; |
| 217 |
setPrevPtr(listp); |
| 218 |
setNext(next); |
| 219 |
*listp = this; |
| 220 |
if (next) { |
| 221 |
next->setPrevPtr(this->addressOfNext()); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
void init(detail::OrderedHashTableObject* target, Kind kind, |
| 226 |
TableIteratorObject** listp) { |
| 227 |
initReservedSlot(TargetSlot, ObjectValue(*target)); |
| 228 |
setReservedSlotPrivateUint32Unbarriered(KindSlot, uint32_t(kind)); |
| 229 |
setReservedSlotPrivateUint32Unbarriered(IndexSlot, 0); |
| 230 |
setReservedSlotPrivateUint32Unbarriered(CountSlot, 0); |
| 231 |
link(listp); |
| 232 |
} |
| 233 |
|
| 234 |
void assertActiveIteratorFor(JSObject* target) { |
| 235 |
MOZ_ASSERT(&getReservedSlot(TargetSlot).toObject() == target); |
| 236 |
MOZ_ASSERT(*getPrevPtr() == this); |
| 237 |
MOZ_ASSERT(getNext() != this); |
| 238 |
} |
| 239 |
|
| 240 |
protected: |
| 241 |
bool isActive() const { return getReservedSlot(TargetSlot).isObject(); } |
| 242 |
|
| 243 |
void finish() { |
| 244 |
MOZ_ASSERT(isActive()); |
| 245 |
unlink(); |
| 246 |
// Mark iterator inactive. |
| 247 |
setReservedSlot(TargetSlot, UndefinedValue()); |
| 248 |
} |
| 249 |
void unlink() { |
| 250 |
MOZ_ASSERT(isActive()); |
| 251 |
TableIteratorObject** prevp = getPrevPtr(); |
| 252 |
TableIteratorObject* next = getNext(); |
| 253 |
*prevp = next; |
| 254 |
if (next) { |
| 255 |
next->setPrevPtr(prevp); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
// Update list pointers after a compacting GC moved this iterator in memory. |
| 260 |
// Note: this isn't used for nursery iterators because in that case we have to |
| 261 |
// rebuild the list with clearNurseryIterators and relinkNurseryIterator. |
| 262 |
void updateListAfterMove(TableIteratorObject* old) { |
| 263 |
MOZ_ASSERT(!IsInsideNursery(old)); |
| 264 |
MOZ_ASSERT(isActive()); |
| 265 |
MOZ_ASSERT(old != this); |
| 266 |
|
| 267 |
TableIteratorObject** prevp = getPrevPtr(); |
| 268 |
MOZ_ASSERT(*prevp == old); |
| 269 |
*prevp = this; |
| 270 |
|
| 271 |
if (TableIteratorObject* next = getNext()) { |
| 272 |
MOZ_ASSERT(next->getPrevPtr() == old->addressOfNext()); |
| 273 |
next->setPrevPtr(this->addressOfNext()); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
Kind kind() const { |
| 278 |
uint32_t i = getReservedSlot(KindSlot).toPrivateUint32(); |
| 279 |
MOZ_ASSERT(i == uint32_t(Kind::Keys) || i == uint32_t(Kind::Values) || |
| 280 |
i == uint32_t(Kind::Entries)); |
| 281 |
return Kind(i); |
| 282 |
} |
| 283 |
|
| 284 |
public: |
| 285 |
static constexpr size_t offsetOfTarget() { |
| 286 |
return getFixedSlotOffset(TargetSlot); |
| 287 |
} |
| 288 |
static constexpr size_t offsetOfIndex() { |
| 289 |
return getFixedSlotOffset(IndexSlot); |
| 290 |
} |
| 291 |
static constexpr size_t offsetOfCount() { |
| 292 |
return getFixedSlotOffset(CountSlot); |
| 293 |
} |
| 294 |
static constexpr size_t offsetOfPrevPtr() { |
| 295 |
return getFixedSlotOffset(PrevPtrSlot); |
| 296 |
} |
| 297 |
static constexpr size_t offsetOfNext() { |
| 298 |
return getFixedSlotOffset(NextSlot); |
| 299 |
} |
| 300 |
}; |
| 301 |
|
| 302 |
namespace detail { |
| 303 |
|
| 304 |
/* |
| 305 |
* detail::OrderedHashTableImpl is the underlying code used to implement both |
| 306 |
* OrderedHashMapImpl and OrderedHashSetImpl. Programs should use one of those |
| 307 |
* two templates rather than OrderedHashTableImpl. |
| 308 |
*/ |
| 309 |
template <class T, class Ops> |
| 310 |
class MOZ_STACK_CLASS OrderedHashTableImpl { |
| 311 |
public: |
| 312 |
using Key = typename Ops::KeyType; |
| 313 |
using MutableKey = std::remove_const_t<Key>; |
| 314 |
using UnbarrieredKey = typename RemoveBarrier<MutableKey>::Type; |
| 315 |
using Lookup = typename Ops::Lookup; |
| 316 |
using HashCodeScrambler = mozilla::HashCodeScrambler; |
| 317 |
static constexpr size_t SlotCount = OrderedHashTableObject::SlotCount; |
| 318 |
|
| 319 |
struct Data { |
| 320 |
T element; |
| 321 |
Data* chain; |
| 322 |
|
| 323 |
Data(const T& e, Data* c) : element(e), chain(c) {} |
| 324 |
Data(T&& e, Data* c) : element(std::move(e)), chain(c) {} |
| 325 |
}; |
| 326 |
|
| 327 |
private: |
| 328 |
using Slots = OrderedHashTableObject::Slots; |
| 329 |
OrderedHashTableObject* const obj; |
| 330 |
|
| 331 |
// Whether we have allocated a buffer for this object. This buffer is |
| 332 |
// allocated when adding the first entry and it contains the data array, the |
| 333 |
// hash table buckets and the hash code scrambler. |
| 334 |
bool hasAllocatedBuffer() const { |
| 335 |
MOZ_ASSERT(hasInitializedSlots()); |
| 336 |
return obj->getReservedSlot(Slots::DataSlot).toPrivate() != nullptr; |
| 337 |
} |
| 338 |
|
| 339 |
// Hash table. Has hashBuckets() elements. |
| 340 |
// Note: a single malloc buffer is used for the data and hashTable arrays and |
| 341 |
// the HashCodeScrambler. The pointer in DataSlot points to the start of this |
| 342 |
// buffer. |
| 343 |
Data** getHashTable() const { |
| 344 |
MOZ_ASSERT(hasAllocatedBuffer()); |
| 345 |
Value v = obj->getReservedSlot(Slots::HashTableSlot); |
| 346 |
return static_cast<Data**>(v.toPrivate()); |
| 347 |
} |
| 348 |
void setHashTable(Data** table) { |
| 349 |
obj->setReservedSlotPrivateUnbarriered(Slots::HashTableSlot, table); |
| 350 |
} |
| 351 |
|
| 352 |
// Array of Data objects. Elements data[0:dataLength] are constructed and the |
| 353 |
// total capacity is dataCapacity. |
| 354 |
// |
| 355 |
// maybeData returns nullptr if this object doesn't have a buffer yet. |
| 356 |
// getData asserts the object has a buffer. |
| 357 |
Data* maybeData() const { |
| 358 |
Value v = obj->getReservedSlot(Slots::DataSlot); |
| 359 |
return static_cast<Data*>(v.toPrivate()); |
| 360 |
} |
| 361 |
Data* getData() const { |
| 362 |
MOZ_ASSERT(hasAllocatedBuffer()); |
| 363 |
return maybeData(); |
| 364 |
} |
| 365 |
void setData(Data* data) { |
| 366 |
obj->setReservedSlotPrivateUnbarriered(Slots::DataSlot, data); |
| 367 |
} |
| 368 |
|
| 369 |
// Number of constructed elements in the data array. |
| 370 |
uint32_t getDataLength() const { |
| 371 |
return obj->getReservedSlot(Slots::DataLengthSlot).toPrivateUint32(); |
| 372 |
} |
| 373 |
void setDataLength(uint32_t length) { |
| 374 |
obj->setReservedSlotPrivateUint32Unbarriered(Slots::DataLengthSlot, length); |
| 375 |
} |
| 376 |
|
| 377 |
// Size of data array, in elements. |
| 378 |
uint32_t getDataCapacity() const { |
| 379 |
return obj->getReservedSlot(Slots::DataCapacitySlot).toPrivateUint32(); |
| 380 |
} |
| 381 |
void setDataCapacity(uint32_t capacity) { |
| 382 |
obj->setReservedSlotPrivateUint32Unbarriered(Slots::DataCapacitySlot, |
| 383 |
capacity); |
| 384 |
} |
| 385 |
|
| 386 |
// The number of elements in this table. This is different from dataLength |
| 387 |
// because the data array can contain empty/removed elements. |
| 388 |
uint32_t getLiveCount() const { |
| 389 |
return obj->getReservedSlot(Slots::LiveCountSlot).toPrivateUint32(); |
| 390 |
} |
| 391 |
void setLiveCount(uint32_t liveCount) { |
| 392 |
obj->setReservedSlotPrivateUint32Unbarriered(Slots::LiveCountSlot, |
| 393 |
liveCount); |
| 394 |
} |
| 395 |
|
| 396 |
// Multiplicative hash shift. |
| 397 |
uint32_t getHashShift() const { |
| 398 |
MOZ_ASSERT(hasAllocatedBuffer(), |
| 399 |
"hash shift is meaningless if there's no hash table"); |
| 400 |
return obj->getReservedSlot(Slots::HashShiftSlot).toPrivateUint32(); |
| 401 |
} |
| 402 |
void setHashShift(uint32_t hashShift) { |
| 403 |
obj->setReservedSlotPrivateUint32Unbarriered(Slots::HashShiftSlot, |
| 404 |
hashShift); |
| 405 |
} |
| 406 |
|
| 407 |
// List of all active iterators on this table in the tenured heap. Populated |
| 408 |
// when iterators are created. |
| 409 |
TableIteratorObject* getTenuredIterators() const { |
| 410 |
Value v = obj->getReservedSlot(Slots::TenuredIteratorsSlot); |
| 411 |
return static_cast<TableIteratorObject*>(v.toPrivate()); |
| 412 |
} |
| 413 |
void setTenuredIterators(TableIteratorObject* iter) { |
| 414 |
obj->setReservedSlotPrivateUnbarriered(Slots::TenuredIteratorsSlot, iter); |
| 415 |
} |
| 416 |
TableIteratorObject** addressOfTenuredIterators() const { |
| 417 |
static constexpr size_t slot = Slots::TenuredIteratorsSlot; |
| 418 |
return obj->addressOfFixedSlotPrivatePtr<TableIteratorObject>(slot); |
| 419 |
} |
| 420 |
|
| 421 |
// List of all active iterators on this table in the GC nursery. |
| 422 |
// Populated when iterators are created. This is cleared at the start |
| 423 |
// of minor GC and rebuilt when iterators are moved. |
| 424 |
TableIteratorObject* getNurseryIterators() const { |
| 425 |
Value v = obj->getReservedSlot(Slots::NurseryIteratorsSlot); |
| 426 |
return static_cast<TableIteratorObject*>(v.toPrivate()); |
| 427 |
} |
| 428 |
void setNurseryIterators(TableIteratorObject* iter) { |
| 429 |
obj->setReservedSlotPrivateUnbarriered(Slots::NurseryIteratorsSlot, iter); |
| 430 |
} |
| 431 |
TableIteratorObject** addressOfNurseryIterators() const { |
| 432 |
static constexpr size_t slot = Slots::NurseryIteratorsSlot; |
| 433 |
return obj->addressOfFixedSlotPrivatePtr<TableIteratorObject>(slot); |
| 434 |
} |
| 435 |
|
| 436 |
// Returns a pointer to the list of tenured or nursery iterators depending on |
| 437 |
// where |iter| is allocated. |
| 438 |
TableIteratorObject** addressOfIterators(TableIteratorObject* iter) { |
| 439 |
return IsInsideNursery(iter) ? addressOfNurseryIterators() |
| 440 |
: addressOfTenuredIterators(); |
| 441 |
} |
| 442 |
|
| 443 |
// Scrambler to not reveal pointer hash codes. |
| 444 |
const HashCodeScrambler* getHashCodeScrambler() const { |
| 445 |
MOZ_ASSERT(hasAllocatedBuffer()); |
| 446 |
Value v = obj->getReservedSlot(Slots::HashCodeScramblerSlot); |
| 447 |
return static_cast<const HashCodeScrambler*>(v.toPrivate()); |
| 448 |
} |
| 449 |
void setHashCodeScrambler(HashCodeScrambler* hcs) { |
| 450 |
obj->setReservedSlotPrivateUnbarriered(Slots::HashCodeScramblerSlot, hcs); |
| 451 |
} |
| 452 |
|
| 453 |
static constexpr uint32_t hashShiftToNumHashBuckets(uint32_t hashShift) { |
| 454 |
return 1 << (js::kHashNumberBits - hashShift); |
| 455 |
} |
| 456 |
static constexpr uint32_t numHashBucketsToDataCapacity( |
| 457 |
uint32_t numHashBuckets) { |
| 458 |
return uint32_t(numHashBuckets * FillFactor); |
| 459 |
} |
| 460 |
|
| 461 |
// Logarithm base 2 of the number of buckets in the hash table initially. |
| 462 |
static constexpr uint32_t InitialBucketsLog2 = 1; |
| 463 |
static constexpr uint32_t InitialBuckets = 1 << InitialBucketsLog2; |
| 464 |
static constexpr uint32_t InitialHashShift = |
| 465 |
js::kHashNumberBits - InitialBucketsLog2; |
| 466 |
|
| 467 |
// The maximum load factor (mean number of entries per bucket). |
| 468 |
// It is an invariant that |
| 469 |
// dataCapacity == floor(hashBuckets * FillFactor). |
| 470 |
// |
| 471 |
// The fill factor should be between 2 and 4, and it should be chosen so that |
| 472 |
// the fill factor times sizeof(Data) is close to but <= a power of 2. |
| 473 |
// This fixed fill factor was chosen to make the size of the data |
| 474 |
// array, in bytes, close to a power of two when sizeof(T) is 16. |
| 475 |
static constexpr double FillFactor = 8.0 / 3.0; |
| 476 |
|
| 477 |
// The minimum permitted value of (liveCount / dataLength). |
| 478 |
// If that ratio drops below this value, we shrink the table. |
| 479 |
static constexpr double MinDataFill = 0.25; |
| 480 |
|
| 481 |
// Note: a lower hash shift results in a higher capacity. |
| 482 |
static constexpr uint32_t MinHashShift = 8; |
| 483 |
static constexpr uint32_t MaxHashBuckets = |
| 484 |
hashShiftToNumHashBuckets(MinHashShift); // 16777216 |
| 485 |
static constexpr uint32_t MaxDataCapacity = |
| 486 |
numHashBucketsToDataCapacity(MaxHashBuckets); // 44739242 |
| 487 |
|
| 488 |
template <typename F> |
| 489 |
void forEachIterator(F&& f) { |
| 490 |
TableIteratorObject* next; |
| 491 |
for (TableIteratorObject* iter = getTenuredIterators(); iter; iter = next) { |
| 492 |
next = iter->getNext(); |
| 493 |
f(iter); |
| 494 |
} |
| 495 |
for (TableIteratorObject* iter = getNurseryIterators(); iter; iter = next) { |
| 496 |
next = iter->getNext(); |
| 497 |
f(iter); |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
bool hasInitializedSlots() const { |
| 502 |
return !obj->getReservedSlot(Slots::DataSlot).isUndefined(); |
| 503 |
} |
| 504 |
|
| 505 |
static MOZ_ALWAYS_INLINE bool calcAllocSize(uint32_t dataCapacity, |
| 506 |
uint32_t buckets, |
| 507 |
size_t* numBytes) { |
| 508 |
using CheckedSize = mozilla::CheckedInt<size_t>; |
| 509 |
auto res = CheckedSize(dataCapacity) * sizeof(Data) + |
| 510 |
CheckedSize(sizeof(HashCodeScrambler)) + |
| 511 |
CheckedSize(buckets) * sizeof(Data*); |
| 512 |
if (MOZ_UNLIKELY(!res.isValid())) { |
| 513 |
return false; |
| 514 |
} |
| 515 |
*numBytes = res.value(); |
| 516 |
return true; |
| 517 |
} |
| 518 |
|
| 519 |
// Allocate a single buffer that stores the data array followed by the hash |
| 520 |
// code scrambler and the hash table entries. |
| 521 |
using AllocationResult = |
| 522 |
std::tuple<Data*, Data**, HashCodeScrambler*, size_t>; |
| 523 |
AllocationResult allocateBuffer(JSContext* cx, uint32_t dataCapacity, |
| 524 |
uint32_t buckets) { |
| 525 |
MOZ_ASSERT(dataCapacity <= MaxDataCapacity); |
| 526 |
MOZ_ASSERT(buckets <= MaxHashBuckets); |
| 527 |
|
| 528 |
size_t numBytes = 0; |
| 529 |
if (MOZ_UNLIKELY(!calcAllocSize(dataCapacity, buckets, &numBytes))) { |
| 530 |
ReportAllocationOverflow(cx); |
| 531 |
return {}; |
| 532 |
} |
| 533 |
|
| 534 |
void* buf = obj->allocateCellBuffer(cx, numBytes); |
| 535 |
if (!buf) { |
| 536 |
return {}; |
| 537 |
} |
| 538 |
|
| 539 |
return getBufferParts(buf, numBytes, dataCapacity, buckets); |
| 540 |
} |
| 541 |
|
| 542 |
static AllocationResult getBufferParts(void* buf, size_t numBytes, |
| 543 |
uint32_t dataCapacity, |
| 544 |
uint32_t buckets) { |
| 545 |
static_assert(alignof(Data) % alignof(HashCodeScrambler) == 0, |
| 546 |
"Hash code scrambler must be aligned properly"); |
| 547 |
static_assert(alignof(HashCodeScrambler) % alignof(Data*) == 0, |
| 548 |
"Hash table entries must be aligned properly"); |
| 549 |
|
| 550 |
auto* data = static_cast<Data*>(buf); |
| 551 |
auto* hcs = reinterpret_cast<HashCodeScrambler*>(data + dataCapacity); |
| 552 |
auto** table = reinterpret_cast<Data**>(hcs + 1); |
| 553 |
|
| 554 |
MOZ_ASSERT(uintptr_t(table + buckets) == uintptr_t(buf) + numBytes); |
| 555 |
|
| 556 |
return {data, table, hcs, numBytes}; |
| 557 |
} |
| 558 |
|
| 559 |
[[nodiscard]] bool initBuffer(JSContext* cx) { |
| 560 |
MOZ_ASSERT(!hasAllocatedBuffer()); |
| 561 |
MOZ_ASSERT(getDataLength() == 0); |
| 562 |
MOZ_ASSERT(getLiveCount() == 0); |
| 563 |
|
| 564 |
constexpr uint32_t buckets = InitialBuckets; |
| 565 |
constexpr uint32_t capacity = uint32_t(buckets * FillFactor); |
| 566 |
|
| 567 |
auto [dataAlloc, tableAlloc, hcsAlloc, numBytes] = |
| 568 |
allocateBuffer(cx, capacity, buckets); |
| 569 |
if (!dataAlloc) { |
| 570 |
return false; |
| 571 |
} |
| 572 |
|
| 573 |
AddCellMemory(obj, numBytes, MemoryUse::MapObjectData); |
| 574 |
|
| 575 |
*hcsAlloc = cx->realm()->randomHashCodeScrambler(); |
| 576 |
|
| 577 |
std::uninitialized_fill_n(tableAlloc, buckets, nullptr); |
| 578 |
|
| 579 |
setHashTable(tableAlloc); |
| 580 |
setData(dataAlloc); |
| 581 |
setDataCapacity(capacity); |
| 582 |
setHashShift(InitialHashShift); |
| 583 |
setHashCodeScrambler(hcsAlloc); |
| 584 |
MOZ_ASSERT(hashBuckets() == buckets); |
| 585 |
return true; |
| 586 |
} |
| 587 |
|
| 588 |
void updateHashTableForRekey(Data* entry, HashNumber oldHash, |
| 589 |
HashNumber newHash) { |
| 590 |
uint32_t hashShift = getHashShift(); |
| 591 |
oldHash >>= hashShift; |
| 592 |
newHash >>= hashShift; |
| 593 |
|
| 594 |
if (oldHash == newHash) { |
| 595 |
return; |
| 596 |
} |
| 597 |
|
| 598 |
// Remove this entry from its old hash chain. (If this crashes reading |
| 599 |
// nullptr, it would mean we did not find this entry on the hash chain where |
| 600 |
// we expected it. That probably means the key's hash code changed since it |
| 601 |
// was inserted, breaking the hash code invariant.) |
| 602 |
Data** hashTable = getHashTable(); |
| 603 |
Data** ep = &hashTable[oldHash]; |
| 604 |
while (*ep != entry) { |
| 605 |
ep = &(*ep)->chain; |
| 606 |
} |
| 607 |
*ep = entry->chain; |
| 608 |
|
| 609 |
// Add it to the new hash chain. We could just insert it at the beginning of |
| 610 |
// the chain. Instead, we do a bit of work to preserve the invariant that |
| 611 |
// hash chains always go in reverse insertion order (descending memory |
| 612 |
// order). No code currently depends on this invariant, so it's fine to kill |
| 613 |
// it if needed. |
| 614 |
ep = &hashTable[newHash]; |
| 615 |
while (*ep && *ep > entry) { |
| 616 |
ep = &(*ep)->chain; |
| 617 |
} |
| 618 |
entry->chain = *ep; |
| 619 |
*ep = entry; |
| 620 |
} |
| 621 |
|
| 622 |
public: |
| 623 |
explicit OrderedHashTableImpl(OrderedHashTableObject* obj) : obj(obj) {} |
| 624 |
|
| 625 |
void initSlots() { |
| 626 |
MOZ_ASSERT(!hasInitializedSlots(), "init must be called at most once"); |
| 627 |
setHashTable(nullptr); |
| 628 |
setData(nullptr); |
| 629 |
setDataLength(0); |
| 630 |
setDataCapacity(0); |
| 631 |
setLiveCount(0); |
| 632 |
setHashShift(0); |
| 633 |
setTenuredIterators(nullptr); |
| 634 |
setNurseryIterators(nullptr); |
| 635 |
setHashCodeScrambler(nullptr); |
| 636 |
} |
| 637 |
|
| 638 |
void destroy(JS::GCContext* gcx) { |
| 639 |
if (!hasInitializedSlots()) { |
| 640 |
return; |
| 641 |
} |
| 642 |
if (Data* data = maybeData()) { |
| 643 |
freeData(gcx, data, getDataLength(), getDataCapacity(), hashBuckets()); |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
void maybeMoveBufferOnPromotion(Nursery& nursery) { |
| 648 |
if (!hasAllocatedBuffer()) { |
| 649 |
return; |
| 650 |
} |
| 651 |
|
| 652 |
Data* oldData = getData(); |
| 653 |
uint32_t dataCapacity = getDataCapacity(); |
| 654 |
uint32_t buckets = hashBuckets(); |
| 655 |
|
| 656 |
size_t numBytes = 0; |
| 657 |
MOZ_ALWAYS_TRUE(calcAllocSize(dataCapacity, buckets, &numBytes)); |
| 658 |
|
| 659 |
void* buf = oldData; |
| 660 |
Nursery::WasBufferMoved result = |
| 661 |
nursery.maybeMoveNurseryOrMallocBufferOnPromotion( |
| 662 |
&buf, obj, numBytes, MemoryUse::MapObjectData); |
| 663 |
if (result == Nursery::BufferNotMoved) { |
| 664 |
return; |
| 665 |
} |
| 666 |
|
| 667 |
// The buffer was moved in memory. Update reserved slots and fix up the |
| 668 |
// |Data*| pointers for the hash table chains. |
| 669 |
// TODO(bug 1931492): consider storing indices instead of pointers to |
| 670 |
// simplify this. |
| 671 |
|
| 672 |
auto [data, table, hcs, numBytesUnused] = |
| 673 |
getBufferParts(buf, numBytes, dataCapacity, buckets); |
| 674 |
|
| 675 |
auto entryIndex = [=](const Data* entry) { |
| 676 |
MOZ_ASSERT(entry >= oldData); |
| 677 |
MOZ_ASSERT(size_t(entry - oldData) < dataCapacity); |
| 678 |
return entry - oldData; |
| 679 |
}; |
| 680 |
|
| 681 |
for (uint32_t i = 0, len = getDataLength(); i < len; i++) { |
| 682 |
if (const Data* chain = data[i].chain) { |
| 683 |
data[i].chain = data + entryIndex(chain); |
| 684 |
} |
| 685 |
} |
| 686 |
for (uint32_t i = 0; i < buckets; i++) { |
| 687 |
if (const Data* chain = table[i]) { |
| 688 |
table[i] = data + entryIndex(chain); |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
setData(data); |
| 693 |
setHashTable(table); |
| 694 |
setHashCodeScrambler(hcs); |
| 695 |
} |
| 696 |
|
| 697 |
size_t sizeOfExcludingObject(mozilla::MallocSizeOf mallocSizeOf) const { |
| 698 |
size_t size = 0; |
| 699 |
if (hasInitializedSlots() && hasAllocatedBuffer()) { |
| 700 |
// Note: this also includes the HashCodeScrambler and the hashTable array. |
| 701 |
size += mallocSizeOf(getData()); |
| 702 |
} |
| 703 |
return size; |
| 704 |
} |
| 705 |
|
| 706 |
/* Return the number of elements in the table. */ |
| 707 |
uint32_t count() const { return getLiveCount(); } |
| 708 |
|
| 709 |
/* True if any element matches l. */ |
| 710 |
bool has(const Lookup& l) const { return lookup(l) != nullptr; } |
| 711 |
|
| 712 |
/* Return a pointer to the element, if any, that matches l, or nullptr. */ |
| 713 |
T* get(const Lookup& l) { |
| 714 |
Data* e = lookup(l); |
| 715 |
return e ? &e->element : nullptr; |
| 716 |
} |
| 717 |
|
| 718 |
/* |
| 719 |
* If the table already contains an entry that matches |element|, |
| 720 |
* replace that entry with |element|. Otherwise add a new entry. |
| 721 |
* |
| 722 |
* On success, return true, whether there was already a matching element or |
| 723 |
* not. On allocation failure, return false. If this returns false, it |
| 724 |
* means the element was not added to the table. |
| 725 |
*/ |
| 726 |
template <typename ElementInput> |
| 727 |
[[nodiscard]] bool put(JSContext* cx, ElementInput&& element) { |
| 728 |
HashNumber h; |
| 729 |
if (hasAllocatedBuffer()) { |
| 730 |
h = prepareHash(Ops::getKey(element)); |
| 731 |
if (Data* e = lookup(Ops::getKey(element), h)) { |
| 732 |
e->element = std::forward<ElementInput>(element); |
| 733 |
return true; |
| 734 |
} |
| 735 |
if (getDataLength() == getDataCapacity() && !rehashOnFull(cx)) { |
| 736 |
return false; |
| 737 |
} |
| 738 |
} else { |
| 739 |
if (!initBuffer(cx)) { |
| 740 |
return false; |
| 741 |
} |
| 742 |
h = prepareHash(Ops::getKey(element)); |
| 743 |
} |
| 744 |
auto [entry, chain] = addEntry(h); |
| 745 |
new (entry) Data(std::forward<ElementInput>(element), chain); |
| 746 |
return true; |
| 747 |
} |
| 748 |
|
| 749 |
#ifdef NIGHTLY_BUILD |
| 750 |
/* |
| 751 |
* If the table already contains an entry that matches |element|, |
| 752 |
* return that entry. Otherwise add a new entry. |
| 753 |
* |
| 754 |
* On success, return a pointer to the element, whether there was already a |
| 755 |
* matching element or not. On allocation failure, return a nullptr. If this |
| 756 |
* returns a nullptr, it means the element was not added to the table. |
| 757 |
*/ |
| 758 |
template <typename ElementInput> |
| 759 |
[[nodiscard]] T* getOrAdd(JSContext* cx, ElementInput&& element) { |
| 760 |
HashNumber h; |
| 761 |
if (hasAllocatedBuffer()) { |
| 762 |
h = prepareHash(Ops::getKey(element)); |
| 763 |
if (Data* e = lookup(Ops::getKey(element), h)) { |
| 764 |
return &e->element; |
| 765 |
} |
| 766 |
if (getDataLength() == getDataCapacity() && !rehashOnFull(cx)) { |
| 767 |
return nullptr; |
| 768 |
} |
| 769 |
} else { |
| 770 |
if (!initBuffer(cx)) { |
| 771 |
return nullptr; |
| 772 |
} |
| 773 |
h = prepareHash(Ops::getKey(element)); |
| 774 |
} |
| 775 |
auto [entry, chain] = addEntry(h); |
| 776 |
new (entry) Data(std::forward<ElementInput>(element), chain); |
| 777 |
return &entry->element; |
| 778 |
} |
| 779 |
#endif // #ifdef NIGHTLY_BUILD |
| 780 |
|
| 781 |
/* |
| 782 |
* If the table contains an element matching l, remove it and return true. |
| 783 |
* Otherwise return false. |
| 784 |
*/ |
| 785 |
bool remove(JSContext* cx, const Lookup& l) { |
| 786 |
// Note: This could be optimized so that removing the last entry, |
| 787 |
// data[dataLength - 1], decrements dataLength. LIFO use cases would |
| 788 |
// benefit. |
| 789 |
|
| 790 |
// If a matching entry exists, empty it. |
| 791 |
Data* e = lookup(l); |
| 792 |
if (e == nullptr) { |
| 793 |
return false; |
| 794 |
} |
| 795 |
|
| 796 |
MOZ_ASSERT(uint32_t(e - getData()) < getDataCapacity()); |
| 797 |
|
| 798 |
uint32_t liveCount = getLiveCount(); |
| 799 |
liveCount--; |
| 800 |
setLiveCount(liveCount); |
| 801 |
Ops::makeEmpty(&e->element); |
| 802 |
|
| 803 |
// Update active iterators. |
| 804 |
uint32_t pos = e - getData(); |
| 805 |
forEachIterator( |
| 806 |
[this, pos](auto* iter) { IterOps::onRemove(obj, iter, pos); }); |
| 807 |
|
| 808 |
// If many entries have been removed, try to shrink the table. Ignore OOM |
| 809 |
// because shrinking the table is an optimization and it's okay for it to |
| 810 |
// fail. |
| 811 |
if (hashBuckets() > InitialBuckets && |
| 812 |
liveCount < getDataLength() * MinDataFill) { |
| 813 |
if (!rehash(cx, getHashShift() + 1)) { |
| 814 |
cx->recoverFromOutOfMemory(); |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
return true; |
| 819 |
} |
| 820 |
|
| 821 |
/* |
| 822 |
* Remove all entries. |
| 823 |
* |
| 824 |
* The effect on active iterators is the same as removing all entries; in |
| 825 |
* particular, those iterators are still active and will see any entries |
| 826 |
* added after a clear(). |
| 827 |
*/ |
| 828 |
void clear(JSContext* cx) { |
| 829 |
if (getDataLength() != 0) { |
| 830 |
destroyData(getData(), getDataLength()); |
| 831 |
setDataLength(0); |
| 832 |
setLiveCount(0); |
| 833 |
|
| 834 |
size_t buckets = hashBuckets(); |
| 835 |
std::fill_n(getHashTable(), buckets, nullptr); |
| 836 |
|
| 837 |
forEachIterator([](auto* iter) { IterOps::onClear(iter); }); |
| 838 |
|
| 839 |
// Try to shrink the table. Ignore OOM because shrinking the table is an |
| 840 |
// optimization and it's okay for it to fail. |
| 841 |
if (buckets > InitialBuckets) { |
| 842 |
if (!rehash(cx, InitialHashShift)) { |
| 843 |
cx->recoverFromOutOfMemory(); |
| 844 |
} |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
MOZ_ASSERT(getDataLength() == 0); |
| 849 |
MOZ_ASSERT(getLiveCount() == 0); |
| 850 |
} |
| 851 |
|
| 852 |
class IterOps { |
| 853 |
friend class OrderedHashTableImpl; |
| 854 |
|
| 855 |
static void init(OrderedHashTableObject* table, TableIteratorObject* iter, |
| 856 |
TableIteratorObject::Kind kind) { |
| 857 |
auto** listp = OrderedHashTableImpl(table).addressOfIterators(iter); |
| 858 |
iter->init(table, kind, listp); |
| 859 |
seek(table, iter); |
| 860 |
} |
| 861 |
|
| 862 |
static void seek(OrderedHashTableObject* table, TableIteratorObject* iter) { |
| 863 |
iter->assertActiveIteratorFor(table); |
| 864 |
const Data* data = OrderedHashTableImpl(table).maybeData(); |
| 865 |
uint32_t dataLength = OrderedHashTableImpl(table).getDataLength(); |
| 866 |
uint32_t i = iter->getIndex(); |
| 867 |
while (i < dataLength && Ops::isEmpty(Ops::getKey(data[i].element))) { |
| 868 |
i++; |
| 869 |
} |
| 870 |
iter->setIndex(i); |
| 871 |
} |
| 872 |
|
| 873 |
// The hash table calls this when an entry is removed. |
| 874 |
// j is the index of the removed entry. |
| 875 |
static void onRemove(OrderedHashTableObject* table, |
| 876 |
TableIteratorObject* iter, uint32_t j) { |
| 877 |
iter->assertActiveIteratorFor(table); |
| 878 |
uint32_t i = iter->getIndex(); |
| 879 |
if (j < i) { |
| 880 |
iter->setCount(iter->getCount() - 1); |
| 881 |
} |
| 882 |
if (j == i) { |
| 883 |
seek(table, iter); |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
// The hash table calls this when the table is resized or compacted. |
| 888 |
// Since |count| is the number of nonempty entries to the left of |index|, |
| 889 |
// discarding the empty entries will not affect |count|, and it will make |
| 890 |
// |index| and |count| equal. |
| 891 |
static void onCompact(TableIteratorObject* iter) { |
| 892 |
iter->setIndex(iter->getCount()); |
| 893 |
} |
| 894 |
|
| 895 |
// The hash table calls this when cleared. |
| 896 |
static void onClear(TableIteratorObject* iter) { |
| 897 |
iter->setIndex(0); |
| 898 |
iter->setCount(0); |
| 899 |
} |
| 900 |
|
| 901 |
// If the iterator reached the end of the data array, we're done: mark the |
| 902 |
// iterator inactive, remove it from the linked list, and return |true|. |
| 903 |
// Else, call |f| for the current entry, advance the iterator to the next |
| 904 |
// entry, and return |false|. |
| 905 |
template <typename F> |
| 906 |
static bool next(OrderedHashTableObject* obj, TableIteratorObject* iter, |
| 907 |
F&& f) { |
| 908 |
iter->assertActiveIteratorFor(obj); |
| 909 |
|
| 910 |
OrderedHashTableImpl table(obj); |
| 911 |
uint32_t index = iter->getIndex(); |
| 912 |
|
| 913 |
if (index >= table.getDataLength()) { |
| 914 |
iter->finish(); |
| 915 |
return true; |
| 916 |
} |
| 917 |
|
| 918 |
f(iter->kind(), table.getData()[index].element); |
| 919 |
|
| 920 |
iter->setCount(iter->getCount() + 1); |
| 921 |
iter->setIndex(index + 1); |
| 922 |
seek(obj, iter); |
| 923 |
return false; |
| 924 |
} |
| 925 |
}; |
| 926 |
|
| 927 |
// Calls |f| for each entry in the table. This function must not mutate the |
| 928 |
// table. |
| 929 |
template <typename F> |
| 930 |
[[nodiscard]] bool forEachEntry(F&& f) const { |
| 931 |
const Data* data = maybeData(); |
| 932 |
uint32_t dataLength = getDataLength(); |
| 933 |
#ifdef DEBUG |
| 934 |
uint32_t liveCount = getLiveCount(); |
| 935 |
#endif |
| 936 |
for (uint32_t i = 0; i < dataLength; i++) { |
| 937 |
if (!Ops::isEmpty(Ops::getKey(data[i].element))) { |
| 938 |
if (!f(data[i].element)) { |
| 939 |
return false; |
| 940 |
} |
| 941 |
} |
| 942 |
} |
| 943 |
MOZ_ASSERT(maybeData() == data); |
| 944 |
MOZ_ASSERT(getDataLength() == dataLength); |
| 945 |
MOZ_ASSERT(getLiveCount() == liveCount); |
| 946 |
return true; |
| 947 |
} |
| 948 |
#ifdef DEBUG |
| 949 |
// Like forEachEntry, but infallible and the function is called at most |
| 950 |
// maxCount times. This is useful for debug assertions. |
| 951 |
template <typename F> |
| 952 |
void forEachEntryUpTo(size_t maxCount, F&& f) const { |
| 953 |
MOZ_ASSERT(maxCount > 0); |
| 954 |
const Data* data = maybeData(); |
| 955 |
uint32_t dataLength = getDataLength(); |
| 956 |
uint32_t liveCount = getLiveCount(); |
| 957 |
size_t count = 0; |
| 958 |
for (uint32_t i = 0; i < dataLength; i++) { |
| 959 |
if (!Ops::isEmpty(Ops::getKey(data[i].element))) { |
| 960 |
f(data[i].element); |
| 961 |
count++; |
| 962 |
if (count == maxCount) { |
| 963 |
break; |
| 964 |
} |
| 965 |
} |
| 966 |
} |
| 967 |
MOZ_ASSERT(maybeData() == data); |
| 968 |
MOZ_ASSERT(getDataLength() == dataLength); |
| 969 |
MOZ_ASSERT(getLiveCount() == liveCount); |
| 970 |
} |
| 971 |
#endif |
| 972 |
|
| 973 |
void trace(JSTracer* trc) { |
| 974 |
Data* data = maybeData(); |
| 975 |
uint32_t dataLength = getDataLength(); |
| 976 |
for (uint32_t i = 0; i < dataLength; i++) { |
| 977 |
if (!Ops::isEmpty(Ops::getKey(data[i].element))) { |
| 978 |
Ops::trace(trc, this, i, data[i].element); |
| 979 |
} |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
// For use by the implementation of Ops::trace. |
| 984 |
void traceKey(JSTracer* trc, uint32_t index, const Key& key) { |
| 985 |
MOZ_ASSERT(index < getDataLength()); |
| 986 |
UnbarrieredKey newKey = key; |
| 987 |
JS::GCPolicy<UnbarrieredKey>::trace(trc, &newKey, |
| 988 |
"OrderedHashTableObject key"); |
| 989 |
if (newKey != key) { |
| 990 |
rekey(&getData()[index], newKey); |
| 991 |
} |
| 992 |
} |
| 993 |
template <typename Value> |
| 994 |
void traceValue(JSTracer* trc, Value& value) { |
| 995 |
JS::GCPolicy<Value>::trace(trc, &value, "OrderedHashMapObject value"); |
| 996 |
} |
| 997 |
|
| 998 |
void initIterator(TableIteratorObject* iter, |
| 999 |
TableIteratorObject::Kind kind) const { |
| 1000 |
IterOps::init(obj, iter, kind); |
| 1001 |
} |
| 1002 |
template <typename F> |
| 1003 |
bool iteratorNext(TableIteratorObject* iter, F&& f) const { |
| 1004 |
return IterOps::next(obj, iter, f); |
| 1005 |
} |
| 1006 |
|
| 1007 |
void clearNurseryIterators() { |
| 1008 |
if (TableIteratorObject* iter = getNurseryIterators()) { |
| 1009 |
iter->setPrevPtr(nullptr); |
| 1010 |
} |
| 1011 |
setNurseryIterators(nullptr); |
| 1012 |
} |
| 1013 |
void relinkNurseryIterator(TableIteratorObject* iter) { |
| 1014 |
auto** listp = addressOfIterators(iter); |
| 1015 |
iter->link(listp); |
| 1016 |
} |
| 1017 |
|
| 1018 |
void updateIteratorsAfterMove(OrderedHashTableObject* old) { |
| 1019 |
if (TableIteratorObject* iter = getTenuredIterators()) { |
| 1020 |
MOZ_ASSERT(iter->getPrevPtr() == |
| 1021 |
OrderedHashTableImpl(old).addressOfTenuredIterators()); |
| 1022 |
iter->setPrevPtr(addressOfTenuredIterators()); |
| 1023 |
} |
| 1024 |
if (TableIteratorObject* iter = getNurseryIterators()) { |
| 1025 |
MOZ_ASSERT(iter->getPrevPtr() == |
| 1026 |
OrderedHashTableImpl(old).addressOfNurseryIterators()); |
| 1027 |
iter->setPrevPtr(addressOfNurseryIterators()); |
| 1028 |
} |
| 1029 |
} |
| 1030 |
|
| 1031 |
bool hasNurseryIterators() const { return getNurseryIterators(); } |
| 1032 |
|
| 1033 |
/* |
| 1034 |
* Change the value of the given key. |
| 1035 |
* |
| 1036 |
* This calls Ops::hash on both the current key and the new key. |
| 1037 |
* Ops::hash on the current key must return the same hash code as |
| 1038 |
* when the entry was added to the table. |
| 1039 |
*/ |
| 1040 |
void rekeyOneEntry(const Key& current, const Key& newKey, const T& element) { |
| 1041 |
if (current == newKey) { |
| 1042 |
return; |
| 1043 |
} |
| 1044 |
|
| 1045 |
HashNumber currentHash = prepareHash(current); |
| 1046 |
HashNumber newHash = prepareHash(newKey); |
| 1047 |
|
| 1048 |
Data* entry = lookup(current, currentHash); |
| 1049 |
MOZ_ASSERT(entry); |
| 1050 |
entry->element = element; |
| 1051 |
|
| 1052 |
updateHashTableForRekey(entry, currentHash, newHash); |
| 1053 |
} |
| 1054 |
|
| 1055 |
static constexpr size_t offsetOfDataElement() { |
| 1056 |
static_assert(offsetof(Data, element) == 0, |
| 1057 |
"TableIteratorLoadEntry and TableIteratorAdvance depend on " |
| 1058 |
"offsetof(Data, element) being 0"); |
| 1059 |
return offsetof(Data, element); |
| 1060 |
} |
| 1061 |
static constexpr size_t offsetOfDataChain() { return offsetof(Data, chain); } |
| 1062 |
static constexpr size_t sizeofData() { return sizeof(Data); } |
| 1063 |
|
| 1064 |
#ifdef DEBUG |
| 1065 |
mozilla::Maybe<HashNumber> hash(const Lookup& l) const { |
| 1066 |
// We can only compute the hash number if we have an allocated buffer |
| 1067 |
// because the buffer contains the hash code scrambler. |
| 1068 |
if (!hasAllocatedBuffer()) { |
| 1069 |
return {}; |
| 1070 |
} |
| 1071 |
return mozilla::Some(prepareHash(l)); |
| 1072 |
} |
| 1073 |
#endif |
| 1074 |
|
| 1075 |
private: |
| 1076 |
HashNumber prepareHash(const Lookup& l) const { |
| 1077 |
MOZ_ASSERT(hasAllocatedBuffer(), |
| 1078 |
"the hash code scrambler is allocated in the buffer"); |
| 1079 |
const HashCodeScrambler& hcs = *getHashCodeScrambler(); |
| 1080 |
return mozilla::ScrambleHashCode(Ops::hash(l, hcs)); |
| 1081 |
} |
| 1082 |
|
| 1083 |
/* The size of the hash table, in elements. Always a power of two. */ |
| 1084 |
uint32_t hashBuckets() const { |
| 1085 |
return hashShiftToNumHashBuckets(getHashShift()); |
| 1086 |
} |
| 1087 |
|
| 1088 |
void destroyData(Data* data, uint32_t length) { |
| 1089 |
Data* end = data + length; |
| 1090 |
for (Data* p = data; p != end; p++) { |
| 1091 |
p->~Data(); |
| 1092 |
} |
| 1093 |
} |
| 1094 |
|
| 1095 |
void freeData(JS::GCContext* gcx, Data* data, uint32_t length, |
| 1096 |
uint32_t capacity, uint32_t hashBuckets) { |
| 1097 |
MOZ_ASSERT(data); |
| 1098 |
MOZ_ASSERT(capacity > 0); |
| 1099 |
|
| 1100 |
destroyData(data, length); |
| 1101 |
|
| 1102 |
size_t numBytes; |
| 1103 |
MOZ_ALWAYS_TRUE(calcAllocSize(capacity, hashBuckets, &numBytes)); |
| 1104 |
|
| 1105 |
if (IsInsideNursery(obj)) { |
| 1106 |
if (gcx->runtime()->gc.nursery().isInside(data)) { |
| 1107 |
return; |
| 1108 |
} |
| 1109 |
gcx->runtime()->gc.nursery().removeMallocedBuffer(data, numBytes); |
| 1110 |
} |
| 1111 |
|
| 1112 |
gcx->free_(obj, data, numBytes, MemoryUse::MapObjectData); |
| 1113 |
} |
| 1114 |
|
| 1115 |
Data* lookup(const Lookup& l, HashNumber h) const { |
| 1116 |
MOZ_ASSERT(hasAllocatedBuffer()); |
| 1117 |
Data** hashTable = getHashTable(); |
| 1118 |
uint32_t hashShift = getHashShift(); |
| 1119 |
for (Data* e = hashTable[h >> hashShift]; e; e = e->chain) { |
| 1120 |
if (Ops::match(Ops::getKey(e->element), l)) { |
| 1121 |
return e; |
| 1122 |
} |
| 1123 |
} |
| 1124 |
return nullptr; |
| 1125 |
} |
| 1126 |
|
| 1127 |
Data* lookup(const Lookup& l) const { |
| 1128 |
// Note: checking |getLiveCount() > 0| is a minor performance optimization |
| 1129 |
// but this check is also required for correctness because it implies |
| 1130 |
// |hasAllocatedBuffer()|. |
| 1131 |
if (getLiveCount() == 0) { |
| 1132 |
return nullptr; |
| 1133 |
} |
| 1134 |
return lookup(l, prepareHash(l)); |
| 1135 |
} |
| 1136 |
|
| 1137 |
std::tuple<Data*, Data*> addEntry(HashNumber hash) { |
| 1138 |
uint32_t dataLength = getDataLength(); |
| 1139 |
MOZ_ASSERT(dataLength < getDataCapacity()); |
| 1140 |
|
| 1141 |
Data* entry = &getData()[dataLength]; |
| 1142 |
setDataLength(dataLength + 1); |
| 1143 |
setLiveCount(getLiveCount() + 1); |
| 1144 |
|
| 1145 |
Data** hashTable = getHashTable(); |
| 1146 |
hash >>= getHashShift(); |
| 1147 |
Data* chain = hashTable[hash]; |
| 1148 |
hashTable[hash] = entry; |
| 1149 |
|
| 1150 |
return std::make_tuple(entry, chain); |
| 1151 |
} |
| 1152 |
|
| 1153 |
/* This is called after rehashing the table. */ |
| 1154 |
void compacted() { |
| 1155 |
// If we had any empty entries, compacting may have moved live entries |
| 1156 |
// to the left within the data array. Notify all active iterators of |
| 1157 |
// the change. |
| 1158 |
forEachIterator([](auto* iter) { IterOps::onCompact(iter); }); |
| 1159 |
} |
| 1160 |
|
| 1161 |
/* Compact the entries in the data array and rehash them. */ |
| 1162 |
void rehashInPlace() { |
| 1163 |
Data** hashTable = getHashTable(); |
| 1164 |
std::fill_n(hashTable, hashBuckets(), nullptr); |
| 1165 |
|
| 1166 |
Data* const data = getData(); |
| 1167 |
uint32_t hashShift = getHashShift(); |
| 1168 |
Data* wp = data; |
| 1169 |
Data* end = data + getDataLength(); |
| 1170 |
for (Data* rp = data; rp != end; rp++) { |
| 1171 |
if (!Ops::isEmpty(Ops::getKey(rp->element))) { |
| 1172 |
HashNumber h = prepareHash(Ops::getKey(rp->element)) >> hashShift; |
| 1173 |
if (rp != wp) { |
| 1174 |
wp->element = std::move(rp->element); |
| 1175 |
} |
| 1176 |
wp->chain = hashTable[h]; |
| 1177 |
hashTable[h] = wp; |
| 1178 |
wp++; |
| 1179 |
} |
| 1180 |
} |
| 1181 |
MOZ_ASSERT(wp == data + getLiveCount()); |
| 1182 |
|
| 1183 |
while (wp != end) { |
| 1184 |
wp->~Data(); |
| 1185 |
wp++; |
| 1186 |
} |
| 1187 |
setDataLength(getLiveCount()); |
| 1188 |
compacted(); |
| 1189 |
} |
| 1190 |
|
| 1191 |
[[nodiscard]] bool rehashOnFull(JSContext* cx) { |
| 1192 |
MOZ_ASSERT(getDataLength() == getDataCapacity()); |
| 1193 |
|
| 1194 |
// If the hashTable is more than 1/4 deleted data, simply rehash in |
| 1195 |
// place to free up some space. Otherwise, grow the table. |
| 1196 |
uint32_t newHashShift = getLiveCount() >= getDataCapacity() * 0.75 |
| 1197 |
? getHashShift() - 1 |
| 1198 |
: getHashShift(); |
| 1199 |
return rehash(cx, newHashShift); |
| 1200 |
} |
| 1201 |
|
| 1202 |
/* |
| 1203 |
* Grow, shrink, or compact both the hash table and data array. |
| 1204 |
* |
| 1205 |
* On success, this returns true, dataLength == liveCount, and there are no |
| 1206 |
* empty elements in data[0:dataLength]. On allocation failure, this |
| 1207 |
* leaves everything as it was and returns false. |
| 1208 |
*/ |
| 1209 |
[[nodiscard]] bool rehash(JSContext* cx, uint32_t newHashShift) { |
| 1210 |
// If the size of the table is not changing, rehash in place to avoid |
| 1211 |
// allocating memory. |
| 1212 |
if (newHashShift == getHashShift()) { |
| 1213 |
rehashInPlace(); |
| 1214 |
return true; |
| 1215 |
} |
| 1216 |
|
| 1217 |
if (MOZ_UNLIKELY(newHashShift < MinHashShift)) { |
| 1218 |
ReportAllocationOverflow(cx); |
| 1219 |
return false; |
| 1220 |
} |
| 1221 |
|
| 1222 |
uint32_t newHashBuckets = hashShiftToNumHashBuckets(newHashShift); |
| 1223 |
uint32_t newCapacity = numHashBucketsToDataCapacity(newHashBuckets); |
| 1224 |
|
| 1225 |
auto [newData, newHashTable, newHcs, numBytes] = |
| 1226 |
allocateBuffer(cx, newCapacity, newHashBuckets); |
| 1227 |
if (!newData) { |
| 1228 |
return false; |
| 1229 |
} |
| 1230 |
|
| 1231 |
*newHcs = *getHashCodeScrambler(); |
| 1232 |
|
| 1233 |
std::uninitialized_fill_n(newHashTable, newHashBuckets, nullptr); |
| 1234 |
|
| 1235 |
Data* const oldData = getData(); |
| 1236 |
const uint32_t oldDataLength = getDataLength(); |
| 1237 |
|
| 1238 |
Data* wp = newData; |
| 1239 |
Data* end = oldData + oldDataLength; |
| 1240 |
for (Data* p = oldData; p != end; p++) { |
| 1241 |
if (!Ops::isEmpty(Ops::getKey(p->element))) { |
| 1242 |
HashNumber h = prepareHash(Ops::getKey(p->element)) >> newHashShift; |
| 1243 |
new (wp) Data(std::move(p->element), newHashTable[h]); |
| 1244 |
newHashTable[h] = wp; |
| 1245 |
wp++; |
| 1246 |
} |
| 1247 |
} |
| 1248 |
MOZ_ASSERT(wp == newData + getLiveCount()); |
| 1249 |
|
| 1250 |
freeData(obj->runtimeFromMainThread()->gcContext(), oldData, oldDataLength, |
| 1251 |
getDataCapacity(), hashBuckets()); |
| 1252 |
|
| 1253 |
AddCellMemory(obj, numBytes, MemoryUse::MapObjectData); |
| 1254 |
|
| 1255 |
setHashTable(newHashTable); |
| 1256 |
setData(newData); |
| 1257 |
setDataLength(getLiveCount()); |
| 1258 |
setDataCapacity(newCapacity); |
| 1259 |
setHashShift(newHashShift); |
| 1260 |
setHashCodeScrambler(newHcs); |
| 1261 |
MOZ_ASSERT(hashBuckets() == newHashBuckets); |
| 1262 |
|
| 1263 |
compacted(); |
| 1264 |
return true; |
| 1265 |
} |
| 1266 |
|
| 1267 |
// Change the key of the front entry. |
| 1268 |
// |
| 1269 |
// This calls Ops::hash on both the current key and the new key. Ops::hash on |
| 1270 |
// the current key must return the same hash code as when the entry was added |
| 1271 |
// to the table. |
| 1272 |
void rekey(Data* entry, const UnbarrieredKey& k) { |
| 1273 |
HashNumber oldHash = prepareHash(Ops::getKey(entry->element)); |
| 1274 |
HashNumber newHash = prepareHash(k); |
| 1275 |
reinterpret_cast<UnbarrieredKey&>(Ops::getKeyRef(entry->element)) = k; |
| 1276 |
updateHashTableForRekey(entry, oldHash, newHash); |
| 1277 |
} |
| 1278 |
}; |
| 1279 |
|
| 1280 |
} // namespace detail |
| 1281 |
|
| 1282 |
class OrderedHashMapObject : public detail::OrderedHashTableObject {}; |
| 1283 |
|
| 1284 |
template <class Key, class Value, class OrderedHashPolicy> |
| 1285 |
class MOZ_STACK_CLASS OrderedHashMapImpl { |
| 1286 |
public: |
| 1287 |
class Entry { |
| 1288 |
template <class, class> |
| 1289 |
friend class detail::OrderedHashTableImpl; |
| 1290 |
void operator=(const Entry& rhs) { |
| 1291 |
const_cast<Key&>(key) = rhs.key; |
| 1292 |
value = rhs.value; |
| 1293 |
} |
| 1294 |
|
| 1295 |
void operator=(Entry&& rhs) { |
| 1296 |
MOZ_ASSERT(this != &rhs, "self-move assignment is prohibited"); |
| 1297 |
const_cast<Key&>(key) = std::move(rhs.key); |
| 1298 |
value = std::move(rhs.value); |
| 1299 |
} |
| 1300 |
|
| 1301 |
public: |
| 1302 |
Entry() = default; |
| 1303 |
explicit Entry(const Key& k) : key(k) {} |
| 1304 |
template <typename V> |
| 1305 |
Entry(const Key& k, V&& v) : key(k), value(std::forward<V>(v)) {} |
| 1306 |
Entry(Entry&& rhs) : key(std::move(rhs.key)), value(std::move(rhs.value)) {} |
| 1307 |
|
| 1308 |
const Key key{}; |
| 1309 |
Value value{}; |
| 1310 |
|
| 1311 |
static constexpr size_t offsetOfKey() { return offsetof(Entry, key); } |
| 1312 |
static constexpr size_t offsetOfValue() { return offsetof(Entry, value); } |
| 1313 |
}; |
| 1314 |
|
| 1315 |
private: |
| 1316 |
struct MapOps; |
| 1317 |
using Impl = detail::OrderedHashTableImpl<Entry, MapOps>; |
| 1318 |
|
| 1319 |
struct MapOps : OrderedHashPolicy { |
| 1320 |
using KeyType = Key; |
| 1321 |
static void makeEmpty(Entry* e) { |
| 1322 |
OrderedHashPolicy::makeEmpty(const_cast<Key*>(&e->key)); |
| 1323 |
|
| 1324 |
// Clear the value. Destroying it is another possibility, but that |
| 1325 |
// would complicate class Entry considerably. |
| 1326 |
e->value = Value(); |
| 1327 |
} |
| 1328 |
static const Key& getKey(const Entry& e) { return e.key; } |
| 1329 |
static Key& getKeyRef(Entry& e) { return const_cast<Key&>(e.key); } |
| 1330 |
static void trace(JSTracer* trc, Impl* table, uint32_t index, |
| 1331 |
Entry& entry) { |
| 1332 |
table->traceKey(trc, index, entry.key); |
| 1333 |
table->traceValue(trc, entry.value); |
| 1334 |
} |
| 1335 |
}; |
| 1336 |
|
| 1337 |
Impl impl; |
| 1338 |
|
| 1339 |
public: |
| 1340 |
using Lookup = typename Impl::Lookup; |
| 1341 |
static constexpr size_t SlotCount = Impl::SlotCount; |
| 1342 |
|
| 1343 |
explicit OrderedHashMapImpl(OrderedHashMapObject* obj) : impl(obj) {} |
| 1344 |
|
| 1345 |
void initSlots() { impl.initSlots(); } |
| 1346 |
uint32_t count() const { return impl.count(); } |
| 1347 |
bool has(const Lookup& key) const { return impl.has(key); } |
| 1348 |
template <typename F> |
| 1349 |
[[nodiscard]] bool forEachEntry(F&& f) const { |
| 1350 |
return impl.forEachEntry(f); |
| 1351 |
} |
| 1352 |
#ifdef DEBUG |
| 1353 |
template <typename F> |
| 1354 |
void forEachEntryUpTo(size_t maxCount, F&& f) const { |
| 1355 |
impl.forEachEntryUpTo(maxCount, f); |
| 1356 |
} |
| 1357 |
#endif |
| 1358 |
Entry* get(const Lookup& key) { return impl.get(key); } |
| 1359 |
bool remove(JSContext* cx, const Lookup& key) { return impl.remove(cx, key); } |
| 1360 |
void clear(JSContext* cx) { impl.clear(cx); } |
| 1361 |
|
| 1362 |
void destroy(JS::GCContext* gcx) { impl.destroy(gcx); } |
| 1363 |
|
| 1364 |
template <typename K, typename V> |
| 1365 |
[[nodiscard]] bool put(JSContext* cx, K&& key, V&& value) { |
| 1366 |
return impl.put(cx, Entry(std::forward<K>(key), std::forward<V>(value))); |
| 1367 |
} |
| 1368 |
|
| 1369 |
#ifdef NIGHTLY_BUILD |
| 1370 |
template <typename K, typename V> |
| 1371 |
[[nodiscard]] Entry* getOrAdd(JSContext* cx, K&& key, V&& value) { |
| 1372 |
return impl.getOrAdd(cx, |
| 1373 |
Entry(std::forward<K>(key), std::forward<V>(value))); |
| 1374 |
} |
| 1375 |
#endif // #ifdef NIGHTLY_BUILD |
| 1376 |
|
| 1377 |
#ifdef DEBUG |
| 1378 |
mozilla::Maybe<HashNumber> hash(const Lookup& key) const { |
| 1379 |
return impl.hash(key); |
| 1380 |
} |
| 1381 |
#endif |
| 1382 |
|
| 1383 |
template <typename GetNewKey> |
| 1384 |
mozilla::Maybe<Key> rekeyOneEntry(Lookup& current, GetNewKey&& getNewKey) { |
| 1385 |
// TODO: This is inefficient because we also look up the entry in |
| 1386 |
// impl.rekeyOneEntry below. |
| 1387 |
const Entry* e = get(current); |
| 1388 |
if (!e) { |
| 1389 |
return mozilla::Nothing(); |
| 1390 |
} |
| 1391 |
|
| 1392 |
Key newKey = getNewKey(current); |
| 1393 |
impl.rekeyOneEntry(current, newKey, Entry(newKey, e->value)); |
| 1394 |
return mozilla::Some(newKey); |
| 1395 |
} |
| 1396 |
|
| 1397 |
void initIterator(MapIteratorObject* iter, |
| 1398 |
TableIteratorObject::Kind kind) const { |
| 1399 |
impl.initIterator(iter, kind); |
| 1400 |
} |
| 1401 |
template <typename F> |
| 1402 |
bool iteratorNext(MapIteratorObject* iter, F&& f) const { |
| 1403 |
return impl.iteratorNext(iter, f); |
| 1404 |
} |
| 1405 |
|
| 1406 |
void clearNurseryIterators() { impl.clearNurseryIterators(); } |
| 1407 |
void relinkNurseryIterator(MapIteratorObject* iter) { |
| 1408 |
impl.relinkNurseryIterator(iter); |
| 1409 |
} |
| 1410 |
void updateIteratorsAfterMove(OrderedHashMapObject* old) { |
| 1411 |
impl.updateIteratorsAfterMove(old); |
| 1412 |
} |
| 1413 |
bool hasNurseryIterators() const { return impl.hasNurseryIterators(); } |
| 1414 |
|
| 1415 |
void maybeMoveBufferOnPromotion(Nursery& nursery) { |
| 1416 |
return impl.maybeMoveBufferOnPromotion(nursery); |
| 1417 |
} |
| 1418 |
|
| 1419 |
void trace(JSTracer* trc) { impl.trace(trc); } |
| 1420 |
|
| 1421 |
static constexpr size_t offsetOfEntryKey() { return Entry::offsetOfKey(); } |
| 1422 |
static constexpr size_t offsetOfImplDataElement() { |
| 1423 |
return Impl::offsetOfDataElement(); |
| 1424 |
} |
| 1425 |
static constexpr size_t offsetOfImplDataChain() { |
| 1426 |
return Impl::offsetOfDataChain(); |
| 1427 |
} |
| 1428 |
static constexpr size_t sizeofImplData() { return Impl::sizeofData(); } |
| 1429 |
|
| 1430 |
size_t sizeOfExcludingObject(mozilla::MallocSizeOf mallocSizeOf) const { |
| 1431 |
return impl.sizeOfExcludingObject(mallocSizeOf); |
| 1432 |
} |
| 1433 |
}; |
| 1434 |
|
| 1435 |
class OrderedHashSetObject : public detail::OrderedHashTableObject {}; |
| 1436 |
|
| 1437 |
template <class T, class OrderedHashPolicy> |
| 1438 |
class MOZ_STACK_CLASS OrderedHashSetImpl { |
| 1439 |
private: |
| 1440 |
struct SetOps; |
| 1441 |
using Impl = detail::OrderedHashTableImpl<T, SetOps>; |
| 1442 |
|
| 1443 |
struct SetOps : OrderedHashPolicy { |
| 1444 |
using KeyType = const T; |
| 1445 |
static const T& getKey(const T& v) { return v; } |
| 1446 |
static T& getKeyRef(T& e) { return e; } |
| 1447 |
static void trace(JSTracer* trc, Impl* table, uint32_t index, T& entry) { |
| 1448 |
table->traceKey(trc, index, entry); |
| 1449 |
} |
| 1450 |
}; |
| 1451 |
|
| 1452 |
Impl impl; |
| 1453 |
|
| 1454 |
public: |
| 1455 |
using Lookup = typename Impl::Lookup; |
| 1456 |
static constexpr size_t SlotCount = Impl::SlotCount; |
| 1457 |
|
| 1458 |
explicit OrderedHashSetImpl(OrderedHashSetObject* obj) : impl(obj) {} |
| 1459 |
|
| 1460 |
void initSlots() { impl.initSlots(); } |
| 1461 |
uint32_t count() const { return impl.count(); } |
| 1462 |
bool has(const Lookup& value) const { return impl.has(value); } |
| 1463 |
template <typename F> |
| 1464 |
[[nodiscard]] bool forEachEntry(F&& f) const { |
| 1465 |
return impl.forEachEntry(f); |
| 1466 |
} |
| 1467 |
#ifdef DEBUG |
| 1468 |
template <typename F> |
| 1469 |
void forEachEntryUpTo(size_t maxCount, F&& f) const { |
| 1470 |
impl.forEachEntryUpTo(maxCount, f); |
| 1471 |
} |
| 1472 |
#endif |
| 1473 |
template <typename Input> |
| 1474 |
[[nodiscard]] bool put(JSContext* cx, Input&& value) { |
| 1475 |
return impl.put(cx, std::forward<Input>(value)); |
| 1476 |
} |
| 1477 |
bool remove(JSContext* cx, const Lookup& value) { |
| 1478 |
return impl.remove(cx, value); |
| 1479 |
} |
| 1480 |
void clear(JSContext* cx) { impl.clear(cx); } |
| 1481 |
|
| 1482 |
void destroy(JS::GCContext* gcx) { impl.destroy(gcx); } |
| 1483 |
|
| 1484 |
#ifdef DEBUG |
| 1485 |
mozilla::Maybe<HashNumber> hash(const Lookup& value) const { |
| 1486 |
return impl.hash(value); |
| 1487 |
} |
| 1488 |
#endif |
| 1489 |
|
| 1490 |
template <typename GetNewKey> |
| 1491 |
mozilla::Maybe<T> rekeyOneEntry(Lookup& current, GetNewKey&& getNewKey) { |
| 1492 |
// TODO: This is inefficient because we also look up the entry in |
| 1493 |
// impl.rekeyOneEntry below. |
| 1494 |
if (!has(current)) { |
| 1495 |
return mozilla::Nothing(); |
| 1496 |
} |
| 1497 |
|
| 1498 |
T newKey = getNewKey(current); |
| 1499 |
impl.rekeyOneEntry(current, newKey, newKey); |
| 1500 |
return mozilla::Some(newKey); |
| 1501 |
} |
| 1502 |
|
| 1503 |
void initIterator(SetIteratorObject* iter, |
| 1504 |
TableIteratorObject::Kind kind) const { |
| 1505 |
impl.initIterator(iter, kind); |
| 1506 |
} |
| 1507 |
template <typename F> |
| 1508 |
bool iteratorNext(SetIteratorObject* iter, F&& f) const { |
| 1509 |
return impl.iteratorNext(iter, f); |
| 1510 |
} |
| 1511 |
|
| 1512 |
void clearNurseryIterators() { impl.clearNurseryIterators(); } |
| 1513 |
void relinkNurseryIterator(SetIteratorObject* iter) { |
| 1514 |
impl.relinkNurseryIterator(iter); |
| 1515 |
} |
| 1516 |
void updateIteratorsAfterMove(OrderedHashSetObject* old) { |
| 1517 |
impl.updateIteratorsAfterMove(old); |
| 1518 |
} |
| 1519 |
bool hasNurseryIterators() const { return impl.hasNurseryIterators(); } |
| 1520 |
|
| 1521 |
void maybeMoveBufferOnPromotion(Nursery& nursery) { |
| 1522 |
return impl.maybeMoveBufferOnPromotion(nursery); |
| 1523 |
} |
| 1524 |
|
| 1525 |
void trace(JSTracer* trc) { impl.trace(trc); } |
| 1526 |
|
| 1527 |
static constexpr size_t offsetOfEntryKey() { return 0; } |
| 1528 |
static constexpr size_t offsetOfImplDataElement() { |
| 1529 |
return Impl::offsetOfDataElement(); |
| 1530 |
} |
| 1531 |
static constexpr size_t offsetOfImplDataChain() { |
| 1532 |
return Impl::offsetOfDataChain(); |
| 1533 |
} |
| 1534 |
static constexpr size_t sizeofImplData() { return Impl::sizeofData(); } |
| 1535 |
|
| 1536 |
size_t sizeOfExcludingObject(mozilla::MallocSizeOf mallocSizeOf) const { |
| 1537 |
return impl.sizeOfExcludingObject(mallocSizeOf); |
| 1538 |
} |
| 1539 |
}; |
| 1540 |
|
| 1541 |
} // namespace js |
| 1542 |
|
| 1543 |
#endif /* builtin_OrderedHashTableObject_h */ |