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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 // If an entry with matching key is found, returns that entry. | 54 // If an entry with matching key is found, returns that entry. |
55 // Otherwise, NULL is returned. | 55 // Otherwise, NULL is returned. |
56 Entry* Lookup(void* key, uint32_t hash) const; | 56 Entry* Lookup(void* key, uint32_t hash) const; |
57 | 57 |
58 // If an entry with matching key is found, returns that entry. | 58 // If an entry with matching key is found, returns that entry. |
59 // If no matching entry is found, a new entry is inserted with | 59 // If no matching entry is found, a new entry is inserted with |
60 // corresponding key, key hash, and NULL value. | 60 // corresponding key, key hash, and NULL value. |
61 Entry* LookupOrInsert(void* key, uint32_t hash, | 61 Entry* LookupOrInsert(void* key, uint32_t hash, |
62 AllocationPolicy allocator = AllocationPolicy()); | 62 AllocationPolicy allocator = AllocationPolicy()); |
63 | 63 |
| 64 Entry* InsertNew(void* key, uint32_t hash, |
| 65 AllocationPolicy allocator = AllocationPolicy()); |
| 66 |
64 // Removes the entry with matching key. | 67 // Removes the entry with matching key. |
65 // It returns the value of the deleted entry | 68 // It returns the value of the deleted entry |
66 // or null if there is no value for such key. | 69 // or null if there is no value for such key. |
67 void* Remove(void* key, uint32_t hash); | 70 void* Remove(void* key, uint32_t hash); |
68 | 71 |
69 // Empties the hash map (occupancy() == 0). | 72 // Empties the hash map (occupancy() == 0). |
70 void Clear(); | 73 void Clear(); |
71 | 74 |
72 // The number of (non-empty) entries in the table. | 75 // The number of (non-empty) entries in the table. |
73 uint32_t occupancy() const { return occupancy_; } | 76 uint32_t occupancy() const { return occupancy_; } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 template <class AllocationPolicy> | 130 template <class AllocationPolicy> |
128 typename TemplateHashMapImpl<AllocationPolicy>::Entry* | 131 typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
129 TemplateHashMapImpl<AllocationPolicy>::LookupOrInsert( | 132 TemplateHashMapImpl<AllocationPolicy>::LookupOrInsert( |
130 void* key, uint32_t hash, AllocationPolicy allocator) { | 133 void* key, uint32_t hash, AllocationPolicy allocator) { |
131 // Find a matching entry. | 134 // Find a matching entry. |
132 Entry* p = Probe(key, hash); | 135 Entry* p = Probe(key, hash); |
133 if (p->key != NULL) { | 136 if (p->key != NULL) { |
134 return p; | 137 return p; |
135 } | 138 } |
136 | 139 |
| 140 return InsertNew(key, hash, allocator); |
| 141 } |
| 142 |
| 143 template <class AllocationPolicy> |
| 144 typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
| 145 TemplateHashMapImpl<AllocationPolicy>::InsertNew(void* key, uint32_t hash, |
| 146 AllocationPolicy allocator) { |
| 147 // Find a matching entry. |
| 148 Entry* p = Probe(key, hash); |
| 149 DCHECK(p->key == NULL); |
| 150 |
137 // No entry found; insert one. | 151 // No entry found; insert one. |
138 p->key = key; | 152 p->key = key; |
139 p->value = NULL; | 153 p->value = NULL; |
140 p->hash = hash; | 154 p->hash = hash; |
141 p->order = occupancy_; | 155 p->order = occupancy_; |
142 occupancy_++; | 156 occupancy_++; |
143 | 157 |
144 // Grow the map if we reached >= 80% occupancy. | 158 // Grow the map if we reached >= 80% occupancy. |
145 if (occupancy_ + occupancy_ / 4 >= capacity_) { | 159 if (occupancy_ + occupancy_ / 4 >= capacity_) { |
146 Resize(allocator); | 160 Resize(allocator); |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator)); | 357 return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator)); |
344 } | 358 } |
345 return Iterator(this, this->Lookup(key, key->Hash())); | 359 return Iterator(this, this->Lookup(key, key->Hash())); |
346 } | 360 } |
347 }; | 361 }; |
348 | 362 |
349 } // namespace base | 363 } // namespace base |
350 } // namespace v8 | 364 } // namespace v8 |
351 | 365 |
352 #endif // V8_BASE_HASHMAP_H_ | 366 #endif // V8_BASE_HASHMAP_H_ |
OLD | NEW |