v8  9.4.146 (node 16.13.0)
V8 is Google's open source JavaScript engine
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 
12 #include "cppgc/internal/finalizer-trait.h"
13 #include "cppgc/internal/name-trait.h"
14 #include "cppgc/trace-trait.h"
15 #include "v8config.h" // NOLINT(build/include_directory)
16 
17 namespace cppgc {
18 namespace internal {
19 
20 using GCInfoIndex = uint16_t;
21 
22 // Acquires a new GC info object and returns the index. In addition, also
23 // updates `registered_index` atomically.
24 V8_EXPORT GCInfoIndex
25 EnsureGCInfoIndex(std::atomic<GCInfoIndex>& registered_index,
26  FinalizationCallback, TraceCallback, NameCallback, bool);
27 
28 // Fold types based on finalizer behavior. Note that finalizer characteristics
29 // align with trace behavior, i.e., destructors are virtual when trace methods
30 // are and vice versa.
31 template <typename T, typename ParentMostGarbageCollectedType>
32 struct GCInfoFolding {
33  static constexpr bool kHasVirtualDestructorAtBase =
34  std::has_virtual_destructor<ParentMostGarbageCollectedType>::value;
35  static constexpr bool kBothTypesAreTriviallyDestructible =
36  std::is_trivially_destructible<ParentMostGarbageCollectedType>::value &&
37  std::is_trivially_destructible<T>::value;
38  static constexpr bool kHasCustomFinalizerDispatchAtBase =
40  ParentMostGarbageCollectedType>::value;
41 #ifdef CPPGC_SUPPORTS_OBJECT_NAMES
42  static constexpr bool kWantsDetailedObjectNames = true;
43 #else // !CPPGC_SUPPORTS_OBJECT_NAMES
44  static constexpr bool kWantsDetailedObjectNames = false;
45 #endif // !CPPGC_SUPPORTS_OBJECT_NAMES
46 
47  // Folding would regresses name resolution when deriving names from C++
48  // class names as it would just folds a name to the base class name.
49  using ResultType = std::conditional_t<(kHasVirtualDestructorAtBase ||
53  ParentMostGarbageCollectedType, T>;
54 };
55 
56 // Trait determines how the garbage collector treats objects wrt. to traversing,
57 // finalization, and naming.
58 template <typename T>
59 struct GCInfoTrait final {
60  static GCInfoIndex Index() {
61  static_assert(sizeof(T), "T must be fully defined");
62  static std::atomic<GCInfoIndex>
63  registered_index; // Uses zero initialization.
64  const GCInfoIndex index = registered_index.load(std::memory_order_acquire);
65  return index ? index
66  : EnsureGCInfoIndex(
67  registered_index, FinalizerTrait<T>::kCallback,
68  TraceTrait<T>::Trace, NameTrait<T>::GetName,
69  std::is_polymorphic<T>::value);
70  }
71 };
72 
73 } // namespace internal
74 } // namespace cppgc
75 
76 #endif // INCLUDE_CPPGC_INTERNAL_GC_INFO_H_