v8  9.4.146 (node 16.13.0)
V8 is Google's open source JavaScript engine
garbage-collected.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_GARBAGE_COLLECTED_H_
6 #define INCLUDE_CPPGC_GARBAGE_COLLECTED_H_
7 
8 #include <type_traits>
9 
10 #include "cppgc/internal/api-constants.h"
11 #include "cppgc/platform.h"
12 #include "cppgc/trace-trait.h"
13 #include "cppgc/type-traits.h"
14 
15 namespace cppgc {
16 
17 class Visitor;
18 
19 namespace internal {
20 
22  public:
23  // Must use MakeGarbageCollected.
24  void* operator new(size_t) = delete;
25  void* operator new[](size_t) = delete;
26  // The garbage collector is taking care of reclaiming the object. Also,
27  // virtual destructor requires an unambiguous, accessible 'operator delete'.
28  void operator delete(void*) {
29 #ifdef V8_ENABLE_CHECKS
30  internal::Abort();
31 #endif // V8_ENABLE_CHECKS
32  }
33  void operator delete[](void*) = delete;
34 
35  protected:
36  GarbageCollectedBase() = default;
37 };
38 
39 } // namespace internal
40 
41 /**
42  * Base class for managed objects. Only descendent types of `GarbageCollected`
43  * can be constructed using `MakeGarbageCollected()`. Must be inherited from as
44  * left-most base class.
45  *
46  * Types inheriting from GarbageCollected must provide a method of
47  * signature `void Trace(cppgc::Visitor*) const` that dispatchs all managed
48  * pointers to the visitor and delegates to garbage-collected base classes.
49  * The method must be virtual if the type is not directly a child of
50  * GarbageCollected and marked as final.
51  *
52  * \code
53  * // Example using final class.
54  * class FinalType final : public GarbageCollected<FinalType> {
55  * public:
56  * void Trace(cppgc::Visitor* visitor) const {
57  * // Dispatch using visitor->Trace(...);
58  * }
59  * };
60  *
61  * // Example using non-final base class.
62  * class NonFinalBase : public GarbageCollected<NonFinalBase> {
63  * public:
64  * virtual void Trace(cppgc::Visitor*) const {}
65  * };
66  *
67  * class FinalChild final : public NonFinalBase {
68  * public:
69  * void Trace(cppgc::Visitor* visitor) const final {
70  * // Dispatch using visitor->Trace(...);
71  * NonFinalBase::Trace(visitor);
72  * }
73  * };
74  * \endcode
75  */
76 template <typename T>
78  public:
79  using IsGarbageCollectedTypeMarker = void;
80  using ParentMostGarbageCollectedType = T;
81 
82  protected:
83  GarbageCollected() = default;
84 };
85 
86 /**
87  * Base class for managed mixin objects. Such objects cannot be constructed
88  * directly but must be mixed into the inheritance hierarchy of a
89  * GarbageCollected object.
90  *
91  * Types inheriting from GarbageCollectedMixin must override a virtual method
92  * of signature `void Trace(cppgc::Visitor*) const` that dispatchs all managed
93  * pointers to the visitor and delegates to base classes.
94  *
95  * \code
96  * class Mixin : public GarbageCollectedMixin {
97  * public:
98  * void Trace(cppgc::Visitor* visitor) const override {
99  * // Dispatch using visitor->Trace(...);
100  * }
101  * };
102  * \endcode
103  */
105  public:
106  using IsGarbageCollectedMixinTypeMarker = void;
107 
108  /**
109  * This Trace method must be overriden by objects inheriting from
110  * GarbageCollectedMixin.
111  */
112  virtual void Trace(cppgc::Visitor*) const {}
113 };
114 
115 } // namespace cppgc
116 
117 #endif // INCLUDE_CPPGC_GARBAGE_COLLECTED_H_