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 21 class GarbageCollectedBase { 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'. delete(void *)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> 77 class GarbageCollected : public internal::GarbageCollectedBase { 78 public: 79 using IsGarbageCollectedTypeMarker = void; 80 81 protected: 82 GarbageCollected() = default; 83 }; 84 85 /** 86 * Base class for managed mixin objects. Such objects cannot be constructed 87 * directly but must be mixed into the inheritance hierarchy of a 88 * GarbageCollected object. 89 * 90 * Types inheriting from GarbageCollectedMixin must override a virtual method 91 * of signature `void Trace(cppgc::Visitor*) const` that dispatchs all managed 92 * pointers to the visitor and delegates to base classes. 93 * 94 * \code 95 * class Mixin : public GarbageCollectedMixin { 96 * public: 97 * void Trace(cppgc::Visitor* visitor) const override { 98 * // Dispatch using visitor->Trace(...); 99 * } 100 * }; 101 * \endcode 102 */ 103 class GarbageCollectedMixin : public internal::GarbageCollectedBase { 104 public: 105 using IsGarbageCollectedMixinTypeMarker = void; 106 107 /** 108 * This Trace method must be overriden by objects inheriting from 109 * GarbageCollectedMixin. 110 */ Trace(cppgc::Visitor *)111 virtual void Trace(cppgc::Visitor*) const {} 112 }; 113 114 } // namespace cppgc 115 116 #endif // INCLUDE_CPPGC_GARBAGE_COLLECTED_H_ 117