v8  9.0.257(node16.0.0)
V8 is Google's open source JavaScript engine
member.h
Go to the documentation of this file.
1 // Copyright 2020 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 INCLUDE_CPPGC_MEMBER_H_
6 #define INCLUDE_CPPGC_MEMBER_H_
7 
8 #include <atomic>
9 #include <cstddef>
10 #include <type_traits>
11 
12 #include "cppgc/internal/pointer-policies.h"
13 #include "cppgc/sentinel-pointer.h"
14 #include "cppgc/type-traits.h"
15 #include "v8config.h" // NOLINT(build/include_directory)
16 
17 namespace cppgc {
18 
19 class Visitor;
20 
21 namespace internal {
22 
23 // MemberBase always refers to the object as const object and defers to
24 // BasicMember on casting to the right type as needed.
25 class MemberBase {
26  protected:
27  MemberBase() = default;
28  explicit MemberBase(const void* value) : raw_(value) {}
29 
30  const void** GetRawSlot() const { return &raw_; }
31  const void* GetRaw() const { return raw_; }
32  void SetRaw(void* value) { raw_ = value; }
33 
34  const void* GetRawAtomic() const {
35  return reinterpret_cast<const std::atomic<const void*>*>(&raw_)->load(
36  std::memory_order_relaxed);
37  }
38  void SetRawAtomic(const void* value) {
39  reinterpret_cast<std::atomic<const void*>*>(&raw_)->store(
40  value, std::memory_order_relaxed);
41  }
42 
43  void ClearFromGC() const { raw_ = nullptr; }
44 
45  private:
46  mutable const void* raw_ = nullptr;
47 };
48 
49 // The basic class from which all Member classes are 'generated'.
50 template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
51  typename CheckingPolicy>
52 class BasicMember final : private MemberBase, private CheckingPolicy {
53  public:
54  using PointeeType = T;
55 
56  constexpr BasicMember() = default;
57  constexpr BasicMember(std::nullptr_t) {} // NOLINT
58  BasicMember(SentinelPointer s) : MemberBase(s) {} // NOLINT
59  BasicMember(T* raw) : MemberBase(raw) { // NOLINT
60  InitializingWriteBarrier();
61  this->CheckPointer(Get());
62  }
63  BasicMember(T& raw) : BasicMember(&raw) {} // NOLINT
64  // Copy ctor.
65  BasicMember(const BasicMember& other) : BasicMember(other.Get()) {}
66  // Allow heterogeneous construction.
67  template <typename U, typename OtherBarrierPolicy, typename OtherWeaknessTag,
68  typename OtherCheckingPolicy,
69  typename = std::enable_if_t<std::is_base_of<T, U>::value>>
70  BasicMember( // NOLINT
71  const BasicMember<U, OtherWeaknessTag, OtherBarrierPolicy,
72  OtherCheckingPolicy>& other)
73  : BasicMember(other.Get()) {}
74  // Move ctor.
75  BasicMember(BasicMember&& other) noexcept : BasicMember(other.Get()) {
76  other.Clear();
77  }
78  // Allow heterogeneous move construction.
79  template <typename U, typename OtherBarrierPolicy, typename OtherWeaknessTag,
80  typename OtherCheckingPolicy,
81  typename = std::enable_if_t<std::is_base_of<T, U>::value>>
82  BasicMember( // NOLINT
83  BasicMember<U, OtherWeaknessTag, OtherBarrierPolicy,
84  OtherCheckingPolicy>&& other) noexcept
85  : BasicMember(other.Get()) {
86  other.Clear();
87  }
88  // Construction from Persistent.
89  template <typename U, typename PersistentWeaknessPolicy,
90  typename PersistentLocationPolicy,
91  typename PersistentCheckingPolicy,
92  typename = std::enable_if_t<std::is_base_of<T, U>::value>>
93  BasicMember( // NOLINT
94  const BasicPersistent<U, PersistentWeaknessPolicy,
95  PersistentLocationPolicy, PersistentCheckingPolicy>&
96  p)
97  : BasicMember(p.Get()) {}
98 
99  // Copy assignment.
100  BasicMember& operator=(const BasicMember& other) {
101  return operator=(other.Get());
102  }
103  // Allow heterogeneous copy assignment.
104  template <typename U, typename OtherWeaknessTag, typename OtherBarrierPolicy,
105  typename OtherCheckingPolicy,
106  typename = std::enable_if_t<std::is_base_of<T, U>::value>>
107  BasicMember& operator=(
108  const BasicMember<U, OtherWeaknessTag, OtherBarrierPolicy,
109  OtherCheckingPolicy>& other) {
110  return operator=(other.Get());
111  }
112  // Move assignment.
113  BasicMember& operator=(BasicMember&& other) noexcept {
114  operator=(other.Get());
115  other.Clear();
116  return *this;
117  }
118  // Heterogeneous move assignment.
119  template <typename U, typename OtherWeaknessTag, typename OtherBarrierPolicy,
120  typename OtherCheckingPolicy,
121  typename = std::enable_if_t<std::is_base_of<T, U>::value>>
122  BasicMember& operator=(BasicMember<U, OtherWeaknessTag, OtherBarrierPolicy,
123  OtherCheckingPolicy>&& other) noexcept {
124  operator=(other.Get());
125  other.Clear();
126  return *this;
127  }
128  // Assignment from Persistent.
129  template <typename U, typename PersistentWeaknessPolicy,
130  typename PersistentLocationPolicy,
131  typename PersistentCheckingPolicy,
132  typename = std::enable_if_t<std::is_base_of<T, U>::value>>
133  BasicMember& operator=(
134  const BasicPersistent<U, PersistentWeaknessPolicy,
135  PersistentLocationPolicy, PersistentCheckingPolicy>&
136  other) {
137  return operator=(other.Get());
138  }
139  BasicMember& operator=(T* other) {
140  SetRawAtomic(other);
141  AssigningWriteBarrier();
142  this->CheckPointer(Get());
143  return *this;
144  }
145  BasicMember& operator=(std::nullptr_t) {
146  Clear();
147  return *this;
148  }
149  BasicMember& operator=(SentinelPointer s) {
151  return *this;
152  }
153 
154  template <typename OtherWeaknessTag, typename OtherBarrierPolicy,
155  typename OtherCheckingPolicy>
156  void Swap(BasicMember<T, OtherWeaknessTag, OtherBarrierPolicy,
157  OtherCheckingPolicy>& other) {
158  T* tmp = Get();
159  *this = other;
160  other = tmp;
161  }
162 
163  explicit operator bool() const { return Get(); }
164  operator T*() const { return Get(); } // NOLINT
165  T* operator->() const { return Get(); }
166  T& operator*() const { return *Get(); }
167 
168  // CFI cast exemption to allow passing SentinelPointer through T* and support
169  // heterogeneous assignments between different Member and Persistent handles
170  // based on their actual types.
171  V8_CLANG_NO_SANITIZE("cfi-unrelated-cast") T* Get() const {
172  // Executed by the mutator, hence non atomic load.
173  //
174  // The const_cast below removes the constness from MemberBase storage. The
175  // following static_cast re-adds any constness if specified through the
176  // user-visible template parameter T.
177  return static_cast<T*>(const_cast<void*>(MemberBase::GetRaw()));
178  }
179 
180  void Clear() { SetRawAtomic(nullptr); }
181 
182  T* Release() {
183  T* result = Get();
184  Clear();
185  return result;
186  }
187 
188  const T** GetSlotForTesting() const {
189  return reinterpret_cast<const T**>(GetRawSlot());
190  }
191 
192  private:
193  const T* GetRawAtomic() const {
194  return static_cast<const T*>(MemberBase::GetRawAtomic());
195  }
196 
197  void InitializingWriteBarrier() const {
198  WriteBarrierPolicy::InitializingBarrier(GetRawSlot(), GetRaw());
199  }
200  void AssigningWriteBarrier() const {
201  WriteBarrierPolicy::AssigningBarrier(GetRawSlot(), GetRaw());
202  }
203 
204  void ClearFromGC() const { MemberBase::ClearFromGC(); }
205 
206  friend class cppgc::Visitor;
207  template <typename U>
208  friend struct cppgc::TraceTrait;
209 };
210 
211 template <typename T1, typename WeaknessTag1, typename WriteBarrierPolicy1,
212  typename CheckingPolicy1, typename T2, typename WeaknessTag2,
213  typename WriteBarrierPolicy2, typename CheckingPolicy2>
214 bool operator==(
215  BasicMember<T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1> member1,
216  BasicMember<T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2>
217  member2) {
218  return member1.Get() == member2.Get();
219 }
220 
221 template <typename T1, typename WeaknessTag1, typename WriteBarrierPolicy1,
222  typename CheckingPolicy1, typename T2, typename WeaknessTag2,
223  typename WriteBarrierPolicy2, typename CheckingPolicy2>
224 bool operator!=(
225  BasicMember<T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1> member1,
226  BasicMember<T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2>
227  member2) {
228  return !(member1 == member2);
229 }
230 
231 template <typename T, typename WriteBarrierPolicy, typename CheckingPolicy>
232 struct IsWeak<
233  internal::BasicMember<T, WeakMemberTag, WriteBarrierPolicy, CheckingPolicy>>
234  : std::true_type {};
235 
236 } // namespace internal
237 
238 /**
239  * Members are used in classes to contain strong pointers to other garbage
240  * collected objects. All Member fields of a class must be traced in the class'
241  * trace method.
242  */
243 template <typename T>
244 using Member = internal::BasicMember<T, internal::StrongMemberTag,
246 
247 /**
248  * WeakMember is similar to Member in that it is used to point to other garbage
249  * collected objects. However instead of creating a strong pointer to the
250  * object, the WeakMember creates a weak pointer, which does not keep the
251  * pointee alive. Hence if all pointers to to a heap allocated object are weak
252  * the object will be garbage collected. At the time of GC the weak pointers
253  * will automatically be set to null.
254  */
255 template <typename T>
256 using WeakMember = internal::BasicMember<T, internal::WeakMemberTag,
258 
259 /**
260  * UntracedMember is a pointer to an on-heap object that is not traced for some
261  * reason. Do not use this unless you know what you are doing. Keeping raw
262  * pointers to on-heap objects is prohibited unless used from stack. Pointee
263  * must be kept alive through other means.
264  */
265 template <typename T>
266 using UntracedMember = internal::BasicMember<T, internal::UntracedMemberTag,
268 
269 } // namespace cppgc
270 
271 #endif // INCLUDE_CPPGC_MEMBER_H_
cppgc::internal::operator!=
bool operator!=(BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1 > member1, BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2 > member2)
Definition: member.h:224
cppgc::internal::BasicMember::BasicMember
constexpr BasicMember(std::nullptr_t)
Definition: member.h:57
cppgc::internal::BasicMember::operator=
BasicMember & operator=(BasicMember< U, OtherWeaknessTag, OtherBarrierPolicy, OtherCheckingPolicy > &&other) noexcept
Definition: member.h:122
cppgc::Visitor::BasicPersistent
friend class internal::BasicPersistent
Definition: visitor.h:325
cppgc::internal::IsWeak
Definition: type-traits.h:36
cppgc::internal::BasicMember::operator*
T & operator*() const
Definition: member.h:166
cppgc::internal::BasicMember::Clear
void Clear()
Definition: member.h:180
cppgc::internal::BasicMember::operator bool
operator bool() const
Definition: member.h:163
cppgc::internal::BasicMember::operator=
BasicMember & operator=(std::nullptr_t)
Definition: member.h:145
cppgc::internal::BasicMember::operator=
BasicMember & operator=(const BasicMember< U, OtherWeaknessTag, OtherBarrierPolicy, OtherCheckingPolicy > &other)
Definition: member.h:107
cppgc::internal::SentinelPointer
Definition: sentinel-pointer.h:15
cppgc::internal::BasicMember::BasicMember
BasicMember(T *raw)
Definition: member.h:59
cppgc::internal::DijkstraWriteBarrierPolicy
Definition: pointer-policies.h:25
cppgc::internal::MemberBase::MemberBase
MemberBase()=default
cppgc::internal::MemberBase
Definition: member.h:25
cppgc::internal::BasicMember::BasicMember
BasicMember(SentinelPointer s)
Definition: member.h:58
cppgc::internal::MemberBase::GetRawAtomic
const void * GetRawAtomic() const
Definition: member.h:34
cppgc::internal::BasicMember::operator->
T * operator->() const
Definition: member.h:165
cppgc::internal::BasicMember::GetSlotForTesting
const T ** GetSlotForTesting() const
Definition: member.h:188
cppgc::internal::BasicMember::BasicMember
BasicMember(T &raw)
Definition: member.h:63
cppgc::internal::MemberBase::GetRawSlot
const void ** GetRawSlot() const
Definition: member.h:30
cppgc
Definition: allocation.h:17
cppgc::internal::BasicMember::BasicMember
BasicMember(const BasicPersistent< U, PersistentWeaknessPolicy, PersistentLocationPolicy, PersistentCheckingPolicy > &p)
Definition: member.h:93
cppgc::internal::MemberBase::MemberBase
MemberBase(const void *value)
Definition: member.h:28
cppgc::internal::BasicMember::operator=
BasicMember & operator=(SentinelPointer s)
Definition: member.h:149
cppgc::internal::BasicMember::operator=
BasicMember & operator=(const BasicMember &other)
Definition: member.h:100
cppgc::internal::BasicMember::operator=
BasicMember & operator=(const BasicPersistent< U, PersistentWeaknessPolicy, PersistentLocationPolicy, PersistentCheckingPolicy > &other)
Definition: member.h:133
cppgc::internal::BasicMember::BasicMember
constexpr BasicMember()=default
cppgc::internal::BasicMember::operator=
BasicMember & operator=(T *other)
Definition: member.h:139
cppgc::internal::BasicMember::BasicMember
BasicMember(BasicMember &&other) noexcept
Definition: member.h:75
cppgc::internal::BasicMember::Release
T * Release()
Definition: member.h:182
cppgc::internal::operator==
bool operator==(BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1 > member1, BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2 > member2)
Definition: member.h:214
cppgc::Visitor
Definition: visitor.h:52
cppgc::internal::BasicMember::BasicMember
BasicMember(BasicMember< U, OtherWeaknessTag, OtherBarrierPolicy, OtherCheckingPolicy > &&other) noexcept
Definition: member.h:82
cppgc::internal::BasicMember::BasicMember
BasicMember(const BasicMember< U, OtherWeaknessTag, OtherBarrierPolicy, OtherCheckingPolicy > &other)
Definition: member.h:70
cppgc::internal::MemberBase::ClearFromGC
void ClearFromGC() const
Definition: member.h:43
cppgc::internal::BasicMember::operator=
BasicMember & operator=(BasicMember &&other) noexcept
Definition: member.h:113
cppgc::internal::MemberBase::GetRaw
const void * GetRaw() const
Definition: member.h:31
cppgc::internal::BasicMember::operator T*
operator T*() const
Definition: member.h:164
cppgc::internal::BasicMember::Swap
void Swap(BasicMember< T, OtherWeaknessTag, OtherBarrierPolicy, OtherCheckingPolicy > &other)
Definition: member.h:156
cppgc::internal::MemberBase::SetRaw
void SetRaw(void *value)
Definition: member.h:32
cppgc::internal::NoWriteBarrierPolicy
Definition: pointer-policies.h:45
cppgc::internal
Definition: allocation.h:22
cppgc::internal::MemberBase::SetRawAtomic
void SetRawAtomic(const void *value)
Definition: member.h:38
cppgc::internal::BasicMember::BasicMember
BasicMember(const BasicMember &other)
Definition: member.h:65
cppgc::internal::BasicMember::TraceTrait
friend struct cppgc::TraceTrait
Definition: member.h:208
V8_CLANG_NO_SANITIZE
#define V8_CLANG_NO_SANITIZE(what)
Definition: v8config.h:478