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_ADDRESS_MAP_H_ | 5 #ifndef V8_ADDRESS_MAP_H_ |
6 #define V8_ADDRESS_MAP_H_ | 6 #define V8_ADDRESS_MAP_H_ |
7 | 7 |
| 8 #include "include/v8.h" |
8 #include "src/assert-scope.h" | 9 #include "src/assert-scope.h" |
9 #include "src/base/hashmap.h" | 10 #include "src/base/hashmap.h" |
10 #include "src/objects.h" | 11 #include "src/objects.h" |
11 | 12 |
12 namespace v8 { | 13 namespace v8 { |
13 namespace internal { | 14 namespace internal { |
14 | 15 |
15 class AddressMapBase { | 16 template <typename Type> |
16 protected: | 17 class PointerToIndexHashMap |
17 static void SetValue(base::HashMap::Entry* entry, uint32_t v) { | 18 : public base::TemplateHashMapImpl<uintptr_t, uint32_t, |
18 entry->value = reinterpret_cast<void*>(v); | 19 base::KeyEqualityMatcher<intptr_t>, |
| 20 base::DefaultAllocationPolicy> { |
| 21 public: |
| 22 typedef base::TemplateHashMapEntry<uintptr_t, uint32_t> Entry; |
| 23 |
| 24 inline void Set(Type value, uint32_t index) { |
| 25 uintptr_t key = Key(value); |
| 26 LookupOrInsert(key, Hash(key))->value = index; |
19 } | 27 } |
20 | 28 |
21 static uint32_t GetValue(base::HashMap::Entry* entry) { | 29 inline Maybe<uint32_t> Get(Type value) const { |
22 return static_cast<uint32_t>(reinterpret_cast<intptr_t>(entry->value)); | 30 uintptr_t key = Key(value); |
23 } | 31 Entry* entry = Lookup(key, Hash(key)); |
24 | 32 if (entry == nullptr) return Nothing<uint32_t>(); |
25 inline static base::HashMap::Entry* LookupEntry(base::HashMap* map, | 33 return Just(entry->value); |
26 HeapObject* obj, | |
27 bool insert) { | |
28 if (insert) { | |
29 map->LookupOrInsert(Key(obj), Hash(obj)); | |
30 } | |
31 return map->Lookup(Key(obj), Hash(obj)); | |
32 } | 34 } |
33 | 35 |
34 private: | 36 private: |
35 static uint32_t Hash(HeapObject* obj) { | 37 static uintptr_t Key(Type value) { |
36 return static_cast<int32_t>(reinterpret_cast<intptr_t>(obj->address())); | 38 return reinterpret_cast<uintptr_t>(value); |
37 } | 39 } |
38 | 40 |
39 static void* Key(HeapObject* obj) { | 41 static uint32_t Hash(uintptr_t key) { |
40 return reinterpret_cast<void*>(obj->address()); | 42 return static_cast<uint32_t>(key >> kPointerSizeLog2); |
41 } | 43 } |
42 }; | 44 }; |
43 | 45 |
44 class RootIndexMap : public AddressMapBase { | 46 class AddressToIndexHashMap : public PointerToIndexHashMap<Address> {}; |
| 47 class HeapObjectToIndexHashMap : public PointerToIndexHashMap<HeapObject*> {}; |
| 48 |
| 49 class RootIndexMap { |
45 public: | 50 public: |
46 explicit RootIndexMap(Isolate* isolate); | 51 explicit RootIndexMap(Isolate* isolate); |
47 | 52 |
48 static const int kInvalidRootIndex = -1; | 53 static const int kInvalidRootIndex = -1; |
49 | 54 |
50 int Lookup(HeapObject* obj) { | 55 int Lookup(HeapObject* obj) { |
51 base::HashMap::Entry* entry = LookupEntry(map_, obj, false); | 56 Maybe<uint32_t> maybe_index = map_->Get(obj); |
52 if (entry) return GetValue(entry); | 57 return maybe_index.IsJust() ? maybe_index.FromJust() : kInvalidRootIndex; |
53 return kInvalidRootIndex; | |
54 } | 58 } |
55 | 59 |
56 private: | 60 private: |
57 base::HashMap* map_; | 61 HeapObjectToIndexHashMap* map_; |
58 | 62 |
59 DISALLOW_COPY_AND_ASSIGN(RootIndexMap); | 63 DISALLOW_COPY_AND_ASSIGN(RootIndexMap); |
60 }; | 64 }; |
61 | 65 |
62 class SerializerReference { | 66 class SerializerReference { |
63 public: | 67 public: |
64 SerializerReference() : bitfield_(Special(kInvalidValue)) {} | 68 SerializerReference() : bitfield_(Special(kInvalidValue)) {} |
65 | 69 |
66 static SerializerReference FromBitfield(uint32_t bitfield) { | 70 static SerializerReference FromBitfield(uint32_t bitfield) { |
67 return SerializerReference(bitfield); | 71 return SerializerReference(bitfield); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 class SpaceBits : public BitField<int, kValueIndexSize, kSpaceTagSize> {}; | 183 class SpaceBits : public BitField<int, kValueIndexSize, kSpaceTagSize> {}; |
180 STATIC_ASSERT(SpaceBits::kNext == 32); | 184 STATIC_ASSERT(SpaceBits::kNext == 32); |
181 | 185 |
182 uint32_t bitfield_; | 186 uint32_t bitfield_; |
183 | 187 |
184 friend class SerializerReferenceMap; | 188 friend class SerializerReferenceMap; |
185 }; | 189 }; |
186 | 190 |
187 // Mapping objects to their location after deserialization. | 191 // Mapping objects to their location after deserialization. |
188 // This is used during building, but not at runtime by V8. | 192 // This is used during building, but not at runtime by V8. |
189 class SerializerReferenceMap : public AddressMapBase { | 193 class SerializerReferenceMap { |
190 public: | 194 public: |
191 SerializerReferenceMap() | 195 SerializerReferenceMap() |
192 : no_allocation_(), map_(), attached_reference_index_(0) {} | 196 : no_allocation_(), map_(), attached_reference_index_(0) {} |
193 | 197 |
194 SerializerReference Lookup(HeapObject* obj) { | 198 SerializerReference Lookup(HeapObject* obj) { |
195 base::HashMap::Entry* entry = LookupEntry(&map_, obj, false); | 199 Maybe<uint32_t> maybe_index = map_.Get(obj); |
196 return entry ? SerializerReference(GetValue(entry)) : SerializerReference(); | 200 return maybe_index.IsJust() ? SerializerReference(maybe_index.FromJust()) |
| 201 : SerializerReference(); |
197 } | 202 } |
198 | 203 |
199 void Add(HeapObject* obj, SerializerReference b) { | 204 void Add(HeapObject* obj, SerializerReference b) { |
200 DCHECK(b.is_valid()); | 205 DCHECK(b.is_valid()); |
201 DCHECK_NULL(LookupEntry(&map_, obj, false)); | 206 DCHECK(map_.Get(obj).IsNothing()); |
202 base::HashMap::Entry* entry = LookupEntry(&map_, obj, true); | 207 map_.Set(obj, b.bitfield_); |
203 SetValue(entry, b.bitfield_); | |
204 } | 208 } |
205 | 209 |
206 SerializerReference AddAttachedReference(HeapObject* attached_reference) { | 210 SerializerReference AddAttachedReference(HeapObject* attached_reference) { |
207 SerializerReference reference = | 211 SerializerReference reference = |
208 SerializerReference::AttachedReference(attached_reference_index_++); | 212 SerializerReference::AttachedReference(attached_reference_index_++); |
209 Add(attached_reference, reference); | 213 Add(attached_reference, reference); |
210 return reference; | 214 return reference; |
211 } | 215 } |
212 | 216 |
213 private: | 217 private: |
214 DisallowHeapAllocation no_allocation_; | 218 DisallowHeapAllocation no_allocation_; |
215 base::HashMap map_; | 219 HeapObjectToIndexHashMap map_; |
216 int attached_reference_index_; | 220 int attached_reference_index_; |
217 DISALLOW_COPY_AND_ASSIGN(SerializerReferenceMap); | 221 DISALLOW_COPY_AND_ASSIGN(SerializerReferenceMap); |
218 }; | 222 }; |
219 | 223 |
220 } // namespace internal | 224 } // namespace internal |
221 } // namespace v8 | 225 } // namespace v8 |
222 | 226 |
223 #endif // V8_ADDRESS_MAP_H_ | 227 #endif // V8_ADDRESS_MAP_H_ |
OLD | NEW |