v8 10.2.154 (node 18.16.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
14#include "cppgc/trace-trait.h"
15#include "v8config.h" // NOLINT(build/include_directory)
16
17namespace cppgc {
18namespace internal {
19
20using GCInfoIndex = uint16_t;
21
23 // Acquires a new GC info object and returns the index. In addition, also
24 // updates `registered_index` atomically.
25 template <typename T>
27 std::atomic<GCInfoIndex>& registered_index) {
28 return EnsureGCInfoIndexTraitDispatch<T>{}(registered_index);
29 }
30
31 private:
32 template <typename T, bool = std::is_polymorphic<T>::value,
35 struct EnsureGCInfoIndexTraitDispatch;
36
37 static GCInfoIndex EnsureGCInfoIndexPolymorphic(std::atomic<GCInfoIndex>&,
41 static GCInfoIndex EnsureGCInfoIndexPolymorphic(std::atomic<GCInfoIndex>&,
44 static GCInfoIndex EnsureGCInfoIndexPolymorphic(std::atomic<GCInfoIndex>&,
46 static GCInfoIndex EnsureGCInfoIndexPolymorphic(std::atomic<GCInfoIndex>&,
48 static GCInfoIndex EnsureGCInfoIndexNonPolymorphic(std::atomic<GCInfoIndex>&,
51
53 static GCInfoIndex EnsureGCInfoIndexNonPolymorphic(std::atomic<GCInfoIndex>&,
56 static GCInfoIndex EnsureGCInfoIndexNonPolymorphic(std::atomic<GCInfoIndex>&,
59 static GCInfoIndex EnsureGCInfoIndexNonPolymorphic(std::atomic<GCInfoIndex>&,
61};
62
63#define DISPATCH(is_polymorphic, has_finalizer, has_non_hidden_name, function) \
64 template <typename T> \
65 struct EnsureGCInfoIndexTrait::EnsureGCInfoIndexTraitDispatch< \
66 T, is_polymorphic, has_finalizer, has_non_hidden_name> { \
67 V8_INLINE GCInfoIndex \
68 operator()(std::atomic<GCInfoIndex>& registered_index) { \
69 return function; \
70 } \
71 };
72
73// --------------------------------------------------------------------- //
74// DISPATCH(is_polymorphic, has_finalizer, has_non_hidden_name, function)
75// --------------------------------------------------------------------- //
76DISPATCH(true, true, true, //
77 EnsureGCInfoIndexPolymorphic(registered_index, //
82 EnsureGCInfoIndexPolymorphic(registered_index, //
83 TraceTrait<T>::Trace, //
84 FinalizerTrait<T>::kCallback)) //
86 EnsureGCInfoIndexPolymorphic(registered_index, //
87 TraceTrait<T>::Trace, //
88 NameTrait<T>::GetName)) //
90 EnsureGCInfoIndexPolymorphic(registered_index, //
91 TraceTrait<T>::Trace)) //
94 TraceTrait<T>::Trace, //
95 FinalizerTrait<T>::kCallback, //
96 NameTrait<T>::GetName)) //
99 TraceTrait<T>::Trace, //
100 FinalizerTrait<T>::kCallback)) //
103 TraceTrait<T>::Trace, //
104 NameTrait<T>::GetName)) //
107 TraceTrait<T>::Trace)) //
108
109#undef DISPATCH
110
111// Fold types based on finalizer behavior. Note that finalizer characteristics
112// align with trace behavior, i.e., destructors are virtual when trace methods
113// are and vice versa.
114template <typename T, typename ParentMostGarbageCollectedType>
116 static constexpr bool kHasVirtualDestructorAtBase =
117 std::has_virtual_destructor<ParentMostGarbageCollectedType>::value;
119 std::is_trivially_destructible<ParentMostGarbageCollectedType>::value &&
120 std::is_trivially_destructible<T>::value;
121 static constexpr bool kHasCustomFinalizerDispatchAtBase =
123 ParentMostGarbageCollectedType>::value;
124#ifdef CPPGC_SUPPORTS_OBJECT_NAMES
125 static constexpr bool kWantsDetailedObjectNames = true;
126#else // !CPPGC_SUPPORTS_OBJECT_NAMES
127 static constexpr bool kWantsDetailedObjectNames = false;
128#endif // !CPPGC_SUPPORTS_OBJECT_NAMES
129
130 // Folding would regresses name resolution when deriving names from C++
131 // class names as it would just folds a name to the base class name.
132 using ResultType = std::conditional_t<(kHasVirtualDestructorAtBase ||
136 ParentMostGarbageCollectedType, T>;
137};
138
139// Trait determines how the garbage collector treats objects wrt. to traversing,
140// finalization, and naming.
141template <typename T>
142struct GCInfoTrait final {
144 static_assert(sizeof(T), "T must be fully defined");
145 static std::atomic<GCInfoIndex>
146 registered_index; // Uses zero initialization.
147 const GCInfoIndex index = registered_index.load(std::memory_order_acquire);
148 return index ? index
149 : EnsureGCInfoIndexTrait::EnsureIndex<T>(registered_index);
150 }
151};
152
153} // namespace internal
154} // namespace cppgc
155
156#endif // INCLUDE_CPPGC_INTERNAL_GC_INFO_H_
#define DISPATCH(is_polymorphic, has_finalizer, has_non_hidden_name, function)
Definition gc-info.h:63
void(*)(void *) FinalizationCallback
HeapObjectName(*)(const void *) NameCallback
Definition name-trait.h:115
EnsureGCInfoIndexNonPolymorphic(registered_index, TraceTrait< T >::Trace, FinalizerTrait< T >::kCallback, NameTrait< T >::GetName)) DISPATCH(false
EnsureGCInfoIndexPolymorphic(registered_index, TraceTrait< T >::Trace, FinalizerTrait< T >::kCallback)) DISPATCH(true
uint16_t GCInfoIndex
Definition gc-info.h:20
void(*)(Visitor *visitor, const void *object) TraceCallback
Definition trace-trait.h:34
static V8_INLINE GCInfoIndex EnsureIndex(std::atomic< GCInfoIndex > &registered_index)
Definition gc-info.h:26
static constexpr bool kHasCustomFinalizerDispatchAtBase
Definition gc-info.h:121
static constexpr bool kHasVirtualDestructorAtBase
Definition gc-info.h:116
static constexpr bool kWantsDetailedObjectNames
Definition gc-info.h:127
std::conditional_t<(kHasVirtualDestructorAtBase||kBothTypesAreTriviallyDestructible||kHasCustomFinalizerDispatchAtBase) &&!kWantsDetailedObjectNames, ParentMostGarbageCollectedType, T > ResultType
Definition gc-info.h:136
static constexpr bool kBothTypesAreTriviallyDestructible
Definition gc-info.h:118
static V8_INLINE GCInfoIndex Index()
Definition gc-info.h:143
#define V8_EXPORT
Definition v8config.h:578
#define V8_INLINE
Definition v8config.h:425