blob: 77d2d3c33e7426e58c2595952e5331111a616280 (
plain)
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
|
// 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/sweeper.h"
#include <vector>
#include "src/heap/cppgc/free-list.h"
#include "src/heap/cppgc/globals.h"
#include "src/heap/cppgc/heap-object-header-inl.h"
#include "src/heap/cppgc/heap-object-header.h"
#include "src/heap/cppgc/heap-page.h"
#include "src/heap/cppgc/heap-space.h"
#include "src/heap/cppgc/heap-visitor.h"
#include "src/heap/cppgc/object-start-bitmap-inl.h"
#include "src/heap/cppgc/object-start-bitmap.h"
#include "src/heap/cppgc/raw-heap.h"
#include "src/heap/cppgc/sanitizers.h"
namespace cppgc {
namespace internal {
namespace {
class ObjectStartBitmapVerifier
: private HeapVisitor<ObjectStartBitmapVerifier> {
friend class HeapVisitor<ObjectStartBitmapVerifier>;
public:
void Verify(RawHeap* heap) { Traverse(heap); }
private:
bool VisitNormalPage(NormalPage* page) {
// Remember bitmap and reset previous pointer.
bitmap_ = &page->object_start_bitmap();
prev_ = nullptr;
return false;
}
bool VisitHeapObjectHeader(HeapObjectHeader* header) {
if (header->IsLargeObject()) return true;
auto* raw_header = reinterpret_cast<ConstAddress>(header);
CHECK(bitmap_->CheckBit(raw_header));
if (prev_) {
CHECK_EQ(prev_, bitmap_->FindHeader(raw_header - 1));
}
prev_ = header;
return true;
}
ObjectStartBitmap* bitmap_ = nullptr;
HeapObjectHeader* prev_ = nullptr;
};
struct SpaceState {
BaseSpace::Pages unswept_pages;
};
using SpaceStates = std::vector<SpaceState>;
bool SweepNormalPage(NormalPage* page) {
constexpr auto kAtomicAccess = HeapObjectHeader::AccessMode::kAtomic;
auto* space = NormalPageSpace::From(page->space());
ObjectStartBitmap& bitmap = page->object_start_bitmap();
bitmap.Clear();
Address start_of_gap = page->PayloadStart();
for (Address begin = page->PayloadStart(), end = page->PayloadEnd();
begin != end;) {
HeapObjectHeader* header = reinterpret_cast<HeapObjectHeader*>(begin);
const size_t size = header->GetSize();
// Check if this is a free list entry.
if (header->IsFree<kAtomicAccess>()) {
SET_MEMORY_INACCESIBLE(header, std::min(kFreeListEntrySize, size));
begin += size;
continue;
}
// Check if object is not marked (not reachable).
if (!header->IsMarked<kAtomicAccess>()) {
header->Finalize();
SET_MEMORY_INACCESIBLE(header, size);
begin += size;
continue;
}
// The object is alive.
const Address header_address = reinterpret_cast<Address>(header);
if (start_of_gap != header_address) {
space->AddToFreeList(start_of_gap,
static_cast<size_t>(header_address - start_of_gap));
}
header->Unmark<kAtomicAccess>();
bitmap.SetBit(begin);
begin += size;
start_of_gap = begin;
}
if (start_of_gap != page->PayloadStart() &&
start_of_gap != page->PayloadEnd()) {
space->AddToFreeList(
start_of_gap, static_cast<size_t>(page->PayloadEnd() - start_of_gap));
}
const bool is_empty = (start_of_gap == page->PayloadStart());
return is_empty;
}
// This visitor:
// - resets linear allocation buffers and clears free lists for all spaces;
// - moves all Heap pages to local Sweeper's state (SpaceStates).
class PrepareForSweepVisitor final
: public HeapVisitor<PrepareForSweepVisitor> {
public:
explicit PrepareForSweepVisitor(SpaceStates* states) : states_(states) {}
bool VisitNormalPageSpace(NormalPageSpace* space) {
space->ResetLinearAllocationBuffer();
space->free_list().Clear();
(*states_)[space->index()].unswept_pages = space->RemoveAllPages();
return true;
}
bool VisitLargePageSpace(LargePageSpace* space) {
(*states_)[space->index()].unswept_pages = space->RemoveAllPages();
return true;
}
private:
SpaceStates* states_;
};
class MutatorThreadSweepVisitor final
: private HeapVisitor<MutatorThreadSweepVisitor> {
friend class HeapVisitor<MutatorThreadSweepVisitor>;
public:
explicit MutatorThreadSweepVisitor(SpaceStates* space_states) {
for (SpaceState& state : *space_states) {
for (BasePage* page : state.unswept_pages) {
Traverse(page);
}
state.unswept_pages.clear();
}
}
private:
bool VisitNormalPage(NormalPage* page) {
const bool is_empty = SweepNormalPage(page);
if (is_empty) {
NormalPage::Destroy(page);
} else {
page->space()->AddPage(page);
}
return true;
}
bool VisitLargePage(LargePage* page) {
if (page->ObjectHeader()->IsMarked()) {
page->space()->AddPage(page);
} else {
page->ObjectHeader()->Finalize();
LargePage::Destroy(page);
}
return true;
}
};
} // namespace
class Sweeper::SweeperImpl final {
public:
explicit SweeperImpl(RawHeap* heap) : heap_(heap) {
space_states_.resize(heap_->size());
}
void Start(Config config) {
is_in_progress_ = true;
#if DEBUG
ObjectStartBitmapVerifier().Verify(heap_);
#endif
PrepareForSweepVisitor(&space_states_).Traverse(heap_);
if (config == Config::kAtomic) {
Finish();
} else {
DCHECK_EQ(Config::kIncrementalAndConcurrent, config);
// TODO(chromium:1056170): Schedule concurrent sweeping.
}
}
void Finish() {
if (!is_in_progress_) return;
MutatorThreadSweepVisitor s(&space_states_);
is_in_progress_ = false;
}
private:
SpaceStates space_states_;
RawHeap* heap_;
bool is_in_progress_ = false;
};
Sweeper::Sweeper(RawHeap* heap) : impl_(std::make_unique<SweeperImpl>(heap)) {}
Sweeper::~Sweeper() = default;
void Sweeper::Start(Config config) { impl_->Start(config); }
void Sweeper::Finish() { impl_->Finish(); }
} // namespace internal
} // namespace cppgc
|