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

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: Fixed the issues pointed out. Created 8 years, 6 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
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::Allocator Allocator;
42 typedef typename AllocationPolicy::Deallocator 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, uint32_t capacity = 8,
49 Allocator allocator = Allocator(),
50 Deallocator deallocator = Deallocator());
46 51
47 ~TemplateHashMapImpl(); 52 ~TemplateHashMapImpl();
48 53
49 // HashMap entries are (key, value, hash) triplets. 54 // HashMap entries are (key, value, hash) triplets.
50 // Some clients may not need to use the value slot 55 // Some clients may not need to use the value slot
51 // (e.g. implementers of sets, where the key is the value). 56 // (e.g. implementers of sets, where the key is the value).
52 struct Entry { 57 struct Entry {
53 void* key; 58 void* key;
54 void* value; 59 void* value;
55 uint32_t hash; // the full hash value for key 60 uint32_t hash; // the full hash value for key
56 }; 61 };
57 62
58 // If an entry with matching key is found, Lookup() 63 // If an entry with matching key is found, Lookup()
59 // returns that entry. If no matching entry is found, 64 // returns that entry. If no matching entry is found,
60 // but insert is set, a new entry is inserted with 65 // but insert is set, a new entry is inserted with
61 // corresponding key, key hash, and NULL value. 66 // corresponding key, key hash, and NULL value.
62 // Otherwise, NULL is returned. 67 // Otherwise, NULL is returned.
63 Entry* Lookup(void* key, uint32_t hash, bool insert); 68 Entry* Lookup(void* key, uint32_t hash, bool insert,
69 Allocator allocator = Allocator());
64 70
65 // Removes the entry with matching key. 71 // Removes the entry with matching key.
66 // It returns the value of the deleted entry 72 // It returns the value of the deleted entry
67 // or null if there is no value for such key. 73 // or null if there is no value for such key.
68 void* Remove(void* key, uint32_t hash); 74 void* Remove(void* key, uint32_t hash);
69 75
70 // Empties the hash map (occupancy() == 0). 76 // Empties the hash map (occupancy() == 0).
71 void Clear(); 77 void Clear();
72 78
73 // The number of (non-empty) entries in the table. 79 // 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 93 // If entries are inserted during iteration, the effect of
88 // calling Next() is undefined. 94 // calling Next() is undefined.
89 Entry* Start() const; 95 Entry* Start() const;
90 Entry* Next(Entry* p) const; 96 Entry* Next(Entry* p) const;
91 97
92 private: 98 private:
93 MatchFun match_; 99 MatchFun match_;
94 Entry* map_; 100 Entry* map_;
95 uint32_t capacity_; 101 uint32_t capacity_;
96 uint32_t occupancy_; 102 uint32_t occupancy_;
103 Deallocator deallocator_;
97 104
98 Entry* map_end() const { return map_ + capacity_; } 105 Entry* map_end() const { return map_ + capacity_; }
99 Entry* Probe(void* key, uint32_t hash); 106 Entry* Probe(void* key, uint32_t hash);
100 void Initialize(uint32_t capacity); 107 void Initialize(uint32_t capacity, Allocator allocator);
101 void Resize(); 108 void Resize(Allocator allocator);
102 }; 109 };
103 110
104 typedef TemplateHashMapImpl<FreeStoreAllocationPolicy> HashMap; 111 typedef TemplateHashMapImpl<FreeStoreAllocationPolicy> HashMap;
105 112
106 template<class P> 113 template<class AllocationPolicy>
107 TemplateHashMapImpl<P>::TemplateHashMapImpl(MatchFun match, 114 TemplateHashMapImpl<AllocationPolicy>::TemplateHashMapImpl(
108 uint32_t initial_capacity) { 115 MatchFun match, uint32_t initial_capacity, Allocator allocator,
116 Deallocator deallocator) : deallocator_(deallocator) {
109 match_ = match; 117 match_ = match;
110 Initialize(initial_capacity); 118 Initialize(initial_capacity, allocator);
111 } 119 }
112 120
113 121
114 template<class P> 122 template<class AllocationPolicy>
115 TemplateHashMapImpl<P>::~TemplateHashMapImpl() { 123 TemplateHashMapImpl<AllocationPolicy>::~TemplateHashMapImpl() {
116 P::Delete(map_); 124 deallocator_.Delete(map_);
117 } 125 }
118 126
119 127
120 template<class P> 128 template<class AllocationPolicy>
121 typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Lookup( 129 typename TemplateHashMapImpl<AllocationPolicy>::Entry*
122 void* key, uint32_t hash, bool insert) { 130 TemplateHashMapImpl<AllocationPolicy>::Lookup(
131 void* key, uint32_t hash, bool insert, Allocator allocator) {
123 // Find a matching entry. 132 // Find a matching entry.
124 Entry* p = Probe(key, hash); 133 Entry* p = Probe(key, hash);
125 if (p->key != NULL) { 134 if (p->key != NULL) {
126 return p; 135 return p;
127 } 136 }
128 137
129 // No entry found; insert one if necessary. 138 // No entry found; insert one if necessary.
130 if (insert) { 139 if (insert) {
131 p->key = key; 140 p->key = key;
132 p->value = NULL; 141 p->value = NULL;
133 p->hash = hash; 142 p->hash = hash;
134 occupancy_++; 143 occupancy_++;
135 144
136 // Grow the map if we reached >= 80% occupancy. 145 // Grow the map if we reached >= 80% occupancy.
137 if (occupancy_ + occupancy_/4 >= capacity_) { 146 if (occupancy_ + occupancy_/4 >= capacity_) {
138 Resize(); 147 Resize(allocator);
139 p = Probe(key, hash); 148 p = Probe(key, hash);
140 } 149 }
141 150
142 return p; 151 return p;
143 } 152 }
144 153
145 // No entry found and none inserted. 154 // No entry found and none inserted.
146 return NULL; 155 return NULL;
147 } 156 }
148 157
149 158
150 template<class P> 159 template<class AllocationPolicy>
151 void* TemplateHashMapImpl<P>::Remove(void* key, uint32_t hash) { 160 void* TemplateHashMapImpl<AllocationPolicy>::Remove(void* key, uint32_t hash) {
152 // Lookup the entry for the key to remove. 161 // Lookup the entry for the key to remove.
153 Entry* p = Probe(key, hash); 162 Entry* p = Probe(key, hash);
154 if (p->key == NULL) { 163 if (p->key == NULL) {
155 // Key not found nothing to remove. 164 // Key not found nothing to remove.
156 return NULL; 165 return NULL;
157 } 166 }
158 167
159 void* value = p->value; 168 void* value = p->value;
160 // To remove an entry we need to ensure that it does not create an empty 169 // 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 170 // 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 } 211 }
203 } 212 }
204 213
205 // Clear the entry which is allowed to en emptied. 214 // Clear the entry which is allowed to en emptied.
206 p->key = NULL; 215 p->key = NULL;
207 occupancy_--; 216 occupancy_--;
208 return value; 217 return value;
209 } 218 }
210 219
211 220
212 template<class P> 221 template<class AllocationPolicy>
213 void TemplateHashMapImpl<P>::Clear() { 222 void TemplateHashMapImpl<AllocationPolicy>::Clear() {
214 // Mark all entries as empty. 223 // Mark all entries as empty.
215 const Entry* end = map_end(); 224 const Entry* end = map_end();
216 for (Entry* p = map_; p < end; p++) { 225 for (Entry* p = map_; p < end; p++) {
217 p->key = NULL; 226 p->key = NULL;
218 } 227 }
219 occupancy_ = 0; 228 occupancy_ = 0;
220 } 229 }
221 230
222 231
223 template<class P> 232 template<class AllocationPolicy>
224 typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Start() const { 233 typename TemplateHashMapImpl<AllocationPolicy>::Entry*
234 TemplateHashMapImpl<AllocationPolicy>::Start() const {
225 return Next(map_ - 1); 235 return Next(map_ - 1);
226 } 236 }
227 237
228 238
229 template<class P> 239 template<class AllocationPolicy>
230 typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Next(Entry* p) 240 typename TemplateHashMapImpl<AllocationPolicy>::Entry*
231 const { 241 TemplateHashMapImpl<AllocationPolicy>::Next(Entry* p) const {
232 const Entry* end = map_end(); 242 const Entry* end = map_end();
233 ASSERT(map_ - 1 <= p && p < end); 243 ASSERT(map_ - 1 <= p && p < end);
234 for (p++; p < end; p++) { 244 for (p++; p < end; p++) {
235 if (p->key != NULL) { 245 if (p->key != NULL) {
236 return p; 246 return p;
237 } 247 }
238 } 248 }
239 return NULL; 249 return NULL;
240 } 250 }
241 251
242 252
243 template<class P> 253 template<class AllocationPolicy>
244 typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Probe(void* key, 254 typename TemplateHashMapImpl<AllocationPolicy>::Entry*
245 uint32_t hash) { 255 TemplateHashMapImpl<AllocationPolicy>::Probe(void* key, uint32_t hash) {
246 ASSERT(key != NULL); 256 ASSERT(key != NULL);
247 257
248 ASSERT(IsPowerOf2(capacity_)); 258 ASSERT(IsPowerOf2(capacity_));
249 Entry* p = map_ + (hash & (capacity_ - 1)); 259 Entry* p = map_ + (hash & (capacity_ - 1));
250 const Entry* end = map_end(); 260 const Entry* end = map_end();
251 ASSERT(map_ <= p && p < end); 261 ASSERT(map_ <= p && p < end);
252 262
253 ASSERT(occupancy_ < capacity_); // Guarantees loop termination. 263 ASSERT(occupancy_ < capacity_); // Guarantees loop termination.
254 while (p->key != NULL && (hash != p->hash || !match_(key, p->key))) { 264 while (p->key != NULL && (hash != p->hash || !match_(key, p->key))) {
255 p++; 265 p++;
256 if (p >= end) { 266 if (p >= end) {
257 p = map_; 267 p = map_;
258 } 268 }
259 } 269 }
260 270
261 return p; 271 return p;
262 } 272 }
263 273
264 274
265 template<class P> 275 template<class AllocationPolicy>
266 void TemplateHashMapImpl<P>::Initialize(uint32_t capacity) { 276 void TemplateHashMapImpl<AllocationPolicy>::Initialize(uint32_t capacity,
277 Allocator allocator) {
267 ASSERT(IsPowerOf2(capacity)); 278 ASSERT(IsPowerOf2(capacity));
268 map_ = reinterpret_cast<Entry*>(P::New(capacity * sizeof(Entry))); 279 map_ = reinterpret_cast<Entry*>(allocator.New(capacity * sizeof(Entry)));
269 if (map_ == NULL) { 280 if (map_ == NULL) {
270 v8::internal::FatalProcessOutOfMemory("HashMap::Initialize"); 281 v8::internal::FatalProcessOutOfMemory("HashMap::Initialize");
271 return; 282 return;
272 } 283 }
273 capacity_ = capacity; 284 capacity_ = capacity;
274 Clear(); 285 Clear();
275 } 286 }
276 287
277 288
278 template<class P> 289 template<class AllocationPolicy>
279 void TemplateHashMapImpl<P>::Resize() { 290 void TemplateHashMapImpl<AllocationPolicy>::Resize(Allocator allocator) {
280 Entry* map = map_; 291 Entry* map = map_;
281 uint32_t n = occupancy_; 292 uint32_t n = occupancy_;
282 293
283 // Allocate larger map. 294 // Allocate larger map.
284 Initialize(capacity_ * 2); 295 Initialize(capacity_ * 2, allocator);
285 296
286 // Rehash all current entries. 297 // Rehash all current entries.
287 for (Entry* p = map; n > 0; p++) { 298 for (Entry* p = map; n > 0; p++) {
288 if (p->key != NULL) { 299 if (p->key != NULL) {
289 Lookup(p->key, p->hash, true)->value = p->value; 300 Lookup(p->key, p->hash, true)->value = p->value;
290 n--; 301 n--;
291 } 302 }
292 } 303 }
293 304
294 // Delete old map. 305 // Delete old map.
295 P::Delete(map); 306 deallocator_.Delete(map);
296 } 307 }
297 308
298 309
299 // A hash map for pointer keys and values with an STL-like interface. 310 // A hash map for pointer keys and values with an STL-like interface.
300 template<class Key, class Value, class AllocationPolicy> 311 template<class Key, class Value, class AllocationPolicy>
301 class TemplateHashMap: private TemplateHashMapImpl<AllocationPolicy> { 312 class TemplateHashMap: private TemplateHashMapImpl<AllocationPolicy> {
302 public: 313 public:
303 STATIC_ASSERT(sizeof(Key*) == sizeof(void*)); // NOLINT 314 STATIC_ASSERT(sizeof(Key*) == sizeof(void*)); // NOLINT
304 STATIC_ASSERT(sizeof(Value*) == sizeof(void*)); // NOLINT 315 STATIC_ASSERT(sizeof(Value*) == sizeof(void*)); // NOLINT
316 typedef typename AllocationPolicy::Allocator Allocator;
317 typedef typename AllocationPolicy::Deallocator Deallocator;
318
305 struct value_type { 319 struct value_type {
306 Key* first; 320 Key* first;
307 Value* second; 321 Value* second;
308 }; 322 };
309 323
310 class Iterator { 324 class Iterator {
311 public: 325 public:
312 Iterator& operator++() { 326 Iterator& operator++() {
313 entry_ = map_->Next(entry_); 327 entry_ = map_->Next(entry_);
314 return *this; 328 return *this;
315 } 329 }
316 330
317 value_type* operator->() { return reinterpret_cast<value_type*>(entry_); } 331 value_type* operator->() { return reinterpret_cast<value_type*>(entry_); }
318 bool operator!=(const Iterator& other) { return entry_ != other.entry_; } 332 bool operator!=(const Iterator& other) { return entry_ != other.entry_; }
319 333
320 private: 334 private:
321 Iterator(const TemplateHashMapImpl<AllocationPolicy>* map, 335 Iterator(const TemplateHashMapImpl<AllocationPolicy>* map,
322 typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry) : 336 typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry) :
323 map_(map), entry_(entry) { } 337 map_(map), entry_(entry) { }
324 338
325 const TemplateHashMapImpl<AllocationPolicy>* map_; 339 const TemplateHashMapImpl<AllocationPolicy>* map_;
326 typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry_; 340 typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry_;
327 341
328 friend class TemplateHashMap; 342 friend class TemplateHashMap;
329 }; 343 };
330 344
331 TemplateHashMap( 345 TemplateHashMap(
332 typename TemplateHashMapImpl<AllocationPolicy>::MatchFun match) 346 typename TemplateHashMapImpl<AllocationPolicy>::MatchFun match,
333 : TemplateHashMapImpl<AllocationPolicy>(match) { } 347 Allocator allocator = Allocator(),
348 Deallocator deallocator = Deallocator())
349 : TemplateHashMapImpl<AllocationPolicy>(match, 8, allocator,
350 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/splay-tree.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698