2020-04-09 13:26:19 +00:00
|
|
|
// Copyright 2020 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef INCLUDE_CPPGC_LIVENESS_BROKER_H_
|
|
|
|
#define INCLUDE_CPPGC_LIVENESS_BROKER_H_
|
|
|
|
|
2020-04-20 18:19:29 +00:00
|
|
|
#include "cppgc/heap.h"
|
|
|
|
#include "cppgc/member.h"
|
2022-07-22 12:29:06 +00:00
|
|
|
#include "cppgc/sentinel-pointer.h"
|
2020-04-20 18:19:29 +00:00
|
|
|
#include "cppgc/trace-trait.h"
|
2020-04-27 16:29:03 +00:00
|
|
|
#include "v8config.h" // NOLINT(build/include_directory)
|
2020-04-09 13:26:19 +00:00
|
|
|
|
|
|
|
namespace cppgc {
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
class LivenessBrokerFactory;
|
|
|
|
} // namespace internal
|
|
|
|
|
2020-05-27 13:32:18 +00:00
|
|
|
/**
|
|
|
|
* The broker is passed to weak callbacks to allow (temporarily) querying
|
|
|
|
* the liveness state of an object. References to non-live objects must be
|
2020-10-19 08:12:55 +00:00
|
|
|
* cleared when `IsHeapObjectAlive()` returns false.
|
2020-05-27 13:32:18 +00:00
|
|
|
*
|
|
|
|
* \code
|
|
|
|
* class GCedWithCustomWeakCallback final
|
|
|
|
* : public GarbageCollected<GCedWithCustomWeakCallback> {
|
|
|
|
* public:
|
|
|
|
* UntracedMember<Bar> bar;
|
|
|
|
*
|
|
|
|
* void CustomWeakCallbackMethod(const LivenessBroker& broker) {
|
|
|
|
* if (!broker.IsHeapObjectAlive(bar))
|
|
|
|
* bar = nullptr;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* void Trace(cppgc::Visitor* visitor) const {
|
|
|
|
* visitor->RegisterWeakCallbackMethod<
|
|
|
|
* GCedWithCustomWeakCallback,
|
|
|
|
* &GCedWithCustomWeakCallback::CustomWeakCallbackMethod>(this);
|
|
|
|
* }
|
|
|
|
* };
|
|
|
|
* \endcode
|
|
|
|
*/
|
2020-04-09 13:26:19 +00:00
|
|
|
class V8_EXPORT LivenessBroker final {
|
|
|
|
public:
|
|
|
|
template <typename T>
|
|
|
|
bool IsHeapObjectAlive(const T* object) const {
|
2022-07-22 12:29:06 +00:00
|
|
|
// - nullptr objects are considered alive to allow weakness to be used from
|
2021-08-11 10:06:09 +00:00
|
|
|
// stack while running into a conservative GC. Treating nullptr as dead
|
2022-07-22 12:29:06 +00:00
|
|
|
// would mean that e.g. custom collections could not be strongified on
|
|
|
|
// stack.
|
|
|
|
// - Sentinel pointers are also preserved in weakness and not cleared.
|
|
|
|
return !object || object == kSentinelPointer ||
|
2020-04-09 13:26:19 +00:00
|
|
|
IsHeapObjectAliveImpl(
|
|
|
|
TraceTrait<T>::GetTraceDescriptor(object).base_object_payload);
|
|
|
|
}
|
|
|
|
|
2021-01-26 13:16:28 +00:00
|
|
|
template <typename T>
|
|
|
|
bool IsHeapObjectAlive(const WeakMember<T>& weak_member) const {
|
2022-07-22 12:29:06 +00:00
|
|
|
return IsHeapObjectAlive<T>(weak_member.Get());
|
2021-01-26 13:16:28 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 13:26:19 +00:00
|
|
|
template <typename T>
|
|
|
|
bool IsHeapObjectAlive(const UntracedMember<T>& untraced_member) const {
|
2022-07-22 12:29:06 +00:00
|
|
|
return IsHeapObjectAlive<T>(untraced_member.Get());
|
2020-04-09 13:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
LivenessBroker() = default;
|
|
|
|
|
|
|
|
bool IsHeapObjectAliveImpl(const void*) const;
|
|
|
|
|
|
|
|
friend class internal::LivenessBrokerFactory;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace cppgc
|
|
|
|
|
|
|
|
#endif // INCLUDE_CPPGC_LIVENESS_BROKER_H_
|