| Index: src/hashmap.h
|
| diff --git a/src/hashmap.h b/src/hashmap.h
|
| index 91843b81b81244ba4cdec3bd3ef3fb391ee0da44..cda7e53c2297dbadad66a113137f994906c8f13b 100644
|
| --- a/src/hashmap.h
|
| +++ b/src/hashmap.h
|
| @@ -38,11 +38,17 @@ namespace internal {
|
| template<class AllocationPolicy>
|
| class TemplateHashMapImpl {
|
| public:
|
| + typedef typename AllocationPolicy::Alloc Allocator;
|
| + typedef typename AllocationPolicy::Dealloc Deallocator;
|
| +
|
| typedef bool (*MatchFun) (void* key1, void* key2);
|
|
|
| // initial_capacity is the size of the initial hash map;
|
| // it must be a power of 2 (and thus must not be 0).
|
| - TemplateHashMapImpl(MatchFun match, uint32_t initial_capacity = 8);
|
| + TemplateHashMapImpl(MatchFun match,
|
| + Allocator allocator = Allocator(),
|
| + Deallocator deallocator = Deallocator(),
|
| + uint32_t initial_capacity = 8);
|
|
|
| ~TemplateHashMapImpl();
|
|
|
| @@ -60,7 +66,8 @@ class TemplateHashMapImpl {
|
| // but insert is set, a new entry is inserted with
|
| // corresponding key, key hash, and NULL value.
|
| // Otherwise, NULL is returned.
|
| - Entry* Lookup(void* key, uint32_t hash, bool insert);
|
| + Entry* Lookup(void* key, uint32_t hash, bool insert,
|
| + Allocator allocator = Allocator());
|
|
|
| // Removes the entry with matching key.
|
| // It returns the value of the deleted entry
|
| @@ -94,32 +101,35 @@ class TemplateHashMapImpl {
|
| Entry* map_;
|
| uint32_t capacity_;
|
| uint32_t occupancy_;
|
| + Deallocator deallocator_;
|
|
|
| Entry* map_end() const { return map_ + capacity_; }
|
| Entry* Probe(void* key, uint32_t hash);
|
| - void Initialize(uint32_t capacity);
|
| - void Resize();
|
| + void Initialize(uint32_t capacity, Allocator allocator);
|
| + void Resize(Allocator allocator);
|
| };
|
|
|
| typedef TemplateHashMapImpl<FreeStoreAllocationPolicy> HashMap;
|
|
|
| -template<class P>
|
| -TemplateHashMapImpl<P>::TemplateHashMapImpl(MatchFun match,
|
| - uint32_t initial_capacity) {
|
| +template<class AllocationPolicy>
|
| +TemplateHashMapImpl<AllocationPolicy>::TemplateHashMapImpl(
|
| + MatchFun match, Allocator allocator, Deallocator deallocator,
|
| + uint32_t initial_capacity) : deallocator_(deallocator) {
|
| match_ = match;
|
| - Initialize(initial_capacity);
|
| + Initialize(initial_capacity, allocator);
|
| }
|
|
|
|
|
| -template<class P>
|
| -TemplateHashMapImpl<P>::~TemplateHashMapImpl() {
|
| - P::Delete(map_);
|
| +template<class AllocationPolicy>
|
| +TemplateHashMapImpl<AllocationPolicy>::~TemplateHashMapImpl() {
|
| + deallocator_.Delete(map_);
|
| }
|
|
|
|
|
| -template<class P>
|
| -typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Lookup(
|
| - void* key, uint32_t hash, bool insert) {
|
| +template<class AllocationPolicy>
|
| +typename TemplateHashMapImpl<AllocationPolicy>::Entry*
|
| +TemplateHashMapImpl<AllocationPolicy>::Lookup(
|
| + void* key, uint32_t hash, bool insert, Allocator allocator) {
|
| // Find a matching entry.
|
| Entry* p = Probe(key, hash);
|
| if (p->key != NULL) {
|
| @@ -135,7 +145,7 @@ typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Lookup(
|
|
|
| // Grow the map if we reached >= 80% occupancy.
|
| if (occupancy_ + occupancy_/4 >= capacity_) {
|
| - Resize();
|
| + Resize(allocator);
|
| p = Probe(key, hash);
|
| }
|
|
|
| @@ -147,8 +157,8 @@ typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Lookup(
|
| }
|
|
|
|
|
| -template<class P>
|
| -void* TemplateHashMapImpl<P>::Remove(void* key, uint32_t hash) {
|
| +template<class AllocationPolicy>
|
| +void* TemplateHashMapImpl<AllocationPolicy>::Remove(void* key, uint32_t hash) {
|
| // Lookup the entry for the key to remove.
|
| Entry* p = Probe(key, hash);
|
| if (p->key == NULL) {
|
| @@ -209,8 +219,8 @@ void* TemplateHashMapImpl<P>::Remove(void* key, uint32_t hash) {
|
| }
|
|
|
|
|
| -template<class P>
|
| -void TemplateHashMapImpl<P>::Clear() {
|
| +template<class AllocationPolicy>
|
| +void TemplateHashMapImpl<AllocationPolicy>::Clear() {
|
| // Mark all entries as empty.
|
| const Entry* end = map_end();
|
| for (Entry* p = map_; p < end; p++) {
|
| @@ -220,15 +230,16 @@ void TemplateHashMapImpl<P>::Clear() {
|
| }
|
|
|
|
|
| -template<class P>
|
| -typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Start() const {
|
| +template<class AllocationPolicy>
|
| +typename TemplateHashMapImpl<AllocationPolicy>::Entry*
|
| + TemplateHashMapImpl<AllocationPolicy>::Start() const {
|
| return Next(map_ - 1);
|
| }
|
|
|
|
|
| -template<class P>
|
| -typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Next(Entry* p)
|
| - const {
|
| +template<class AllocationPolicy>
|
| +typename TemplateHashMapImpl<AllocationPolicy>::Entry*
|
| + TemplateHashMapImpl<AllocationPolicy>::Next(Entry* p) const {
|
| const Entry* end = map_end();
|
| ASSERT(map_ - 1 <= p && p < end);
|
| for (p++; p < end; p++) {
|
| @@ -240,9 +251,9 @@ typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Next(Entry* p)
|
| }
|
|
|
|
|
| -template<class P>
|
| -typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Probe(void* key,
|
| - uint32_t hash) {
|
| +template<class AllocationPolicy>
|
| +typename TemplateHashMapImpl<AllocationPolicy>::Entry*
|
| + TemplateHashMapImpl<AllocationPolicy>::Probe(void* key, uint32_t hash) {
|
| ASSERT(key != NULL);
|
|
|
| ASSERT(IsPowerOf2(capacity_));
|
| @@ -262,10 +273,11 @@ typename TemplateHashMapImpl<P>::Entry* TemplateHashMapImpl<P>::Probe(void* key,
|
| }
|
|
|
|
|
| -template<class P>
|
| -void TemplateHashMapImpl<P>::Initialize(uint32_t capacity) {
|
| +template<class AllocationPolicy>
|
| +void TemplateHashMapImpl<AllocationPolicy>::Initialize(uint32_t capacity,
|
| + Allocator allocator) {
|
| ASSERT(IsPowerOf2(capacity));
|
| - map_ = reinterpret_cast<Entry*>(P::New(capacity * sizeof(Entry)));
|
| + map_ = reinterpret_cast<Entry*>(allocator.New(capacity * sizeof(Entry)));
|
| if (map_ == NULL) {
|
| v8::internal::FatalProcessOutOfMemory("HashMap::Initialize");
|
| return;
|
| @@ -275,13 +287,13 @@ void TemplateHashMapImpl<P>::Initialize(uint32_t capacity) {
|
| }
|
|
|
|
|
| -template<class P>
|
| -void TemplateHashMapImpl<P>::Resize() {
|
| +template<class AllocationPolicy>
|
| +void TemplateHashMapImpl<AllocationPolicy>::Resize(Allocator allocator) {
|
| Entry* map = map_;
|
| uint32_t n = occupancy_;
|
|
|
| // Allocate larger map.
|
| - Initialize(capacity_ * 2);
|
| + Initialize(capacity_ * 2, allocator);
|
|
|
| // Rehash all current entries.
|
| for (Entry* p = map; n > 0; p++) {
|
| @@ -292,7 +304,7 @@ void TemplateHashMapImpl<P>::Resize() {
|
| }
|
|
|
| // Delete old map.
|
| - P::Delete(map);
|
| + deallocator_.Delete(map);
|
| }
|
|
|
|
|
| @@ -302,6 +314,9 @@ class TemplateHashMap: private TemplateHashMapImpl<AllocationPolicy> {
|
| public:
|
| STATIC_ASSERT(sizeof(Key*) == sizeof(void*)); // NOLINT
|
| STATIC_ASSERT(sizeof(Value*) == sizeof(void*)); // NOLINT
|
| + typedef typename AllocationPolicy::Alloc Allocator;
|
| + typedef typename AllocationPolicy::Dealloc Deallocator;
|
| +
|
| struct value_type {
|
| Key* first;
|
| Value* second;
|
| @@ -329,8 +344,10 @@ class TemplateHashMap: private TemplateHashMapImpl<AllocationPolicy> {
|
| };
|
|
|
| TemplateHashMap(
|
| - typename TemplateHashMapImpl<AllocationPolicy>::MatchFun match)
|
| - : TemplateHashMapImpl<AllocationPolicy>(match) { }
|
| + typename TemplateHashMapImpl<AllocationPolicy>::MatchFun match,
|
| + Allocator allocator = Allocator(),
|
| + Deallocator deallocator = Deallocator())
|
| + : TemplateHashMapImpl<AllocationPolicy>(match, allocator, deallocator) { }
|
|
|
| Iterator begin() const { return Iterator(this, this->Start()); }
|
| Iterator end() const { return Iterator(this, NULL); }
|
|
|