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_GARBAGE_COLLECTOR_H_
6#define V8_HEAP_CPPGC_GARBAGE_COLLECTOR_H_
7
8#include "include/cppgc/common.h"
9#include "src/heap/cppgc/marker.h"
10#include "src/heap/cppgc/sweeper.h"
11
12namespace cppgc {
13namespace internal {
14
15// GC interface that allows abstraction over the actual GC invocation. This is
16// needed to mock/fake GC for testing.
17class GarbageCollector {
18 public:
19  struct Config {
20    using CollectionType = Marker::MarkingConfig::CollectionType;
21    using StackState = cppgc::Heap::StackState;
22    using MarkingType = Marker::MarkingConfig::MarkingType;
23    using SweepingType = Sweeper::SweepingConfig::SweepingType;
24    using FreeMemoryHandling = Sweeper::SweepingConfig::FreeMemoryHandling;
25    using IsForcedGC = Marker::MarkingConfig::IsForcedGC;
26
27    static constexpr Config ConservativeAtomicConfig() {
28      return {CollectionType::kMajor, StackState::kMayContainHeapPointers,
29              MarkingType::kAtomic, SweepingType::kAtomic};
30    }
31
32    static constexpr Config PreciseAtomicConfig() {
33      return {CollectionType::kMajor, StackState::kNoHeapPointers,
34              MarkingType::kAtomic, SweepingType::kAtomic};
35    }
36
37    static constexpr Config ConservativeIncrementalConfig() {
38      return {CollectionType::kMajor, StackState::kMayContainHeapPointers,
39              MarkingType::kIncremental, SweepingType::kAtomic};
40    }
41
42    static constexpr Config PreciseIncrementalConfig() {
43      return {CollectionType::kMajor, StackState::kNoHeapPointers,
44              MarkingType::kIncremental, SweepingType::kAtomic};
45    }
46
47    static constexpr Config
48    PreciseIncrementalMarkingConcurrentSweepingConfig() {
49      return {CollectionType::kMajor, StackState::kNoHeapPointers,
50              MarkingType::kIncremental,
51              SweepingType::kIncrementalAndConcurrent};
52    }
53
54    static constexpr Config MinorPreciseAtomicConfig() {
55      return {CollectionType::kMinor, StackState::kNoHeapPointers,
56              MarkingType::kAtomic, SweepingType::kAtomic};
57    }
58
59    static constexpr Config MinorConservativeAtomicConfig() {
60      return {CollectionType::kMinor, StackState::kMayContainHeapPointers,
61              MarkingType::kAtomic, SweepingType::kAtomic};
62    }
63
64    CollectionType collection_type = CollectionType::kMajor;
65    StackState stack_state = StackState::kMayContainHeapPointers;
66    MarkingType marking_type = MarkingType::kAtomic;
67    SweepingType sweeping_type = SweepingType::kAtomic;
68    FreeMemoryHandling free_memory_handling = FreeMemoryHandling::kDoNotDiscard;
69    IsForcedGC is_forced_gc = IsForcedGC::kNotForced;
70  };
71
72  // Executes a garbage collection specified in config.
73  virtual void CollectGarbage(Config) = 0;
74  virtual void StartIncrementalGarbageCollection(Config) = 0;
75
76  // The current epoch that the GC maintains. The epoch is increased on every
77  // GC invocation.
78  virtual size_t epoch() const = 0;
79
80  // Returns a non-null state if the stack state if overriden.
81  virtual const EmbedderStackState* override_stack_state() const = 0;
82};
83
84}  // namespace internal
85}  // namespace cppgc
86
87#endif  // V8_HEAP_CPPGC_GARBAGE_COLLECTOR_H_
88