forked from llvm-class/project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc.h
465 lines (375 loc) · 11.8 KB
/
gc.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#ifndef GC_H
#define GC_H
#include <array>
#include <deque>
#include <vector>
#include <cstdint>
#include <cstddef>
#include <iostream>
#include <cassert>
#include <cstring>
// #define GC_DEBUG 1
namespace gc {
typedef size_t index_t;
// NodeIndex is a container to pass around the location of an object on the
// heap. Location is given by page index and object index (relative to page).
struct NodeIndex {
public:
static constexpr index_t MaxPage = 0xffffffff;
NodeIndex(index_t pageNum, index_t idx) : pageNum(pageNum), idx(idx) {}
const index_t pageNum;
const index_t idx;
static const NodeIndex & nodeNotFound() {
static NodeIndex nodeNotFound(MaxPage, 0);
return nodeNotFound;
}
bool found() const {
return pageNum != MaxPage;
}
};
// A Page of memory holding a constant number of equally sized objects T
template <typename T>
class Page {
public:
// Node is the memory allocated to hold one T
struct Node {
uint8_t data[sizeof(T)];
};
static_assert(sizeof(Node) == sizeof(T), "");
static constexpr size_t nodeSize = sizeof(Node);
static constexpr index_t pageSize = 3600 / nodeSize;
static constexpr size_t size = pageSize * nodeSize;
// A valid object is:
// 1. Within the page boundaries
// 2. Pointing to the beginning of a Node cell
// 3. Pointing to a node cell which is not unused
bool isValidObj(void* ptr) const {
auto addr = reinterpret_cast<uintptr_t>(ptr);
if (addr < first || addr > last)
return false;
auto idx = getClosestIndex(addr);
return reinterpret_cast<uintptr_t>(&node[idx]) == addr &&
isAlloc[idx];
}
index_t getIndex(void* ptr) const {
auto addr = reinterpret_cast<uintptr_t>(ptr);
return getClosestIndex(addr);
}
T* alloc() {
if (freelist.size()) {
auto idx = freelist.front();
freelist.pop_front();
assert(!isAlloc[idx] && !mark[idx]);
isAlloc[idx] = true;
return getAt(idx);
}
return nullptr;
}
Page() : first(reinterpret_cast<uintptr_t>(&node[0])),
last(reinterpret_cast<uintptr_t>(&node[pageSize - 1])) {
assert(getClosestIndex((uintptr_t)first + 1) == 0);
assert(getClosestIndex((uintptr_t)last + 1) == pageSize - 1);
mark.fill(false);
isAlloc.fill(false);
for (index_t i = 0; i < pageSize; ++i)
freelist.push_back(i);
}
void sweep() {
for (index_t i = 0; i < pageSize; ++i) {
if (!mark[i]) {
if (isAlloc[i]) {
freeNode(i);
}
} else {
mark[i] = false;
}
}
}
index_t free() const {
return freelist.size() * nodeSize;
}
void verify() const {
for (auto i : freelist)
assert(!mark[i] && !isAlloc[i]);
for (index_t i = 0; i < pageSize; ++i) {
assert(isAlloc[i] || !mark[i]);
}
}
bool empty() const {
return freelist.size() == pageSize;
}
void setMark(index_t idx) {
assert(isAlloc[idx]);
assert(!mark[idx]);
mark[idx] = true;
}
bool isMarked(index_t idx) const {
return mark[idx];
}
Page(Page const &) = delete;
void operator= (Page const &) = delete;
const uintptr_t first, last;
private:
// Freeing a node will
// 1. call the destructor of said object
// 2. push the cell to the freelist
void freeNode(index_t idx) {
getAt(idx)->~T();
#ifdef GC_DEBUG
memset(&node[idx], 0xd, nodeSize);
#endif
freelist.push_back(idx);
isAlloc[idx] = false;
}
T* getAt(index_t idx) {
return reinterpret_cast<T*>(&node[idx]);
}
index_t getClosestIndex(uintptr_t addr) const {
uintptr_t start = reinterpret_cast<uintptr_t>(&node);
// Integer division -> floor
return (addr - start) / nodeSize;
}
std::array<Node, pageSize> node;
std::array<bool, pageSize> mark;
std::array<bool, pageSize> isAlloc;
std::deque<index_t> freelist;
};
// Arena implements the arena interface plus the alloc()
// function. Memory is provided by a pool of Page<T>'s.
template <typename T>
class Arena {
public:
static constexpr size_t nodeSize = Page<T>::nodeSize;
T* alloc(bool grow) {
for (auto p : page) {
auto res = p->alloc();
if (res) return res;
}
if (page.size() > 0 && !grow) return nullptr;
if (page.size() == NodeIndex::MaxPage) {
throw std::bad_alloc();
}
auto p = new Page<T>;
registerPage(p);
auto n = p->alloc();
assert(n);
return n;
}
NodeIndex findNode(void* ptr) {
uintptr_t addr = reinterpret_cast<uintptr_t>(ptr);
// Simple performance hack, which allows us to quickly discard
// implausible pointers without iterating the page list.
if (addr < minAddr || addr > maxAddr)
return NodeIndex::nodeNotFound();
for (index_t i = 0; i < page.size(); i++) {
if (page[i]->isValidObj(ptr)) {
return NodeIndex(i, page[i]->getIndex(ptr));
}
}
return NodeIndex::nodeNotFound();
}
void sweep() {
for (auto pi = page.begin(); pi != page.end(); ) {
auto p = *pi;
p->sweep();
// If a page is completely empty we release it
if (p->empty()) {
delete p;
pi = page.erase(pi);
} else {
pi++;
}
}
}
size_t free() const {
size_t f = 0;
for (auto p : page)
f += p->free();
return f;
}
size_t size() const {
return page.size() * Page<T>::size;
}
void verify() const {
for (auto p : page)
p->verify();
}
bool isMarked(NodeIndex & i) const {
return page[i.pageNum]->isMarked(i.idx);
}
void setMark(NodeIndex & i) {
page[i.pageNum]->setMark(i.idx);
}
Arena() : minAddr(-1), maxAddr(0) {}
~Arena() {
for (auto p : page) {
delete p;
}
page.clear();
}
Arena(Arena const &) = delete;
void operator= (Arena const &) = delete;
private:
void registerPage(Page<T> * p) {
// TODO: we never update the boundaries if we remove a page
page.push_front(p);
if (p->first < minAddr)
minAddr = p->first;
if (p->last > maxAddr)
maxAddr = p->last;
}
std::deque<Page<T> *> page;
uintptr_t minAddr, maxAddr;
};
class GarbageCollector {
public:
constexpr static size_t INITIAL_HEAP_SIZE = 4096;
constexpr static double GC_GROW_RATE = 1.3f;
// Interface to request memory from the GC
template <typename T>
static void* alloc() {
return inst().doAlloc<T>();
}
// GC users must implement the recursion for their Object types.
// They can leave the implementation empty for leaf nodes or delegate to
// genericMarkImpl for a naive but simple heap scan.
template <typename T>
void markImpl(T * obj);
// A very generic mark implementation who visits every possible slot of
// an object.
template <typename T>
void genericMarkImpl(T * obj) {
static_assert(sizeof(T) % sizeof(void*) == 0,
"Cannot deal with unaligned objects");
void ** finger = reinterpret_cast<void**>(obj);
size_t slots = sizeof(obj) / sizeof(void*);
for (size_t i = 0; i < slots; ++i) {
markMaybe(*finger);
++finger;
}
}
private:
template <typename T>
void* doAlloc() {
auto res = arena<T>().alloc(false);
if (!res) {
doGc();
res = arena<T>().alloc(false);
if (!res) res = arena<T>().alloc(true);
}
assert(maybePointer(res));
return res;
}
// The following template foo makes sure we have one arena for each
// HeapObject type. The area is available through area<T>() function.
template <typename T>
struct ArenaInst {
public:
static Arena<T> & get() {
static Arena<T> arena;
return arena;
}
};
template <typename T>
Arena<T> & arena() const {
return ArenaInst<T>::get();
}
size_t size() const;
size_t free() const;
void scanStack() {
// Clobber all registers:
// -> forces all variables currently hold in registers to be spilled
// to the stack where our stackScan can find them.
__asm__ __volatile__ ( "nop" : :
: "%rax", "%rbx", "%rcx", "%rdx", "%rsi", "%rdi",
"%r8", "%r9", "%r10", "%r11", "%r12",
"%r13", "%r14", "%r15");
_scanStack();
}
// Performance Hack: we assume the first page of memory not to contain any
// heap pointers. Maybe there is also a reasonable upper limit?
bool maybePointer(void* p) {
return p > (void*)4096;
}
const void * BOTTOM_OF_STACK;
// The stack scan traverses the memory of the C stack and looks at every
// possible stack slot. If we find a valid heap pointer we mark the
// object as well as all objects reachable through it as live.
void __attribute__ ((noinline)) _scanStack() {
void ** p = (void**)__builtin_frame_address(0);
while (p < BOTTOM_OF_STACK) {
if (maybePointer(*p))
markMaybe(*p);
p++;
}
}
void mark() {
// TODO: maybe some mechanism to define static roots?
scanStack();
}
// The core mark & sweep algorithm
void doGc() {
#ifdef GC_DEBUG
unsigned memUsage = size() - free();
verify();
#endif
mark();
#ifdef GC_DEBUG
verify();
#endif
sweep();
#ifdef GC_DEBUG
verify();
unsigned memUsage2 = size() - free();
assert(memUsage2 <= memUsage);
std::cout << "reclaiming " << memUsage - memUsage2
<< "b, used " << memUsage2 << "b, total " << size() << "b\n";
#endif
}
// First step of marking is to determine if a pointer is a valid object and
// if yes to find the arena and page which contains it. Then the type
// specific part is handled in mark<T>(ptr, i).
void markMaybe(void * ptr);
// Sometimes we already know the type of a pointer which gives us faster
// lookup.
template <typename T>
void mark(T * obj) {
auto i = arena<T>().findNode(obj);
assert(i.found());
mark(obj, i);
}
// Generic mark algorithm is to check if an object was marked before. Iff
// not we will mark it first (to deal with cycles) and then delegate to the
// user defined markImpl<T> for the specific type to scan its child nodes.
template <typename T>
void mark(T * obj, NodeIndex & i) {
auto & a = arena<T>();
if (!a.isMarked(i)) {
a.setMark(i);
markImpl<T>(obj);
}
}
// Delete everything which is not reachable anymore
void sweep();
void verify() const;
static GarbageCollector & inst() {
static GarbageCollector gc;
return gc;
}
// TODO: setting the BOTTOM_OF_STACK here is a bit brittle, as we just
// assume that the site of the first allocation is the earliest call frame
// holding live objects.
GarbageCollector() : BOTTOM_OF_STACK(__builtin_frame_address(0)) { }
GarbageCollector(GarbageCollector const &) = delete;
void operator=(GarbageCollector const &) = delete;
};
} // namespace gc
template <typename OBJECT>
struct HeapObject {
// Overriding new makes sure memory is allocated through the GC
static void* operator new(size_t sz) {
return gc::GarbageCollector::alloc<OBJECT>();
}
void operator delete(void*) = delete;
};
#endif