V8  latest master commit
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 
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
31 #endif // V8_ENABLE_CHECKS
32  }
33  void operator delete[](void*) = delete;
34 
35  protected:
36  GarbageCollectedBase() = default;
37 };
38 
39 } // namespace internal
40 
76 template <typename>
78  public:
80 
81  protected:
82  GarbageCollected() = default;
83 };
84 
104  public:
106 
107  // Sentinel used to mark not-fully-constructed mixins.
108  static constexpr void* kNotFullyConstructedObject = nullptr;
109 
110  // Provide default implementation that indicate that the vtable is not yet
111  // set up properly. This is used to to get GCInfo objects for mixins so that
112  // these objects can be processed later on.
114  return {kNotFullyConstructedObject, nullptr};
115  }
116 
121  virtual void Trace(cppgc::Visitor*) const {}
122 };
123 
147 #define USING_GARBAGE_COLLECTED_MIXIN() \
148  public: \
149  /* Marker is used by clang to check for proper usages of the macro. */ \
150  typedef int HasUsingGarbageCollectedMixinMacro; \
151  \
152  TraceDescriptor GetTraceDescriptor() const override { \
153  static_assert( \
154  internal::IsSubclassOfTemplate< \
155  std::remove_const_t<std::remove_pointer_t<decltype(this)>>, \
156  cppgc::GarbageCollected>::value, \
157  "Only garbage collected objects can have garbage collected mixins"); \
158  return {this, TraceTrait<std::remove_const_t< \
159  std::remove_pointer_t<decltype(this)>>>::Trace}; \
160  } \
161  \
162  private: \
163  static_assert(true, "Force semicolon.")
164 
177 #define MERGE_GARBAGE_COLLECTED_MIXINS() \
178  public: \
179  /* When using multiple mixins the methods become */ \
180  /* ambigous. Providing additional implementations */ \
181  /* disambiguate them again. */ \
182  TraceDescriptor GetTraceDescriptor() const override { \
183  return {kNotFullyConstructedObject, nullptr}; \
184  } \
185  \
186  private: \
187  static_assert(true, "Force semicolon.")
188 
189 } // namespace cppgc
190 
191 #endif // INCLUDE_CPPGC_GARBAGE_COLLECTED_H_
cppgc::internal::Abort
V8_EXPORT void Abort()
cppgc::GarbageCollected
Definition: garbage-collected.h:77
cppgc::GarbageCollected::GarbageCollected
GarbageCollected()=default
cppgc
Definition: allocation.h:18
cppgc::GarbageCollectedMixin::kNotFullyConstructedObject
static constexpr void * kNotFullyConstructedObject
Definition: garbage-collected.h:108
api-constants.h
cppgc::TraceDescriptor
Definition: trace-trait.h:27
cppgc::GarbageCollectedMixin::Trace
virtual void Trace(cppgc::Visitor *) const
Definition: garbage-collected.h:121
trace-trait.h
cppgc::GarbageCollectedMixin::GetTraceDescriptor
virtual TraceDescriptor GetTraceDescriptor() const
Definition: garbage-collected.h:113
cppgc::GarbageCollectedMixin::IsGarbageCollectedMixinTypeMarker
void IsGarbageCollectedMixinTypeMarker
Definition: garbage-collected.h:105
cppgc::Visitor
Definition: visitor.h:27
platform.h
cppgc::internal::GarbageCollectedBase::GarbageCollectedBase
GarbageCollectedBase()=default
cppgc::GarbageCollectedMixin
Definition: garbage-collected.h:103
type-traits.h
cppgc::internal::GarbageCollectedBase
Definition: garbage-collected.h:21
cppgc::GarbageCollected::IsGarbageCollectedTypeMarker
void IsGarbageCollectedTypeMarker
Definition: garbage-collected.h:79