Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:42:26

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_LIVENESS_BROKER_H_
0006 #define INCLUDE_CPPGC_LIVENESS_BROKER_H_
0007 
0008 #include "cppgc/heap.h"
0009 #include "cppgc/member.h"
0010 #include "cppgc/sentinel-pointer.h"
0011 #include "cppgc/trace-trait.h"
0012 #include "v8config.h"  // NOLINT(build/include_directory)
0013 
0014 namespace cppgc {
0015 
0016 namespace internal {
0017 class LivenessBrokerFactory;
0018 }  // namespace internal
0019 
0020 /**
0021  * The broker is passed to weak callbacks to allow (temporarily) querying
0022  * the liveness state of an object. References to non-live objects must be
0023  * cleared when `IsHeapObjectAlive()` returns false.
0024  *
0025  * \code
0026  * class GCedWithCustomWeakCallback final
0027  *   : public GarbageCollected<GCedWithCustomWeakCallback> {
0028  *  public:
0029  *   UntracedMember<Bar> bar;
0030  *
0031  *   void CustomWeakCallbackMethod(const LivenessBroker& broker) {
0032  *     if (!broker.IsHeapObjectAlive(bar))
0033  *       bar = nullptr;
0034  *   }
0035  *
0036  *   void Trace(cppgc::Visitor* visitor) const {
0037  *     visitor->RegisterWeakCallbackMethod<
0038  *         GCedWithCustomWeakCallback,
0039  *         &GCedWithCustomWeakCallback::CustomWeakCallbackMethod>(this);
0040  *   }
0041  * };
0042  * \endcode
0043  */
0044 class V8_EXPORT LivenessBroker final {
0045  public:
0046   template <typename T>
0047   bool IsHeapObjectAlive(const T* object) const {
0048     // - nullptr objects are considered alive to allow weakness to be used from
0049     // stack while running into a conservative GC. Treating nullptr as dead
0050     // would mean that e.g. custom collections could not be strongified on
0051     // stack.
0052     // - Sentinel pointers are also preserved in weakness and not cleared.
0053     return !object || object == kSentinelPointer ||
0054            IsHeapObjectAliveImpl(
0055                TraceTrait<T>::GetTraceDescriptor(object).base_object_payload);
0056   }
0057 
0058   template <typename T>
0059   bool IsHeapObjectAlive(const WeakMember<T>& weak_member) const {
0060     return IsHeapObjectAlive<T>(weak_member.Get());
0061   }
0062 
0063   template <typename T>
0064   bool IsHeapObjectAlive(const UntracedMember<T>& untraced_member) const {
0065     return IsHeapObjectAlive<T>(untraced_member.Get());
0066   }
0067 
0068  private:
0069   LivenessBroker() = default;
0070 
0071   bool IsHeapObjectAliveImpl(const void*) const;
0072 
0073   friend class internal::LivenessBrokerFactory;
0074 };
0075 
0076 }  // namespace cppgc
0077 
0078 #endif  // INCLUDE_CPPGC_LIVENESS_BROKER_H_