Loading...
Searching...
No Matches
gc-info.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_GC_INFO_H_
6#define INCLUDE_CPPGC_INTERNAL_GC_INFO_H_
7
8#include <atomic>
9#include <cstdint>
10#include <type_traits>
11
15#include "cppgc/trace-trait.h"
16#include "v8config.h" // NOLINT(build/include_directory)
17
18namespace cppgc {
19namespace internal {
20
21using GCInfoIndex = uint16_t;
22
24 // Acquires a new GC info object and updates `registered_index` with the index
25 // that identifies that new info accordingly.
26 template <typename T>
28 std::atomic<GCInfoIndex>& registered_index) {
29 return EnsureGCInfoIndexTraitDispatch<T>{}(registered_index);
30 }
31
32 private:
33 template <typename T, bool = FinalizerTrait<T>::HasFinalizer(),
35 struct EnsureGCInfoIndexTraitDispatch;
36
38 EnsureGCInfoIndex(std::atomic<GCInfoIndex>&, TraceCallback,
40 static GCInfoIndex V8_PRESERVE_MOST EnsureGCInfoIndex(
41 std::atomic<GCInfoIndex>&, TraceCallback, FinalizationCallback);
43 EnsureGCInfoIndex(std::atomic<GCInfoIndex>&, TraceCallback, NameCallback);
45 EnsureGCInfoIndex(std::atomic<GCInfoIndex>&, TraceCallback);
46};
47
48#define DISPATCH(has_finalizer, has_non_hidden_name, function) \
49 template <typename T> \
50 struct EnsureGCInfoIndexTrait::EnsureGCInfoIndexTraitDispatch< \
51 T, has_finalizer, has_non_hidden_name> { \
52 V8_INLINE GCInfoIndex \
53 operator()(std::atomic<GCInfoIndex>& registered_index) { \
54 return function; \
55 } \
56 };
57
58// ------------------------------------------------------- //
59// DISPATCH(has_finalizer, has_non_hidden_name, function) //
60// ------------------------------------------------------- //
61DISPATCH(true, true, //
62 EnsureGCInfoIndex(registered_index, //
66DISPATCH(true, false, //
67 EnsureGCInfoIndex(registered_index, //
70DISPATCH(false, true, //
71 EnsureGCInfoIndex(registered_index, //
74DISPATCH(false, false, //
75 EnsureGCInfoIndex(registered_index, //
77
78#undef DISPATCH
79
80// Trait determines how the garbage collector treats objects wrt. to traversing,
81// finalization, and naming.
82template <typename T>
83struct GCInfoTrait final {
85 static_assert(sizeof(T), "T must be fully defined");
86 static std::atomic<GCInfoIndex>
87 registered_index; // Uses zero initialization.
88 GCInfoIndex index = registered_index.load(std::memory_order_acquire);
89 if (V8_UNLIKELY(!index)) {
90 index = EnsureGCInfoIndexTrait::EnsureIndex<T>(registered_index);
91 CPPGC_DCHECK(index != 0);
92 CPPGC_DCHECK(index == registered_index.load(std::memory_order_acquire));
93 }
94 return index;
95 }
96
97 static constexpr void CheckCallbacksAreDefined() {
98 // No USE() macro available.
99 (void)static_cast<TraceCallback>(TraceTrait<T>::Trace);
101 (void)static_cast<NameCallback>(NameTrait<T>::GetName);
102 }
103};
104
105// Fold types based on finalizer behavior. Note that finalizer characteristics
106// align with trace behavior, i.e., destructors are virtual when trace methods
107// are and vice versa.
108template <typename T, typename ParentMostGarbageCollectedType>
109struct GCInfoFolding final {
110 static constexpr bool kHasVirtualDestructorAtBase =
111 std::has_virtual_destructor<ParentMostGarbageCollectedType>::value;
113 std::is_trivially_destructible<ParentMostGarbageCollectedType>::value &&
114 std::is_trivially_destructible<T>::value;
115 static constexpr bool kHasCustomFinalizerDispatchAtBase =
117 ParentMostGarbageCollectedType>::value;
118#ifdef CPPGC_SUPPORTS_OBJECT_NAMES
119 static constexpr bool kWantsDetailedObjectNames = true;
120#else // !CPPGC_SUPPORTS_OBJECT_NAMES
121 static constexpr bool kWantsDetailedObjectNames = false;
122#endif // !CPPGC_SUPPORTS_OBJECT_NAMES
123
124 // Always true. Forces the compiler to resolve callbacks which ensures that
125 // both modes don't break without requiring compiling a separate
126 // configuration. Only a single GCInfo (for `ResultType` below) will actually
127 // be instantiated but existence (and well-formedness) of all callbacks is
128 // checked.
129 static constexpr bool WantToFold() {
130 if constexpr ((kHasVirtualDestructorAtBase ||
136 return true;
137 }
138 return false;
139 }
140
141 // Folding would regress name resolution when deriving names from C++
142 // class names as it would just folds a name to the base class name.
144 std::conditional_t<WantToFold(), ParentMostGarbageCollectedType, T>;
145};
146
147} // namespace internal
148} // namespace cppgc
149
150#endif // INCLUDE_CPPGC_INTERNAL_GC_INFO_H_
Definition: name-trait.h:75
#define DISPATCH(has_finalizer, has_non_hidden_name, function)
Definition: gc-info.h:48
#define CPPGC_DCHECK(condition)
Definition: logging.h:36
uint16_t GCInfoIndex
Definition: gc-info.h:21
HeapObjectName(*)(const void *, HeapObjectNameForUnnamedObject) NameCallback
Definition: name-trait.h:134
void(*)(void *) FinalizationCallback
Definition: finalizer-trait.h:15
Definition: allocation.h:38
void(*)(Visitor *visitor, const void *object) TraceCallback
Definition: trace-trait.h:38
Definition: trace-trait.h:105
static GCInfoIndex EnsureIndex(std::atomic< GCInfoIndex > &registered_index)
Definition: gc-info.h:27
Definition: finalizer-trait.h:66
static constexpr FinalizationCallback kCallback
Definition: finalizer-trait.h:83
Definition: gc-info.h:109
static constexpr bool kBothTypesAreTriviallyDestructible
Definition: gc-info.h:112
static constexpr bool kHasVirtualDestructorAtBase
Definition: gc-info.h:110
static constexpr bool kHasCustomFinalizerDispatchAtBase
Definition: gc-info.h:115
static constexpr bool kWantsDetailedObjectNames
Definition: gc-info.h:121
static constexpr bool WantToFold()
Definition: gc-info.h:129
std::conditional_t< WantToFold(), ParentMostGarbageCollectedType, T > ResultType
Definition: gc-info.h:144
Definition: gc-info.h:83
static GCInfoIndex Index()
Definition: gc-info.h:84
static constexpr void CheckCallbacksAreDefined()
Definition: gc-info.h:97
static void Trace(Visitor *visitor, const void *self)
Definition: trace-trait.h:97
#define V8_EXPORT
Definition: v8config.h:793
#define V8_INLINE
Definition: v8config.h:499
#define V8_UNLIKELY(condition)
Definition: v8config.h:649
#define V8_PRESERVE_MOST
Definition: v8config.h:587