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

Side by Side Diff: src/heap/identity-map.h

Issue 1105693002: Implement IdentityMap<V>, a robust, GC-safe object-identity HashMap. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 7 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
OLDNEW
(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> handle);
42 RawEntry FindEntry(Handle<Object> handle);
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> handle);
51 RawEntry Insert(Handle<Object> handle);
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.
Erik Corry 2015/04/27 15:39:13 This is a very strange API. I would either ban SM
titzer 2015/04/27 16:36:48 I could solve this with a special cell for SMI #0.
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> handle) {
79 return reinterpret_cast<V*>(GetEntry(handle));
80 }
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> handle) {
Erik Corry 2015/04/27 15:39:13 Why does this not return a handle?
titzer 2015/04/27 16:36:48 Because the value type V is not a heap object. E.g
87 return reinterpret_cast<V*>(FindEntry(handle));
88 }
89
90 // Set the value for the given key.
91 void Set(Handle<Object> handle, V value) {
Erik Corry 2015/04/27 15:39:13 handle should be called key? Also several other p
titzer 2015/04/27 16:36:48 Done.
92 *(reinterpret_cast<V*>(GetEntry(handle))) = value;
93 }
94 };
95 }
96 } // namespace v8::internal
97
98 #endif // V8_HEAP_IDENTITY_MAP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698