Loading...
Searching...
No Matches
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
15#include "cppgc/type-traits.h"
16#include "v8config.h" // NOLINT(build/include_directory)
17
18namespace cppgc {
19namespace internal {
20
21class HeapBase;
22class PersistentRegion;
23class CrossThreadPersistentRegion;
24
25// Tags to distinguish between strong and weak member types.
26class StrongMemberTag;
27class WeakMemberTag;
28class UntracedMemberTag;
29
31 // Since in initializing writes the source object is always white, having no
32 // barrier doesn't break the tri-color invariant.
33 V8_INLINE static void InitializingBarrier(const void*, const void*) {}
34 V8_INLINE static void InitializingBarrier(const void*, RawPointer storage) {
35 }
36#if defined(CPPGC_POINTER_COMPRESSION)
37 V8_INLINE static void InitializingBarrier(const void*,
38 CompressedPointer storage) {}
39#endif
40
41 template <WriteBarrierSlotType SlotType>
42 V8_INLINE static void AssigningBarrier(const void* slot,
43 const void* value) {
44#ifdef CPPGC_SLIM_WRITE_BARRIER
46 WriteBarrier::CombinedWriteBarrierSlow<SlotType>(slot);
47 }
48#else // !CPPGC_SLIM_WRITE_BARRIER
50 const WriteBarrier::Type type =
51 WriteBarrier::GetWriteBarrierType(slot, value, params);
52 WriteBarrier(type, params, slot, value);
53#endif // !CPPGC_SLIM_WRITE_BARRIER
54 }
55
56 template <WriteBarrierSlotType SlotType>
57 V8_INLINE static void AssigningBarrier(const void* slot, RawPointer storage) {
58 static_assert(
60 "Assigning storages of Member and UncompressedMember is not supported");
61#ifdef CPPGC_SLIM_WRITE_BARRIER
63 WriteBarrier::CombinedWriteBarrierSlow<SlotType>(slot);
64 }
65#else // !CPPGC_SLIM_WRITE_BARRIER
67 const WriteBarrier::Type type =
68 WriteBarrier::GetWriteBarrierType(slot, storage, params);
69 WriteBarrier(type, params, slot, storage.Load());
70#endif // !CPPGC_SLIM_WRITE_BARRIER
71 }
72
73#if defined(CPPGC_POINTER_COMPRESSION)
74 template <WriteBarrierSlotType SlotType>
75 V8_INLINE static void AssigningBarrier(const void* slot,
76 CompressedPointer storage) {
77 static_assert(
79 "Assigning storages of Member and UncompressedMember is not supported");
80#ifdef CPPGC_SLIM_WRITE_BARRIER
82 WriteBarrier::CombinedWriteBarrierSlow<SlotType>(slot);
83 }
84#else // !CPPGC_SLIM_WRITE_BARRIER
85 WriteBarrier::Params params;
86 const WriteBarrier::Type type =
87 WriteBarrier::GetWriteBarrierType(slot, storage, params);
88 WriteBarrier(type, params, slot, storage.Load());
89#endif // !CPPGC_SLIM_WRITE_BARRIER
90 }
91#endif // defined(CPPGC_POINTER_COMPRESSION)
92
93 private:
94 V8_INLINE static void WriteBarrier(WriteBarrier::Type type,
95 const WriteBarrier::Params& params,
96 const void* slot, const void* value) {
97 switch (type) {
101 break;
104 break;
106 break;
107 }
108 }
109};
110
112 V8_INLINE static void InitializingBarrier(const void*, const void*) {}
113 V8_INLINE static void InitializingBarrier(const void*, RawPointer storage) {}
114#if defined(CPPGC_POINTER_COMPRESSION)
115 V8_INLINE static void InitializingBarrier(const void*,
116 CompressedPointer storage) {}
117#endif
118 template <WriteBarrierSlotType>
119 V8_INLINE static void AssigningBarrier(const void*, const void*) {}
120 template <WriteBarrierSlotType, typename MemberStorage>
121 V8_INLINE static void AssigningBarrier(const void*, MemberStorage) {}
122};
123
125 protected:
126 void CheckPointerImpl(const void* ptr, bool points_to_payload,
127 bool check_off_heap_assignments);
128
129 const HeapBase* heap_ = nullptr;
130};
131
132template <bool kCheckOffHeapAssignments>
135 protected:
136 template <typename T>
138 if (raw_pointer.IsCleared() || raw_pointer.IsSentinel()) {
139 return;
140 }
141 CheckPointersImplTrampoline<T>::Call(
142 this, static_cast<const T*>(raw_pointer.Load()));
143 }
144#if defined(CPPGC_POINTER_COMPRESSION)
145 template <typename T>
146 V8_INLINE void CheckPointer(CompressedPointer compressed_pointer) {
147 if (compressed_pointer.IsCleared() || compressed_pointer.IsSentinel()) {
148 return;
149 }
150 CheckPointersImplTrampoline<T>::Call(
151 this, static_cast<const T*>(compressed_pointer.Load()));
152 }
153#endif
154 template <typename T>
155 void CheckPointer(const T* ptr) {
156 if (!ptr || (kSentinelPointer == ptr)) {
157 return;
158 }
159 CheckPointersImplTrampoline<T>::Call(this, ptr);
160 }
161
162 private:
163 template <typename T, bool = IsCompleteV<T>>
164 struct CheckPointersImplTrampoline {
165 static void Call(SameThreadEnabledCheckingPolicy* policy, const T* ptr) {
166 policy->CheckPointerImpl(ptr, false, kCheckOffHeapAssignments);
167 }
168 };
169
170 template <typename T>
171 struct CheckPointersImplTrampoline<T, true> {
172 static void Call(SameThreadEnabledCheckingPolicy* policy, const T* ptr) {
173 policy->CheckPointerImpl(ptr, IsGarbageCollectedTypeV<T>,
174 kCheckOffHeapAssignments);
175 }
176 };
177};
178
180 protected:
181 template <typename T>
183 template <typename T>
185#if defined(CPPGC_POINTER_COMPRESSION)
186 template <typename T>
187 V8_INLINE void CheckPointer(CompressedPointer) {}
188#endif
189};
190
191#ifdef CPPGC_ENABLE_SLOW_API_CHECKS
192// Off heap members are not connected to object graph and thus cannot ressurect
193// dead objects.
195 SameThreadEnabledCheckingPolicy<false /* kCheckOffHeapAssignments*/>;
197 SameThreadEnabledCheckingPolicy<true /* kCheckOffHeapAssignments*/>;
198#else // !CPPGC_ENABLE_SLOW_API_CHECKS
201#endif // !CPPGC_ENABLE_SLOW_API_CHECKS
202// For CT(W)P neither marking information (for value), nor objectstart bitmap
203// (for slot) are guaranteed to be present because there's no synchronization
204// between heaps after marking.
206
208 public:
209 constexpr SourceLocation Location() const { return location_; }
210
211 protected:
212 constexpr KeepLocationPolicy() = default;
213 constexpr explicit KeepLocationPolicy(SourceLocation location)
214 : location_(location) {}
215
216 // KeepLocationPolicy must not copy underlying source locations.
219
220 // Location of the original moved from object should be preserved.
223
224 private:
225 SourceLocation location_;
226};
227
229 public:
230 constexpr SourceLocation Location() const { return {}; }
231
232 protected:
233 constexpr IgnoreLocationPolicy() = default;
235};
236
237#if CPPGC_SUPPORTS_OBJECT_NAMES
238using DefaultLocationPolicy = KeepLocationPolicy;
239#else
241#endif
242
244 using IsStrongPersistent = std::true_type;
245 static V8_EXPORT PersistentRegion& GetPersistentRegion(const void* object);
246};
247
249 using IsStrongPersistent = std::false_type;
250 static V8_EXPORT PersistentRegion& GetPersistentRegion(const void* object);
251};
252
254 using IsStrongPersistent = std::true_type;
256 const void* object);
257};
258
260 using IsStrongPersistent = std::false_type;
262 const void* object);
263};
264
265// Forward declarations setting up the default policies.
266template <typename T, typename WeaknessPolicy,
267 typename LocationPolicy = DefaultLocationPolicy,
268 typename CheckingPolicy = DefaultCrossThreadPersistentCheckingPolicy>
270template <typename T, typename WeaknessPolicy,
271 typename LocationPolicy = DefaultLocationPolicy,
272 typename CheckingPolicy = DefaultPersistentCheckingPolicy>
273class BasicPersistent;
274template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
275 typename CheckingPolicy = DefaultMemberCheckingPolicy,
276 typename StorageType = DefaultMemberStorage>
277class BasicMember;
278
279} // namespace internal
280
281} // namespace cppgc
282
283#endif // INCLUDE_CPPGC_INTERNAL_POINTER_POLICIES_H_
Definition: cross-thread-persistent.h:74
Definition: member.h:79
Definition: persistent.h:54
Definition: persistent-node.h:185
Definition: pointer-policies.h:179
void CheckPointer(T *)
Definition: pointer-policies.h:182
void CheckPointer(RawPointer)
Definition: pointer-policies.h:184
Definition: pointer-policies.h:228
constexpr IgnoreLocationPolicy()=default
constexpr IgnoreLocationPolicy(SourceLocation)
Definition: pointer-policies.h:234
constexpr SourceLocation Location() const
Definition: pointer-policies.h:230
Definition: pointer-policies.h:207
constexpr KeepLocationPolicy()=default
KeepLocationPolicy & operator=(KeepLocationPolicy &&)=default
constexpr SourceLocation Location() const
Definition: pointer-policies.h:209
KeepLocationPolicy(KeepLocationPolicy &&)=default
constexpr KeepLocationPolicy(SourceLocation location)
Definition: pointer-policies.h:213
KeepLocationPolicy & operator=(const KeepLocationPolicy &)=delete
KeepLocationPolicy(const KeepLocationPolicy &)=delete
Definition: persistent-node.h:138
Definition: member-storage.h:228
const void * Load() const
Definition: member-storage.h:242
bool IsSentinel() const
Definition: member-storage.h:257
bool IsCleared() const
Definition: member-storage.h:255
void CheckPointerImpl(const void *ptr, bool points_to_payload, bool check_off_heap_assignments)
Definition: pointer-policies.h:134
void CheckPointer(RawPointer raw_pointer)
Definition: pointer-policies.h:137
void CheckPointer(const T *ptr)
Definition: pointer-policies.h:155
Definition: write-barrier.h:39
Type
Definition: write-barrier.h:41
static Type GetWriteBarrierType(const void *slot, const void *value, Params &params)
Definition: write-barrier.h:393
static void GenerationalBarrier(const Params &params, const void *slot)
Definition: write-barrier.h:105
static bool IsEnabled()
Definition: write-barrier.h:118
static void DijkstraMarkingBarrier(const Params &params, const void *object)
Definition: write-barrier.h:424
Definition: v8-source-location.h:22
IgnoreLocationPolicy DefaultLocationPolicy
Definition: pointer-policies.h:240
DisabledCheckingPolicy DefaultMemberCheckingPolicy
Definition: pointer-policies.h:199
DisabledCheckingPolicy DefaultPersistentCheckingPolicy
Definition: pointer-policies.h:200
Definition: allocation.h:38
constexpr internal::SentinelPointer kSentinelPointer
Definition: sentinel-pointer.h:35
Definition: pointer-policies.h:30
static void AssigningBarrier(const void *slot, const void *value)
Definition: pointer-policies.h:42
static void AssigningBarrier(const void *slot, RawPointer storage)
Definition: pointer-policies.h:57
static void InitializingBarrier(const void *, const void *)
Definition: pointer-policies.h:33
static void InitializingBarrier(const void *, RawPointer storage)
Definition: pointer-policies.h:34
Definition: pointer-policies.h:111
static void InitializingBarrier(const void *, const void *)
Definition: pointer-policies.h:112
static void AssigningBarrier(const void *, MemberStorage)
Definition: pointer-policies.h:121
static void InitializingBarrier(const void *, RawPointer storage)
Definition: pointer-policies.h:113
static void AssigningBarrier(const void *, const void *)
Definition: pointer-policies.h:119
Definition: pointer-policies.h:253
static CrossThreadPersistentRegion & GetPersistentRegion(const void *object)
std::true_type IsStrongPersistent
Definition: pointer-policies.h:254
Definition: pointer-policies.h:243
static PersistentRegion & GetPersistentRegion(const void *object)
std::true_type IsStrongPersistent
Definition: pointer-policies.h:244
Definition: pointer-policies.h:259
static CrossThreadPersistentRegion & GetPersistentRegion(const void *object)
std::false_type IsStrongPersistent
Definition: pointer-policies.h:260
Definition: pointer-policies.h:248
std::false_type IsStrongPersistent
Definition: pointer-policies.h:249
static PersistentRegion & GetPersistentRegion(const void *object)
Definition: write-barrier.h:53
#define V8_EXPORT
Definition: v8config.h:854
#define V8_INLINE
Definition: v8config.h:508
#define V8_UNLIKELY(condition)
Definition: v8config.h:667