V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
slots.h
1 // Copyright 2018 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_OBJECTS_SLOTS_H_
6 #define V8_OBJECTS_SLOTS_H_
7 
8 #include "src/globals.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 class ObjectPtr;
14 
15 template <typename Subclass, typename Data, size_t SlotDataSize>
16 class SlotBase {
17  public:
18  using TData = Data;
19  // TODO(ishell): This should eventually become just sizeof(TData) once
20  // pointer compression is implemented.
21  static constexpr size_t kSlotDataSize = SlotDataSize;
22 
23  Subclass& operator++() { // Prefix increment.
24  ptr_ += kSlotDataSize;
25  return *static_cast<Subclass*>(this);
26  }
27  Subclass operator++(int) { // Postfix increment.
28  Subclass result = *static_cast<Subclass*>(this);
29  ptr_ += kSlotDataSize;
30  return result;
31  }
32  Subclass& operator--() { // Prefix decrement.
33  ptr_ -= kSlotDataSize;
34  return *static_cast<Subclass*>(this);
35  }
36  Subclass operator--(int) { // Postfix decrement.
37  Subclass result = *static_cast<Subclass*>(this);
38  ptr_ -= kSlotDataSize;
39  return result;
40  }
41 
42  bool operator<(const SlotBase& other) const { return ptr_ < other.ptr_; }
43  bool operator<=(const SlotBase& other) const { return ptr_ <= other.ptr_; }
44  bool operator>(const SlotBase& other) const { return ptr_ > other.ptr_; }
45  bool operator>=(const SlotBase& other) const { return ptr_ >= other.ptr_; }
46  bool operator==(const SlotBase& other) const { return ptr_ == other.ptr_; }
47  bool operator!=(const SlotBase& other) const { return ptr_ != other.ptr_; }
48  size_t operator-(const SlotBase& other) const {
49  DCHECK_GE(ptr_, other.ptr_);
50  return static_cast<size_t>((ptr_ - other.ptr_) / kSlotDataSize);
51  }
52  Subclass operator-(int i) const { return Subclass(ptr_ - i * kSlotDataSize); }
53  Subclass operator+(int i) const { return Subclass(ptr_ + i * kSlotDataSize); }
54  friend Subclass operator+(int i, const Subclass& slot) {
55  return Subclass(slot.ptr_ + i * kSlotDataSize);
56  }
57  Subclass& operator+=(int i) {
58  ptr_ += i * kSlotDataSize;
59  return *static_cast<Subclass*>(this);
60  }
61  Subclass operator-(int i) { return Subclass(ptr_ - i * kSlotDataSize); }
62  Subclass& operator-=(int i) {
63  ptr_ -= i * kSlotDataSize;
64  return *static_cast<Subclass*>(this);
65  }
66 
67  void* ToVoidPtr() const { return reinterpret_cast<void*>(address()); }
68 
69  Address address() const { return ptr_; }
70  // For symmetry with Handle.
71  TData* location() const { return reinterpret_cast<TData*>(ptr_); }
72 
73  protected:
74  STATIC_ASSERT(IsAligned(kSlotDataSize, kTaggedSize));
75  explicit SlotBase(Address ptr) : ptr_(ptr) {
76  DCHECK(IsAligned(ptr, kTaggedSize));
77  }
78 
79  private:
80  // This field usually describes an on-heap address (a slot within an object),
81  // so its type should not be a pointer to another C++ wrapper class.
82  // Type safety is provided by well-defined conversion operations.
83  Address ptr_;
84 };
85 
86 // An ObjectSlot instance describes a kTaggedSize-sized field ("slot") holding
87 // a tagged pointer (smi or heap object).
88 // Its address() is the address of the slot.
89 // The slot's contents can be read and written using operator* and store().
90 class ObjectSlot : public SlotBase<ObjectSlot, Tagged_t, kTaggedSize> {
91  public:
92  ObjectSlot() : SlotBase(kNullAddress) {}
93  explicit ObjectSlot(Address ptr) : SlotBase(ptr) {}
94  explicit ObjectSlot(Address* ptr)
95  : SlotBase(reinterpret_cast<Address>(ptr)) {}
96  inline explicit ObjectSlot(ObjectPtr* object);
97  explicit ObjectSlot(Object const* const* ptr)
98  : SlotBase(reinterpret_cast<Address>(ptr)) {}
99  template <typename T>
101  : SlotBase(slot.address()) {}
102 
103  Object* operator*() const { return *reinterpret_cast<Object**>(address()); }
104  inline void store(Object* value) const;
105 
106  inline ObjectPtr Acquire_Load() const;
107  inline ObjectPtr Relaxed_Load() const;
108  inline void Relaxed_Store(ObjectPtr value) const;
109  inline void Release_Store(ObjectPtr value) const;
110  inline ObjectPtr Release_CompareAndSwap(ObjectPtr old,
111  ObjectPtr target) const;
112  // Old-style alternative for the above, temporarily separate to allow
113  // incremental transition.
114  // TODO(3770): Get rid of the duplication when the migration is complete.
115  inline Object* Acquire_Load1() const;
116  inline void Relaxed_Store1(Object* value) const;
117  inline void Release_Store1(Object* value) const;
118 };
119 
120 // A MaybeObjectSlot instance describes a kTaggedSize-sized field ("slot")
121 // holding a possibly-weak tagged pointer (think: MaybeObject).
122 // Its address() is the address of the slot.
123 // The slot's contents can be read and written using operator* and store().
125  : public SlotBase<MaybeObjectSlot, Tagged_t, kTaggedSize> {
126  public:
127  explicit MaybeObjectSlot(Address ptr) : SlotBase(ptr) {}
128  explicit MaybeObjectSlot(Object** ptr)
129  : SlotBase(reinterpret_cast<Address>(ptr)) {}
130  template <typename T>
132  : SlotBase(slot.address()) {}
133 
134  inline MaybeObject operator*() const;
135  inline void store(MaybeObject value) const;
136 
137  inline MaybeObject Relaxed_Load() const;
138  inline void Relaxed_Store(MaybeObject value) const;
139  inline void Release_CompareAndSwap(MaybeObject old, MaybeObject target) const;
140 };
141 
142 // A HeapObjectSlot instance describes a kTaggedSize-sized field ("slot")
143 // holding a weak or strong pointer to a heap object (think:
144 // HeapObjectReference).
145 // Its address() is the address of the slot.
146 // The slot's contents can be read and written using operator* and store().
147 // In case it is known that that slot contains a strong heap object pointer,
148 // ToHeapObject() can be used to retrieve that heap object.
149 class HeapObjectSlot : public SlotBase<HeapObjectSlot, Tagged_t, kTaggedSize> {
150  public:
151  HeapObjectSlot() : SlotBase(kNullAddress) {}
152  explicit HeapObjectSlot(Address ptr) : SlotBase(ptr) {}
153  template <typename T>
155  : SlotBase(slot.address()) {}
156 
157  inline HeapObjectReference operator*() const;
158  inline void store(HeapObjectReference value) const;
159 
160  HeapObject* ToHeapObject() const {
161  DCHECK((*location() & kHeapObjectTagMask) == kHeapObjectTag);
162  return reinterpret_cast<HeapObject*>(*location());
163  }
164 
165  void StoreHeapObject(HeapObject* value) const {
166  *reinterpret_cast<HeapObject**>(address()) = value;
167  }
168 };
169 
170 } // namespace internal
171 } // namespace v8
172 
173 #endif // V8_OBJECTS_SLOTS_H_
Definition: libplatform.h:13
Definition: v8.h:963