V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
address-map.h
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_ADDRESS_MAP_H_
6 #define V8_ADDRESS_MAP_H_
7 
8 #include "include/v8.h"
9 #include "src/assert-scope.h"
10 #include "src/base/hashmap.h"
11 #include "src/objects.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 template <typename Type>
18  : public base::TemplateHashMapImpl<uintptr_t, uint32_t,
19  base::KeyEqualityMatcher<intptr_t>,
20  base::DefaultAllocationPolicy> {
21  public:
23 
24  inline void Set(Type value, uint32_t index) {
25  uintptr_t key = Key(value);
26  LookupOrInsert(key, Hash(key))->value = index;
27  }
28 
29  inline Maybe<uint32_t> Get(Type value) const {
30  uintptr_t key = Key(value);
31  Entry* entry = Lookup(key, Hash(key));
32  if (entry == nullptr) return Nothing<uint32_t>();
33  return Just(entry->value);
34  }
35 
36  private:
37  static inline uintptr_t Key(Type value);
38 
39  static uint32_t Hash(uintptr_t key) { return static_cast<uint32_t>(key); }
40 };
41 
42 template <>
43 inline uintptr_t PointerToIndexHashMap<Address>::Key(Address value) {
44  return static_cast<uintptr_t>(value);
45 }
46 
47 template <typename Type>
48 inline uintptr_t PointerToIndexHashMap<Type>::Key(Type value) {
49  return reinterpret_cast<uintptr_t>(value);
50 }
51 
52 class AddressToIndexHashMap : public PointerToIndexHashMap<Address> {};
53 class HeapObjectToIndexHashMap : public PointerToIndexHashMap<HeapObject*> {};
54 
55 class RootIndexMap {
56  public:
57  explicit RootIndexMap(Isolate* isolate);
58 
59  // Returns true on successful lookup and sets *|out_root_list|.
60  bool Lookup(HeapObject* obj, RootIndex* out_root_list) const {
61  Maybe<uint32_t> maybe_index = map_->Get(obj);
62  if (maybe_index.IsJust()) {
63  *out_root_list = static_cast<RootIndex>(maybe_index.FromJust());
64  return true;
65  }
66  return false;
67  }
68  bool Lookup(Address obj, RootIndex* out_root_list) {
69  return Lookup(reinterpret_cast<HeapObject*>(obj), out_root_list);
70  }
71 
72  private:
74 
75  DISALLOW_COPY_AND_ASSIGN(RootIndexMap);
76 };
77 
78 } // namespace internal
79 } // namespace v8
80 
81 #endif // V8_ADDRESS_MAP_H_
Definition: v8.h:56
V8_INLINE T FromJust() const
Definition: v8.h:8683
Definition: libplatform.h:13
Definition: v8.h:3740