blob: 8d2efc080e2dd4dee2b169369c64b6a086a21a2f (
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
|
#include <stdbool.h>
#include "./patio/patio.h"
#include "./alloc.h"
#include "./gc.h"
#include "./primitives.h"
// == GC objects ==
bool is_gc_object(ShedObject object) {
// Check that the object is not one of:
// * unit (0)
// * false (0)
// * true (1)
return !(object == 0 || object == 1);
}
// == Marking ==
// The mark value for each object is initialized to 0. Each mark phase
// alternates between using 0b010 and 0b100 as the mark value. We check only for
// the bit set to 1, meaning static values can have a mark value of 0b110 to be
// considered always marked.
PatioInt current_mark_value = 0b010;
void alternate_mark_value() {
current_mark_value ^= 0b110;
}
bool is_marked(struct ShedGcObjectHeader* object) {
return (object->mark & current_mark_value) == current_mark_value;
}
void mark(struct ShedGcObjectHeader* object) {
if (object == NULL || is_marked(object)) {
return;
}
object->mark = current_mark_value;
struct ShedGcObjectHeader** children = (struct ShedGcObjectHeader**) (
((void*) object) + object->children_offset
);
for (size_t child_index = 0; child_index < object->children_count; child_index++) {
mark(children[child_index]);
}
}
// == Roots ==
typedef size_t ShedGcRootIndex;
ShedGcRootIndex gc_root_stack_index = 0;
struct ShedGcObjectHeader* gc_root_stack[16384];
ShedGcRootIndex shed__runtime__gc_get_root_stack_index() {
return gc_root_stack_index;
}
void shed__runtime__gc_set_root_stack_index(ShedGcRootIndex new_gc_root_stack_index) {
gc_root_stack_index = new_gc_root_stack_index;
}
static void shed__runtime__gc_push_root_gc_object(struct ShedGcObjectHeader* gc_root) {
gc_root_stack[gc_root_stack_index] = gc_root;
gc_root_stack_index++;
}
void shed__runtime__gc_push_root(ShedObject gc_root) {
if (!is_gc_object(gc_root)) {
return;
}
shed__runtime__gc_push_root_gc_object((struct ShedGcObjectHeader*) gc_root);
}
// == Allocation ==
struct ShedGcObjectHeader* first = NULL;
void* shed__runtime__gc_alloc(size_t size, uint32_t children_offset, uint32_t children_count) {
struct ShedGcObjectHeader* object = malloc(size);
object->next = first;
object->mark = 0;
object->children_offset = children_offset;
object->children_count = children_count;
// TODO: is it possible to leave adding the root to the caller, and avoid
// having to add so many roots? (Since some values will immediately become a
// descendant of a root.)
shed__runtime__gc_push_root_gc_object(object);
first = object;
return object;
}
// == Collection ==
void mark_reachable() {
for (size_t i = 0; i < gc_root_stack_index; i++) {
mark(gc_root_stack[i]);
}
}
void sweep() {
struct ShedGcObjectHeader** object = &first;
while (*object != NULL) {
if (is_marked(*object)) {
object = &(*object)->next;
} else {
struct ShedGcObjectHeader* next = (*object)->next;
free(*object);
*object = next;
}
}
}
void shed__runtime__gc_collect() {
// We only want to collect periodically, but we currently always collect to
// hopefully surface errors more quickly.
alternate_mark_value();
mark_reachable();
sweep();
}
|