Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: src/hashmap.h

Issue 10448007: Split an allocation policy into an allocator and a deallocator. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Make TemplateHashMapImpl consistent with the rest of the approach. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/full-codegen.h ('k') | src/isolate.h » ('j') | src/list.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 20 matching lines...) Expand all
31 #include "allocation.h" 31 #include "allocation.h"
32 #include "checks.h" 32 #include "checks.h"
33 #include "utils.h" 33 #include "utils.h"
34 34
35 namespace v8 { 35 namespace v8 {
36 namespace internal { 36 namespace internal {
37 37
38 template<class AllocationPolicy> 38 template<class AllocationPolicy>
39 class TemplateHashMapImpl { 39 class TemplateHashMapImpl {
40 public: 40 public:
41 typedef typename AllocationPolicy::Alloc Allocator;
42 typedef typename AllocationPolicy::Dealloc Deallocator;
43
41 typedef bool (*MatchFun) (void* key1, void* key2); 44 typedef bool (*MatchFun) (void* key1, void* key2);
42 45
43 // initial_capacity is the size of the initial hash map; 46 // initial_capacity is the size of the initial hash map;
44 // it must be a power of 2 (and thus must not be 0). 47 // it must be a power of 2 (and thus must not be 0).
45 TemplateHashMapImpl(MatchFun match, uint32_t initial_capacity = 8); 48 TemplateHashMapImpl(MatchFun match,
49 Allocator allocator = Allocator(),
50 Deallocator deallocator = Deallocator(),
51 uint32_t initial_capacity = 8);
46 52
47 ~TemplateHashMapImpl(); 53 ~TemplateHashMapImpl();
48 54
49 // HashMap entries are (key, value, hash) triplets. 55 // HashMap entries are (key, value, hash) triplets.
50 // Some clients may not need to use the value slot 56 // Some clients may not need to use the value slot
51 // (e.g. implementers of sets, where the key is the value). 57 // (e.g. implementers of sets, where the key is the value).
52 struct Entry { 58 struct Entry {
53 void* key; 59 void* key;
54 void* value; 60 void* value;
55 uint32_t hash; // the full hash value for key 61 uint32_t hash; // the full hash value for key
56 }; 62 };
57 63
58 // If an entry with matching key is found, Lookup() 64 // If an entry with matching key is found, Lookup()
59 // returns that entry. If no matching entry is found, 65 // returns that entry. If no matching entry is found,
60 // but insert is set, a new entry is inserted with 66 // but insert is set, a new entry is inserted with
61 // corresponding key, key hash, and NULL value. 67 // corresponding key, key hash, and NULL value.
62 // Otherwise, NULL is returned. 68 // Otherwise, NULL is returned.
63 Entry* Lookup(void* key, uint32_t hash, bool insert); 69 Entry* Lookup(void* key, uint32_t hash, bool insert,
70 Allocator allocator = Allocator());
64 71
65 // Removes the entry with matching key. 72 // Removes the entry with matching key.
66 // It returns the value of the deleted entry 73 // It returns the value of the deleted entry
67 // or null if there is no value for such key. 74 // or null if there is no value for such key.
68 void* Remove(void* key, uint32_t hash); 75 void* Remove(void* key, uint32_t hash);
69 76
70 // Empties the hash map (occupancy() == 0). 77 // Empties the hash map (occupancy() == 0).
71 void Clear(); 78 void Clear();
72 79
73 // The number of (non-empty) entries in the table. 80 // The number of (non-empty) entries in the table.
(...skipping 13 matching lines...) Expand all
87 // If entries are inserted during iteration, the effect of 94 // If entries are inserted during iteration, the effect of
88 // calling Next() is undefined. 95 // calling Next() is undefined.
89 Entry* Start() const; 96 Entry* Start() const;
90 Entry* Next(Entry* p) const; 97 Entry* Next(Entry* p) const;
91 98
92 private: 99 private:
93 MatchFun match_; 100 MatchFun match_;
94 Entry* map_; 101 Entry* map_;
95 uint32_t capacity_; 102 uint32_t capacity_;
96 uint32_t occupancy_; 103 uint32_t occupancy_;
104 Deallocator deallocator_;
97 105
98 Entry* map_end() const { return map_ + capacity_; } 106 Entry* map_end() const { return map_ + capacity_; }
99 Entry* Probe(void* key, uint32_t hash); 107 Entry* Probe(void* key, uint32_t hash);
100 void Initialize(uint32_t capacity); 108 void Initialize(uint32_t capacity, Allocator allocator);
101 void Resize(); 109 void Resize(Allocator allocator);
102 }; 110 };
103 111
104 typedef TemplateHashMapImpl<FreeStoreAllocationPolicy> HashMap; 112 typedef TemplateHashMapImpl<FreeStoreAllocationPolicy> HashMap;
105 113
106 template<class P> 114 template<class AllocationPolicy>
107 TemplateHashMapImpl<P>::TemplateHashMapImpl(MatchFun match, 115 TemplateHashMapImpl<AllocationPolicy>::TemplateHashMapImpl(
108 uint32_t initial_capacity) { 116 MatchFun match, Allocator allocator, Deallocator deallocator,
117 uint32_t initial_capacity) : deallocator_(deallocator) {
109 match_ = match; 118 match_ = match;
110 Initialize(initial_capacity); 119 Initialize(initial_capacity, allocator);
111 } 120 }
112 121
113 122
114 template<class P> 123 template<class AllocationPolicy>
115 TemplateHashMapImpl<P>::~TemplateHashMapImpl() { 124 TemplateHashMapImpl<AllocationPolicy>::~TemplateHashMapImpl() {
116 P::Delete(map_); 125 deallocator_.Delete(map_);
117 } 126 }
118 127
119 128
120 template<class P> 129 template<class AllocationPolicy>
121 typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Lookup( 130 typename TemplateHashMapImpl<AllocationPolicy>::Entry*
122 void* key, uint32_t hash, bool insert) { 131 TemplateHashMapImpl<AllocationPolicy>::Lookup(
132 void* key, uint32_t hash, bool insert, Allocator allocator) {
123 // Find a matching entry. 133 // Find a matching entry.
124 Entry* p = Probe(key, hash); 134 Entry* p = Probe(key, hash);
125 if (p->key != NULL) { 135 if (p->key != NULL) {
126 return p; 136 return p;
127 } 137 }
128 138
129 // No entry found; insert one if necessary. 139 // No entry found; insert one if necessary.
130 if (insert) { 140 if (insert) {
131 p->key = key; 141 p->key = key;
132 p->value = NULL; 142 p->value = NULL;
133 p->hash = hash; 143 p->hash = hash;
134 occupancy_++; 144 occupancy_++;
135 145
136 // Grow the map if we reached >= 80% occupancy. 146 // Grow the map if we reached >= 80% occupancy.
137 if (occupancy_ + occupancy_/4 >= capacity_) { 147 if (occupancy_ + occupancy_/4 >= capacity_) {
138 Resize(); 148 Resize(allocator);
139 p = Probe(key, hash); 149 p = Probe(key, hash);
140 } 150 }
141 151
142 return p; 152 return p;
143 } 153 }
144 154
145 // No entry found and none inserted. 155 // No entry found and none inserted.
146 return NULL; 156 return NULL;
147 } 157 }
148 158
149 159
150 template<class P> 160 template<class AllocationPolicy>
151 void* TemplateHashMapImpl<P>::Remove(void* key, uint32_t hash) { 161 void* TemplateHashMapImpl<AllocationPolicy>::Remove(void* key, uint32_t hash) {
152 // Lookup the entry for the key to remove. 162 // Lookup the entry for the key to remove.
153 Entry* p = Probe(key, hash); 163 Entry* p = Probe(key, hash);
154 if (p->key == NULL) { 164 if (p->key == NULL) {
155 // Key not found nothing to remove. 165 // Key not found nothing to remove.
156 return NULL; 166 return NULL;
157 } 167 }
158 168
159 void* value = p->value; 169 void* value = p->value;
160 // To remove an entry we need to ensure that it does not create an empty 170 // To remove an entry we need to ensure that it does not create an empty
161 // entry that will cause the search for another entry to stop too soon. If all 171 // entry that will cause the search for another entry to stop too soon. If all
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 } 212 }
203 } 213 }
204 214
205 // Clear the entry which is allowed to en emptied. 215 // Clear the entry which is allowed to en emptied.
206 p->key = NULL; 216 p->key = NULL;
207 occupancy_--; 217 occupancy_--;
208 return value; 218 return value;
209 } 219 }
210 220
211 221
212 template<class P> 222 template<class AllocationPolicy>
213 void TemplateHashMapImpl<P>::Clear() { 223 void TemplateHashMapImpl<AllocationPolicy>::Clear() {
214 // Mark all entries as empty. 224 // Mark all entries as empty.
215 const Entry* end = map_end(); 225 const Entry* end = map_end();
216 for (Entry* p = map_; p < end; p++) { 226 for (Entry* p = map_; p < end; p++) {
217 p->key = NULL; 227 p->key = NULL;
218 } 228 }
219 occupancy_ = 0; 229 occupancy_ = 0;
220 } 230 }
221 231
222 232
223 template<class P> 233 template<class AllocationPolicy>
224 typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Start() const { 234 typename TemplateHashMapImpl<AllocationPolicy>::Entry*
235 TemplateHashMapImpl<AllocationPolicy>::Start() const {
225 return Next(map_ - 1); 236 return Next(map_ - 1);
226 } 237 }
227 238
228 239
229 template<class P> 240 template<class AllocationPolicy>
230 typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Next(Entry* p) 241 typename TemplateHashMapImpl<AllocationPolicy>::Entry*
231 const { 242 TemplateHashMapImpl<AllocationPolicy>::Next(Entry* p) const {
232 const Entry* end = map_end(); 243 const Entry* end = map_end();
233 ASSERT(map_ - 1 <= p && p < end); 244 ASSERT(map_ - 1 <= p && p < end);
234 for (p++; p < end; p++) { 245 for (p++; p < end; p++) {
235 if (p->key != NULL) { 246 if (p->key != NULL) {
236 return p; 247 return p;
237 } 248 }
238 } 249 }
239 return NULL; 250 return NULL;
240 } 251 }
241 252
242 253
243 template<class P> 254 template<class AllocationPolicy>
244 typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Probe(void* key, 255 typename TemplateHashMapImpl<AllocationPolicy>::Entry*
245 uint32_t hash) { 256 TemplateHashMapImpl<AllocationPolicy>::Probe(void* key, uint32_t hash) {
246 ASSERT(key != NULL); 257 ASSERT(key != NULL);
247 258
248 ASSERT(IsPowerOf2(capacity_)); 259 ASSERT(IsPowerOf2(capacity_));
249 Entry* p = map_ + (hash & (capacity_ - 1)); 260 Entry* p = map_ + (hash & (capacity_ - 1));
250 const Entry* end = map_end(); 261 const Entry* end = map_end();
251 ASSERT(map_ <= p && p < end); 262 ASSERT(map_ <= p && p < end);
252 263
253 ASSERT(occupancy_ < capacity_); // Guarantees loop termination. 264 ASSERT(occupancy_ < capacity_); // Guarantees loop termination.
254 while (p->key != NULL && (hash != p->hash || !match_(key, p->key))) { 265 while (p->key != NULL && (hash != p->hash || !match_(key, p->key))) {
255 p++; 266 p++;
256 if (p >= end) { 267 if (p >= end) {
257 p = map_; 268 p = map_;
258 } 269 }
259 } 270 }
260 271
261 return p; 272 return p;
262 } 273 }
263 274
264 275
265 template<class P> 276 template<class AllocationPolicy>
266 void TemplateHashMapImpl<P>::Initialize(uint32_t capacity) { 277 void TemplateHashMapImpl<AllocationPolicy>::Initialize(uint32_t capacity,
278 Allocator allocator) {
267 ASSERT(IsPowerOf2(capacity)); 279 ASSERT(IsPowerOf2(capacity));
268 map_ = reinterpret_cast<Entry*>(P::New(capacity * sizeof(Entry))); 280 map_ = reinterpret_cast<Entry*>(allocator.New(capacity * sizeof(Entry)));
269 if (map_ == NULL) { 281 if (map_ == NULL) {
270 v8::internal::FatalProcessOutOfMemory("HashMap::Initialize"); 282 v8::internal::FatalProcessOutOfMemory("HashMap::Initialize");
271 return; 283 return;
272 } 284 }
273 capacity_ = capacity; 285 capacity_ = capacity;
274 Clear(); 286 Clear();
275 } 287 }
276 288
277 289
278 template<class P> 290 template<class AllocationPolicy>
279 void TemplateHashMapImpl<P>::Resize() { 291 void TemplateHashMapImpl<AllocationPolicy>::Resize(Allocator allocator) {
280 Entry* map = map_; 292 Entry* map = map_;
281 uint32_t n = occupancy_; 293 uint32_t n = occupancy_;
282 294
283 // Allocate larger map. 295 // Allocate larger map.
284 Initialize(capacity_ * 2); 296 Initialize(capacity_ * 2, allocator);
285 297
286 // Rehash all current entries. 298 // Rehash all current entries.
287 for (Entry* p = map; n > 0; p++) { 299 for (Entry* p = map; n > 0; p++) {
288 if (p->key != NULL) { 300 if (p->key != NULL) {
289 Lookup(p->key, p->hash, true)->value = p->value; 301 Lookup(p->key, p->hash, true)->value = p->value;
290 n--; 302 n--;
291 } 303 }
292 } 304 }
293 305
294 // Delete old map. 306 // Delete old map.
295 P::Delete(map); 307 deallocator_.Delete(map);
296 } 308 }
297 309
298 310
299 // A hash map for pointer keys and values with an STL-like interface. 311 // A hash map for pointer keys and values with an STL-like interface.
300 template<class Key, class Value, class AllocationPolicy> 312 template<class Key, class Value, class AllocationPolicy>
301 class TemplateHashMap: private TemplateHashMapImpl<AllocationPolicy> { 313 class TemplateHashMap: private TemplateHashMapImpl<AllocationPolicy> {
302 public: 314 public:
303 STATIC_ASSERT(sizeof(Key*) == sizeof(void*)); // NOLINT 315 STATIC_ASSERT(sizeof(Key*) == sizeof(void*)); // NOLINT
304 STATIC_ASSERT(sizeof(Value*) == sizeof(void*)); // NOLINT 316 STATIC_ASSERT(sizeof(Value*) == sizeof(void*)); // NOLINT
317 typedef typename AllocationPolicy::Alloc Allocator;
318 typedef typename AllocationPolicy::Dealloc Deallocator;
319
305 struct value_type { 320 struct value_type {
306 Key* first; 321 Key* first;
307 Value* second; 322 Value* second;
308 }; 323 };
309 324
310 class Iterator { 325 class Iterator {
311 public: 326 public:
312 Iterator& operator++() { 327 Iterator& operator++() {
313 entry_ = map_->Next(entry_); 328 entry_ = map_->Next(entry_);
314 return *this; 329 return *this;
315 } 330 }
316 331
317 value_type* operator->() { return reinterpret_cast<value_type*>(entry_); } 332 value_type* operator->() { return reinterpret_cast<value_type*>(entry_); }
318 bool operator!=(const Iterator& other) { return entry_ != other.entry_; } 333 bool operator!=(const Iterator& other) { return entry_ != other.entry_; }
319 334
320 private: 335 private:
321 Iterator(const TemplateHashMapImpl<AllocationPolicy>* map, 336 Iterator(const TemplateHashMapImpl<AllocationPolicy>* map,
322 typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry) : 337 typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry) :
323 map_(map), entry_(entry) { } 338 map_(map), entry_(entry) { }
324 339
325 const TemplateHashMapImpl<AllocationPolicy>* map_; 340 const TemplateHashMapImpl<AllocationPolicy>* map_;
326 typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry_; 341 typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry_;
327 342
328 friend class TemplateHashMap; 343 friend class TemplateHashMap;
329 }; 344 };
330 345
331 TemplateHashMap( 346 TemplateHashMap(
332 typename TemplateHashMapImpl<AllocationPolicy>::MatchFun match) 347 typename TemplateHashMapImpl<AllocationPolicy>::MatchFun match,
333 : TemplateHashMapImpl<AllocationPolicy>(match) { } 348 Allocator allocator = Allocator(),
349 Deallocator deallocator = Deallocator())
350 : TemplateHashMapImpl<AllocationPolicy>(match, allocator, deallocator) { }
334 351
335 Iterator begin() const { return Iterator(this, this->Start()); } 352 Iterator begin() const { return Iterator(this, this->Start()); }
336 Iterator end() const { return Iterator(this, NULL); } 353 Iterator end() const { return Iterator(this, NULL); }
337 Iterator find(Key* key, bool insert = false) { 354 Iterator find(Key* key, bool insert = false) {
338 return Iterator(this, this->Lookup(key, key->Hash(), insert)); 355 return Iterator(this, this->Lookup(key, key->Hash(), insert));
339 } 356 }
340 }; 357 };
341 358
342 } } // namespace v8::internal 359 } } // namespace v8::internal
343 360
344 #endif // V8_HASHMAP_H_ 361 #endif // V8_HASHMAP_H_
OLDNEW
« no previous file with comments | « src/full-codegen.h ('k') | src/isolate.h » ('j') | src/list.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698