OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // The reason we write our own hash map instead of using unordered_map in STL, | 5 // The reason we write our own hash map instead of using unordered_map in STL, |
6 // is that STL containers use a mutex pool on debug build, which will lead to | 6 // is that STL containers use a mutex pool on debug build, which will lead to |
7 // deadlock when we are using async signal handler. | 7 // deadlock when we are using async signal handler. |
8 | 8 |
9 #ifndef V8_BASE_HASHMAP_H_ | 9 #ifndef V8_BASE_HASHMAP_H_ |
10 #define V8_BASE_HASHMAP_H_ | 10 #define V8_BASE_HASHMAP_H_ |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 | 48 |
49 ~TemplateHashMapImpl(); | 49 ~TemplateHashMapImpl(); |
50 | 50 |
51 // If an entry with matching key is found, returns that entry. | 51 // If an entry with matching key is found, returns that entry. |
52 // Otherwise, nullptr is returned. | 52 // Otherwise, nullptr is returned. |
53 Entry* Lookup(const Key& key, uint32_t hash) const; | 53 Entry* Lookup(const Key& key, uint32_t hash) const; |
54 | 54 |
55 // If an entry with matching key is found, returns that entry. | 55 // If an entry with matching key is found, returns that entry. |
56 // If no matching entry is found, a new entry is inserted with | 56 // If no matching entry is found, a new entry is inserted with |
57 // corresponding key, key hash, and default initialized value. | 57 // corresponding key, key hash, and default initialized value. |
58 Entry* LookupOrInsert(const Key& key, uint32_t hash); | 58 Entry* LookupOrInsert(const Key& key, uint32_t hash, |
| 59 AllocationPolicy allocator = AllocationPolicy()); |
59 | 60 |
60 Entry* InsertNew(const Key& key, uint32_t hash); | 61 Entry* InsertNew(const Key& key, uint32_t hash, |
| 62 AllocationPolicy allocator = AllocationPolicy()); |
61 | 63 |
62 // Removes the entry with matching key. | 64 // Removes the entry with matching key. |
63 // It returns the value of the deleted entry | 65 // It returns the value of the deleted entry |
64 // or null if there is no value for such key. | 66 // or null if there is no value for such key. |
65 Value Remove(const Key& key, uint32_t hash); | 67 Value Remove(const Key& key, uint32_t hash); |
66 | 68 |
67 // Empties the hash map (occupancy() == 0). | 69 // Empties the hash map (occupancy() == 0). |
68 void Clear(); | 70 void Clear(); |
69 | 71 |
70 // The number of (non-empty) entries in the table. | 72 // The number of (non-empty) entries in the table. |
(...skipping 22 matching lines...) Expand all Loading... |
93 static bool PointersMatch(typename MatchFunHelper<Key>::KeyRef key1, | 95 static bool PointersMatch(typename MatchFunHelper<Key>::KeyRef key1, |
94 typename MatchFunHelper<Key>::KeyRef key2) { | 96 typename MatchFunHelper<Key>::KeyRef key2) { |
95 return key1 == key2; | 97 return key1 == key2; |
96 } | 98 } |
97 | 99 |
98 private: | 100 private: |
99 MatchFun match_; | 101 MatchFun match_; |
100 Entry* map_; | 102 Entry* map_; |
101 uint32_t capacity_; | 103 uint32_t capacity_; |
102 uint32_t occupancy_; | 104 uint32_t occupancy_; |
103 AllocationPolicy allocator_; | |
104 | 105 |
105 Entry* map_end() const { return map_ + capacity_; } | 106 Entry* map_end() const { return map_ + capacity_; } |
106 Entry* Probe(const Key& key, uint32_t hash) const; | 107 Entry* Probe(const Key& key, uint32_t hash) const; |
107 Entry* FillEmptyEntry(Entry* entry, const Key& key, const Value& value, | 108 Entry* FillEmptyEntry(Entry* entry, const Key& key, const Value& value, |
108 uint32_t hash); | 109 uint32_t hash, |
109 void Initialize(uint32_t capacity); | 110 AllocationPolicy allocator = AllocationPolicy()); |
110 void Resize(); | 111 void Initialize(uint32_t capacity, AllocationPolicy allocator); |
| 112 void Resize(AllocationPolicy allocator); |
111 }; | 113 }; |
112 | 114 |
113 template <typename Key> | 115 template <typename Key> |
114 struct MatchFunHelper { | 116 struct MatchFunHelper { |
115 typedef const Key& KeyRef; | 117 typedef const Key& KeyRef; |
116 typedef bool (*Fun)(KeyRef key1, KeyRef key2); | 118 typedef bool (*Fun)(KeyRef key1, KeyRef key2); |
117 }; | 119 }; |
118 | 120 |
119 template <typename Key> | 121 template <typename Key> |
120 struct MatchFunHelper<Key*> { | 122 struct MatchFunHelper<Key*> { |
121 typedef Key* KeyRef; | 123 typedef Key* KeyRef; |
122 typedef bool (*Fun)(KeyRef key1, KeyRef key2); | 124 typedef bool (*Fun)(KeyRef key1, KeyRef key2); |
123 }; | 125 }; |
124 | 126 |
125 typedef TemplateHashMapImpl<void*, void*, DefaultAllocationPolicy> HashMap; | 127 typedef TemplateHashMapImpl<void*, void*, DefaultAllocationPolicy> HashMap; |
126 | 128 |
127 template <typename Key, typename Value, class AllocationPolicy> | 129 template <typename Key, typename Value, class AllocationPolicy> |
128 TemplateHashMapImpl<Key, Value, AllocationPolicy>::TemplateHashMapImpl( | 130 TemplateHashMapImpl<Key, Value, AllocationPolicy>::TemplateHashMapImpl( |
129 MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator) | 131 MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator) { |
130 : allocator_(allocator) { | |
131 match_ = match; | 132 match_ = match; |
132 Initialize(initial_capacity); | 133 Initialize(initial_capacity, allocator); |
133 } | 134 } |
134 | 135 |
135 template <typename Key, typename Value, class AllocationPolicy> | 136 template <typename Key, typename Value, class AllocationPolicy> |
136 TemplateHashMapImpl<Key, Value, AllocationPolicy>::~TemplateHashMapImpl() { | 137 TemplateHashMapImpl<Key, Value, AllocationPolicy>::~TemplateHashMapImpl() { |
137 allocator_.Delete(map_); | 138 AllocationPolicy::Delete(map_); |
138 } | 139 } |
139 | 140 |
140 template <typename Key, typename Value, class AllocationPolicy> | 141 template <typename Key, typename Value, class AllocationPolicy> |
141 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry* | 142 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry* |
142 TemplateHashMapImpl<Key, Value, AllocationPolicy>::Lookup(const Key& key, | 143 TemplateHashMapImpl<Key, Value, AllocationPolicy>::Lookup(const Key& key, |
143 uint32_t hash) const { | 144 uint32_t hash) const { |
144 Entry* entry = Probe(key, hash); | 145 Entry* entry = Probe(key, hash); |
145 return entry->exists() ? entry : nullptr; | 146 return entry->exists() ? entry : nullptr; |
146 } | 147 } |
147 | 148 |
148 template <typename Key, typename Value, class AllocationPolicy> | 149 template <typename Key, typename Value, class AllocationPolicy> |
149 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry* | 150 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry* |
150 TemplateHashMapImpl<Key, Value, AllocationPolicy>::LookupOrInsert( | 151 TemplateHashMapImpl<Key, Value, AllocationPolicy>::LookupOrInsert( |
151 const Key& key, uint32_t hash) { | 152 const Key& key, uint32_t hash, AllocationPolicy allocator) { |
152 // Find a matching entry. | 153 // Find a matching entry. |
153 Entry* entry = Probe(key, hash); | 154 Entry* entry = Probe(key, hash); |
154 if (entry->exists()) { | 155 if (entry->exists()) { |
155 return entry; | 156 return entry; |
156 } | 157 } |
157 | 158 |
158 return FillEmptyEntry(entry, key, Value(), hash); | 159 return FillEmptyEntry(entry, key, Value(), hash, allocator); |
159 } | 160 } |
160 | 161 |
161 template <typename Key, typename Value, class AllocationPolicy> | 162 template <typename Key, typename Value, class AllocationPolicy> |
162 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry* | 163 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry* |
163 TemplateHashMapImpl<Key, Value, AllocationPolicy>::InsertNew(const Key& key, | 164 TemplateHashMapImpl<Key, Value, AllocationPolicy>::InsertNew( |
164 uint32_t hash) { | 165 const Key& key, uint32_t hash, AllocationPolicy allocator) { |
165 Entry* entry = Probe(key, hash); | 166 Entry* entry = Probe(key, hash); |
166 return FillEmptyEntry(entry, key, Value(), hash); | 167 return FillEmptyEntry(entry, key, Value(), hash, allocator); |
167 } | 168 } |
168 | 169 |
169 template <typename Key, typename Value, class AllocationPolicy> | 170 template <typename Key, typename Value, class AllocationPolicy> |
170 Value TemplateHashMapImpl<Key, Value, AllocationPolicy>::Remove(const Key& key, | 171 Value TemplateHashMapImpl<Key, Value, AllocationPolicy>::Remove(const Key& key, |
171 uint32_t hash) { | 172 uint32_t hash) { |
172 // Lookup the entry for the key to remove. | 173 // Lookup the entry for the key to remove. |
173 Entry* p = Probe(key, hash); | 174 Entry* p = Probe(key, hash); |
174 if (!p->exists()) { | 175 if (!p->exists()) { |
175 // Key not found nothing to remove. | 176 // Key not found nothing to remove. |
176 return nullptr; | 177 return nullptr; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 entry = map_; | 273 entry = map_; |
273 } | 274 } |
274 } | 275 } |
275 | 276 |
276 return entry; | 277 return entry; |
277 } | 278 } |
278 | 279 |
279 template <typename Key, typename Value, class AllocationPolicy> | 280 template <typename Key, typename Value, class AllocationPolicy> |
280 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry* | 281 typename TemplateHashMapImpl<Key, Value, AllocationPolicy>::Entry* |
281 TemplateHashMapImpl<Key, Value, AllocationPolicy>::FillEmptyEntry( | 282 TemplateHashMapImpl<Key, Value, AllocationPolicy>::FillEmptyEntry( |
282 Entry* entry, const Key& key, const Value& value, uint32_t hash) { | 283 Entry* entry, const Key& key, const Value& value, uint32_t hash, |
| 284 AllocationPolicy allocator) { |
283 DCHECK(!entry->exists()); | 285 DCHECK(!entry->exists()); |
284 | 286 |
285 new (entry) Entry(key, value, hash); | 287 new (entry) Entry(key, value, hash); |
286 occupancy_++; | 288 occupancy_++; |
287 | 289 |
288 // Grow the map if we reached >= 80% occupancy. | 290 // Grow the map if we reached >= 80% occupancy. |
289 if (occupancy_ + occupancy_ / 4 >= capacity_) { | 291 if (occupancy_ + occupancy_ / 4 >= capacity_) { |
290 Resize(); | 292 Resize(allocator); |
291 entry = Probe(key, hash); | 293 entry = Probe(key, hash); |
292 } | 294 } |
293 | 295 |
294 return entry; | 296 return entry; |
295 } | 297 } |
296 | 298 |
297 template <typename Key, typename Value, class AllocationPolicy> | 299 template <typename Key, typename Value, class AllocationPolicy> |
298 void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Initialize( | 300 void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Initialize( |
299 uint32_t capacity) { | 301 uint32_t capacity, AllocationPolicy allocator) { |
300 DCHECK(base::bits::IsPowerOfTwo32(capacity)); | 302 DCHECK(base::bits::IsPowerOfTwo32(capacity)); |
301 map_ = reinterpret_cast<Entry*>(allocator_.New(capacity * sizeof(Entry))); | 303 map_ = reinterpret_cast<Entry*>(allocator.New(capacity * sizeof(Entry))); |
302 if (map_ == nullptr) { | 304 if (map_ == nullptr) { |
303 FATAL("Out of memory: HashMap::Initialize"); | 305 FATAL("Out of memory: HashMap::Initialize"); |
304 return; | 306 return; |
305 } | 307 } |
306 capacity_ = capacity; | 308 capacity_ = capacity; |
307 Clear(); | 309 Clear(); |
308 } | 310 } |
309 | 311 |
310 template <typename Key, typename Value, class AllocationPolicy> | 312 template <typename Key, typename Value, class AllocationPolicy> |
311 void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Resize() { | 313 void TemplateHashMapImpl<Key, Value, AllocationPolicy>::Resize( |
| 314 AllocationPolicy allocator) { |
312 Entry* map = map_; | 315 Entry* map = map_; |
313 uint32_t n = occupancy_; | 316 uint32_t n = occupancy_; |
314 | 317 |
315 // Allocate larger map. | 318 // Allocate larger map. |
316 Initialize(capacity_ * 2); | 319 Initialize(capacity_ * 2, allocator); |
317 | 320 |
318 // Rehash all current entries. | 321 // Rehash all current entries. |
319 for (Entry* entry = map; n > 0; entry++) { | 322 for (Entry* entry = map; n > 0; entry++) { |
320 if (entry->exists()) { | 323 if (entry->exists()) { |
321 Entry* new_entry = Probe(entry->key, entry->hash); | 324 Entry* new_entry = Probe(entry->key, entry->hash); |
322 new_entry = | 325 new_entry = FillEmptyEntry(new_entry, entry->key, entry->value, |
323 FillEmptyEntry(new_entry, entry->key, entry->value, entry->hash); | 326 entry->hash, allocator); |
324 n--; | 327 n--; |
325 } | 328 } |
326 } | 329 } |
327 | 330 |
328 // Delete old map. | 331 // Delete old map. |
329 allocator_.Delete(map); | 332 AllocationPolicy::Delete(map); |
330 } | 333 } |
331 | 334 |
332 // A hash map for pointer keys and values with an STL-like interface. | 335 // A hash map for pointer keys and values with an STL-like interface. |
333 template <class Key, class Value, class AllocationPolicy> | 336 template <class Key, class Value, class AllocationPolicy> |
334 class TemplateHashMap | 337 class TemplateHashMap |
335 : private TemplateHashMapImpl<void*, void*, AllocationPolicy> { | 338 : private TemplateHashMapImpl<void*, void*, AllocationPolicy> { |
336 public: | 339 public: |
337 STATIC_ASSERT(sizeof(Key*) == sizeof(void*)); // NOLINT | 340 STATIC_ASSERT(sizeof(Key*) == sizeof(void*)); // NOLINT |
338 STATIC_ASSERT(sizeof(Value*) == sizeof(void*)); // NOLINT | 341 STATIC_ASSERT(sizeof(Value*) == sizeof(void*)); // NOLINT |
339 struct value_type { | 342 struct value_type { |
(...skipping 27 matching lines...) Expand all Loading... |
367 void*, void*, AllocationPolicy>::MatchFun match, | 370 void*, void*, AllocationPolicy>::MatchFun match, |
368 AllocationPolicy allocator = AllocationPolicy()) | 371 AllocationPolicy allocator = AllocationPolicy()) |
369 : TemplateHashMapImpl<void*, void*, AllocationPolicy>( | 372 : TemplateHashMapImpl<void*, void*, AllocationPolicy>( |
370 match, | 373 match, |
371 TemplateHashMapImpl<void*, void*, | 374 TemplateHashMapImpl<void*, void*, |
372 AllocationPolicy>::kDefaultHashMapCapacity, | 375 AllocationPolicy>::kDefaultHashMapCapacity, |
373 allocator) {} | 376 allocator) {} |
374 | 377 |
375 Iterator begin() const { return Iterator(this, this->Start()); } | 378 Iterator begin() const { return Iterator(this, this->Start()); } |
376 Iterator end() const { return Iterator(this, nullptr); } | 379 Iterator end() const { return Iterator(this, nullptr); } |
377 Iterator find(Key* key, bool insert = false) { | 380 Iterator find(Key* key, bool insert = false, |
| 381 AllocationPolicy allocator = AllocationPolicy()) { |
378 if (insert) { | 382 if (insert) { |
379 return Iterator(this, this->LookupOrInsert(key, key->Hash())); | 383 return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator)); |
380 } | 384 } |
381 return Iterator(this, this->Lookup(key, key->Hash())); | 385 return Iterator(this, this->Lookup(key, key->Hash())); |
382 } | 386 } |
383 }; | 387 }; |
384 | 388 |
385 } // namespace base | 389 } // namespace base |
386 } // namespace v8 | 390 } // namespace v8 |
387 | 391 |
388 #endif // V8_BASE_HASHMAP_H_ | 392 #endif // V8_BASE_HASHMAP_H_ |
OLD | NEW |