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 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 // Clear the entry which is allowed to en emptied. | 222 // Clear the entry which is allowed to en emptied. |
223 p->clear(); | 223 p->clear(); |
224 occupancy_--; | 224 occupancy_--; |
225 return value; | 225 return value; |
226 } | 226 } |
227 | 227 |
228 template <typename Key, typename Value, typename MatchFun, | 228 template <typename Key, typename Value, typename MatchFun, |
229 class AllocationPolicy> | 229 class AllocationPolicy> |
230 void TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Clear() { | 230 void TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Clear() { |
231 // Mark all entries as empty. | 231 // Mark all entries as empty. |
232 const Entry* end = map_end(); | 232 for (size_t i = 0; i < capacity_; ++i) { |
233 for (Entry* entry = map_; entry < end; entry++) { | 233 map_[i].clear(); |
234 entry->clear(); | |
235 } | 234 } |
236 occupancy_ = 0; | 235 occupancy_ = 0; |
237 } | 236 } |
238 | 237 |
239 template <typename Key, typename Value, typename MatchFun, | 238 template <typename Key, typename Value, typename MatchFun, |
240 class AllocationPolicy> | 239 class AllocationPolicy> |
241 typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry* | 240 typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry* |
242 TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Start() const { | 241 TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Start() const { |
243 return Next(map_ - 1); | 242 return Next(map_ - 1); |
244 } | 243 } |
(...skipping 12 matching lines...) Expand all Loading... |
257 } | 256 } |
258 return nullptr; | 257 return nullptr; |
259 } | 258 } |
260 | 259 |
261 template <typename Key, typename Value, typename MatchFun, | 260 template <typename Key, typename Value, typename MatchFun, |
262 class AllocationPolicy> | 261 class AllocationPolicy> |
263 typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry* | 262 typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry* |
264 TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Probe( | 263 TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Probe( |
265 const Key& key, uint32_t hash) const { | 264 const Key& key, uint32_t hash) const { |
266 DCHECK(base::bits::IsPowerOfTwo32(capacity_)); | 265 DCHECK(base::bits::IsPowerOfTwo32(capacity_)); |
267 Entry* entry = map_ + (hash & (capacity_ - 1)); | 266 size_t i = hash & (capacity_ - 1); |
268 const Entry* end = map_end(); | 267 DCHECK(i < capacity_); |
269 DCHECK(map_ <= entry && entry < end); | |
270 | 268 |
271 DCHECK(occupancy_ < capacity_); // Guarantees loop termination. | 269 DCHECK(occupancy_ < capacity_); // Guarantees loop termination. |
272 while (entry->exists() && !match_(hash, entry->hash, key, entry->key)) { | 270 while (map_[i].exists() && !match_(hash, map_[i].hash, key, map_[i].key)) { |
273 entry++; | 271 i = (i + 1) & (capacity_ - 1); |
274 if (entry >= end) { | |
275 entry = map_; | |
276 } | |
277 } | 272 } |
278 | 273 |
279 return entry; | 274 return &map_[i]; |
280 } | 275 } |
281 | 276 |
282 template <typename Key, typename Value, typename MatchFun, | 277 template <typename Key, typename Value, typename MatchFun, |
283 class AllocationPolicy> | 278 class AllocationPolicy> |
284 typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry* | 279 typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry* |
285 TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::FillEmptyEntry( | 280 TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::FillEmptyEntry( |
286 Entry* entry, const Key& key, const Value& value, uint32_t hash, | 281 Entry* entry, const Key& key, const Value& value, uint32_t hash, |
287 AllocationPolicy allocator) { | 282 AllocationPolicy allocator) { |
288 DCHECK(!entry->exists()); | 283 DCHECK(!entry->exists()); |
289 | 284 |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator)); | 450 return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator)); |
456 } | 451 } |
457 return Iterator(this, this->Lookup(key, key->Hash())); | 452 return Iterator(this, this->Lookup(key, key->Hash())); |
458 } | 453 } |
459 }; | 454 }; |
460 | 455 |
461 } // namespace base | 456 } // namespace base |
462 } // namespace v8 | 457 } // namespace v8 |
463 | 458 |
464 #endif // V8_BASE_HASHMAP_H_ | 459 #endif // V8_BASE_HASHMAP_H_ |
OLD | NEW |