5 #ifndef INCLUDE_CPPGC_VISITOR_H_
6 #define INCLUDE_CPPGC_VISITOR_H_
8 #include "cppgc/custom-space.h"
9 #include "cppgc/ephemeron-pair.h"
10 #include "cppgc/garbage-collected.h"
11 #include "cppgc/internal/logging.h"
12 #include "cppgc/internal/pointer-policies.h"
13 #include "cppgc/liveness-broker.h"
14 #include "cppgc/member.h"
15 #include "cppgc/sentinel-pointer.h"
16 #include "cppgc/source-location.h"
17 #include "cppgc/trace-trait.h"
18 #include "cppgc/type-traits.h"
23 template <
typename T,
typename WeaknessPolicy,
typename LocationPolicy,
24 typename CheckingPolicy>
26 template <
typename T,
typename WeaknessPolicy,
typename LocationPolicy,
27 typename CheckingPolicy>
29 class ConservativeTracingVisitor;
34 using WeakCallback =
void (*)(
const LivenessBroker&,
const void*);
72 static_assert(
sizeof(T),
"Pointee type must be fully defined.");
74 "T must be GarbageCollected or GarbageCollectedMixin type");
87 void Trace(
const Member<T>& member) {
88 const T* value = member.GetRawAtomic();
99 void Trace(
const WeakMember<T>& weak_member) {
100 static_assert(
sizeof(T),
"Pointee type must be fully defined.");
102 "T must be GarbageCollected or GarbageCollectedMixin type");
104 "Weak references to compactable objects are not allowed");
106 const T* value = weak_member.GetRawAtomic();
115 &HandleWeak<WeakMember<T>>
, &weak_member
);
124 template <
typename T>
131 CheckObjectNotInConstruction(&object);
142 template <
typename T,
void (T::*method)(
const LivenessBroker&)>
153 template <
typename K,
typename V>
155 TraceEphemeron(ephemeron_pair.key, &ephemeron_pair.value);
168 template <
typename KeyType,
typename ValueType>
170 const Member<ValueType>* member_value) {
171 const KeyType* key = weak_member_key.GetRawAtomic();
176 const ValueType* value = member_value->GetRawAtomic();
181 TraceTrait<ValueType>::GetTraceDescriptor(value);
183 const void* key_base_object_payload =
184 TraceTrait<KeyType>::GetTraceDescriptor(key).base_object_payload;
201 template <
typename KeyType,
typename ValueType>
203 const ValueType* value) {
204 static_assert(!IsGarbageCollectedOrMixinTypeV<ValueType>,
205 "garbage-collected types must use WeakMember and Member");
206 const KeyType* key = weak_member_key.GetRawAtomic();
212 TraceTrait<ValueType>::GetTraceDescriptor(value);
218 const void* key_base_object_payload =
219 TraceTrait<KeyType>::GetTraceDescriptor(key).base_object_payload;
230 template <
typename T>
232 const T* value = weak_member.GetRawAtomic();
244 template <
typename T>
249 TraceTrait<T>::GetWeakTraceDescriptor(object)
, callback
,
259 template <
typename T>
262 "Only references to objects allocated on compactable spaces "
263 "should be registered as movable slots.");
264 static_assert(!IsGarbageCollectedMixinTypeV<T>,
265 "Mixin types do not support compaction.");
291 const void* parameter, TraceCallback callback, size_t deferred_size) {
299 const void* weak_member) {}
307 WeakCallback callback,
const void* data) {}
311 template <
typename T,
void (T::*method)(
const LivenessBroker&)>
312 static void WeakCallbackMethodDelegate(
const LivenessBroker& info,
316 (
const_cast<T*>(
static_cast<
const T*>(self))->*method)(info);
319 template <
typename PointerType>
320 static void HandleWeak(
const LivenessBroker& info,
const void* object) {
321 const PointerType* weak =
static_cast<
const PointerType*>(object);
322 auto* raw_ptr = weak->GetFromGC();
325 if (!info.IsHeapObjectAlive(raw_ptr)) {
330 template <
typename Persistent,
331 std::enable_if_t<Persistent::IsStrongPersistent::value>* =
nullptr>
333 using PointeeType =
typename Persistent::PointeeType;
334 static_assert(
sizeof(PointeeType),
335 "Persistent's pointee type must be fully defined");
337 "Persistent's pointee type must be GarbageCollected or "
338 "GarbageCollectedMixin");
339 auto* ptr = p.GetFromGC();
347 typename WeakPersistent,
348 std::enable_if_t<!WeakPersistent::IsStrongPersistent::value>* =
nullptr>
349 void TraceRoot(
const WeakPersistent& p,
const SourceLocation& loc) {
350 using PointeeType =
typename WeakPersistent::PointeeType;
351 static_assert(
sizeof(PointeeType),
352 "Persistent's pointee type must be fully defined");
354 "Persistent's pointee type must be GarbageCollected or "
355 "GarbageCollectedMixin");
357 "Weak references to compactable objects are not allowed");
358 auto* ptr = p.GetFromGC();
360 &HandleWeak<WeakPersistent>
, &p
, loc
);
367 template <
typename T,
typename WeaknessPolicy,
typename LocationPolicy,
368 typename CheckingPolicy>
370 template <
typename T,
typename WeaknessPolicy,
typename LocationPolicy,
371 typename CheckingPolicy>