1515#include " v8config.h" // NOLINT(build/include_directory)
1616
1717namespace cppgc {
18-
19- class Visitor ;
20-
2118namespace internal {
2219
23- class PersistentBase {
24- protected:
25- PersistentBase () = default ;
26- explicit PersistentBase (void * raw) : raw_(raw) {}
27-
28- void * GetValue () const { return raw_; }
29- void SetValue (void * value) { raw_ = value; }
30-
31- PersistentNode* GetNode () const { return node_; }
32- void SetNode (PersistentNode* node) { node_ = node; }
33-
34- // Performs a shallow clear which assumes that internal persistent nodes are
35- // destroyed elsewhere.
36- void ClearFromGC () const {
37- raw_ = nullptr ;
38- node_ = nullptr ;
39- }
40-
41- private:
42- mutable void * raw_ = nullptr ;
43- mutable PersistentNode* node_ = nullptr ;
44-
45- friend class PersistentRegion ;
46- };
47-
4820// The basic class from which all Persistent classes are generated.
4921template <typename T, typename WeaknessPolicy, typename LocationPolicy,
5022 typename CheckingPolicy>
51- class BasicPersistent final : public PersistentBase,
52- public LocationPolicy,
53- private WeaknessPolicy,
54- private CheckingPolicy {
23+ class BasicPersistent : public LocationPolicy ,
24+ private WeaknessPolicy,
25+ private CheckingPolicy {
5526 public:
5627 using typename WeaknessPolicy::IsStrongPersistent;
5728 using PointeeType = T;
@@ -67,15 +38,15 @@ class BasicPersistent final : public PersistentBase,
6738
6839 BasicPersistent ( // NOLINT
6940 SentinelPointer s, const SourceLocation& loc = SourceLocation::Current())
70- : PersistentBase(s ), LocationPolicy(loc ) {}
41+ : LocationPolicy(loc ), raw_(s ) {}
7142
72- // Raw value constructors .
43+ // Raw value contstructors .
7344 BasicPersistent (T* raw, // NOLINT
7445 const SourceLocation& loc = SourceLocation::Current())
75- : PersistentBase(raw ), LocationPolicy(loc ) {
46+ : LocationPolicy(loc ), raw_(raw ) {
7647 if (!IsValid ()) return ;
77- SetNode ( WeaknessPolicy::GetPersistentRegion (GetValue ())
78- . AllocateNode ( this , &BasicPersistent::Trace) );
48+ node_ = WeaknessPolicy::GetPersistentRegion (raw_). AllocateNode (
49+ this , &BasicPersistent::Trace);
7950 this ->CheckPointer (Get ());
8051 }
8152
@@ -103,11 +74,13 @@ class BasicPersistent final : public PersistentBase,
10374 BasicPersistent (
10475 BasicPersistent&& other,
10576 const SourceLocation& loc = SourceLocation::Current()) noexcept
106- : PersistentBase(std::move(other)), LocationPolicy(std::move(other)) {
77+ : LocationPolicy(std::move(other)),
78+ raw_ (std::move(other.raw_)),
79+ node_(std::move(other.node_)) {
10780 if (!IsValid ()) return ;
108- GetNode () ->UpdateOwner (this );
109- other.SetValue ( nullptr ) ;
110- other.SetNode ( nullptr ) ;
81+ node_ ->UpdateOwner (this );
82+ other.raw_ = nullptr ;
83+ other.node_ = nullptr ;
11184 this ->CheckPointer (Get ());
11285 }
11386
@@ -141,12 +114,13 @@ class BasicPersistent final : public PersistentBase,
141114 BasicPersistent& operator =(BasicPersistent&& other) {
142115 if (this == &other) return *this ;
143116 Clear ();
144- PersistentBase::operator =(std::move (other));
145117 LocationPolicy::operator =(std::move (other));
118+ raw_ = std::move (other.raw_ );
119+ node_ = std::move (other.node_ );
146120 if (!IsValid ()) return *this ;
147- GetNode () ->UpdateOwner (this );
148- other.SetValue ( nullptr ) ;
149- other.SetNode ( nullptr ) ;
121+ node_ ->UpdateOwner (this );
122+ other.raw_ = nullptr ;
123+ other.node_ = nullptr ;
150124 this ->CheckPointer (Get ());
151125 return *this ;
152126 }
@@ -182,7 +156,7 @@ class BasicPersistent final : public PersistentBase,
182156 T* operator ->() const { return Get (); }
183157 T& operator *() const { return *Get (); }
184158
185- T* Get () const { return static_cast <T*>( GetValue ()) ; }
159+ T* Get () const { return raw_ ; }
186160
187161 void Clear () { Assign (nullptr ); }
188162
@@ -202,35 +176,29 @@ class BasicPersistent final : public PersistentBase,
202176 // Ideally, handling kSentinelPointer would be done by the embedder. On the
203177 // other hand, having Persistent aware of it is beneficial since no node
204178 // gets wasted.
205- return GetValue () != nullptr && GetValue () != kSentinelPointer ;
179+ return raw_ != nullptr && raw_ != kSentinelPointer ;
206180 }
207181
208182 void Assign (T* ptr) {
209183 if (IsValid ()) {
210184 if (ptr && ptr != kSentinelPointer ) {
211185 // Simply assign the pointer reusing the existing node.
212- SetValue ( ptr) ;
186+ raw_ = ptr;
213187 this ->CheckPointer (ptr);
214188 return ;
215189 }
216- WeaknessPolicy::GetPersistentRegion (GetValue ()) .FreeNode (GetNode () );
217- SetNode ( nullptr ) ;
190+ WeaknessPolicy::GetPersistentRegion (raw_) .FreeNode (node_ );
191+ node_ = nullptr ;
218192 }
219- SetValue ( ptr) ;
193+ raw_ = ptr;
220194 if (!IsValid ()) return ;
221- SetNode ( WeaknessPolicy::GetPersistentRegion (GetValue ())
222- . AllocateNode ( this , &BasicPersistent::Trace) );
195+ node_ = WeaknessPolicy::GetPersistentRegion (raw_). AllocateNode (
196+ this , &BasicPersistent::Trace);
223197 this ->CheckPointer (Get ());
224198 }
225199
226- void ClearFromGC () const {
227- if (IsValid ()) {
228- WeaknessPolicy::GetPersistentRegion (GetValue ()).FreeNode (GetNode ());
229- PersistentBase::ClearFromGC ();
230- }
231- }
232-
233- friend class cppgc ::Visitor;
200+ T* raw_ = nullptr ;
201+ PersistentNode* node_ = nullptr ;
234202};
235203
236204template <typename T1 , typename WeaknessPolicy1, typename LocationPolicy1,
0 commit comments