Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-02 08:54:43

0001 // Copyright 2020 the V8 project authors. All rights reserved.
0002 // Use of this source code is governed by a BSD-style license that can be
0003 // found in the LICENSE file.
0004 
0005 #ifndef INCLUDE_CPPGC_PREFINALIZER_H_
0006 #define INCLUDE_CPPGC_PREFINALIZER_H_
0007 
0008 #include "cppgc/internal/compiler-specific.h"
0009 #include "cppgc/liveness-broker.h"
0010 
0011 namespace cppgc {
0012 
0013 namespace internal {
0014 
0015 class V8_EXPORT PrefinalizerRegistration final {
0016  public:
0017   using Callback = bool (*)(const cppgc::LivenessBroker&, void*);
0018 
0019   PrefinalizerRegistration(void*, Callback);
0020 
0021   void* operator new(size_t, void* location) = delete;
0022   void* operator new(size_t) = delete;
0023 };
0024 
0025 }  // namespace internal
0026 
0027 /**
0028  * Macro must be used in the private section of `Class` and registers a
0029  * prefinalization callback `void Class::PreFinalizer()`. The callback is
0030  * invoked on garbage collection after the collector has found an object to be
0031  * dead.
0032  *
0033  * Callback properties:
0034  * - The callback is invoked before a possible destructor for the corresponding
0035  *   object.
0036  * - The callback may access the whole object graph, irrespective of whether
0037  *   objects are considered dead or alive.
0038  * - The callback is invoked on the same thread as the object was created on.
0039  *
0040  * Example:
0041  * \code
0042  * class WithPrefinalizer : public GarbageCollected<WithPrefinalizer> {
0043  *   CPPGC_USING_PRE_FINALIZER(WithPrefinalizer, Dispose);
0044  *
0045  *  public:
0046  *   void Trace(Visitor*) const {}
0047  *   void Dispose() { prefinalizer_called = true; }
0048  *   ~WithPrefinalizer() {
0049  *     // prefinalizer_called == true
0050  *   }
0051  *  private:
0052  *   bool prefinalizer_called = false;
0053  * };
0054  * \endcode
0055  */
0056 #define CPPGC_USING_PRE_FINALIZER(Class, PreFinalizer)                         \
0057  public:                                                                       \
0058   static bool InvokePreFinalizer(const cppgc::LivenessBroker& liveness_broker, \
0059                                  void* object) {                               \
0060     static_assert(cppgc::IsGarbageCollectedOrMixinTypeV<Class>,                \
0061                   "Only garbage collected objects can have prefinalizers");    \
0062     Class* self = static_cast<Class*>(object);                                 \
0063     if (liveness_broker.IsHeapObjectAlive(self)) return false;                 \
0064     self->PreFinalizer();                                                      \
0065     return true;                                                               \
0066   }                                                                            \
0067                                                                                \
0068  private:                                                                      \
0069   CPPGC_NO_UNIQUE_ADDRESS cppgc::internal::PrefinalizerRegistration            \
0070       prefinalizer_dummy_{this, Class::InvokePreFinalizer};                    \
0071   static_assert(true, "Force semicolon.")
0072 
0073 }  // namespace cppgc
0074 
0075 #endif  // INCLUDE_CPPGC_PREFINALIZER_H_