| 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 22 matching lines...) Expand all Loading... |
| 33 // to pass in a non-default AllocationPolicy but want to use the | 33 // to pass in a non-default AllocationPolicy but want to use the |
| 34 // default value of capacity specified by the implementation. | 34 // default value of capacity specified by the implementation. |
| 35 static const uint32_t kDefaultHashMapCapacity = 8; | 35 static const uint32_t kDefaultHashMapCapacity = 8; |
| 36 | 36 |
| 37 // initial_capacity is the size of the initial hash map; | 37 // initial_capacity is the size of the initial hash map; |
| 38 // it must be a power of 2 (and thus must not be 0). | 38 // it must be a power of 2 (and thus must not be 0). |
| 39 TemplateHashMapImpl(uint32_t capacity = kDefaultHashMapCapacity, | 39 TemplateHashMapImpl(uint32_t capacity = kDefaultHashMapCapacity, |
| 40 MatchFun match = MatchFun(), | 40 MatchFun match = MatchFun(), |
| 41 AllocationPolicy allocator = AllocationPolicy()); | 41 AllocationPolicy allocator = AllocationPolicy()); |
| 42 | 42 |
| 43 // Clones the given hashmap and creates a copy with the same entries. |
| 44 TemplateHashMapImpl(const TemplateHashMapImpl<Key, Value, MatchFun, |
| 45 AllocationPolicy>* original, |
| 46 AllocationPolicy allocator = AllocationPolicy()); |
| 47 |
| 43 ~TemplateHashMapImpl(); | 48 ~TemplateHashMapImpl(); |
| 44 | 49 |
| 45 // If an entry with matching key is found, returns that entry. | 50 // If an entry with matching key is found, returns that entry. |
| 46 // Otherwise, nullptr is returned. | 51 // Otherwise, nullptr is returned. |
| 47 Entry* Lookup(const Key& key, uint32_t hash) const; | 52 Entry* Lookup(const Key& key, uint32_t hash) const; |
| 48 | 53 |
| 49 // If an entry with matching key is found, returns that entry. | 54 // If an entry with matching key is found, returns that entry. |
| 50 // If no matching entry is found, a new entry is inserted with | 55 // If no matching entry is found, a new entry is inserted with |
| 51 // corresponding key, key hash, and default initialized value. | 56 // corresponding key, key hash, and default initialized value. |
| 52 Entry* LookupOrInsert(const Key& key, uint32_t hash, | 57 Entry* LookupOrInsert(const Key& key, uint32_t hash, |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 // TODO(leszeks): This takes up space even if it has no state, maybe replace | 117 // TODO(leszeks): This takes up space even if it has no state, maybe replace |
| 113 // with something that does the empty base optimisation e.g. std::tuple | 118 // with something that does the empty base optimisation e.g. std::tuple |
| 114 MatchFun match_; | 119 MatchFun match_; |
| 115 | 120 |
| 116 Entry* map_end() const { return map_ + capacity_; } | 121 Entry* map_end() const { return map_ + capacity_; } |
| 117 Entry* Probe(const Key& key, uint32_t hash) const; | 122 Entry* Probe(const Key& key, uint32_t hash) const; |
| 118 Entry* FillEmptyEntry(Entry* entry, const Key& key, const Value& value, | 123 Entry* FillEmptyEntry(Entry* entry, const Key& key, const Value& value, |
| 119 uint32_t hash, | 124 uint32_t hash, |
| 120 AllocationPolicy allocator = AllocationPolicy()); | 125 AllocationPolicy allocator = AllocationPolicy()); |
| 121 void Resize(AllocationPolicy allocator); | 126 void Resize(AllocationPolicy allocator); |
| 127 |
| 128 DISALLOW_COPY_AND_ASSIGN(TemplateHashMapImpl); |
| 122 }; | 129 }; |
| 123 template <typename Key, typename Value, typename MatchFun, | 130 template <typename Key, typename Value, typename MatchFun, |
| 124 class AllocationPolicy> | 131 class AllocationPolicy> |
| 125 TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>:: | 132 TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>:: |
| 126 TemplateHashMapImpl(uint32_t initial_capacity, MatchFun match, | 133 TemplateHashMapImpl(uint32_t initial_capacity, MatchFun match, |
| 127 AllocationPolicy allocator) | 134 AllocationPolicy allocator) |
| 128 : match_(match) { | 135 : match_(match) { |
| 129 Initialize(initial_capacity, allocator); | 136 Initialize(initial_capacity, allocator); |
| 130 } | 137 } |
| 131 | 138 |
| 132 template <typename Key, typename Value, typename MatchFun, | 139 template <typename Key, typename Value, typename MatchFun, |
| 133 class AllocationPolicy> | 140 class AllocationPolicy> |
| 141 TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>:: |
| 142 TemplateHashMapImpl(const TemplateHashMapImpl<Key, Value, MatchFun, |
| 143 AllocationPolicy>* original, |
| 144 AllocationPolicy allocator) |
| 145 : capacity_(original->capacity_), |
| 146 occupancy_(original->occupancy_), |
| 147 match_(original->match_) { |
| 148 map_ = reinterpret_cast<Entry*>(allocator.New(capacity_ * sizeof(Entry))); |
| 149 memcpy(map_, original->map_, capacity_ * sizeof(Entry)); |
| 150 } |
| 151 |
| 152 template <typename Key, typename Value, typename MatchFun, |
| 153 class AllocationPolicy> |
| 134 TemplateHashMapImpl<Key, Value, MatchFun, | 154 TemplateHashMapImpl<Key, Value, MatchFun, |
| 135 AllocationPolicy>::~TemplateHashMapImpl() { | 155 AllocationPolicy>::~TemplateHashMapImpl() { |
| 136 AllocationPolicy::Delete(map_); | 156 AllocationPolicy::Delete(map_); |
| 137 } | 157 } |
| 138 | 158 |
| 139 template <typename Key, typename Value, typename MatchFun, | 159 template <typename Key, typename Value, typename MatchFun, |
| 140 class AllocationPolicy> | 160 class AllocationPolicy> |
| 141 typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry* | 161 typename TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Entry* |
| 142 TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Lookup( | 162 TemplateHashMapImpl<Key, Value, MatchFun, AllocationPolicy>::Lookup( |
| 143 const Key& key, uint32_t hash) const { | 163 const Key& key, uint32_t hash) const { |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 Base; | 395 Base; |
| 376 | 396 |
| 377 public: | 397 public: |
| 378 typedef bool (*MatchFun)(void*, void*); | 398 typedef bool (*MatchFun)(void*, void*); |
| 379 | 399 |
| 380 CustomMatcherTemplateHashMapImpl( | 400 CustomMatcherTemplateHashMapImpl( |
| 381 MatchFun match, uint32_t capacity = Base::kDefaultHashMapCapacity, | 401 MatchFun match, uint32_t capacity = Base::kDefaultHashMapCapacity, |
| 382 AllocationPolicy allocator = AllocationPolicy()) | 402 AllocationPolicy allocator = AllocationPolicy()) |
| 383 : Base(capacity, HashEqualityThenKeyMatcher<void*, MatchFun>(match), | 403 : Base(capacity, HashEqualityThenKeyMatcher<void*, MatchFun>(match), |
| 384 allocator) {} | 404 allocator) {} |
| 405 |
| 406 CustomMatcherTemplateHashMapImpl( |
| 407 const CustomMatcherTemplateHashMapImpl<AllocationPolicy>* original, |
| 408 AllocationPolicy allocator = AllocationPolicy()) |
| 409 : Base(original, allocator) {} |
| 410 |
| 411 private: |
| 412 DISALLOW_COPY_AND_ASSIGN(CustomMatcherTemplateHashMapImpl); |
| 385 }; | 413 }; |
| 386 | 414 |
| 387 typedef CustomMatcherTemplateHashMapImpl<DefaultAllocationPolicy> | 415 typedef CustomMatcherTemplateHashMapImpl<DefaultAllocationPolicy> |
| 388 CustomMatcherHashMap; | 416 CustomMatcherHashMap; |
| 389 | 417 |
| 390 // Match function which compares keys directly by equality. | 418 // Match function which compares keys directly by equality. |
| 391 template <typename Key> | 419 template <typename Key> |
| 392 struct KeyEqualityMatcher { | 420 struct KeyEqualityMatcher { |
| 393 bool operator()(uint32_t hash1, uint32_t hash2, const Key& key1, | 421 bool operator()(uint32_t hash1, uint32_t hash2, const Key& key1, |
| 394 const Key& key2) const { | 422 const Key& key2) const { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator)); | 493 return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator)); |
| 466 } | 494 } |
| 467 return Iterator(this, this->Lookup(key, key->Hash())); | 495 return Iterator(this, this->Lookup(key, key->Hash())); |
| 468 } | 496 } |
| 469 }; | 497 }; |
| 470 | 498 |
| 471 } // namespace base | 499 } // namespace base |
| 472 } // namespace v8 | 500 } // namespace v8 |
| 473 | 501 |
| 474 #endif // V8_BASE_HASHMAP_H_ | 502 #endif // V8_BASE_HASHMAP_H_ |
| OLD | NEW |