2020-04-07 12:35:52 +00:00
|
|
|
// Copyright 2020 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef INCLUDE_CPPGC_VISITOR_H_
|
|
|
|
#define INCLUDE_CPPGC_VISITOR_H_
|
|
|
|
|
2020-10-23 12:55:45 +00:00
|
|
|
#include "cppgc/custom-space.h"
|
2020-10-15 09:03:15 +00:00
|
|
|
#include "cppgc/ephemeron-pair.h"
|
2020-04-20 18:19:29 +00:00
|
|
|
#include "cppgc/garbage-collected.h"
|
|
|
|
#include "cppgc/internal/logging.h"
|
|
|
|
#include "cppgc/internal/pointer-policies.h"
|
|
|
|
#include "cppgc/liveness-broker.h"
|
|
|
|
#include "cppgc/member.h"
|
2021-07-21 18:01:42 +00:00
|
|
|
#include "cppgc/sentinel-pointer.h"
|
2020-04-20 18:19:29 +00:00
|
|
|
#include "cppgc/source-location.h"
|
|
|
|
#include "cppgc/trace-trait.h"
|
2020-10-23 12:55:45 +00:00
|
|
|
#include "cppgc/type-traits.h"
|
2020-04-07 12:35:52 +00:00
|
|
|
|
|
|
|
namespace cppgc {
|
2020-06-08 15:26:04 +00:00
|
|
|
|
2020-04-07 12:35:52 +00:00
|
|
|
namespace internal {
|
2020-10-09 12:06:43 +00:00
|
|
|
template <typename T, typename WeaknessPolicy, typename LocationPolicy,
|
|
|
|
typename CheckingPolicy>
|
|
|
|
class BasicCrossThreadPersistent;
|
2020-06-08 15:26:04 +00:00
|
|
|
template <typename T, typename WeaknessPolicy, typename LocationPolicy,
|
|
|
|
typename CheckingPolicy>
|
|
|
|
class BasicPersistent;
|
2020-07-02 15:02:27 +00:00
|
|
|
class ConservativeTracingVisitor;
|
2020-04-07 12:35:52 +00:00
|
|
|
class VisitorBase;
|
2020-07-07 09:59:51 +00:00
|
|
|
class VisitorFactory;
|
2020-04-07 12:35:52 +00:00
|
|
|
} // namespace internal
|
|
|
|
|
2020-04-14 09:44:20 +00:00
|
|
|
using WeakCallback = void (*)(const LivenessBroker&, const void*);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Visitor passed to trace methods. All managed pointers must have called the
|
2020-06-08 15:26:04 +00:00
|
|
|
* Visitor's trace method on them.
|
|
|
|
*
|
|
|
|
* \code
|
|
|
|
* class Foo final : public GarbageCollected<Foo> {
|
|
|
|
* public:
|
|
|
|
* void Trace(Visitor* visitor) const {
|
|
|
|
* visitor->Trace(foo_);
|
|
|
|
* visitor->Trace(weak_foo_);
|
|
|
|
* }
|
|
|
|
* private:
|
|
|
|
* Member<Foo> foo_;
|
|
|
|
* WeakMember<Foo> weak_foo_;
|
|
|
|
* };
|
|
|
|
* \endcode
|
2020-04-14 09:44:20 +00:00
|
|
|
*/
|
2020-10-09 12:47:43 +00:00
|
|
|
class V8_EXPORT Visitor {
|
2020-04-07 12:35:52 +00:00
|
|
|
public:
|
2020-07-07 09:59:51 +00:00
|
|
|
class Key {
|
|
|
|
private:
|
|
|
|
Key() = default;
|
|
|
|
friend class internal::VisitorFactory;
|
|
|
|
};
|
|
|
|
|
|
|
|
explicit Visitor(Key) {}
|
|
|
|
|
2020-07-02 15:02:27 +00:00
|
|
|
virtual ~Visitor() = default;
|
|
|
|
|
2020-06-08 15:26:04 +00:00
|
|
|
/**
|
|
|
|
* Trace method for Member.
|
|
|
|
*
|
|
|
|
* \param member Member reference retaining an object.
|
|
|
|
*/
|
2020-04-07 12:35:52 +00:00
|
|
|
template <typename T>
|
|
|
|
void Trace(const Member<T>& member) {
|
|
|
|
const T* value = member.GetRawAtomic();
|
2020-04-14 15:44:08 +00:00
|
|
|
CPPGC_DCHECK(value != kSentinelPointer);
|
2022-07-19 17:36:03 +00:00
|
|
|
TraceImpl(value);
|
2020-04-07 12:35:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 15:26:04 +00:00
|
|
|
/**
|
|
|
|
* Trace method for WeakMember.
|
|
|
|
*
|
|
|
|
* \param weak_member WeakMember reference weakly retaining an object.
|
|
|
|
*/
|
2020-04-14 09:44:20 +00:00
|
|
|
template <typename T>
|
|
|
|
void Trace(const WeakMember<T>& weak_member) {
|
2020-05-13 08:47:25 +00:00
|
|
|
static_assert(sizeof(T), "Pointee type must be fully defined.");
|
2021-01-22 15:19:31 +00:00
|
|
|
static_assert(internal::IsGarbageCollectedOrMixinType<T>::value,
|
2020-06-08 15:26:04 +00:00
|
|
|
"T must be GarbageCollected or GarbageCollectedMixin type");
|
2020-10-23 12:55:45 +00:00
|
|
|
static_assert(!internal::IsAllocatedOnCompactableSpace<T>::value,
|
|
|
|
"Weak references to compactable objects are not allowed");
|
2020-04-14 09:44:20 +00:00
|
|
|
|
|
|
|
const T* value = weak_member.GetRawAtomic();
|
|
|
|
|
|
|
|
// Bailout assumes that WeakMember emits write barrier.
|
|
|
|
if (!value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-01 13:05:39 +00:00
|
|
|
CPPGC_DCHECK(value != kSentinelPointer);
|
2020-04-14 09:44:20 +00:00
|
|
|
VisitWeak(value, TraceTrait<T>::GetTraceDescriptor(value),
|
2020-04-15 13:37:22 +00:00
|
|
|
&HandleWeak<WeakMember<T>>, &weak_member);
|
|
|
|
}
|
|
|
|
|
2023-01-26 21:48:21 +00:00
|
|
|
#if defined(CPPGC_POINTER_COMPRESSION)
|
|
|
|
/**
|
|
|
|
* Trace method for UncompressedMember.
|
|
|
|
*
|
|
|
|
* \param member UncompressedMember reference retaining an object.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
void Trace(const subtle::UncompressedMember<T>& member) {
|
|
|
|
const T* value = member.GetRawAtomic();
|
|
|
|
CPPGC_DCHECK(value != kSentinelPointer);
|
|
|
|
TraceImpl(value);
|
|
|
|
}
|
|
|
|
#endif // defined(CPPGC_POINTER_COMPRESSION)
|
|
|
|
|
2020-06-08 15:26:04 +00:00
|
|
|
/**
|
|
|
|
* Trace method for inlined objects that are not allocated themselves but
|
|
|
|
* otherwise follow managed heap layout and have a Trace() method.
|
|
|
|
*
|
|
|
|
* \param object reference of the inlined object.
|
|
|
|
*/
|
2020-05-13 08:47:25 +00:00
|
|
|
template <typename T>
|
|
|
|
void Trace(const T& object) {
|
|
|
|
#if V8_ENABLE_CHECKS
|
|
|
|
// This object is embedded in potentially multiple nested objects. The
|
|
|
|
// outermost object must not be in construction as such objects are (a) not
|
|
|
|
// processed immediately, and (b) only processed conservatively if not
|
|
|
|
// otherwise possible.
|
|
|
|
CheckObjectNotInConstruction(&object);
|
|
|
|
#endif // V8_ENABLE_CHECKS
|
|
|
|
TraceTrait<T>::Trace(this, &object);
|
|
|
|
}
|
|
|
|
|
2020-06-08 15:26:04 +00:00
|
|
|
/**
|
|
|
|
* Registers a weak callback method on the object of type T. See
|
|
|
|
* LivenessBroker for an usage example.
|
|
|
|
*
|
|
|
|
* \param object of type T specifying a weak callback method.
|
|
|
|
*/
|
2020-04-16 09:42:19 +00:00
|
|
|
template <typename T, void (T::*method)(const LivenessBroker&)>
|
2020-06-08 15:26:04 +00:00
|
|
|
void RegisterWeakCallbackMethod(const T* object) {
|
|
|
|
RegisterWeakCallback(&WeakCallbackMethodDelegate<T, method>, object);
|
2020-04-16 09:42:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 09:03:15 +00:00
|
|
|
/**
|
|
|
|
* Trace method for EphemeronPair.
|
|
|
|
*
|
|
|
|
* \param ephemeron_pair EphemeronPair reference weakly retaining a key object
|
|
|
|
* and strongly retaining a value object in case the key object is alive.
|
|
|
|
*/
|
|
|
|
template <typename K, typename V>
|
|
|
|
void Trace(const EphemeronPair<K, V>& ephemeron_pair) {
|
2021-01-28 19:50:36 +00:00
|
|
|
TraceEphemeron(ephemeron_pair.key, &ephemeron_pair.value);
|
2021-02-12 15:57:42 +00:00
|
|
|
RegisterWeakCallbackMethod<EphemeronPair<K, V>,
|
|
|
|
&EphemeronPair<K, V>::ClearValueIfKeyIsDead>(
|
|
|
|
&ephemeron_pair);
|
2020-10-15 09:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-04 09:41:56 +00:00
|
|
|
* Trace method for a single ephemeron. Used for tracing a raw ephemeron in
|
|
|
|
* which the `key` and `value` are kept separately.
|
2020-10-15 09:03:15 +00:00
|
|
|
*
|
2021-03-04 09:41:56 +00:00
|
|
|
* \param weak_member_key WeakMember reference weakly retaining a key object.
|
|
|
|
* \param member_value Member reference with ephemeron semantics.
|
2020-10-15 09:03:15 +00:00
|
|
|
*/
|
2021-02-25 22:28:47 +00:00
|
|
|
template <typename KeyType, typename ValueType>
|
2021-03-04 09:41:56 +00:00
|
|
|
void TraceEphemeron(const WeakMember<KeyType>& weak_member_key,
|
|
|
|
const Member<ValueType>* member_value) {
|
|
|
|
const KeyType* key = weak_member_key.GetRawAtomic();
|
|
|
|
if (!key) return;
|
2021-03-02 19:13:33 +00:00
|
|
|
|
|
|
|
// `value` must always be non-null.
|
2021-03-04 09:41:56 +00:00
|
|
|
CPPGC_DCHECK(member_value);
|
|
|
|
const ValueType* value = member_value->GetRawAtomic();
|
|
|
|
if (!value) return;
|
2021-03-02 19:13:33 +00:00
|
|
|
|
2021-03-04 09:41:56 +00:00
|
|
|
// KeyType and ValueType may refer to GarbageCollectedMixin.
|
|
|
|
TraceDescriptor value_desc =
|
|
|
|
TraceTrait<ValueType>::GetTraceDescriptor(value);
|
|
|
|
CPPGC_DCHECK(value_desc.base_object_payload);
|
2021-03-02 19:13:33 +00:00
|
|
|
const void* key_base_object_payload =
|
2021-03-04 09:41:56 +00:00
|
|
|
TraceTrait<KeyType>::GetTraceDescriptor(key).base_object_payload;
|
2021-03-02 19:13:33 +00:00
|
|
|
CPPGC_DCHECK(key_base_object_payload);
|
|
|
|
|
|
|
|
VisitEphemeron(key_base_object_payload, value, value_desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-04 09:41:56 +00:00
|
|
|
* Trace method for a single ephemeron. Used for tracing a raw ephemeron in
|
|
|
|
* which the `key` and `value` are kept separately. Note that this overload
|
|
|
|
* is for non-GarbageCollected `value`s that can be traced though.
|
2021-03-02 19:13:33 +00:00
|
|
|
*
|
2021-03-04 09:41:56 +00:00
|
|
|
* \param key `WeakMember` reference weakly retaining a key object.
|
|
|
|
* \param value Reference weakly retaining a value object. Note that
|
|
|
|
* `ValueType` here should not be `Member`. It is expected that
|
|
|
|
* `TraceTrait<ValueType>::GetTraceDescriptor(value)` returns a
|
|
|
|
* `TraceDescriptor` with a null base pointer but a valid trace method.
|
2021-03-02 19:13:33 +00:00
|
|
|
*/
|
|
|
|
template <typename KeyType, typename ValueType>
|
2021-03-04 09:41:56 +00:00
|
|
|
void TraceEphemeron(const WeakMember<KeyType>& weak_member_key,
|
|
|
|
const ValueType* value) {
|
2021-03-02 19:13:33 +00:00
|
|
|
static_assert(!IsGarbageCollectedOrMixinTypeV<ValueType>,
|
|
|
|
"garbage-collected types must use WeakMember and Member");
|
2021-03-04 09:41:56 +00:00
|
|
|
const KeyType* key = weak_member_key.GetRawAtomic();
|
|
|
|
if (!key) return;
|
2021-03-02 19:13:33 +00:00
|
|
|
|
|
|
|
// `value` must always be non-null.
|
|
|
|
CPPGC_DCHECK(value);
|
2021-02-25 22:28:47 +00:00
|
|
|
TraceDescriptor value_desc =
|
|
|
|
TraceTrait<ValueType>::GetTraceDescriptor(value);
|
2021-03-02 19:13:33 +00:00
|
|
|
// `value_desc.base_object_payload` must be null as this override is only
|
|
|
|
// taken for non-garbage-collected values.
|
|
|
|
CPPGC_DCHECK(!value_desc.base_object_payload);
|
|
|
|
|
2021-02-25 22:28:47 +00:00
|
|
|
// KeyType might be a GarbageCollectedMixin.
|
|
|
|
const void* key_base_object_payload =
|
2021-03-04 09:41:56 +00:00
|
|
|
TraceTrait<KeyType>::GetTraceDescriptor(key).base_object_payload;
|
2021-02-25 22:28:47 +00:00
|
|
|
CPPGC_DCHECK(key_base_object_payload);
|
2021-03-04 09:41:56 +00:00
|
|
|
|
2021-02-25 22:28:47 +00:00
|
|
|
VisitEphemeron(key_base_object_payload, value, value_desc);
|
2020-10-15 09:03:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 08:55:19 +00:00
|
|
|
/**
|
|
|
|
* Trace method that strongifies a WeakMember.
|
|
|
|
*
|
|
|
|
* \param weak_member WeakMember reference retaining an object.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
void TraceStrongly(const WeakMember<T>& weak_member) {
|
|
|
|
const T* value = weak_member.GetRawAtomic();
|
|
|
|
CPPGC_DCHECK(value != kSentinelPointer);
|
2022-07-19 17:36:03 +00:00
|
|
|
TraceImpl(value);
|
2020-10-16 08:55:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 12:17:57 +00:00
|
|
|
/**
|
2022-07-21 09:10:40 +00:00
|
|
|
* Trace method for retaining containers strongly.
|
2020-10-22 12:17:57 +00:00
|
|
|
*
|
2022-07-21 09:10:40 +00:00
|
|
|
* \param object reference to the container.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
void TraceStrongContainer(const T* object) {
|
|
|
|
TraceImpl(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-12-16 09:30:03 +00:00
|
|
|
* Trace method for retaining containers weakly. Note that weak containers
|
|
|
|
* should emit write barriers.
|
2022-07-21 09:10:40 +00:00
|
|
|
*
|
|
|
|
* \param object reference to the container.
|
2020-10-22 12:17:57 +00:00
|
|
|
* \param callback to be invoked.
|
2022-07-21 09:10:40 +00:00
|
|
|
* \param callback_data custom data that is passed to the callback.
|
2020-10-22 12:17:57 +00:00
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
void TraceWeakContainer(const T* object, WeakCallback callback,
|
2022-07-21 09:10:40 +00:00
|
|
|
const void* callback_data) {
|
2020-10-22 12:17:57 +00:00
|
|
|
if (!object) return;
|
|
|
|
VisitWeakContainer(object, TraceTrait<T>::GetTraceDescriptor(object),
|
|
|
|
TraceTrait<T>::GetWeakTraceDescriptor(object), callback,
|
2022-07-21 09:10:40 +00:00
|
|
|
callback_data);
|
2020-10-22 12:17:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 12:55:45 +00:00
|
|
|
/**
|
|
|
|
* Registers a slot containing a reference to an object allocated on a
|
|
|
|
* compactable space. Such references maybe be arbitrarily moved by the GC.
|
|
|
|
*
|
|
|
|
* \param slot location of reference to object that might be moved by the GC.
|
2022-06-09 20:14:20 +00:00
|
|
|
* The slot must contain an uncompressed pointer.
|
2020-10-23 12:55:45 +00:00
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
void RegisterMovableReference(const T** slot) {
|
|
|
|
static_assert(internal::IsAllocatedOnCompactableSpace<T>::value,
|
|
|
|
"Only references to objects allocated on compactable spaces "
|
|
|
|
"should be registered as movable slots.");
|
2020-11-11 14:08:48 +00:00
|
|
|
static_assert(!IsGarbageCollectedMixinTypeV<T>,
|
2020-10-23 12:55:45 +00:00
|
|
|
"Mixin types do not support compaction.");
|
|
|
|
HandleMovableReference(reinterpret_cast<const void**>(slot));
|
|
|
|
}
|
|
|
|
|
2020-06-08 15:26:04 +00:00
|
|
|
/**
|
|
|
|
* Registers a weak callback that is invoked during garbage collection.
|
|
|
|
*
|
|
|
|
* \param callback to be invoked.
|
|
|
|
* \param data custom data that is passed to the callback.
|
|
|
|
*/
|
|
|
|
virtual void RegisterWeakCallback(WeakCallback callback, const void* data) {}
|
2020-04-16 09:42:19 +00:00
|
|
|
|
2020-10-09 12:47:43 +00:00
|
|
|
/**
|
|
|
|
* Defers tracing an object from a concurrent thread to the mutator thread.
|
|
|
|
* Should be called by Trace methods of types that are not safe to trace
|
|
|
|
* concurrently.
|
|
|
|
*
|
|
|
|
* \param parameter tells the trace callback which object was deferred.
|
|
|
|
* \param callback to be invoked for tracing on the mutator thread.
|
|
|
|
* \param deferred_size size of deferred object.
|
|
|
|
*
|
|
|
|
* \returns false if the object does not need to be deferred (i.e. currently
|
|
|
|
* traced on the mutator thread) and true otherwise (i.e. currently traced on
|
|
|
|
* a concurrent thread).
|
|
|
|
*/
|
|
|
|
virtual V8_WARN_UNUSED_RESULT bool DeferTraceToMutatorThreadIfConcurrent(
|
|
|
|
const void* parameter, TraceCallback callback, size_t deferred_size) {
|
|
|
|
// By default tracing is not deferred.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:35:52 +00:00
|
|
|
protected:
|
2020-04-14 09:44:20 +00:00
|
|
|
virtual void Visit(const void* self, TraceDescriptor) {}
|
|
|
|
virtual void VisitWeak(const void* self, TraceDescriptor, WeakCallback,
|
|
|
|
const void* weak_member) {}
|
2021-02-16 10:22:21 +00:00
|
|
|
virtual void VisitEphemeron(const void* key, const void* value,
|
|
|
|
TraceDescriptor value_desc) {}
|
2020-10-22 12:17:57 +00:00
|
|
|
virtual void VisitWeakContainer(const void* self, TraceDescriptor strong_desc,
|
|
|
|
TraceDescriptor weak_desc,
|
|
|
|
WeakCallback callback, const void* data) {}
|
2020-10-23 12:55:45 +00:00
|
|
|
virtual void HandleMovableReference(const void**) {}
|
2020-04-07 12:35:52 +00:00
|
|
|
|
|
|
|
private:
|
2020-04-16 09:42:19 +00:00
|
|
|
template <typename T, void (T::*method)(const LivenessBroker&)>
|
|
|
|
static void WeakCallbackMethodDelegate(const LivenessBroker& info,
|
|
|
|
const void* self) {
|
|
|
|
// Callback is registered through a potential const Trace method but needs
|
|
|
|
// to be able to modify fields. See HandleWeak.
|
|
|
|
(const_cast<T*>(static_cast<const T*>(self))->*method)(info);
|
|
|
|
}
|
|
|
|
|
2020-04-15 13:37:22 +00:00
|
|
|
template <typename PointerType>
|
|
|
|
static void HandleWeak(const LivenessBroker& info, const void* object) {
|
|
|
|
const PointerType* weak = static_cast<const PointerType*>(object);
|
2021-07-21 18:01:42 +00:00
|
|
|
auto* raw_ptr = weak->GetFromGC();
|
|
|
|
if (!info.IsHeapObjectAlive(raw_ptr)) {
|
2020-06-18 07:19:50 +00:00
|
|
|
weak->ClearFromGC();
|
2020-04-14 09:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 17:36:03 +00:00
|
|
|
template <typename T>
|
|
|
|
void TraceImpl(const T* t) {
|
|
|
|
static_assert(sizeof(T), "Pointee type must be fully defined.");
|
|
|
|
static_assert(internal::IsGarbageCollectedOrMixinType<T>::value,
|
|
|
|
"T must be GarbageCollected or GarbageCollectedMixin type");
|
|
|
|
if (!t) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Visit(t, TraceTrait<T>::GetTraceDescriptor(t));
|
|
|
|
}
|
|
|
|
|
2020-05-13 08:47:25 +00:00
|
|
|
#if V8_ENABLE_CHECKS
|
2020-10-09 12:47:43 +00:00
|
|
|
void CheckObjectNotInConstruction(const void* address);
|
2020-05-13 08:47:25 +00:00
|
|
|
#endif // V8_ENABLE_CHECKS
|
|
|
|
|
2020-10-09 12:06:43 +00:00
|
|
|
template <typename T, typename WeaknessPolicy, typename LocationPolicy,
|
|
|
|
typename CheckingPolicy>
|
|
|
|
friend class internal::BasicCrossThreadPersistent;
|
2020-06-08 15:26:04 +00:00
|
|
|
template <typename T, typename WeaknessPolicy, typename LocationPolicy,
|
|
|
|
typename CheckingPolicy>
|
|
|
|
friend class internal::BasicPersistent;
|
2020-07-02 15:02:27 +00:00
|
|
|
friend class internal::ConservativeTracingVisitor;
|
2020-07-07 09:59:51 +00:00
|
|
|
friend class internal::VisitorBase;
|
2020-04-07 12:35:52 +00:00
|
|
|
};
|
|
|
|
|
2022-08-01 10:48:31 +00:00
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
class V8_EXPORT RootVisitor {
|
|
|
|
public:
|
|
|
|
explicit RootVisitor(Visitor::Key) {}
|
|
|
|
|
|
|
|
virtual ~RootVisitor() = default;
|
|
|
|
|
|
|
|
template <typename AnyStrongPersistentType,
|
|
|
|
std::enable_if_t<
|
|
|
|
AnyStrongPersistentType::IsStrongPersistent::value>* = nullptr>
|
|
|
|
void Trace(const AnyStrongPersistentType& p) {
|
|
|
|
using PointeeType = typename AnyStrongPersistentType::PointeeType;
|
|
|
|
const void* object = Extract(p);
|
|
|
|
if (!object) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
VisitRoot(object, TraceTrait<PointeeType>::GetTraceDescriptor(object),
|
|
|
|
p.Location());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename AnyWeakPersistentType,
|
|
|
|
std::enable_if_t<
|
|
|
|
!AnyWeakPersistentType::IsStrongPersistent::value>* = nullptr>
|
|
|
|
void Trace(const AnyWeakPersistentType& p) {
|
|
|
|
using PointeeType = typename AnyWeakPersistentType::PointeeType;
|
|
|
|
static_assert(!internal::IsAllocatedOnCompactableSpace<PointeeType>::value,
|
|
|
|
"Weak references to compactable objects are not allowed");
|
|
|
|
const void* object = Extract(p);
|
|
|
|
if (!object) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
VisitWeakRoot(object, TraceTrait<PointeeType>::GetTraceDescriptor(object),
|
|
|
|
&HandleWeak<AnyWeakPersistentType>, &p, p.Location());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void VisitRoot(const void*, TraceDescriptor, const SourceLocation&) {}
|
|
|
|
virtual void VisitWeakRoot(const void* self, TraceDescriptor, WeakCallback,
|
|
|
|
const void* weak_root, const SourceLocation&) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename AnyPersistentType>
|
|
|
|
static const void* Extract(AnyPersistentType& p) {
|
|
|
|
using PointeeType = typename AnyPersistentType::PointeeType;
|
|
|
|
static_assert(sizeof(PointeeType),
|
|
|
|
"Persistent's pointee type must be fully defined");
|
|
|
|
static_assert(internal::IsGarbageCollectedOrMixinType<PointeeType>::value,
|
|
|
|
"Persistent's pointee type must be GarbageCollected or "
|
|
|
|
"GarbageCollectedMixin");
|
|
|
|
return p.GetFromGC();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename PointerType>
|
|
|
|
static void HandleWeak(const LivenessBroker& info, const void* object) {
|
|
|
|
const PointerType* weak = static_cast<const PointerType*>(object);
|
|
|
|
auto* raw_ptr = weak->GetFromGC();
|
|
|
|
if (!info.IsHeapObjectAlive(raw_ptr)) {
|
|
|
|
weak->ClearFromGC();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace internal
|
2020-04-07 12:35:52 +00:00
|
|
|
} // namespace cppgc
|
|
|
|
|
|
|
|
#endif // INCLUDE_CPPGC_VISITOR_H_
|