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