v8  9.4.146 (node 16.13.0)
V8 is Google's open source JavaScript engine
prefinalizer.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_PREFINALIZER_H_
6 #define INCLUDE_CPPGC_PREFINALIZER_H_
7 
8 #include "cppgc/internal/compiler-specific.h"
9 #include "cppgc/internal/prefinalizer-handler.h"
10 #include "cppgc/liveness-broker.h"
11 
12 namespace cppgc {
13 
14 namespace internal {
15 
16 template <typename T>
17 class PrefinalizerRegistration final {
18  public:
19  explicit PrefinalizerRegistration(T* self) {
20  static_assert(sizeof(&T::InvokePreFinalizer) > 0,
21  "USING_PRE_FINALIZER(T) must be defined.");
22 
23  cppgc::internal::PreFinalizerRegistrationDispatcher::RegisterPrefinalizer(
24  {self, T::InvokePreFinalizer});
25  }
26 
27  void* operator new(size_t, void* location) = delete;
28  void* operator new(size_t) = delete;
29 };
30 
31 } // namespace internal
32 
33 #define CPPGC_USING_PRE_FINALIZER(Class, PreFinalizer)
34  public:
35  static bool InvokePreFinalizer(const cppgc::LivenessBroker& liveness_broker,
36  void* object) {
37  static_assert(cppgc::IsGarbageCollectedOrMixinTypeV<Class>,
38  "Only garbage collected objects can have prefinalizers");
39  Class* self = static_cast<Class*>(object);
40  if (liveness_broker.IsHeapObjectAlive(self)) return false;
41  self->Class::PreFinalizer();
42  return true;
43  }
44 
45  private:
46  CPPGC_NO_UNIQUE_ADDRESS cppgc::internal::PrefinalizerRegistration<Class>
47  prefinalizer_dummy_{this};
48  static_assert(true, "Force semicolon.")
49 
50 } // namespace cppgc
51 
52 #endif // INCLUDE_CPPGC_PREFINALIZER_H_