-
Notifications
You must be signed in to change notification settings - Fork 11
/
GC.HC
229 lines (203 loc) · 4.36 KB
/
GC.HC
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
#ifndef GC_HC
#define GC_HC
// Objects garbaged-collected:
// Env, Malval, PArray, String, List
// Objects not garbage-collected:
// Array, Hashmap, Symtab, SymtabEntry, HashmapEntry, Atom, Function,
// Closure
// This is because Arrays, Hashmaps, etc. are never used in isolation,
// but are used internally by PArray, String, Env, Malval, etc. So, we
// have functions for deleting these types that also delete their internal
// structures. This prevents the GC from doing extra work.
#include "Exception.HC"
#include "Env.HC"
#include "List.HC"
#include "PArray.HC"
#include "String.HC"
#include "Types.HC"
// Run the GC every 100000 allocations.
#define GC_PERIOD 100000
// Global array of all garbage-collected objects.
// _GC_objects is the active array. During the sweep phase we add
// reachable objects to the back array and then swap so it becomes the
// new front.
PArray *_GC_objects;
PArray *_GC_objects_back;
// Objects used by the interpreter that shouldn't be collected (but don't
// necessarily show up in any environments)
// Used in a LIFO manner during evaluation of terms, so we use a stack.
PArray *_GC_stack;
INT _GC_counter;
Env *_GC_root_env;
VOID init_GC()
{
_GC_root_env = NULL;
_GC_objects = _PArrayEmpty_GC;
_GC_objects_back = _PArrayEmpty_GC;
_GC_stack = _PArrayEmpty_GC;
_GC_counter = 0;
}
// only use with types compatible with the GCCommon interface
CHAR *mal_alloc(INT size)
{
CHAR *o = MAlloc(size);
(o(GCCommon*))->_reachable = FALSE;
PArrayPush(_GC_objects, o);
_GC_counter++;
return o;
}
VOID set_GC_root_env(Env *rootEnv)
{
_GC_root_env = rootEnv;
}
extern VOID _print_GC_object(GCCommon *o);
VOID _print_parray(PArray *pa)
{
INT i;
for (i = 0; i < PArraySize(pa); ++i) {
_print_GC_object(PArrayGet(pa, i));
}
}
VOID _print_list(List *l)
{
"list\n"; // TODO
}
VOID _print_GC_object(GCCommon *o)
{
switch (o->_gc_tag) {
case GC_ENV:
/* EnvPrint((Env*)o); */
"env\n";
break;
case GC_MALVAL:
StringPrintLn(PrintMalval(o(Malval*), TRUE, TRUE));
break;
case GC_PARRAY:
_print_parray(o(PArray*));
break;
case GC_STRING:
StringPrintLn(o(String*));
break;
case GC_LIST:
_print_list(o(List*));
break;
default:
throws("_GC_mark: unknown tag");
}
}
VOID GC_push(CHAR *o)
{
PArrayPush(_GC_stack, o);
}
VOID GC_pop()
{
PArrayPop(_GC_stack);
}
VOID GC_mark_object(GCCommon *o)
{
switch (o->_gc_tag) {
case GC_ENV:
EnvGCMark(o);
break;
case GC_MALVAL:
MalvalGCMark(o);
break;
case GC_PARRAY:
PArrayGCMark(o);
break;
case GC_STRING:
StringGCMark(o);
break;
case GC_LIST:
ListGCMark(o);
break;
default:
throws("_GC_mark: unknown tag");
}
}
VOID print_GC_stack()
{
_print_parray(_GC_stack);
}
VOID _GC_mark()
{
if (!_GC_root_env) return;
EnvGCMark(_GC_root_env);
INT i;
PArray *entries = _EnvEntries_GC(_GC_root_env);
for (i = 0; i < PArraySize(_GC_stack); ++i) {
GC_mark_object(PArrayGet(_GC_stack, i));
}
PArrayDelete(entries);
}
VOID _GC_sweep()
{
INT i;
for (i = 0; i < PArraySize(_GC_objects); ++i) {
GCCommon *o = PArrayGet(_GC_objects, i);
if (!o->_reachable) {
switch (o->_gc_tag) {
case GC_ENV:
EnvDelete(o(Env*));
break;
case GC_MALVAL:
MalvalDelete(o(Malval*));
break;
case GC_PARRAY:
PArrayDelete(o(PArray*));
break;
case GC_STRING:
StringDelete(o(String*));
break;
case GC_LIST:
ListDelete(o(List*));
break;
default:
throws("_GC_sweep: unknown tag");
}
}
else {
o->_reachable = FALSE; // reset reachable
PArrayPush(_GC_objects_back, o);
}
}
// Swap arrays and clear the new back array (old front).
PArray *temp = _GC_objects;
_GC_objects = _GC_objects_back;
_GC_objects_back = temp;
PArrayClear(_GC_objects_back);
}
VOID run_GC()
{
_GC_mark;
_GC_sweep;
}
VOID print_GC_status()
{
"_GC_objects size: %ld\n", PArraySize(_GC_objects);
"_GC_stack size: %ld\n", PArraySize(_GC_stack);
}
VOID check_for_GC()
{
if (_GC_counter > GC_PERIOD) {
/* print_GC_status; */
/* "running GC...\n"; */
run_GC;
/* print_GC_status; */
_GC_counter = 0;
}
}
VOID cleanup_GC()
{
PArrayClear(_GC_stack);
_GC_root_env = NULL;
run_GC;
PArrayDelete(_GC_objects);
PArrayDelete(_GC_objects_back);
PArrayDelete(_GC_stack);
}
INT GC_stack_size()
{
return PArraySize(_GC_stack);
}
#endif