-
Notifications
You must be signed in to change notification settings - Fork 2
/
gc.cpp
72 lines (60 loc) · 1.71 KB
/
gc.cpp
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
#include "gc.h"
#include "runtime.h"
namespace gc {
/*
* Type specific parts
*/
template<>
void GarbageCollector::markImpl<RVal>(RVal * value) {
// Leaf node, nothing to do
};
template<>
void GarbageCollector::markImpl<Environment>(Environment * env) {
for (int i = 0; i < env->size; ++i) {
mark(env->bindings[i].value);
}
if (env->parent)
mark(env->parent);
}
/*
* Generic GC implementation. Most things are here since they depend on the
* HEAP_OBJECTS list provided by the runtime. HEAP_OBJECTS is a second order
* macro which lets us statically loop and generate code for each entry.
*/
size_t GarbageCollector::size() const {
size_t size = 0;
#define O(T) size += arena<T>().size();
HEAP_OBJECTS(O);
#undef O
return size;
}
size_t GarbageCollector::free() const {
size_t size = 0;
#define O(T) size += arena<T>().free();
HEAP_OBJECTS(O);
#undef O
return size;
}
void GarbageCollector::markMaybe(void * ptr) {
#define O(T) \
{ \
auto i = arena<T>().findNode(ptr); \
if (i.found()) { \
T * obj = reinterpret_cast<T*>(ptr); \
mark(obj, i); \
} \
}
HEAP_OBJECTS(O);
#undef O
}
void GarbageCollector::sweep() {
#define O(T) arena<T>().sweep();
HEAP_OBJECTS(O);
#undef O
}
void GarbageCollector::verify() const {
#define O(T) arena<T>().verify();
HEAP_OBJECTS(O);
#undef O
}
}