v8  9.0.257(node16.0.0)
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 WeakMember<T>& weak_member) const {
54  return (weak_member != kSentinelPointer) &&
55  IsHeapObjectAlive<T>(weak_member.Get());
56  }
57 
58  template <typename T>
59  bool IsHeapObjectAlive(const UntracedMember<T>& untraced_member) const {
60  return (untraced_member != kSentinelPointer) &&
61  IsHeapObjectAlive<T>(untraced_member.Get());
62  }
63 
64  private:
65  LivenessBroker() = default;
66 
67  bool IsHeapObjectAliveImpl(const void*) const;
68 
69  friend class internal::LivenessBrokerFactory;
70 };
71 
72 } // namespace cppgc
73 
74 #endif // INCLUDE_CPPGC_LIVENESS_BROKER_H_
cppgc::LivenessBroker::IsHeapObjectAlive
bool IsHeapObjectAlive(const UntracedMember< T > &untraced_member) const
Definition: liveness-broker.h:59
cppgc::LivenessBroker::IsHeapObjectAlive
bool IsHeapObjectAlive(const T *object) const
Definition: liveness-broker.h:46
cppgc
Definition: allocation.h:17
cppgc::LivenessBroker::IsHeapObjectAlive
bool IsHeapObjectAlive(const WeakMember< T > &weak_member) const
Definition: liveness-broker.h:53
V8_EXPORT
#define V8_EXPORT
Definition: v8config.h:512
cppgc::kSentinelPointer
constexpr internal::SentinelPointer kSentinelPointer
Definition: sentinel-pointer.h:28
cppgc::internal
Definition: allocation.h:22
cppgc::internal::BasicMember::TraceTrait
friend struct cppgc::TraceTrait
Definition: member.h:208