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/visitor.h"
9 #include "src/heap/cppgc/heap-object-header.h"
10 
11 namespace cppgc {
12 namespace internal {
13 
14 class HeapBase;
15 class HeapObjectHeader;
16 class PageBackend;
17 
18 class VisitorFactory {
19  public:
CreateKey()20   static constexpr Visitor::Key CreateKey() { return {}; }
21 };
22 
23 // Base visitor that is allowed to create a public cppgc::Visitor object and
24 // use its internals.
25 class VisitorBase : public cppgc::Visitor {
26  public:
VisitorBase()27   VisitorBase() : cppgc::Visitor(VisitorFactory::CreateKey()) {}
28   ~VisitorBase() override = default;
29 
30   VisitorBase(const VisitorBase&) = delete;
31   VisitorBase& operator=(const VisitorBase&) = delete;
32 
33   template <typename Persistent>
TraceRootForTesting(const Persistent & p,const SourceLocation & loc)34   void TraceRootForTesting(const Persistent& p, const SourceLocation& loc) {
35     TraceRoot(p, loc);
36   }
37 };
38 
39 // Regular visitor that additionally allows for conservative tracing.
40 class ConservativeTracingVisitor {
41  public:
42   ConservativeTracingVisitor(HeapBase&, PageBackend&, cppgc::Visitor&);
43   virtual ~ConservativeTracingVisitor() = default;
44 
45   ConservativeTracingVisitor(const ConservativeTracingVisitor&) = delete;
46   ConservativeTracingVisitor& operator=(const ConservativeTracingVisitor&) =
47       delete;
48 
49   void TraceConservativelyIfNeeded(const void*);
50   void TraceConservativelyIfNeeded(HeapObjectHeader&);
51 
52  protected:
53   using TraceConservativelyCallback = void(ConservativeTracingVisitor*,
54                                            const HeapObjectHeader&);
55   virtual void V8_EXPORT_PRIVATE
56   VisitFullyConstructedConservatively(HeapObjectHeader&);
VisitInConstructionConservatively(HeapObjectHeader &,TraceConservativelyCallback)57   virtual void VisitInConstructionConservatively(HeapObjectHeader&,
58                                                  TraceConservativelyCallback) {}
59 
60   HeapBase& heap_;
61   PageBackend& page_backend_;
62   cppgc::Visitor& visitor_;
63 };
64 
65 }  // namespace internal
66 }  // namespace cppgc
67 
68 #endif  // V8_HEAP_CPPGC_VISITOR_H_
69