1 // Copyright 2020 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_HEAP_CPPGC_VISITOR_H_
6 #define V8_HEAP_CPPGC_VISITOR_H_
7 
8 #include "include/cppgc/persistent.h"
9 #include "include/cppgc/visitor.h"
10 #include "src/heap/cppgc/heap-object-header.h"
11 
12 namespace cppgc {
13 namespace internal {
14 
15 class HeapBase;
16 class HeapObjectHeader;
17 class PageBackend;
18 
19 class VisitorFactory {
20  public:
CreateKey()21   static constexpr Visitor::Key CreateKey() { return {}; }
22 };
23 
24 // Base visitor that is allowed to create a public cppgc::Visitor object and
25 // use its internals.
26 class VisitorBase : public cppgc::Visitor {
27  public:
VisitorBase()28   VisitorBase() : cppgc::Visitor(VisitorFactory::CreateKey()) {}
29   ~VisitorBase() override = default;
30 
31   VisitorBase(const VisitorBase&) = delete;
32   VisitorBase& operator=(const VisitorBase&) = delete;
33 
34   template <typename Persistent>
TraceRootForTesting(const Persistent & p,const SourceLocation & loc)35   void TraceRootForTesting(const Persistent& p, const SourceLocation& loc) {
36     TraceRoot(p, loc);
37   }
38 };
39 
40 // Regular visitor that additionally allows for conservative tracing.
41 class ConservativeTracingVisitor {
42  public:
43   ConservativeTracingVisitor(HeapBase&, PageBackend&, cppgc::Visitor&);
44   virtual ~ConservativeTracingVisitor() = default;
45 
46   ConservativeTracingVisitor(const ConservativeTracingVisitor&) = delete;
47   ConservativeTracingVisitor& operator=(const ConservativeTracingVisitor&) =
48       delete;
49 
50   void TraceConservativelyIfNeeded(const void*);
51   void TraceConservativelyIfNeeded(HeapObjectHeader&);
52 
53  protected:
54   using TraceConservativelyCallback = void(ConservativeTracingVisitor*,
55                                            const HeapObjectHeader&);
56   virtual void V8_EXPORT_PRIVATE
57   VisitFullyConstructedConservatively(HeapObjectHeader&);
VisitInConstructionConservatively(HeapObjectHeader &,TraceConservativelyCallback)58   virtual void VisitInConstructionConservatively(HeapObjectHeader&,
59                                                  TraceConservativelyCallback) {}
60 
61   HeapBase& heap_;
62   PageBackend& page_backend_;
63   cppgc::Visitor& visitor_;
64 };
65 
66 }  // namespace internal
67 }  // namespace cppgc
68 
69 #endif  // V8_HEAP_CPPGC_VISITOR_H_
70