v8  9.4.146 (node 16.13.0)
V8 is Google's open source JavaScript engine
pointer-policies.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_INTERNAL_POINTER_POLICIES_H_
6 #define INCLUDE_CPPGC_INTERNAL_POINTER_POLICIES_H_
7 
8 #include <cstdint>
9 #include <type_traits>
10 
11 #include "cppgc/internal/write-barrier.h"
12 #include "cppgc/sentinel-pointer.h"
13 #include "cppgc/source-location.h"
14 #include "cppgc/type-traits.h"
15 #include "v8config.h" // NOLINT(build/include_directory)
16 
17 namespace cppgc {
18 namespace internal {
19 
20 class HeapBase;
21 class PersistentRegion;
22 class CrossThreadPersistentRegion;
23 
24 // Tags to distinguish between strong and weak member types.
25 class StrongMemberTag;
26 class WeakMemberTag;
27 class UntracedMemberTag;
28 
30  static void InitializingBarrier(const void*, const void*) {
31  // Since in initializing writes the source object is always white, having no
32  // barrier doesn't break the tri-color invariant.
33  }
34  static void AssigningBarrier(const void* slot, const void* value) {
35  WriteBarrier::Params params;
36  switch (WriteBarrier::GetWriteBarrierType(slot, value, params)) {
37  case WriteBarrier::Type::kGenerational:
38  WriteBarrier::GenerationalBarrier(params, slot);
39  break;
40  case WriteBarrier::Type::kMarking:
41  WriteBarrier::DijkstraMarkingBarrier(params, value);
42  break;
43  case WriteBarrier::Type::kNone:
44  break;
45  }
46  }
47 };
48 
50  static void InitializingBarrier(const void*, const void*) {}
51  static void AssigningBarrier(const void*, const void*) {}
52 };
53 
55  protected:
56  template <typename T>
57  void CheckPointer(const T* ptr) {
58  if (!ptr || (kSentinelPointer == ptr)) return;
59 
60  CheckPointersImplTrampoline<T>::Call(this, ptr);
61  }
62 
63  private:
64  void CheckPointerImpl(const void* ptr, bool points_to_payload);
65 
66  template <typename T, bool = IsCompleteV<T>>
67  struct CheckPointersImplTrampoline {
68  static void Call(EnabledCheckingPolicy* policy, const T* ptr) {
69  policy->CheckPointerImpl(ptr, false);
70  }
71  };
72 
73  template <typename T>
74  struct CheckPointersImplTrampoline<T, true> {
75  static void Call(EnabledCheckingPolicy* policy, const T* ptr) {
76  policy->CheckPointerImpl(ptr, IsGarbageCollectedTypeV<T>);
77  }
78  };
79 
80  const HeapBase* heap_ = nullptr;
81 };
82 
84  protected:
85  void CheckPointer(const void*) {}
86 };
87 
88 #if V8_ENABLE_CHECKS
91 #else
92 using DefaultMemberCheckingPolicy = DisabledCheckingPolicy;
93 using DefaultPersistentCheckingPolicy = DisabledCheckingPolicy;
94 #endif
95 // For CT(W)P neither marking information (for value), nor objectstart bitmap
96 // (for slot) are guaranteed to be present because there's no synchonization
97 // between heaps after marking.
98 using DefaultCrossThreadPersistentCheckingPolicy = DisabledCheckingPolicy;
99 
101  public:
102  constexpr const SourceLocation& Location() const { return location_; }
103 
104  protected:
105  constexpr KeepLocationPolicy() = default;
106  constexpr explicit KeepLocationPolicy(const SourceLocation& location)
107  : location_(location) {}
108 
109  // KeepLocationPolicy must not copy underlying source locations.
112 
113  // Location of the original moved from object should be preserved.
116 
117  private:
118  SourceLocation location_;
119 };
120 
122  public:
123  constexpr SourceLocation Location() const { return {}; }
124 
125  protected:
126  constexpr IgnoreLocationPolicy() = default;
127  constexpr explicit IgnoreLocationPolicy(const SourceLocation&) {}
128 };
129 
130 #if CPPGC_SUPPORTS_OBJECT_NAMES
132 #else
133 using DefaultLocationPolicy = IgnoreLocationPolicy;
134 #endif
135 
137  using IsStrongPersistent = std::true_type;
138  static V8_EXPORT PersistentRegion& GetPersistentRegion(const void* object);
139 };
140 
142  using IsStrongPersistent = std::false_type;
143  static V8_EXPORT PersistentRegion& GetPersistentRegion(const void* object);
144 };
145 
147  using IsStrongPersistent = std::true_type;
148  static V8_EXPORT CrossThreadPersistentRegion& GetPersistentRegion(
149  const void* object);
150 };
151 
153  using IsStrongPersistent = std::false_type;
154  static V8_EXPORT CrossThreadPersistentRegion& GetPersistentRegion(
155  const void* object);
156 };
157 
158 // Forward declarations setting up the default policies.
159 template <typename T, typename WeaknessPolicy,
160  typename LocationPolicy = DefaultLocationPolicy,
161  typename CheckingPolicy = DefaultCrossThreadPersistentCheckingPolicy>
163 template <typename T, typename WeaknessPolicy,
164  typename LocationPolicy = DefaultLocationPolicy,
165  typename CheckingPolicy = DefaultPersistentCheckingPolicy>
166 class BasicPersistent;
167 template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
168  typename CheckingPolicy = DefaultMemberCheckingPolicy>
169 class BasicMember;
170 
171 } // namespace internal
172 
173 } // namespace cppgc
174 
175 #endif // INCLUDE_CPPGC_INTERNAL_POINTER_POLICIES_H_