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#else // !CPPGC_SLIM_WRITE_BARRIER
49 const WriteBarrier::Type type =
50 WriteBarrier::GetWriteBarrierType(slot, value, params);
51 WriteBarrier(type, params, slot, value);
52#endif // !CPPGC_SLIM_WRITE_BARRIER
53 }
54
55 template <WriteBarrierSlotType SlotType>
56 V8_INLINE static void AssigningBarrier(const void* slot, RawPointer storage) {
57 static_assert(
59 "Assigning storages of Member and UncompressedMember is not supported");
60#ifdef CPPGC_SLIM_WRITE_BARRIER
62 WriteBarrier::CombinedWriteBarrierSlow<SlotType>(slot);
63#else // !CPPGC_SLIM_WRITE_BARRIER
65 const WriteBarrier::Type type =
66 WriteBarrier::GetWriteBarrierType(slot, storage, params);
67 WriteBarrier(type, params, slot, storage.Load());
68#endif // !CPPGC_SLIM_WRITE_BARRIER
69 }
70
71#if defined(CPPGC_POINTER_COMPRESSION)
72 template <WriteBarrierSlotType SlotType>
73 V8_INLINE static void AssigningBarrier(const void* slot,
74 CompressedPointer storage) {
75 static_assert(
77 "Assigning storages of Member and UncompressedMember is not supported");
78#ifdef CPPGC_SLIM_WRITE_BARRIER
80 WriteBarrier::CombinedWriteBarrierSlow<SlotType>(slot);
81#else // !CPPGC_SLIM_WRITE_BARRIER
83 const WriteBarrier::Type type =
84 WriteBarrier::GetWriteBarrierType(slot, storage, params);
85 WriteBarrier(type, params, slot, storage.Load());
86#endif // !CPPGC_SLIM_WRITE_BARRIER
87 }
88#endif // defined(CPPGC_POINTER_COMPRESSION)
89
90 private:
91 V8_INLINE static void WriteBarrier(WriteBarrier::Type type,
92 const WriteBarrier::Params& params,
93 const void* slot, const void* value) {
94 switch (type) {
98 break;
101 break;
103 break;
104 }
105 }
106};
107
109 V8_INLINE static void InitializingBarrier(const void*, const void*) {}
110 V8_INLINE static void InitializingBarrier(const void*, RawPointer storage) {}
111#if defined(CPPGC_POINTER_COMPRESSION)
112 V8_INLINE static void InitializingBarrier(const void*,
113 CompressedPointer storage) {}
114#endif
115 template <WriteBarrierSlotType>
116 V8_INLINE static void AssigningBarrier(const void*, const void*) {}
117 template <WriteBarrierSlotType, typename MemberStorage>
118 V8_INLINE static void AssigningBarrier(const void*, MemberStorage) {}
119};
120
122 protected:
123 void CheckPointerImpl(const void* ptr, bool points_to_payload,
124 bool check_off_heap_assignments);
125
126 const HeapBase* heap_ = nullptr;
127};
128
129template <bool kCheckOffHeapAssignments>
132 protected:
133 template <typename T>
135 if (raw_pointer.IsCleared() || raw_pointer.IsSentinel()) {
136 return;
137 }
138 CheckPointersImplTrampoline<T>::Call(
139 this, static_cast<const T*>(raw_pointer.Load()));
140 }
141#if defined(CPPGC_POINTER_COMPRESSION)
142 template <typename T>
143 V8_INLINE void CheckPointer(CompressedPointer compressed_pointer) {
144 if (compressed_pointer.IsCleared() || compressed_pointer.IsSentinel()) {
145 return;
146 }
147 CheckPointersImplTrampoline<T>::Call(
148 this, static_cast<const T*>(compressed_pointer.Load()));
149 }
150#endif
151 template <typename T>
152 void CheckPointer(const T* ptr) {
153 if (!ptr || (kSentinelPointer == ptr)) {
154 return;
155 }
156 CheckPointersImplTrampoline<T>::Call(this, ptr);
157 }
158
159 private:
160 template <typename T, bool = IsCompleteV<T>>
161 struct CheckPointersImplTrampoline {
162 static void Call(SameThreadEnabledCheckingPolicy* policy, const T* ptr) {
163 policy->CheckPointerImpl(ptr, false, kCheckOffHeapAssignments);
164 }
165 };
166
167 template <typename T>
168 struct CheckPointersImplTrampoline<T, true> {
169 static void Call(SameThreadEnabledCheckingPolicy* policy, const T* ptr) {
170 policy->CheckPointerImpl(ptr, IsGarbageCollectedTypeV<T>,
171 kCheckOffHeapAssignments);
172 }
173 };
174};
175
177 protected:
178 template <typename T>
180 template <typename T>
182#if defined(CPPGC_POINTER_COMPRESSION)
183 template <typename T>
184 V8_INLINE void CheckPointer(CompressedPointer) {}
185#endif
186};
187
188#ifdef CPPGC_ENABLE_SLOW_API_CHECKS
189// Off heap members are not connected to object graph and thus cannot ressurect
190// dead objects.
192 SameThreadEnabledCheckingPolicy<false /* kCheckOffHeapAssignments*/>;
194 SameThreadEnabledCheckingPolicy<true /* kCheckOffHeapAssignments*/>;
195#else // !CPPGC_ENABLE_SLOW_API_CHECKS
198#endif // !CPPGC_ENABLE_SLOW_API_CHECKS
199// For CT(W)P neither marking information (for value), nor objectstart bitmap
200// (for slot) are guaranteed to be present because there's no synchronization
201// between heaps after marking.
203
205 public:
206 constexpr const SourceLocation& Location() const { return location_; }
207
208 protected:
209 constexpr KeepLocationPolicy() = default;
210 constexpr explicit KeepLocationPolicy(const SourceLocation& location)
211 : location_(location) {}
212
213 // KeepLocationPolicy must not copy underlying source locations.
216
217 // Location of the original moved from object should be preserved.
220
221 private:
222 SourceLocation location_;
223};
224
226 public:
227 constexpr SourceLocation Location() const { return {}; }
228
229 protected:
230 constexpr IgnoreLocationPolicy() = default;
231 constexpr explicit IgnoreLocationPolicy(const SourceLocation&) {}
232};
233
234#if CPPGC_SUPPORTS_OBJECT_NAMES
235using DefaultLocationPolicy = KeepLocationPolicy;
236#else
238#endif
239
241 using IsStrongPersistent = std::true_type;
242 static V8_EXPORT PersistentRegion& GetPersistentRegion(const void* object);
243};
244
246 using IsStrongPersistent = std::false_type;
247 static V8_EXPORT PersistentRegion& GetPersistentRegion(const void* object);
248};
249
251 using IsStrongPersistent = std::true_type;
253 const void* object);
254};
255
257 using IsStrongPersistent = std::false_type;
259 const void* object);
260};
261
262// Forward declarations setting up the default policies.
263template <typename T, typename WeaknessPolicy,
264 typename LocationPolicy = DefaultLocationPolicy,
265 typename CheckingPolicy = DefaultCrossThreadPersistentCheckingPolicy>
267template <typename T, typename WeaknessPolicy,
268 typename LocationPolicy = DefaultLocationPolicy,
269 typename CheckingPolicy = DefaultPersistentCheckingPolicy>
270class BasicPersistent;
271template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
272 typename CheckingPolicy = DefaultMemberCheckingPolicy,
273 typename StorageType = DefaultMemberStorage>
274class BasicMember;
275
276} // namespace internal
277
278} // namespace cppgc
279
280#endif // INCLUDE_CPPGC_INTERNAL_POINTER_POLICIES_H_
Definition: cross-thread-persistent.h:74
Definition: member.h:77
Definition: persistent.h:54
Definition: persistent-node.h:185
Definition: pointer-policies.h:176
void CheckPointer(T *)
Definition: pointer-policies.h:179
void CheckPointer(RawPointer)
Definition: pointer-policies.h:181
Definition: pointer-policies.h:225
constexpr IgnoreLocationPolicy(const SourceLocation &)
Definition: pointer-policies.h:231
constexpr IgnoreLocationPolicy()=default
constexpr SourceLocation Location() const
Definition: pointer-policies.h:227
Definition: pointer-policies.h:204
constexpr KeepLocationPolicy()=default
constexpr const SourceLocation & Location() const
Definition: pointer-policies.h:206
KeepLocationPolicy & operator=(KeepLocationPolicy &&)=default
KeepLocationPolicy(KeepLocationPolicy &&)=default
constexpr KeepLocationPolicy(const SourceLocation &location)
Definition: pointer-policies.h:210
KeepLocationPolicy & operator=(const KeepLocationPolicy &)=delete
KeepLocationPolicy(const KeepLocationPolicy &)=delete
Definition: persistent-node.h:138
Definition: member-storage.h:203
const void * Load() const
Definition: member-storage.h:217
bool IsSentinel() const
Definition: member-storage.h:232
bool IsCleared() const
Definition: member-storage.h:230
void CheckPointerImpl(const void *ptr, bool points_to_payload, bool check_off_heap_assignments)
Definition: pointer-policies.h:131
void CheckPointer(RawPointer raw_pointer)
Definition: pointer-policies.h:134
void CheckPointer(const T *ptr)
Definition: pointer-policies.h:152
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:388
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:419
Definition: v8-source-location.h:31
IgnoreLocationPolicy DefaultLocationPolicy
Definition: pointer-policies.h:237
DisabledCheckingPolicy DefaultMemberCheckingPolicy
Definition: pointer-policies.h:196
DisabledCheckingPolicy DefaultPersistentCheckingPolicy
Definition: pointer-policies.h:197
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:56
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:108
static void InitializingBarrier(const void *, const void *)
Definition: pointer-policies.h:109
static void AssigningBarrier(const void *, MemberStorage)
Definition: pointer-policies.h:118
static void InitializingBarrier(const void *, RawPointer storage)
Definition: pointer-policies.h:110
static void AssigningBarrier(const void *, const void *)
Definition: pointer-policies.h:116
Definition: pointer-policies.h:250
static CrossThreadPersistentRegion & GetPersistentRegion(const void *object)
std::true_type IsStrongPersistent
Definition: pointer-policies.h:251
Definition: pointer-policies.h:240
static PersistentRegion & GetPersistentRegion(const void *object)
std::true_type IsStrongPersistent
Definition: pointer-policies.h:241
Definition: pointer-policies.h:256
static CrossThreadPersistentRegion & GetPersistentRegion(const void *object)
std::false_type IsStrongPersistent
Definition: pointer-policies.h:257
Definition: pointer-policies.h:245
std::false_type IsStrongPersistent
Definition: pointer-policies.h:246
static PersistentRegion & GetPersistentRegion(const void *object)
Definition: write-barrier.h:53
#define V8_EXPORT
Definition: v8config.h:793
#define V8_INLINE
Definition: v8config.h:499
#define V8_UNLIKELY(condition)
Definition: v8config.h:649