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