| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef V8_IDENTITY_MAP_H_ | 5 #ifndef V8_IDENTITY_MAP_H_ |
| 6 #define V8_IDENTITY_MAP_H_ | 6 #define V8_IDENTITY_MAP_H_ |
| 7 | 7 |
| 8 #include "src/handles.h" | 8 #include "src/handles.h" |
| 9 | 9 |
| 10 namespace v8 { | 10 namespace v8 { |
| 11 namespace internal { | 11 namespace internal { |
| 12 | 12 |
| 13 // Forward declarations. | 13 // Forward declarations. |
| 14 class Heap; | 14 class Heap; |
| 15 class Zone; | 15 class Zone; |
| 16 | 16 |
| 17 // Base class of identity maps contains shared code for all template | 17 // Base class of identity maps contains shared code for all template |
| 18 // instantions. | 18 // instantions. |
| 19 class IdentityMapBase { | 19 class IdentityMapBase { |
| 20 public: | |
| 21 // Enable or disable concurrent mode for this map. Concurrent mode implies | |
| 22 // taking the heap's relocation lock during most operations. | |
| 23 void SetConcurrent(bool concurrent) { concurrent_ = concurrent; } | |
| 24 | |
| 25 protected: | 20 protected: |
| 26 // Allow Tester to access internals, including changing the address of objects | 21 // Allow Tester to access internals, including changing the address of objects |
| 27 // within the {keys_} array in order to simulate a moving GC. | 22 // within the {keys_} array in order to simulate a moving GC. |
| 28 friend class IdentityMapTester; | 23 friend class IdentityMapTester; |
| 29 | 24 |
| 30 typedef void** RawEntry; | 25 typedef void** RawEntry; |
| 31 | 26 |
| 32 IdentityMapBase(Heap* heap, Zone* zone) | 27 IdentityMapBase(Heap* heap, Zone* zone) |
| 33 : heap_(heap), | 28 : heap_(heap), |
| 34 zone_(zone), | 29 zone_(zone), |
| 35 concurrent_(false), | |
| 36 gc_counter_(-1), | 30 gc_counter_(-1), |
| 37 size_(0), | 31 size_(0), |
| 38 mask_(0), | 32 mask_(0), |
| 39 keys_(nullptr), | 33 keys_(nullptr), |
| 40 values_(nullptr) {} | 34 values_(nullptr) {} |
| 41 ~IdentityMapBase(); | 35 ~IdentityMapBase(); |
| 42 | 36 |
| 43 RawEntry GetEntry(Handle<Object> key); | 37 RawEntry GetEntry(Object* key); |
| 44 RawEntry FindEntry(Handle<Object> key); | 38 RawEntry FindEntry(Object* key); |
| 45 | 39 |
| 46 private: | 40 private: |
| 47 // Internal implementation should not be called directly by subclasses. | 41 // Internal implementation should not be called directly by subclasses. |
| 48 int LookupIndex(Object* address); | 42 int LookupIndex(Object* address); |
| 49 int InsertIndex(Object* address); | 43 int InsertIndex(Object* address); |
| 50 void Rehash(); | 44 void Rehash(); |
| 51 void Resize(); | 45 void Resize(); |
| 52 RawEntry Lookup(Handle<Object> key); | 46 RawEntry Lookup(Object* key); |
| 53 RawEntry Insert(Handle<Object> key); | 47 RawEntry Insert(Object* key); |
| 54 int Hash(Object* address); | 48 int Hash(Object* address); |
| 55 | 49 |
| 56 Heap* heap_; | 50 Heap* heap_; |
| 57 Zone* zone_; | 51 Zone* zone_; |
| 58 bool concurrent_; | |
| 59 int gc_counter_; | 52 int gc_counter_; |
| 60 int size_; | 53 int size_; |
| 61 int mask_; | 54 int mask_; |
| 62 Object** keys_; | 55 Object** keys_; |
| 63 void** values_; | 56 void** values_; |
| 64 }; | 57 }; |
| 65 | 58 |
| 66 // Implements an identity map from object addresses to a given value type {V}. | 59 // Implements an identity map from object addresses to a given value type {V}. |
| 67 // The map is robust w.r.t. garbage collection by synchronization with the | 60 // The map is robust w.r.t. garbage collection by synchronization with the |
| 68 // supplied {heap}. | 61 // supplied {heap}. |
| 69 // * Keys are treated as strong roots. | 62 // * Keys are treated as strong roots. |
| 70 // * SMIs are valid keys, except SMI #0. | 63 // * SMIs are valid keys, except SMI #0. |
| 71 // * The value type {V} must be reinterpret_cast'able to {void*} | 64 // * The value type {V} must be reinterpret_cast'able to {void*} |
| 72 // * The value type {V} must not be a heap type. | 65 // * The value type {V} must not be a heap type. |
| 73 template <typename V> | 66 template <typename V> |
| 74 class IdentityMap : public IdentityMapBase { | 67 class IdentityMap : public IdentityMapBase { |
| 75 public: | 68 public: |
| 76 IdentityMap(Heap* heap, Zone* zone) : IdentityMapBase(heap, zone) {} | 69 IdentityMap(Heap* heap, Zone* zone) : IdentityMapBase(heap, zone) {} |
| 77 | 70 |
| 78 // Searches this map for the given key using the object's address | 71 // Searches this map for the given key using the object's address |
| 79 // as the identity, returning: | 72 // as the identity, returning: |
| 80 // found => a pointer to the storage location for the value | 73 // found => a pointer to the storage location for the value |
| 81 // not found => a pointer to a new storage location for the value | 74 // not found => a pointer to a new storage location for the value |
| 82 V* Get(Handle<Object> key) { return reinterpret_cast<V*>(GetEntry(key)); } | 75 V* Get(Handle<Object> key) { return Get(*key); } |
| 76 V* Get(Object* key) { return reinterpret_cast<V*>(GetEntry(key)); } |
| 83 | 77 |
| 84 // Searches this map for the given key using the object's address | 78 // Searches this map for the given key using the object's address |
| 85 // as the identity, returning: | 79 // as the identity, returning: |
| 86 // found => a pointer to the storage location for the value | 80 // found => a pointer to the storage location for the value |
| 87 // not found => {nullptr} | 81 // not found => {nullptr} |
| 88 V* Find(Handle<Object> key) { return reinterpret_cast<V*>(FindEntry(key)); } | 82 V* Find(Handle<Object> key) { return Find(*key); } |
| 83 V* Find(Object* key) { return reinterpret_cast<V*>(FindEntry(key)); } |
| 89 | 84 |
| 90 // Set the value for the given key. | 85 // Set the value for the given key. |
| 91 void Set(Handle<Object> key, V value) { | 86 void Set(Handle<Object> key, V v) { Set(*key, v); } |
| 92 *(reinterpret_cast<V*>(GetEntry(key))) = value; | 87 void Set(Object* key, V v) { *(reinterpret_cast<V*>(GetEntry(key))) = v; } |
| 93 } | |
| 94 }; | 88 }; |
| 95 } // namespace internal | 89 } // namespace internal |
| 96 } // namespace v8 | 90 } // namespace v8 |
| 97 | 91 |
| 98 #endif // V8_IDENTITY_MAP_H_ | 92 #endif // V8_IDENTITY_MAP_H_ |
| OLD | NEW |