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 value type {V} must be reinterpret_cast'able to void*. | |
66 // The map is robust w.r.t. garbage collection through coordination with the | |
67 // supplied {heap}. | |
68 // Note that in general, SMIs are valid keys, but {SMI #0} is _not_ a valid key. | |
69 template <typename V> | |
70 class IdentityMap : public IdentityMapBase { | |
71 public: | |
72 IdentityMap(Heap* heap, Zone* zone) : IdentityMapBase(heap, zone) {} | |
73 | |
74 // Searches this map for the given key using the object's address | |
75 // as the identity, returning: | |
76 // found => a pointer to the storage location for the value | |
77 // not found => a pointer to a new storage location for the value | |
78 V* Get(Handle<Object> key) { return reinterpret_cast<V*>(GetEntry(key)); } | |
79 | |
80 // Searches this map for the given key using the object's address | |
81 // as the identity, returning: | |
82 // found => a pointer to the storage location for the value | |
83 // not found => {nullptr} | |
84 V* Find(Handle<Object> key) { return reinterpret_cast<V*>(FindEntry(key)); } | |
85 | |
86 // Set the value for the given key. | |
87 void Set(Handle<Object> key, V value) { | |
88 *(reinterpret_cast<V*>(GetEntry(key))) = value; | |
Erik Corry
2015/04/28 13:51:16
This assignment takes place without holding the lo
| |
89 } | |
90 }; | |
91 } | |
92 } // namespace v8::internal | |
93 | |
94 #endif // V8_HEAP_IDENTITY_MAP_H_ | |
OLD | NEW |