v8  8.6.395 (node 15.0.1)
V8 is Google's open source JavaScript engine
liveness-broker.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_LIVENESS_BROKER_H_
6 #define INCLUDE_CPPGC_LIVENESS_BROKER_H_
7 
8 #include "cppgc/heap.h"
9 #include "cppgc/member.h"
10 #include "cppgc/trace-trait.h"
11 #include "v8config.h" // NOLINT(build/include_directory)
12 
13 namespace cppgc {
14 
15 namespace internal {
16 class LivenessBrokerFactory;
17 } // namespace internal
18 
19 /**
20  * The broker is passed to weak callbacks to allow (temporarily) querying
21  * the liveness state of an object. References to non-live objects must be
22  * cleared when IsHeapObjectAlive() returns false.
23  *
24  * \code
25  * class GCedWithCustomWeakCallback final
26  * : public GarbageCollected<GCedWithCustomWeakCallback> {
27  * public:
28  * UntracedMember<Bar> bar;
29  *
30  * void CustomWeakCallbackMethod(const LivenessBroker& broker) {
31  * if (!broker.IsHeapObjectAlive(bar))
32  * bar = nullptr;
33  * }
34  *
35  * void Trace(cppgc::Visitor* visitor) const {
36  * visitor->RegisterWeakCallbackMethod<
37  * GCedWithCustomWeakCallback,
38  * &GCedWithCustomWeakCallback::CustomWeakCallbackMethod>(this);
39  * }
40  * };
41  * \endcode
42  */
43 class V8_EXPORT LivenessBroker final {
44  public:
45  template <typename T>
46  bool IsHeapObjectAlive(const T* object) const {
47  return object &&
48  IsHeapObjectAliveImpl(
49  TraceTrait<T>::GetTraceDescriptor(object).base_object_payload);
50  }
51 
52  template <typename T>
53  bool IsHeapObjectAlive(const UntracedMember<T>& untraced_member) const {
54  return (untraced_member != kSentinelPointer) &&
55  IsHeapObjectAlive<T>(untraced_member.Get());
56  }
57 
58  private:
59  LivenessBroker() = default;
60 
61  bool IsHeapObjectAliveImpl(const void*) const;
62 
63  friend class internal::LivenessBrokerFactory;
64 };
65 
66 } // namespace cppgc
67 
68 #endif // INCLUDE_CPPGC_LIVENESS_BROKER_H_