v8 11.3.244 (node 20.3.0)
V8 is Google's open source JavaScript engine
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 EnsureGCInfoIndexTraitDispatch<T>{}(registered_index);
30 }
31
32 private:
33 template <typename T, bool = std::is_polymorphic<T>::value,
36 struct EnsureGCInfoIndexTraitDispatch;
37
38 static void V8_PRESERVE_MOST
39 EnsureGCInfoIndexPolymorphic(std::atomic<GCInfoIndex>&, TraceCallback,
42 std::atomic<GCInfoIndex>&, TraceCallback, FinalizationCallback);
44 std::atomic<GCInfoIndex>&, TraceCallback, NameCallback);
45 static void V8_PRESERVE_MOST
46 EnsureGCInfoIndexPolymorphic(std::atomic<GCInfoIndex>&, TraceCallback);
47 static void V8_PRESERVE_MOST
48 EnsureGCInfoIndexNonPolymorphic(std::atomic<GCInfoIndex>&, TraceCallback,
51 std::atomic<GCInfoIndex>&, TraceCallback, FinalizationCallback);
53 std::atomic<GCInfoIndex>&, TraceCallback, NameCallback);
54 static void V8_PRESERVE_MOST
55 EnsureGCInfoIndexNonPolymorphic(std::atomic<GCInfoIndex>&, TraceCallback);
56};
57
58#define DISPATCH(is_polymorphic, has_finalizer, has_non_hidden_name, function) \
59 template <typename T> \
60 struct EnsureGCInfoIndexTrait::EnsureGCInfoIndexTraitDispatch< \
61 T, is_polymorphic, has_finalizer, has_non_hidden_name> { \
62 V8_INLINE void operator()(std::atomic<GCInfoIndex>& registered_index) { \
63 function; \
64 } \
65 };
66
67// --------------------------------------------------------------------- //
68// DISPATCH(is_polymorphic, has_finalizer, has_non_hidden_name, function)
69// --------------------------------------------------------------------- //
70DISPATCH(true, true, true, //
71 EnsureGCInfoIndexPolymorphic(registered_index, //
76 EnsureGCInfoIndexPolymorphic(registered_index, //
77 TraceTrait<T>::Trace, //
78 FinalizerTrait<T>::kCallback)) //
80 EnsureGCInfoIndexPolymorphic(registered_index, //
81 TraceTrait<T>::Trace, //
82 NameTrait<T>::GetName)) //
84 EnsureGCInfoIndexPolymorphic(registered_index, //
85 TraceTrait<T>::Trace)) //
88 TraceTrait<T>::Trace, //
89 FinalizerTrait<T>::kCallback, //
90 NameTrait<T>::GetName)) //
93 TraceTrait<T>::Trace, //
94 FinalizerTrait<T>::kCallback)) //
97 TraceTrait<T>::Trace, //
98 NameTrait<T>::GetName)) //
101 TraceTrait<T>::Trace)) //
102
103#undef DISPATCH
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>
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 // Folding would regresses name resolution when deriving names from C++
125 // class names as it would just folds a name to the base class name.
126 using ResultType = std::conditional_t<(kHasVirtualDestructorAtBase ||
130 ParentMostGarbageCollectedType, T>;
131};
132
133// Trait determines how the garbage collector treats objects wrt. to traversing,
134// finalization, and naming.
135template <typename T>
136struct GCInfoTrait final {
138 static_assert(sizeof(T), "T must be fully defined");
139 static std::atomic<GCInfoIndex>
140 registered_index; // Uses zero initialization.
141 GCInfoIndex index = registered_index.load(std::memory_order_acquire);
142 if (V8_UNLIKELY(!index)) {
143 EnsureGCInfoIndexTrait::EnsureIndex<T>(registered_index);
144 // Slow path call uses V8_PRESERVE_MOST which does not support return
145 // values (also preserves RAX). Avoid out parameter by just reloading the
146 // value here which at this point is guaranteed to be set.
147 index = registered_index.load(std::memory_order_acquire);
148 CPPGC_DCHECK(index != 0);
149 }
150 return index;
151 }
152};
153
154} // namespace internal
155} // namespace cppgc
156
157#endif // INCLUDE_CPPGC_INTERNAL_GC_INFO_H_
#define DISPATCH(is_polymorphic, has_finalizer, has_non_hidden_name, function)
Definition gc-info.h:58
#define CPPGC_DCHECK(condition)
Definition logging.h:36
void(*)(void *) FinalizationCallback
EnsureGCInfoIndexNonPolymorphic(registered_index, TraceTrait< T >::Trace, FinalizerTrait< T >::kCallback, NameTrait< T >::GetName)) DISPATCH(false
HeapObjectName(*)(const void *, HeapObjectNameForUnnamedObject) NameCallback
Definition name-trait.h:130
EnsureGCInfoIndexPolymorphic(registered_index, TraceTrait< T >::Trace, FinalizerTrait< T >::kCallback)) DISPATCH(true
uint16_t GCInfoIndex
Definition gc-info.h:21
void(*)(Visitor *visitor, const void *object) TraceCallback
Definition trace-trait.h:38
static V8_INLINE void EnsureIndex(std::atomic< GCInfoIndex > &registered_index)
Definition gc-info.h:27
static constexpr bool kHasCustomFinalizerDispatchAtBase
Definition gc-info.h:115
static constexpr bool kHasVirtualDestructorAtBase
Definition gc-info.h:110
static constexpr bool kWantsDetailedObjectNames
Definition gc-info.h:121
std::conditional_t<(kHasVirtualDestructorAtBase||kBothTypesAreTriviallyDestructible||kHasCustomFinalizerDispatchAtBase) &&!kWantsDetailedObjectNames, ParentMostGarbageCollectedType, T > ResultType
Definition gc-info.h:130
static constexpr bool kBothTypesAreTriviallyDestructible
Definition gc-info.h:112
static V8_INLINE GCInfoIndex Index()
Definition gc-info.h:137
#define V8_EXPORT
Definition v8config.h:719
#define V8_INLINE
Definition v8config.h:460
#define V8_UNLIKELY(condition)
Definition v8config.h:598
#define V8_PRESERVE_MOST
Definition v8config.h:536