visitor.h 2.05 KB
Newer Older
1 2 3 4 5 6 7
// 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 V8_HEAP_CPPGC_VISITOR_H_
#define V8_HEAP_CPPGC_VISITOR_H_

8
#include "include/cppgc/persistent.h"
9
#include "include/cppgc/visitor.h"
10
#include "src/heap/cppgc/heap-object-header.h"
11 12 13 14

namespace cppgc {
namespace internal {

15 16 17 18
class HeapBase;
class HeapObjectHeader;
class PageBackend;

19 20 21 22 23
class VisitorFactory {
 public:
  static constexpr Visitor::Key CreateKey() { return {}; }
};

24 25
// Base visitor that is allowed to create a public cppgc::Visitor object and
// use its internals.
26 27
class VisitorBase : public cppgc::Visitor {
 public:
28
  VisitorBase() : cppgc::Visitor(VisitorFactory::CreateKey()) {}
29 30 31 32
  ~VisitorBase() override = default;

  VisitorBase(const VisitorBase&) = delete;
  VisitorBase& operator=(const VisitorBase&) = delete;
33 34 35 36 37 38 39 40 41 42 43

  template <typename T>
  void TraceRootForTesting(const Persistent<T>& p, const SourceLocation& loc) {
    TraceRoot(p, loc);
  }

  template <typename T>
  void TraceRootForTesting(const WeakPersistent<T>& p,
                           const SourceLocation& loc) {
    TraceRoot(p, loc);
  }
44 45
};

46
// Regular visitor that additionally allows for conservative tracing.
47
class ConservativeTracingVisitor {
48
 public:
49 50 51 52 53 54
  ConservativeTracingVisitor(HeapBase&, PageBackend&, cppgc::Visitor&);
  virtual ~ConservativeTracingVisitor() = default;

  ConservativeTracingVisitor(const ConservativeTracingVisitor&) = delete;
  ConservativeTracingVisitor& operator=(const ConservativeTracingVisitor&) =
      delete;
55 56 57 58 59 60 61 62 63 64 65

  void TraceConservativelyIfNeeded(const void*);

 protected:
  using TraceConservativelyCallback = void(ConservativeTracingVisitor*,
                                           const HeapObjectHeader&);
  virtual void VisitConservatively(HeapObjectHeader&,
                                   TraceConservativelyCallback) {}

  HeapBase& heap_;
  PageBackend& page_backend_;
66
  cppgc::Visitor& visitor_;
67 68
};

69 70 71 72
}  // namespace internal
}  // namespace cppgc

#endif  // V8_HEAP_CPPGC_VISITOR_H_