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.
|
|
|
|
|
2020-04-28 17:05:39 +00:00
|
|
|
#ifndef INCLUDE_CPPGC_TRACE_TRAIT_H_
|
|
|
|
#define INCLUDE_CPPGC_TRACE_TRAIT_H_
|
2020-04-07 12:35:52 +00:00
|
|
|
|
|
|
|
#include <type_traits>
|
2020-07-10 10:09:55 +00:00
|
|
|
|
2020-04-20 18:19:29 +00:00
|
|
|
#include "cppgc/type-traits.h"
|
2020-07-10 10:09:55 +00:00
|
|
|
#include "v8config.h" // NOLINT(build/include_directory)
|
2020-04-07 12:35:52 +00:00
|
|
|
|
|
|
|
namespace cppgc {
|
|
|
|
|
|
|
|
class Visitor;
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
2020-06-08 09:02:37 +00:00
|
|
|
// Implementation of the default TraceTrait handling GarbageCollected and
|
|
|
|
// GarbageCollectedMixin.
|
2020-04-07 12:35:52 +00:00
|
|
|
template <typename T,
|
|
|
|
bool =
|
|
|
|
IsGarbageCollectedMixinTypeV<typename std::remove_const<T>::type>>
|
|
|
|
struct TraceTraitImpl;
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
2020-06-08 09:02:37 +00:00
|
|
|
/**
|
|
|
|
* Callback for invoking tracing on a given object.
|
|
|
|
*
|
|
|
|
* \param visitor The visitor to dispatch to.
|
|
|
|
* \param object The object to invoke tracing on.
|
|
|
|
*/
|
|
|
|
using TraceCallback = void (*)(Visitor* visitor, const void* object);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Describes how to trace an object, i.e., how to visit all Oilpan-relevant
|
|
|
|
* fields of an object.
|
|
|
|
*/
|
2020-04-07 12:35:52 +00:00
|
|
|
struct TraceDescriptor {
|
2020-06-08 09:02:37 +00:00
|
|
|
/**
|
|
|
|
* Adjusted base pointer, i.e., the pointer to the class inheriting directly
|
|
|
|
* from GarbageCollected, of the object that is being traced.
|
|
|
|
*/
|
2020-04-07 12:35:52 +00:00
|
|
|
const void* base_object_payload;
|
2020-06-08 09:02:37 +00:00
|
|
|
/**
|
|
|
|
* Callback for tracing the object.
|
|
|
|
*/
|
2020-04-07 12:35:52 +00:00
|
|
|
TraceCallback callback;
|
|
|
|
};
|
|
|
|
|
2020-07-10 10:09:55 +00:00
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
struct V8_EXPORT TraceTraitFromInnerAddressImpl {
|
|
|
|
static TraceDescriptor GetTraceDescriptor(const void* address);
|
|
|
|
};
|
|
|
|
|
2020-06-08 09:02:37 +00:00
|
|
|
/**
|
|
|
|
* Trait specifying how the garbage collector processes an object of type T.
|
|
|
|
*
|
|
|
|
* Advanced users may override handling by creating a specialization for their
|
|
|
|
* type.
|
|
|
|
*/
|
2020-04-07 12:35:52 +00:00
|
|
|
template <typename T>
|
2020-10-08 18:01:07 +00:00
|
|
|
struct TraceTraitBase {
|
2020-04-07 12:35:52 +00:00
|
|
|
static_assert(internal::IsTraceableV<T>, "T must have a Trace() method");
|
|
|
|
|
2020-06-08 09:02:37 +00:00
|
|
|
/**
|
|
|
|
* Accessor for retrieving a TraceDescriptor to process an object of type T.
|
|
|
|
*
|
|
|
|
* \param self The object to be processed.
|
|
|
|
* \returns a TraceDescriptor to process the object.
|
|
|
|
*/
|
2020-04-07 12:35:52 +00:00
|
|
|
static TraceDescriptor GetTraceDescriptor(const void* self) {
|
|
|
|
return internal::TraceTraitImpl<T>::GetTraceDescriptor(
|
|
|
|
static_cast<const T*>(self));
|
|
|
|
}
|
|
|
|
|
2020-06-08 09:02:37 +00:00
|
|
|
/**
|
|
|
|
* Function invoking the tracing for an object of type T.
|
|
|
|
*
|
|
|
|
* \param visitor The visitor to dispatch to.
|
|
|
|
* \param self The object to invoke tracing on.
|
|
|
|
*/
|
2020-04-07 12:35:52 +00:00
|
|
|
static void Trace(Visitor* visitor, const void* self) {
|
2020-04-20 11:50:46 +00:00
|
|
|
static_cast<const T*>(self)->Trace(visitor);
|
2020-04-07 12:35:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-08 18:01:07 +00:00
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct TraceTrait : public internal::TraceTraitBase<T> {};
|
|
|
|
|
2020-04-07 12:35:52 +00:00
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct TraceTraitImpl<T, false> {
|
|
|
|
static TraceDescriptor GetTraceDescriptor(const void* self) {
|
|
|
|
return {self, TraceTrait<T>::Trace};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct TraceTraitImpl<T, true> {
|
|
|
|
static TraceDescriptor GetTraceDescriptor(const void* self) {
|
2020-07-10 10:09:55 +00:00
|
|
|
return internal::TraceTraitFromInnerAddressImpl::GetTraceDescriptor(self);
|
2020-04-07 12:35:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace cppgc
|
|
|
|
|
2020-04-28 17:05:39 +00:00
|
|
|
#endif // INCLUDE_CPPGC_TRACE_TRAIT_H_
|