11cb0ef41Sopenharmony_ci// Copyright 2020 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#ifndef INCLUDE_CPPGC_LIVENESS_BROKER_H_ 61cb0ef41Sopenharmony_ci#define INCLUDE_CPPGC_LIVENESS_BROKER_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include "cppgc/heap.h" 91cb0ef41Sopenharmony_ci#include "cppgc/member.h" 101cb0ef41Sopenharmony_ci#include "cppgc/sentinel-pointer.h" 111cb0ef41Sopenharmony_ci#include "cppgc/trace-trait.h" 121cb0ef41Sopenharmony_ci#include "v8config.h" // NOLINT(build/include_directory) 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_cinamespace cppgc { 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_cinamespace internal { 171cb0ef41Sopenharmony_ciclass LivenessBrokerFactory; 181cb0ef41Sopenharmony_ci} // namespace internal 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci/** 211cb0ef41Sopenharmony_ci * The broker is passed to weak callbacks to allow (temporarily) querying 221cb0ef41Sopenharmony_ci * the liveness state of an object. References to non-live objects must be 231cb0ef41Sopenharmony_ci * cleared when `IsHeapObjectAlive()` returns false. 241cb0ef41Sopenharmony_ci * 251cb0ef41Sopenharmony_ci * \code 261cb0ef41Sopenharmony_ci * class GCedWithCustomWeakCallback final 271cb0ef41Sopenharmony_ci * : public GarbageCollected<GCedWithCustomWeakCallback> { 281cb0ef41Sopenharmony_ci * public: 291cb0ef41Sopenharmony_ci * UntracedMember<Bar> bar; 301cb0ef41Sopenharmony_ci * 311cb0ef41Sopenharmony_ci * void CustomWeakCallbackMethod(const LivenessBroker& broker) { 321cb0ef41Sopenharmony_ci * if (!broker.IsHeapObjectAlive(bar)) 331cb0ef41Sopenharmony_ci * bar = nullptr; 341cb0ef41Sopenharmony_ci * } 351cb0ef41Sopenharmony_ci * 361cb0ef41Sopenharmony_ci * void Trace(cppgc::Visitor* visitor) const { 371cb0ef41Sopenharmony_ci * visitor->RegisterWeakCallbackMethod< 381cb0ef41Sopenharmony_ci * GCedWithCustomWeakCallback, 391cb0ef41Sopenharmony_ci * &GCedWithCustomWeakCallback::CustomWeakCallbackMethod>(this); 401cb0ef41Sopenharmony_ci * } 411cb0ef41Sopenharmony_ci * }; 421cb0ef41Sopenharmony_ci * \endcode 431cb0ef41Sopenharmony_ci */ 441cb0ef41Sopenharmony_ciclass V8_EXPORT LivenessBroker final { 451cb0ef41Sopenharmony_ci public: 461cb0ef41Sopenharmony_ci template <typename T> 471cb0ef41Sopenharmony_ci bool IsHeapObjectAlive(const T* object) const { 481cb0ef41Sopenharmony_ci // - nullptr objects are considered alive to allow weakness to be used from 491cb0ef41Sopenharmony_ci // stack while running into a conservative GC. Treating nullptr as dead 501cb0ef41Sopenharmony_ci // would mean that e.g. custom collections could not be strongified on 511cb0ef41Sopenharmony_ci // stack. 521cb0ef41Sopenharmony_ci // - Sentinel pointers are also preserved in weakness and not cleared. 531cb0ef41Sopenharmony_ci return !object || object == kSentinelPointer || 541cb0ef41Sopenharmony_ci IsHeapObjectAliveImpl( 551cb0ef41Sopenharmony_ci TraceTrait<T>::GetTraceDescriptor(object).base_object_payload); 561cb0ef41Sopenharmony_ci } 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci template <typename T> 591cb0ef41Sopenharmony_ci bool IsHeapObjectAlive(const WeakMember<T>& weak_member) const { 601cb0ef41Sopenharmony_ci return IsHeapObjectAlive<T>(weak_member.Get()); 611cb0ef41Sopenharmony_ci } 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci template <typename T> 641cb0ef41Sopenharmony_ci bool IsHeapObjectAlive(const UntracedMember<T>& untraced_member) const { 651cb0ef41Sopenharmony_ci return IsHeapObjectAlive<T>(untraced_member.Get()); 661cb0ef41Sopenharmony_ci } 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci private: 691cb0ef41Sopenharmony_ci LivenessBroker() = default; 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci bool IsHeapObjectAliveImpl(const void*) const; 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci friend class internal::LivenessBrokerFactory; 741cb0ef41Sopenharmony_ci}; 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci} // namespace cppgc 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci#endif // INCLUDE_CPPGC_LIVENESS_BROKER_H_ 79