1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
// 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.
#include "src/heap/cppgc/heap.h"
#include "include/cppgc/heap-consistency.h"
#include "src/heap/base/stack.h"
#include "src/heap/cppgc/garbage-collector.h"
#include "src/heap/cppgc/gc-invoker.h"
#include "src/heap/cppgc/heap-object-header.h"
#include "src/heap/cppgc/heap-visitor.h"
#include "src/heap/cppgc/marker.h"
#include "src/heap/cppgc/marking-verifier.h"
#include "src/heap/cppgc/prefinalizer-handler.h"
#include "src/heap/cppgc/stats-collector.h"
#include "src/heap/cppgc/sweeper.h"
namespace cppgc {
namespace {
void VerifyCustomSpaces(
const std::vector<std::unique_ptr<CustomSpaceBase>>& custom_spaces) {
// Ensures that user-provided custom spaces have indices that form a sequence
// starting at 0.
#ifdef DEBUG
for (size_t i = 0; i < custom_spaces.size(); ++i) {
DCHECK_EQ(i, custom_spaces[i]->GetCustomSpaceIndex().value);
}
#endif // DEBUG
}
} // namespace
std::unique_ptr<Heap> Heap::Create(std::shared_ptr<cppgc::Platform> platform,
cppgc::Heap::HeapOptions options) {
DCHECK(platform.get());
VerifyCustomSpaces(options.custom_spaces);
return std::make_unique<internal::Heap>(std::move(platform),
std::move(options));
}
void Heap::ForceGarbageCollectionSlow(const char* source, const char* reason,
Heap::StackState stack_state) {
internal::Heap::From(this)->CollectGarbage(
{internal::GarbageCollector::Config::CollectionType::kMajor, stack_state,
MarkingType::kAtomic, SweepingType::kAtomic,
internal::GarbageCollector::Config::FreeMemoryHandling::
kDiscardWherePossible,
internal::GarbageCollector::Config::IsForcedGC::kForced});
}
AllocationHandle& Heap::GetAllocationHandle() {
return internal::Heap::From(this)->object_allocator();
}
HeapHandle& Heap::GetHeapHandle() { return *internal::Heap::From(this); }
namespace internal {
namespace {
class Unmarker final : private HeapVisitor<Unmarker> {
friend class HeapVisitor<Unmarker>;
public:
explicit Unmarker(RawHeap& heap) { Traverse(heap); }
private:
bool VisitHeapObjectHeader(HeapObjectHeader& header) {
if (header.IsMarked()) header.Unmark();
return true;
}
};
void CheckConfig(Heap::Config config, Heap::MarkingType marking_support,
Heap::SweepingType sweeping_support) {
CHECK_WITH_MSG(
(config.collection_type != Heap::Config::CollectionType::kMinor) ||
(config.stack_state == Heap::Config::StackState::kNoHeapPointers),
"Minor GCs with stack is currently not supported");
CHECK_LE(static_cast<int>(config.marking_type),
static_cast<int>(marking_support));
CHECK_LE(static_cast<int>(config.sweeping_type),
static_cast<int>(sweeping_support));
}
} // namespace
Heap::Heap(std::shared_ptr<cppgc::Platform> platform,
cppgc::Heap::HeapOptions options)
: HeapBase(platform, options.custom_spaces, options.stack_support),
gc_invoker_(this, platform_.get(), options.stack_support),
growing_(&gc_invoker_, stats_collector_.get(),
options.resource_constraints, options.marking_support,
options.sweeping_support),
marking_support_(options.marking_support),
sweeping_support_(options.sweeping_support) {
CHECK_IMPLIES(options.marking_support != MarkingType::kAtomic,
platform_->GetForegroundTaskRunner());
CHECK_IMPLIES(options.sweeping_support != SweepingType::kAtomic,
platform_->GetForegroundTaskRunner());
}
Heap::~Heap() {
subtle::NoGarbageCollectionScope no_gc(*this);
// Finish already running GC if any, but don't finalize live objects.
sweeper_.FinishIfRunning();
}
void Heap::CollectGarbage(Config config) {
DCHECK_EQ(Config::MarkingType::kAtomic, config.marking_type);
CheckConfig(config, marking_support_, sweeping_support_);
if (in_no_gc_scope()) return;
config_ = config;
if (!IsMarking()) {
StartGarbageCollection(config);
}
DCHECK(IsMarking());
FinalizeGarbageCollection(config.stack_state);
}
void Heap::StartIncrementalGarbageCollection(Config config) {
DCHECK_NE(Config::MarkingType::kAtomic, config.marking_type);
DCHECK_NE(marking_support_, MarkingType::kAtomic);
CheckConfig(config, marking_support_, sweeping_support_);
if (IsMarking() || in_no_gc_scope()) return;
config_ = config;
StartGarbageCollection(config);
}
void Heap::FinalizeIncrementalGarbageCollectionIfRunning(Config config) {
DCHECK_NE(marking_support_, MarkingType::kAtomic);
CheckConfig(config, marking_support_, sweeping_support_);
if (!IsMarking()) return;
DCHECK(!in_no_gc_scope());
DCHECK_NE(Config::MarkingType::kAtomic, config_.marking_type);
config_ = config;
FinalizeGarbageCollection(config.stack_state);
}
void Heap::StartGarbageCollection(Config config) {
DCHECK(!IsMarking());
DCHECK(!in_no_gc_scope());
// Finish sweeping in case it is still running.
sweeper_.FinishIfRunning();
epoch_++;
#if defined(CPPGC_YOUNG_GENERATION)
if (config.collection_type == Config::CollectionType::kMajor)
Unmarker unmarker(raw_heap());
#endif
const Marker::MarkingConfig marking_config{
config.collection_type, config.stack_state, config.marking_type,
config.is_forced_gc};
marker_ = MarkerFactory::CreateAndStartMarking<Marker>(
AsBase(), platform_.get(), marking_config);
}
void Heap::FinalizeGarbageCollection(Config::StackState stack_state) {
DCHECK(IsMarking());
DCHECK(!in_no_gc_scope());
CHECK(!in_disallow_gc_scope());
config_.stack_state = stack_state;
if (override_stack_state_) {
config_.stack_state = *override_stack_state_;
}
SetStackEndOfCurrentGC(v8::base::Stack::GetCurrentStackPosition());
in_atomic_pause_ = true;
{
// This guards atomic pause marking, meaning that no internal method or
// external callbacks are allowed to allocate new objects.
cppgc::subtle::DisallowGarbageCollectionScope no_gc_scope(*this);
marker_->FinishMarking(config_.stack_state);
}
marker_.reset();
ExecutePreFinalizers();
// TODO(chromium:1056170): replace build flag with dedicated flag.
#if DEBUG
MarkingVerifier verifier(*this);
verifier.Run(config_.stack_state, stack_end_of_current_gc(),
stats_collector()->marked_bytes());
#endif
subtle::NoGarbageCollectionScope no_gc(*this);
const Sweeper::SweepingConfig sweeping_config{
config_.sweeping_type,
Sweeper::SweepingConfig::CompactableSpaceHandling::kSweep,
config_.free_memory_handling};
sweeper_.Start(sweeping_config);
in_atomic_pause_ = false;
sweeper_.NotifyDoneIfNeeded();
}
void Heap::DisableHeapGrowingForTesting() { growing_.DisableForTesting(); }
void Heap::FinalizeIncrementalGarbageCollectionIfNeeded(
Config::StackState stack_state) {
StatsCollector::EnabledScope stats_scope(
stats_collector(), StatsCollector::kMarkIncrementalFinalize);
FinalizeGarbageCollection(stack_state);
}
void Heap::StartIncrementalGarbageCollectionForTesting() {
DCHECK(!IsMarking());
DCHECK(!in_no_gc_scope());
StartGarbageCollection({Config::CollectionType::kMajor,
Config::StackState::kNoHeapPointers,
Config::MarkingType::kIncrementalAndConcurrent,
Config::SweepingType::kIncrementalAndConcurrent});
}
void Heap::FinalizeIncrementalGarbageCollectionForTesting(
EmbedderStackState stack_state) {
DCHECK(!in_no_gc_scope());
DCHECK(IsMarking());
FinalizeGarbageCollection(stack_state);
sweeper_.FinishIfRunning();
}
} // namespace internal
} // namespace cppgc
|