-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.c
executable file
·302 lines (269 loc) · 8.68 KB
/
memory.c
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
#include <stdlib.h>
#include <stdio.h>
#include "common.h"
#include "debug.h"
#include "memory.h"
#include "vm.h"
#include "compiler.h"
#include "table.h"
#define GC_CONSTANT 2
void collectGarbage(VM* vm);
void* reallocate(VM* vm, void* ptr, size_t oldSize, size_t newSize) {
vm->bytesAllocated += newSize - oldSize;
if (vm->isActive) {
//printf("Thinking about it...: at %zu\n", vm->nextGC);
if (newSize > oldSize) {
//printf("THINKING ABOUT IT: at %zu\n", vm->bytesAllocated);
#ifdef DEBUG_STRESS_GC
#ifdef DEBUG_LOG_GC
printf("With %p: %04d -> %04d\n", ptr, oldSize, newSize);
printf("About to collect: at %zu\n", vm->bytesAllocated);
#endif
collectGarbage(vm);
#else // !DEBUG_STRESS_GC
if (vm->bytesAllocated > vm->nextGC) {
//printf("LETS GOOOOOOOO\n");
#ifdef DEBUG_LOG_GC
printf("With %p: %04d -> %04d\n", ptr, oldSize, newSize);
printf("About to collect: at %zu\n", vm->bytesAllocated);
#endif
collectGarbage(vm);
}
#endif
}
}
if (newSize == 0) {
free(ptr);
#ifdef DEBUG_LOG_MEMORY
printf("Freeing %p: %04d -> %04d\n", ptr, oldSize, newSize);
#endif
return NULL;
}
#ifdef DEBUG_LOG_MEMORY
printf("Allocating %p: %04d -> %04d\n", ptr, oldSize, newSize);
#endif
void* result = realloc(ptr, newSize);
if (result == NULL) exit(64);
return result;
}
static void freeObject(VM* vm, Obj* object) {
#ifdef DEBUG_LOG_GC
printf("Freeing %s\n", getObjName(object->type));
#endif
switch (object->type) {
case OBJ_STRING: {
ObjString* string = (ObjString*)object;
FREE(vm, string, ObjString);
break;
}
case OBJ_CELL: {
ObjCell* cell = (ObjCell*)object;
FREE(vm, cell, ObjCell);
break;
}
case OBJ_FUNCTION: {
ObjFunction* func = (ObjFunction*)object;
freeChunk(vm, &func->body);
FREE(vm, func, ObjFunction);
break;
}
case OBJ_NATIVE: {
ObjNative* native = (ObjNative*)object;
FREE(vm, native, ObjNative);
break;
}
case OBJ_CLOSURE: {
ObjClosure* closure = (ObjClosure*)object;
FREE_ARRAY(vm, closure->upvalues, closure->upvalueCount, Value);
FREE_ARRAY(vm, closure->depths, closure->upvalueCount, uint8_t);
FREE(vm, closure, ObjClosure);
break;
}
case OBJ_LIST: {
ObjList* list = (ObjList*)object;
freeValueArray(vm, &list->array);
FREE(vm, list, ObjList);
break;
}
case OBJ_MAP: {
ObjMap* map = (ObjMap*)object;
freeTable(vm, &map->table);
FREE(vm, map, ObjMap);
break;
}
}
}
void freeObjects(VM* vm) {
Obj* object = vm->objects;
while (object != NULL) {
Obj* next = object->next;
freeObject(vm, object);
object = next;
}
}
void markObject(VM* vm, Obj* object) {
if (object == NULL) return;
if (object->colour == MEM_BLACK) return;
object->colour = MEM_BLACK;
object->line = NULL;
if (vm->greyStart == NULL) {
#ifdef DEBUG_LOG_GC
printf("Starting greys with %p : %s\n", (void*)object, getObjName(object->type));
#endif
vm->greyStart = object;
}
else {
#ifdef DEBUG_LOG_GC
printf("Appending %p : %s\n", (void*)object, getObjName(object->type));
#endif
vm->greyEnd->line = object;
}
vm->greyEnd = object;
}
void markValue(VM* vm, Value value) {
if (IS_OBJ(value)) markObject(vm, AS_OBJ(value));
}
static void markArray(VM* vm, ValueArray* array) {
for (int i = 0; i < array->count; i++) {
markValue(vm, array->values[i]);
}
}
static void blackenObject(VM* vm, Obj* object) {
#ifdef DEBUG_LOG_GC
printf("Blackening %p : %s\n", (void*)object, getObjName(object->type));
#endif
switch (object->type) {
case OBJ_CELL: {
ObjCell* cell = (ObjCell*)object;
markValue(vm, cell->car);
markValue(vm, cell->cdr);
break;
}
case OBJ_FUNCTION: {
ObjFunction* function = (ObjFunction*)object;
markObject(vm, (Obj*)function->name);
markArray(vm, &function->body.constants);
break;
}
case OBJ_CLOSURE: {
ObjClosure* closure = (ObjClosure*)object;
markObject(vm, (Obj*)closure->function);
for (int i = 0; i < closure->upvalueCount; i++) {
markValue(vm, closure->upvalues[i]);
}
break;
}
case OBJ_LIST: {
ObjList* list = (ObjList*)object;
markArray(vm, &list->array);
break;
}
case OBJ_MAP: {
ObjMap* map = (ObjMap*)object;
markTable(vm, &map->table);
break;
}
case OBJ_STRING:
case OBJ_NATIVE:
break;
}
}
static void markRoots(VM* vm) {
#ifdef DEBUG_LOG_GC
printf("Slots:\n");
#endif
for (Value* slot = vm->stack; slot < vm->stackTop; slot++) {
markValue(vm, *slot);
#ifdef DEBUG_LOG_GC
printValue(*slot);
printf("\n");
#endif
}
#ifdef DEBUG_LOG_GC
printf("Frames:\n");
#endif
for (int i = 0; i < vm->frameCount; i++) {
markObject(vm, (Obj*)vm->frames[i].function);
markObject(vm, (Obj*)vm->frames[i].closure);
#ifdef DEBUG_LOG_GC
printValue(OBJ_VAL((Obj*)vm->frames[i].function));
printf("\n");
#endif
}
markTable(vm, &vm->globals);
// markCompiler(vm); // I don't think I need this??? Shouldn't have to tiptoe
// around allocation during the compilation phase... just wait until after
}
static void walkLine(VM* vm) {
// Old loop didn't reset VM.greyStart to NULL, may
// have contributed to some objs getting lost
while (vm->greyStart != NULL) {
blackenObject(vm, vm->greyStart);
vm->greyStart = vm->greyStart->line;
}
}
static void sweep(VM* vm) {
Obj* previous = NULL;
Obj* object = vm->objects;
while (object != NULL) {
#ifdef DEBUG_LOG_GC
printf("Looping: ");
#endif
if (object->colour == MEM_BLACK || object->colour == MEM_GREY) {
#ifdef DEBUG_LOG_GC
printf("%p was %d; whiting out\n", (void*)object, object->colour);
#endif
object->colour = MEM_WHITE;
previous = object;
object = object->next;
} else {
#ifdef DEBUG_LOG_GC
printf("%p was %d; freeing\n", (void*)object, object->colour);
#endif
Obj* unreached = object;
object = object->next;
if (previous != NULL) {
previous->next = object;
} else {
vm->objects = object;
}
freeObject(vm, unreached);
}
}
}
void collectGarbage(VM* vm) {
#ifdef DEBUG_LOG_GC
size_t before = vm->bytesAllocated;
#endif
// get all the easy stuff: the stack, globals, interned strings,
// active callframes, etc.
#ifdef DEBUG_LOG_GC
printf("Marking roots\n");
#endif
markRoots(vm);
// traverse linked list "VM->greyStart/Obj->line". Appending elements through "VM->greyEnd"
// saves having to allocate memory for double pointers (which would still have to be dereferenced)
#ifdef DEBUG_LOG_GC
printf("Walking grey line\n");
#endif
walkLine(vm);
// interned strings are only useful if they're being used, and clog the hashtable otherwise,
// so get rid of unused ones
#ifdef DEBUG_LOG_GC
printf("Cleaning strings\n");
#endif
tableRemoveWhite(&vm->strings);
// traverse "VM->objects", removing white-coloured objs; grey-coloured objs are demoted
// to white, this is to allow for 1 cycle of lenience, the idea being it COULD get "picked up" by
// a reference between now and the next gc cycle.
// This does mean the first GC cycle won't clean anything up, but I can always just fiddle with
// it until I get something that works (e.g. the constants could be set lower)
#ifdef DEBUG_LOG_GC
printf("Sweeping\n");
#endif
sweep(vm);
vm->nextGC = vm->bytesAllocated * GC_CONSTANT;
#ifdef DEBUG_LOG_GC
printf("Finished collecting: at %zu (collected %zu bytes) next at %zu\n", vm->bytesAllocated, before - vm->bytesAllocated, vm->nextGC);
#endif
}