| |
| |
| |
|
|
| #ifndef INCLUDE_CPPGC_PERSISTENT_H_ |
| #define INCLUDE_CPPGC_PERSISTENT_H_ |
|
|
| #include <type_traits> |
|
|
| #include "cppgc/internal/persistent-node.h" |
| #include "cppgc/internal/pointer-policies.h" |
| #include "cppgc/sentinel-pointer.h" |
| #include "cppgc/source-location.h" |
| #include "cppgc/type-traits.h" |
| #include "cppgc/visitor.h" |
| #include "v8config.h" |
|
|
| namespace cppgc { |
| namespace internal { |
|
|
| |
| |
| class PersistentBase { |
| protected: |
| PersistentBase() = default; |
| explicit PersistentBase(const void* raw) : raw_(raw) {} |
|
|
| const void* GetValue() const { return raw_; } |
| void SetValue(const void* value) { raw_ = value; } |
|
|
| PersistentNode* GetNode() const { return node_; } |
| void SetNode(PersistentNode* node) { node_ = node; } |
|
|
| |
| |
| void ClearFromGC() const { |
| raw_ = nullptr; |
| node_ = nullptr; |
| } |
|
|
| protected: |
| mutable const void* raw_ = nullptr; |
| mutable PersistentNode* node_ = nullptr; |
|
|
| friend class PersistentRegionBase; |
| }; |
|
|
| |
| template <typename T, typename WeaknessPolicy, typename LocationPolicy, |
| typename CheckingPolicy> |
| class BasicPersistent final : public PersistentBase, |
| public LocationPolicy, |
| private WeaknessPolicy, |
| private CheckingPolicy { |
| public: |
| using typename WeaknessPolicy::IsStrongPersistent; |
| using PointeeType = T; |
|
|
| |
| BasicPersistent( |
| const SourceLocation& loc = SourceLocation::Current()) |
| : LocationPolicy(loc) {} |
|
|
| BasicPersistent(std::nullptr_t, |
| const SourceLocation& loc = SourceLocation::Current()) |
| : LocationPolicy(loc) {} |
|
|
| BasicPersistent( |
| SentinelPointer s, const SourceLocation& loc = SourceLocation::Current()) |
| : PersistentBase(s), LocationPolicy(loc) {} |
|
|
| |
| BasicPersistent(T* raw, |
| const SourceLocation& loc = SourceLocation::Current()) |
| : PersistentBase(raw), LocationPolicy(loc) { |
| if (!IsValid()) return; |
| SetNode(WeaknessPolicy::GetPersistentRegion(GetValue()) |
| .AllocateNode(this, &TraceAsRoot)); |
| this->CheckPointer(Get()); |
| } |
|
|
| BasicPersistent(T& raw, |
| const SourceLocation& loc = SourceLocation::Current()) |
| : BasicPersistent(&raw, loc) {} |
|
|
| |
| BasicPersistent(const BasicPersistent& other, |
| const SourceLocation& loc = SourceLocation::Current()) |
| : BasicPersistent(other.Get(), loc) {} |
|
|
| |
| template <typename U, typename OtherWeaknessPolicy, |
| typename OtherLocationPolicy, typename OtherCheckingPolicy, |
| typename = std::enable_if_t<std::is_base_of<T, U>::value>> |
| BasicPersistent( |
| const BasicPersistent<U, OtherWeaknessPolicy, OtherLocationPolicy, |
| OtherCheckingPolicy>& other, |
| const SourceLocation& loc = SourceLocation::Current()) |
| : BasicPersistent(other.Get(), loc) {} |
|
|
| |
| |
| BasicPersistent( |
| BasicPersistent&& other, |
| const SourceLocation& loc = SourceLocation::Current()) noexcept |
| : PersistentBase(std::move(other)), LocationPolicy(std::move(other)) { |
| if (!IsValid()) return; |
| GetNode()->UpdateOwner(this); |
| other.SetValue(nullptr); |
| other.SetNode(nullptr); |
| this->CheckPointer(Get()); |
| } |
|
|
| |
| template <typename U, typename MemberBarrierPolicy, |
| typename MemberWeaknessTag, typename MemberCheckingPolicy, |
| typename MemberStorageType, |
| typename = std::enable_if_t<std::is_base_of<T, U>::value>> |
| BasicPersistent(const internal::BasicMember< |
| U, MemberBarrierPolicy, MemberWeaknessTag, |
| MemberCheckingPolicy, MemberStorageType>& member, |
| const SourceLocation& loc = SourceLocation::Current()) |
| : BasicPersistent(member.Get(), loc) {} |
|
|
| ~BasicPersistent() { Clear(); } |
|
|
| |
| BasicPersistent& operator=(const BasicPersistent& other) { |
| return operator=(other.Get()); |
| } |
|
|
| template <typename U, typename OtherWeaknessPolicy, |
| typename OtherLocationPolicy, typename OtherCheckingPolicy, |
| typename = std::enable_if_t<std::is_base_of<T, U>::value>> |
| BasicPersistent& operator=( |
| const BasicPersistent<U, OtherWeaknessPolicy, OtherLocationPolicy, |
| OtherCheckingPolicy>& other) { |
| return operator=(other.Get()); |
| } |
|
|
| |
| BasicPersistent& operator=(BasicPersistent&& other) noexcept { |
| if (this == &other) return *this; |
| Clear(); |
| PersistentBase::operator=(std::move(other)); |
| LocationPolicy::operator=(std::move(other)); |
| if (!IsValid()) return *this; |
| GetNode()->UpdateOwner(this); |
| other.SetValue(nullptr); |
| other.SetNode(nullptr); |
| this->CheckPointer(Get()); |
| return *this; |
| } |
|
|
| |
| template <typename U, typename MemberBarrierPolicy, |
| typename MemberWeaknessTag, typename MemberCheckingPolicy, |
| typename MemberStorageType, |
| typename = std::enable_if_t<std::is_base_of<T, U>::value>> |
| BasicPersistent& operator=( |
| const internal::BasicMember<U, MemberBarrierPolicy, MemberWeaknessTag, |
| MemberCheckingPolicy, MemberStorageType>& |
| member) { |
| return operator=(member.Get()); |
| } |
|
|
| BasicPersistent& operator=(T* other) { |
| Assign(other); |
| return *this; |
| } |
|
|
| BasicPersistent& operator=(std::nullptr_t) { |
| Clear(); |
| return *this; |
| } |
|
|
| BasicPersistent& operator=(SentinelPointer s) { |
| Assign(s); |
| return *this; |
| } |
|
|
| explicit operator bool() const { return Get(); } |
| operator T*() const { return Get(); } |
| T* operator->() const { return Get(); } |
| T& operator*() const { return *Get(); } |
|
|
| |
| |
| |
| V8_CLANG_NO_SANITIZE("cfi-unrelated-cast") T* Get() const { |
| |
| |
| |
| return static_cast<T*>(const_cast<void*>(GetValue())); |
| } |
|
|
| void Clear() { |
| |
| |
| if (IsValid()) { |
| WeaknessPolicy::GetPersistentRegion(GetValue()).FreeNode(GetNode()); |
| SetNode(nullptr); |
| } |
| SetValue(nullptr); |
| } |
|
|
| T* Release() { |
| T* result = Get(); |
| Clear(); |
| return result; |
| } |
|
|
| template <typename U, typename OtherWeaknessPolicy = WeaknessPolicy, |
| typename OtherLocationPolicy = LocationPolicy, |
| typename OtherCheckingPolicy = CheckingPolicy> |
| BasicPersistent<U, OtherWeaknessPolicy, OtherLocationPolicy, |
| OtherCheckingPolicy> |
| To() const { |
| return BasicPersistent<U, OtherWeaknessPolicy, OtherLocationPolicy, |
| OtherCheckingPolicy>(static_cast<U*>(Get())); |
| } |
|
|
| private: |
| static void TraceAsRoot(RootVisitor& root_visitor, const void* ptr) { |
| root_visitor.Trace(*static_cast<const BasicPersistent*>(ptr)); |
| } |
|
|
| bool IsValid() const { |
| |
| |
| |
| return GetValue() != nullptr && GetValue() != kSentinelPointer; |
| } |
|
|
| void Assign(T* ptr) { |
| if (IsValid()) { |
| if (ptr && ptr != kSentinelPointer) { |
| |
| SetValue(ptr); |
| this->CheckPointer(ptr); |
| return; |
| } |
| WeaknessPolicy::GetPersistentRegion(GetValue()).FreeNode(GetNode()); |
| SetNode(nullptr); |
| } |
| SetValue(ptr); |
| if (!IsValid()) return; |
| SetNode(WeaknessPolicy::GetPersistentRegion(GetValue()) |
| .AllocateNode(this, &TraceAsRoot)); |
| this->CheckPointer(Get()); |
| } |
|
|
| void ClearFromGC() const { |
| if (IsValid()) { |
| WeaknessPolicy::GetPersistentRegion(GetValue()).FreeNode(GetNode()); |
| PersistentBase::ClearFromGC(); |
| } |
| } |
|
|
| |
| V8_CLANG_NO_SANITIZE("cfi-unrelated-cast") |
| T* GetFromGC() const { |
| return static_cast<T*>(const_cast<void*>(GetValue())); |
| } |
|
|
| friend class internal::RootVisitor; |
| }; |
|
|
| template <typename T1, typename WeaknessPolicy1, typename LocationPolicy1, |
| typename CheckingPolicy1, typename T2, typename WeaknessPolicy2, |
| typename LocationPolicy2, typename CheckingPolicy2> |
| bool operator==(const BasicPersistent<T1, WeaknessPolicy1, LocationPolicy1, |
| CheckingPolicy1>& p1, |
| const BasicPersistent<T2, WeaknessPolicy2, LocationPolicy2, |
| CheckingPolicy2>& p2) { |
| return p1.Get() == p2.Get(); |
| } |
|
|
| template <typename T1, typename WeaknessPolicy1, typename LocationPolicy1, |
| typename CheckingPolicy1, typename T2, typename WeaknessPolicy2, |
| typename LocationPolicy2, typename CheckingPolicy2> |
| bool operator!=(const BasicPersistent<T1, WeaknessPolicy1, LocationPolicy1, |
| CheckingPolicy1>& p1, |
| const BasicPersistent<T2, WeaknessPolicy2, LocationPolicy2, |
| CheckingPolicy2>& p2) { |
| return !(p1 == p2); |
| } |
|
|
| template <typename T1, typename PersistentWeaknessPolicy, |
| typename PersistentLocationPolicy, typename PersistentCheckingPolicy, |
| typename T2, typename MemberWriteBarrierPolicy, |
| typename MemberWeaknessTag, typename MemberCheckingPolicy, |
| typename MemberStorageType> |
| bool operator==( |
| const BasicPersistent<T1, PersistentWeaknessPolicy, |
| PersistentLocationPolicy, PersistentCheckingPolicy>& |
| p, |
| const BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy, |
| MemberCheckingPolicy, MemberStorageType>& m) { |
| return p.Get() == m.Get(); |
| } |
|
|
| template <typename T1, typename PersistentWeaknessPolicy, |
| typename PersistentLocationPolicy, typename PersistentCheckingPolicy, |
| typename T2, typename MemberWriteBarrierPolicy, |
| typename MemberWeaknessTag, typename MemberCheckingPolicy, |
| typename MemberStorageType> |
| bool operator!=( |
| const BasicPersistent<T1, PersistentWeaknessPolicy, |
| PersistentLocationPolicy, PersistentCheckingPolicy>& |
| p, |
| const BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy, |
| MemberCheckingPolicy, MemberStorageType>& m) { |
| return !(p == m); |
| } |
|
|
| template <typename T1, typename MemberWriteBarrierPolicy, |
| typename MemberWeaknessTag, typename MemberCheckingPolicy, |
| typename MemberStorageType, typename T2, |
| typename PersistentWeaknessPolicy, typename PersistentLocationPolicy, |
| typename PersistentCheckingPolicy> |
| bool operator==( |
| const BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy, |
| MemberCheckingPolicy, MemberStorageType>& m, |
| const BasicPersistent<T1, PersistentWeaknessPolicy, |
| PersistentLocationPolicy, PersistentCheckingPolicy>& |
| p) { |
| return m.Get() == p.Get(); |
| } |
|
|
| template <typename T1, typename MemberWriteBarrierPolicy, |
| typename MemberWeaknessTag, typename MemberCheckingPolicy, |
| typename MemberStorageType, typename T2, |
| typename PersistentWeaknessPolicy, typename PersistentLocationPolicy, |
| typename PersistentCheckingPolicy> |
| bool operator!=( |
| const BasicMember<T2, MemberWeaknessTag, MemberWriteBarrierPolicy, |
| MemberCheckingPolicy, MemberStorageType>& m, |
| const BasicPersistent<T1, PersistentWeaknessPolicy, |
| PersistentLocationPolicy, PersistentCheckingPolicy>& |
| p) { |
| return !(m == p); |
| } |
|
|
| template <typename T, typename LocationPolicy, typename CheckingPolicy> |
| struct IsWeak<BasicPersistent<T, internal::WeakPersistentPolicy, LocationPolicy, |
| CheckingPolicy>> : std::true_type {}; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| template <typename T> |
| using Persistent = |
| internal::BasicPersistent<T, internal::StrongPersistentPolicy>; |
|
|
| |
| |
| |
| |
| |
| |
| template <typename T> |
| using WeakPersistent = |
| internal::BasicPersistent<T, internal::WeakPersistentPolicy>; |
|
|
| } |
|
|
| #endif |
|
|