5 #ifndef V8_IDENTITY_MAP_H_ 6 #define V8_IDENTITY_MAP_H_ 8 #include "src/base/functional.h" 9 #include "src/handles.h" 10 #include "src/objects/heap-object.h" 22 bool empty()
const {
return size_ == 0; }
23 int size()
const {
return size_; }
24 int capacity()
const {
return capacity_; }
25 bool is_iterable()
const {
return is_iterable_; }
30 friend class IdentityMapTester;
32 typedef void** RawEntry;
42 is_iterable_(
false) {}
46 RawEntry FindEntry(
Address key)
const;
47 bool DeleteEntry(
Address key,
void** deleted_value);
50 Address KeyAtIndex(
int index)
const;
52 V8_EXPORT_PRIVATE RawEntry EntryAtIndex(
int index)
const;
53 V8_EXPORT_PRIVATE
int NextIndex(
int index)
const;
55 void EnableIteration();
56 void DisableIteration();
58 virtual void** NewPointerArray(
size_t length) = 0;
59 virtual void DeleteArray(
void* array) = 0;
63 int ScanKeysFor(
Address address)
const;
66 int LookupOrInsert(
Address key);
67 bool DeleteIndex(
int index,
void** deleted_value);
69 void Resize(
int new_capacity);
70 int Hash(
Address address)
const;
91 template <
typename V,
class AllocationPolicy>
95 AllocationPolicy allocator = AllocationPolicy())
97 ~IdentityMap()
override { Clear(); };
103 V* Get(Handle<Object> key) {
return Get(*key); }
104 V* Get(Object* key) {
105 return reinterpret_cast<V*
>(GetEntry(reinterpret_cast<Address>(key)));
107 V* Get(ObjectPtr key) {
return reinterpret_cast<V*
>(GetEntry(key.ptr())); }
113 V* Find(Handle<Object> key)
const {
return Find(*key); }
114 V* Find(Object* key)
const {
115 return reinterpret_cast<V*
>(FindEntry(reinterpret_cast<Address>(key)));
117 V* Find(ObjectPtr key)
const {
118 return reinterpret_cast<V*
>(FindEntry(key.ptr()));
122 void Set(Handle<Object> key,
V v) { Set(*key, v); }
123 void Set(Object* key,
V v) {
124 *(
reinterpret_cast<V*
>(GetEntry(reinterpret_cast<Address>(key)))) = v;
126 void Set(ObjectPtr key,
V v) {
127 *(
reinterpret_cast<V*
>(GetEntry(key.ptr()))) = v;
130 bool Delete(Handle<Object> key,
V* deleted_value) {
131 return Delete(*key, deleted_value);
133 bool Delete(Object* key,
V* deleted_value) {
135 bool deleted_something = DeleteEntry(reinterpret_cast<Address>(key), &v);
136 if (deleted_value !=
nullptr && deleted_something) {
137 *deleted_value = *
reinterpret_cast<V*
>(&v);
139 return deleted_something;
141 bool Delete(ObjectPtr key,
V* deleted_value) {
143 bool deleted_something = DeleteEntry(key.ptr(), &v);
144 if (deleted_value !=
nullptr && deleted_something) {
145 *deleted_value = *
reinterpret_cast<V*
>(&v);
147 return deleted_something;
151 void Clear() { IdentityMapBase::Clear(); }
158 index_ = map_->NextIndex(index_);
163 return reinterpret_cast<Object*
>(map_->KeyAtIndex(index_));
166 return reinterpret_cast<V*
>(map_->EntryAtIndex(index_));
169 V* operator*() {
return entry(); }
170 V* operator->() {
return entry(); }
171 bool operator!=(
const Iterator& other) {
return index_ != other.index_; }
185 CHECK(!map_->is_iterable());
186 map_->EnableIteration();
189 CHECK(map_->is_iterable());
190 map_->DisableIteration();
202 void** NewPointerArray(
size_t length)
override {
203 return static_cast<void**
>(allocator_.New(
sizeof(
void*) * length));
205 void DeleteArray(
void* array)
override { allocator_.Delete(array); }
208 AllocationPolicy allocator_;
209 DISALLOW_COPY_AND_ASSIGN(IdentityMap);
215 #endif // V8_IDENTITY_MAP_H_