5#ifndef V8_OBJECTS_OFF_HEAP_HASH_TABLE_INL_H_
6#define V8_OBJECTS_OFF_HEAP_HASH_TABLE_INL_H_
16template <
typename Derived>
18 : number_of_elements_(0),
19 number_of_deleted_elements_(0),
22 capacity * Derived::kEntrySize);
25template <
typename Derived>
28 DCHECK_LT(number_of_elements(), new_table->capacity());
29 DCHECK(new_table->HasSufficientCapacityToAdd(number_of_elements()));
31 Derived* derived_this =
static_cast<Derived*
>(
this);
35 if (!IsKey(
key))
continue;
36 uint32_t hash = Derived::Hash(cage_base,
key);
38 new_table->FindInsertionEntry(cage_base, hash);
39 new_table->SetKey(insertion_index,
key);
40 derived_this->CopyEntryExcludingKeyInto(cage_base,
i, new_table,
43 new_table->number_of_elements_ = number_of_elements();
46template <
typename Derived>
48 int additional_elements,
int* new_capacity) {
53 int capacity_after_shrinking = ComputeCapacityWithShrink(
54 capacity_, number_of_elements_ + additional_elements);
56 if (capacity_after_shrinking < capacity_) {
57 DCHECK(HasSufficientCapacityToAdd(
58 capacity_after_shrinking, number_of_elements_, 0, additional_elements));
59 *new_capacity = capacity_after_shrinking;
61 }
else if (!HasSufficientCapacityToAdd(additional_elements)) {
62 *new_capacity = ComputeCapacity(number_of_elements_ + additional_elements);
71template <
typename Derived>
73 int capacity,
int number_of_elements,
int number_of_deleted_elements,
74 int number_of_additional_elements) {
75 int nof = number_of_elements + number_of_additional_elements;
79 if ((nof < capacity) &&
80 ((number_of_deleted_elements <= (capacity - nof) / 2))) {
81 int needed_free = nof / 2;
82 if (nof + needed_free <= capacity)
return true;
88template <
typename Derived>
92 int raw_capacity = at_least_space_for + (at_least_space_for >> 1);
94 return std::max(capacity, Derived::kMinCapacity);
98template <
typename Derived>
100 int current_capacity,
int at_least_space_for) {
102 DCHECK_GE(current_capacity, Derived::kMinCapacity);
103 if (at_least_space_for > (current_capacity / Derived::kMaxEmptyFactor)) {
104 return current_capacity;
108 int new_capacity = ComputeCapacity(at_least_space_for);
109 DCHECK_GE(new_capacity, at_least_space_for);
111 if (new_capacity < Derived::kMinCapacity)
return current_capacity;
115template <
typename Derived>
123template <
typename Derived>
124template <
typename IsolateT,
typename FindKey>
127 uint32_t hash)
const {
128 const Derived* derived_this =
static_cast<const Derived*
>(
this);
131 entry = NextProbe(entry,
count++, capacity_)) {
136 if (element == deleted_element())
continue;
137 if (Derived::KeyIsMatch(isolate,
key, element))
return entry;
141template <
typename Derived>
145 DCHECK(HasSufficientCapacityToAdd(1));
146 const Derived* derived_this =
static_cast<const Derived*
>(
this);
149 entry = NextProbe(entry,
count++, capacity_)) {
153 if (!IsKey(element))
return entry;
157template <
typename Derived>
158template <
typename IsolateT,
typename FindKey>
160 IsolateT* isolate, FindKey
key, uint32_t hash)
const {
162 DCHECK(HasSufficientCapacityToAdd(1));
163 const Derived* derived_this =
static_cast<const Derived*
>(
this);
167 entry = NextProbe(entry,
count++, capacity_)) {
171 if (element == empty_element()) {
174 return insertion_entry;
177 if (element == deleted_element()) {
184 if (Derived::KeyIsMatch(isolate,
key, element))
return entry;
189template <
typename Derived>
190template <
typename Container,
size_t OffsetOfElementsInContainer>
195 static_assert(OffsetOfElementsInContainer ==
196 sizeof(Container) -
sizeof(
Tagged_t));
198 static_assert(OffsetOfElementsInContainer %
kTaggedSize == 0);
201 sizeof(Container) + GetSizeExcludingHeader(capacity),
202 std::max(
alignof(Container),
alignof(
void*)));
206template <
typename Derived>
bool is_not_found() const
static InternalIndex NotFound()
InternalIndex FindEntryOrInsertionEntry(IsolateT *isolate, FindKey key, uint32_t hash) const
static void * Allocate(int capacity)
InternalIndex FindEntry(IsolateT *isolate, FindKey key, uint32_t hash) const
void RehashInto(PtrComprCageBase cage_base, Derived *new_table)
OffHeapHashTableBase(int capacity)
bool HasSufficientCapacityToAdd(int number_of_additional_elements) const
static void Free(void *container)
bool ShouldResizeToAdd(int number_of_additional_elements, int *new_capacity)
void IterateElements(Root root, RootVisitor *visitor)
static int ComputeCapacity(int at_least_space_for)
static int ComputeCapacityWithShrink(int current_capacity, int at_least_space_for)
OffHeapObjectSlot slot(InternalIndex index, int offset=0) const
static constexpr Tagged< Smi > empty_element()
InternalIndex FindInsertionEntry(PtrComprCageBase cage_base, uint32_t hash) const
virtual void VisitRootPointers(Root root, const char *description, FullObjectSlot start, FullObjectSlot end)=0
V8_BASE_EXPORT constexpr uint32_t RoundUpToPowerOfTwo32(uint32_t value)
constexpr int kTaggedSize
void * AlignedAllocWithRetry(size_t size, size_t alignment)
void MemsetTagged(Tagged_t *start, Tagged< MaybeObject > value, size_t counter)
void AlignedFree(void *ptr)
#define DCHECK_NOT_NULL(val)
#define DCHECK_GE(v1, v2)
#define DCHECK(condition)
#define DCHECK_LT(v1, v2)