blob: efb5dc8baaafd7bdb0f036bca43347f1322aa42d [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdoch61f157c2016-09-16 13:49:30 +01005// 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
7// deadlock when we are using async signal handler.
Steve Blocka7e24c12009-10-30 11:49:00 +00008
Ben Murdoch61f157c2016-09-16 13:49:30 +01009#ifndef V8_BASE_HASHMAP_H_
10#define V8_BASE_HASHMAP_H_
11
12#include <stdlib.h>
13
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014#include "src/base/bits.h"
15#include "src/base/logging.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000016
Steve Blocka7e24c12009-10-30 11:49:00 +000017namespace v8 {
Ben Murdoch61f157c2016-09-16 13:49:30 +010018namespace base {
Steve Blocka7e24c12009-10-30 11:49:00 +000019
Ben Murdoch61f157c2016-09-16 13:49:30 +010020class DefaultAllocationPolicy {
21 public:
22 V8_INLINE void* New(size_t size) { return malloc(size); }
23 V8_INLINE static void Delete(void* p) { free(p); }
24};
25
26template <class AllocationPolicy>
Ben Murdoch3ef787d2012-04-12 10:51:47 +010027class TemplateHashMapImpl {
Steve Blocka7e24c12009-10-30 11:49:00 +000028 public:
Ben Murdoch61f157c2016-09-16 13:49:30 +010029 typedef bool (*MatchFun)(void* key1, void* key2);
Steve Blocka7e24c12009-10-30 11:49:00 +000030
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031 // The default capacity. This is used by the call sites which want
32 // to pass in a non-default AllocationPolicy but want to use the
33 // default value of capacity specified by the implementation.
34 static const uint32_t kDefaultHashMapCapacity = 8;
35
Steve Blocka7e24c12009-10-30 11:49:00 +000036 // initial_capacity is the size of the initial hash map;
37 // it must be a power of 2 (and thus must not be 0).
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038 TemplateHashMapImpl(MatchFun match,
39 uint32_t capacity = kDefaultHashMapCapacity,
40 AllocationPolicy allocator = AllocationPolicy());
Steve Blocka7e24c12009-10-30 11:49:00 +000041
Ben Murdoch3ef787d2012-04-12 10:51:47 +010042 ~TemplateHashMapImpl();
Steve Blocka7e24c12009-10-30 11:49:00 +000043
44 // HashMap entries are (key, value, hash) triplets.
45 // Some clients may not need to use the value slot
46 // (e.g. implementers of sets, where the key is the value).
47 struct Entry {
48 void* key;
49 void* value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050 uint32_t hash; // The full hash value for key
Ben Murdoch61f157c2016-09-16 13:49:30 +010051 int order; // If you never remove entries this is the insertion order.
Steve Blocka7e24c12009-10-30 11:49:00 +000052 };
53
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000054 // If an entry with matching key is found, returns that entry.
Steve Blocka7e24c12009-10-30 11:49:00 +000055 // Otherwise, NULL is returned.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000056 Entry* Lookup(void* key, uint32_t hash) const;
57
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
60 // corresponding key, key hash, and NULL value.
61 Entry* LookupOrInsert(void* key, uint32_t hash,
62 AllocationPolicy allocator = AllocationPolicy());
Steve Blocka7e24c12009-10-30 11:49:00 +000063
64 // Removes the entry with matching key.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065 // It returns the value of the deleted entry
66 // or null if there is no value for such key.
67 void* Remove(void* key, uint32_t hash);
Steve Blocka7e24c12009-10-30 11:49:00 +000068
69 // Empties the hash map (occupancy() == 0).
70 void Clear();
71
72 // The number of (non-empty) entries in the table.
Kristian Monsen0d5e1162010-09-30 15:31:59 +010073 uint32_t occupancy() const { return occupancy_; }
Steve Blocka7e24c12009-10-30 11:49:00 +000074
75 // The capacity of the table. The implementation
76 // makes sure that occupancy is at most 80% of
77 // the table capacity.
Kristian Monsen0d5e1162010-09-30 15:31:59 +010078 uint32_t capacity() const { return capacity_; }
Steve Blocka7e24c12009-10-30 11:49:00 +000079
80 // Iteration
81 //
82 // for (Entry* p = map.Start(); p != NULL; p = map.Next(p)) {
83 // ...
84 // }
85 //
86 // If entries are inserted during iteration, the effect of
87 // calling Next() is undefined.
88 Entry* Start() const;
89 Entry* Next(Entry* p) const;
90
Ben Murdochb8a8cc12014-11-26 15:28:44 +000091 // Some match functions defined for convenience.
Ben Murdoch61f157c2016-09-16 13:49:30 +010092 static bool PointersMatch(void* key1, void* key2) { return key1 == key2; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093
Steve Blocka7e24c12009-10-30 11:49:00 +000094 private:
Steve Blocka7e24c12009-10-30 11:49:00 +000095 MatchFun match_;
96 Entry* map_;
97 uint32_t capacity_;
98 uint32_t occupancy_;
99
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100100 Entry* map_end() const { return map_ + capacity_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000101 Entry* Probe(void* key, uint32_t hash) const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102 void Initialize(uint32_t capacity, AllocationPolicy allocator);
103 void Resize(AllocationPolicy allocator);
Steve Blocka7e24c12009-10-30 11:49:00 +0000104};
105
Ben Murdoch61f157c2016-09-16 13:49:30 +0100106typedef TemplateHashMapImpl<DefaultAllocationPolicy> HashMap;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100107
Ben Murdoch61f157c2016-09-16 13:49:30 +0100108template <class AllocationPolicy>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109TemplateHashMapImpl<AllocationPolicy>::TemplateHashMapImpl(
110 MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100111 match_ = match;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112 Initialize(initial_capacity, allocator);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100113}
114
Ben Murdoch61f157c2016-09-16 13:49:30 +0100115template <class AllocationPolicy>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000116TemplateHashMapImpl<AllocationPolicy>::~TemplateHashMapImpl() {
117 AllocationPolicy::Delete(map_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100118}
119
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000120template <class AllocationPolicy>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121typename TemplateHashMapImpl<AllocationPolicy>::Entry*
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000122TemplateHashMapImpl<AllocationPolicy>::Lookup(void* key, uint32_t hash) const {
123 Entry* p = Probe(key, hash);
124 return p->key != NULL ? p : NULL;
125}
126
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000127template <class AllocationPolicy>
128typename TemplateHashMapImpl<AllocationPolicy>::Entry*
129TemplateHashMapImpl<AllocationPolicy>::LookupOrInsert(
130 void* key, uint32_t hash, AllocationPolicy allocator) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100131 // Find a matching entry.
132 Entry* p = Probe(key, hash);
133 if (p->key != NULL) {
134 return p;
135 }
136
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000137 // No entry found; insert one.
138 p->key = key;
139 p->value = NULL;
140 p->hash = hash;
141 p->order = occupancy_;
142 occupancy_++;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100143
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000144 // Grow the map if we reached >= 80% occupancy.
145 if (occupancy_ + occupancy_ / 4 >= capacity_) {
146 Resize(allocator);
147 p = Probe(key, hash);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100148 }
149
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000150 return p;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100151}
152
Ben Murdoch61f157c2016-09-16 13:49:30 +0100153template <class AllocationPolicy>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000154void* TemplateHashMapImpl<AllocationPolicy>::Remove(void* key, uint32_t hash) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100155 // Lookup the entry for the key to remove.
156 Entry* p = Probe(key, hash);
157 if (p->key == NULL) {
158 // Key not found nothing to remove.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159 return NULL;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100160 }
161
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162 void* value = p->value;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100163 // To remove an entry we need to ensure that it does not create an empty
164 // entry that will cause the search for another entry to stop too soon. If all
165 // the entries between the entry to remove and the next empty slot have their
166 // initial position inside this interval, clearing the entry to remove will
167 // not break the search. If, while searching for the next empty entry, an
168 // entry is encountered which does not have its initial position between the
169 // entry to remove and the position looked at, then this entry can be moved to
170 // the place of the entry to remove without breaking the search for it. The
171 // entry made vacant by this move is now the entry to remove and the process
172 // starts over.
173 // Algorithm from http://en.wikipedia.org/wiki/Open_addressing.
174
175 // This guarantees loop termination as there is at least one empty entry so
176 // eventually the removed entry will have an empty entry after it.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000177 DCHECK(occupancy_ < capacity_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100178
179 // p is the candidate entry to clear. q is used to scan forwards.
180 Entry* q = p; // Start at the entry to remove.
181 while (true) {
182 // Move q to the next entry.
183 q = q + 1;
184 if (q == map_end()) {
185 q = map_;
186 }
187
188 // All entries between p and q have their initial position between p and q
189 // and the entry p can be cleared without breaking the search for these
190 // entries.
191 if (q->key == NULL) {
192 break;
193 }
194
195 // Find the initial position for the entry at position q.
196 Entry* r = map_ + (q->hash & (capacity_ - 1));
197
198 // If the entry at position q has its initial position outside the range
199 // between p and q it can be moved forward to position p and will still be
200 // found. There is now a new candidate entry for clearing.
Ben Murdoch61f157c2016-09-16 13:49:30 +0100201 if ((q > p && (r <= p || r > q)) || (q < p && (r <= p && r > q))) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100202 *p = *q;
203 p = q;
204 }
205 }
206
207 // Clear the entry which is allowed to en emptied.
208 p->key = NULL;
209 occupancy_--;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 return value;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100211}
212
Ben Murdoch61f157c2016-09-16 13:49:30 +0100213template <class AllocationPolicy>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214void TemplateHashMapImpl<AllocationPolicy>::Clear() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100215 // Mark all entries as empty.
216 const Entry* end = map_end();
217 for (Entry* p = map_; p < end; p++) {
218 p->key = NULL;
219 }
220 occupancy_ = 0;
221}
222
Ben Murdoch61f157c2016-09-16 13:49:30 +0100223template <class AllocationPolicy>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000224typename TemplateHashMapImpl<AllocationPolicy>::Entry*
Ben Murdoch61f157c2016-09-16 13:49:30 +0100225TemplateHashMapImpl<AllocationPolicy>::Start() const {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100226 return Next(map_ - 1);
227}
228
Ben Murdoch61f157c2016-09-16 13:49:30 +0100229template <class AllocationPolicy>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230typename TemplateHashMapImpl<AllocationPolicy>::Entry*
Ben Murdoch61f157c2016-09-16 13:49:30 +0100231TemplateHashMapImpl<AllocationPolicy>::Next(Entry* p) const {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100232 const Entry* end = map_end();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 DCHECK(map_ - 1 <= p && p < end);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100234 for (p++; p < end; p++) {
235 if (p->key != NULL) {
236 return p;
237 }
238 }
239 return NULL;
240}
241
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000242template <class AllocationPolicy>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000243typename TemplateHashMapImpl<AllocationPolicy>::Entry*
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000244TemplateHashMapImpl<AllocationPolicy>::Probe(void* key, uint32_t hash) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000245 DCHECK(key != NULL);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100246
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000247 DCHECK(base::bits::IsPowerOfTwo32(capacity_));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100248 Entry* p = map_ + (hash & (capacity_ - 1));
249 const Entry* end = map_end();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000250 DCHECK(map_ <= p && p < end);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100251
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000252 DCHECK(occupancy_ < capacity_); // Guarantees loop termination.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100253 while (p->key != NULL && (hash != p->hash || !match_(key, p->key))) {
254 p++;
255 if (p >= end) {
256 p = map_;
257 }
258 }
259
260 return p;
261}
262
Ben Murdoch61f157c2016-09-16 13:49:30 +0100263template <class AllocationPolicy>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000264void TemplateHashMapImpl<AllocationPolicy>::Initialize(
265 uint32_t capacity, AllocationPolicy allocator) {
266 DCHECK(base::bits::IsPowerOfTwo32(capacity));
267 map_ = reinterpret_cast<Entry*>(allocator.New(capacity * sizeof(Entry)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100268 if (map_ == NULL) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100269 FATAL("Out of memory: HashMap::Initialize");
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100270 return;
271 }
272 capacity_ = capacity;
273 Clear();
274}
275
Ben Murdoch61f157c2016-09-16 13:49:30 +0100276template <class AllocationPolicy>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277void TemplateHashMapImpl<AllocationPolicy>::Resize(AllocationPolicy allocator) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100278 Entry* map = map_;
279 uint32_t n = occupancy_;
280
281 // Allocate larger map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000282 Initialize(capacity_ * 2, allocator);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100283
284 // Rehash all current entries.
285 for (Entry* p = map; n > 0; p++) {
286 if (p->key != NULL) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000287 Entry* entry = LookupOrInsert(p->key, p->hash, allocator);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000288 entry->value = p->value;
289 entry->order = p->order;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100290 n--;
291 }
292 }
293
294 // Delete old map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000295 AllocationPolicy::Delete(map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100296}
297
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100298// A hash map for pointer keys and values with an STL-like interface.
Ben Murdoch61f157c2016-09-16 13:49:30 +0100299template <class Key, class Value, class AllocationPolicy>
300class TemplateHashMap : private TemplateHashMapImpl<AllocationPolicy> {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100301 public:
Ben Murdoch61f157c2016-09-16 13:49:30 +0100302 STATIC_ASSERT(sizeof(Key*) == sizeof(void*)); // NOLINT
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100303 STATIC_ASSERT(sizeof(Value*) == sizeof(void*)); // NOLINT
304 struct value_type {
305 Key* first;
306 Value* second;
307 };
308
309 class Iterator {
310 public:
311 Iterator& operator++() {
312 entry_ = map_->Next(entry_);
313 return *this;
314 }
315
316 value_type* operator->() { return reinterpret_cast<value_type*>(entry_); }
Ben Murdoch61f157c2016-09-16 13:49:30 +0100317 bool operator!=(const Iterator& other) { return entry_ != other.entry_; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100318
319 private:
320 Iterator(const TemplateHashMapImpl<AllocationPolicy>* map,
Ben Murdoch61f157c2016-09-16 13:49:30 +0100321 typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry)
322 : map_(map), entry_(entry) {}
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100323
324 const TemplateHashMapImpl<AllocationPolicy>* map_;
325 typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry_;
326
327 friend class TemplateHashMap;
328 };
329
330 TemplateHashMap(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000331 typename TemplateHashMapImpl<AllocationPolicy>::MatchFun match,
332 AllocationPolicy allocator = AllocationPolicy())
Ben Murdoch61f157c2016-09-16 13:49:30 +0100333 : TemplateHashMapImpl<AllocationPolicy>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000334 match,
335 TemplateHashMapImpl<AllocationPolicy>::kDefaultHashMapCapacity,
Ben Murdoch61f157c2016-09-16 13:49:30 +0100336 allocator) {}
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100337
338 Iterator begin() const { return Iterator(this, this->Start()); }
339 Iterator end() const { return Iterator(this, NULL); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000340 Iterator find(Key* key, bool insert = false,
341 AllocationPolicy allocator = AllocationPolicy()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000342 if (insert) {
343 return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator));
344 }
345 return Iterator(this, this->Lookup(key, key->Hash()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100346 }
347};
Steve Blocka7e24c12009-10-30 11:49:00 +0000348
Ben Murdoch61f157c2016-09-16 13:49:30 +0100349} // namespace base
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000350} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000351
Ben Murdoch61f157c2016-09-16 13:49:30 +0100352#endif // V8_BASE_HASHMAP_H_