/
marksweep_heap.h
184 lines (147 loc) · 4.56 KB
/
marksweep_heap.h
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
#ifndef MARKSWEEP_HEAP_H
#define MARKSWEEP_HEAP_H
#include <unordered_set>
#include <vector>
class MarkSweepHeap; // forward decl for circular dep
// The set of objects where the mark and sweep algorithm starts. Terminology:
// to "root" an object means "add it to the root set".
class RootSet {
public:
// start with 1 live frame and e.g. 32 reserved ones
explicit RootSet(int num_reserved) : num_frames_(1) {
assert(num_reserved > 1);
stack_.reserve(num_reserved);
for (int i = 0; i < num_reserved; ++i) {
stack_.emplace_back(); // Construct std::vector frame IN PLACE.
stack_.back().reserve(16); // Reserve 16 rooted variables per frame.
}
}
// Called on function entry
void PushFrame() {
// Construct more std::vector frames if necessary. We reuse vectors to
// avoid constructing one on every function call.
int num_constructed = stack_.size();
if (num_frames_ >= num_constructed) {
stack_.emplace_back();
stack_.back().reserve(16);
#if 0
num_constructed = roots_.size();
log("num_frames_ %d, num_constructed %d", num_frames_, num_constructed);
assert(num_frames_ + 1 == num_constructed);
#endif
}
num_frames_++;
// log("PushFrame -> %d", num_frames_);
}
// Called on function exit
void PopFrame() {
// Remove all roots owned by the top frame. We're REUSING frames, so not
// calling vector<>::pop().
stack_[num_frames_ - 1].clear();
num_frames_--;
// log("PopFrame -> %d", num_frames_);
}
// Called when returning a value (except in trivial passthrough case)
void RootOnReturn(Obj* root) {
// log("RootOnReturn %p %d", root, num_frames_);
if (root == nullptr) { // No reason to add it
return;
}
// We should have 2 frames because we start with 1 for main(), and main()
// itself can't return GC objects.
assert(num_frames_ > 1);
// Owned by the frame BELOW
stack_[num_frames_ - 2].push_back(root);
}
// Called in 2 situations:
// - the "leaf" Allocate(), which does not have a RootsFrame
// - when catching exceptions:
// catch (IOError e) { gHeap.RootInCurrentFrame(e); }
void RootInCurrentFrame(Obj* root) {
if (root == nullptr) { // No reason to add it
return;
}
assert(num_frames_ > 0);
stack_[num_frames_ - 1].push_back(root);
}
// For testing
int NumFrames() {
return num_frames_;
}
// Calculate size of root set, for unit tests only.
int NumRoots() {
int result = 0;
for (int i = 0; i < num_frames_; ++i) {
result += stack_[i].size();
}
return result;
}
void MarkRoots(MarkSweepHeap* heap);
// A stack of frames that's updated in parallel the call stack.
// This representation is appropriate since multiple stack frames are "in
// play" at once. That is, RootOnReturn() may mutate root_set_[1] while
// root_set_[2] is being pushed/popped/modified.
std::vector<std::vector<Obj*>> stack_;
int num_frames_ = 0; // frames 0 to N-1 are valid
};
class MarkSweepHeap {
public:
// reserve 32 frames to start
MarkSweepHeap() : root_set_(32) {
}
void Init(); // use default threshold
void Init(int gc_threshold);
//
// OLD Local Var Rooting
//
void PushRoot(Obj** p) {
roots_.push_back(p);
}
void PopRoot() {
roots_.pop_back();
}
//
// NEW Return Value Rooting
//
void RootOnReturn(Obj* root) {
root_set_.RootOnReturn(root);
}
void RootInCurrentFrame(Obj* root) {
root_set_.RootInCurrentFrame(root);
}
void* Allocate(size_t num_bytes);
void* Reallocate(void* p, size_t num_bytes);
int Collect();
void MarkObjects(Obj* obj);
void Sweep();
// Cleanup at the end of main() to remain ASAN-safe
void CleanProcessExit();
// Faster exit
void FastProcessExit();
void Report();
bool is_initialized_ = true; // mark/sweep doesn't need to be initialized
// In number of live objects, since we aren't keeping track of total bytes
int gc_threshold_;
// Cumulative stats
int64_t num_allocated_ = 0;
int64_t bytes_allocated_ = 0;
int64_t num_collections_ = 0;
int max_live_ = 0; // max # live after a collection
// current stats
int num_live_ = 0;
// Should we keep track of sizes?
// int64_t bytes_live_ = 0;
// OLD rooting
std::vector<Obj**> roots_;
// NEW rooting
RootSet root_set_;
std::vector<void*> live_objs_;
std::unordered_set<void*> marked_;
private:
void DoProcessExit(bool fast_exit);
DISALLOW_COPY_AND_ASSIGN(MarkSweepHeap);
};
#if defined(MARK_SWEEP) && !defined(BUMP_LEAK)
extern MarkSweepHeap gHeap;
#endif
#endif // MARKSWEEP_HEAP_H