-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.c
236 lines (217 loc) · 5.84 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
//
// Created by kroot on 15/7/19.
//
#include <stdlib.h>
#include "common.h"
#include "compiler.h"
#include "memory.h"
#include "vm.h"
#ifdef DEBUG_LOG_GC
#include <stdio.h>
#include "debug.h"
#endif
#define GC_HEAP_GROW_FACTOR 2
void* reallocate(void* previous, size_t oldSize, size_t newSize) {
vm.bytesAllocated += newSize - oldSize;
if (newSize > oldSize) {
#ifdef DEBUG_STRESS_GC
collectGarbage();
#endif
}
if (vm.bytesAllocated > vm.nextGC) {
collectGarbage();
}
if (newSize == 0) {
free(previous);
return NULL;
}
return realloc(previous, newSize);
}
static void freeObject(Obj* obj) {
#ifdef DEBUG_LOG_GC
printf("%p free type %d\n", (void*)obj, obj->type);
#endif
switch (obj->type) {
case OBJ_BOUND_METHOD: {
FREE(ObjBoundMethod, obj);
break;
}
case OBJ_CLASS: {
ObjClass* klass = (ObjClass*)obj;
freeTable(&klass->methods);
FREE(ObjClass, obj);
break;
}
case OBJ_CLOSURE: {
ObjClosure* closure = (ObjClosure*)obj;
FREE_ARRAY(ObjUpvalue*, closure->upvalues, closure->upvalueCount);
FREE(ObjClosure, obj);
break;
}
case OBJ_FUNCTION: {
ObjFunction* function = (ObjFunction*)obj;
freeChunk(&function->chunk);
FREE(ObjFunction, obj);
break;
}
case OBJ_INSTANCE: {
ObjInstance* instance = (ObjInstance*)obj;
freeTable(&instance->fields);
FREE(ObjInstance, obj);
break;
}
case OBJ_NATIVE: {
FREE(ObjNative, obj);
break;
}
case OBJ_STRING: {
ObjString* string = (ObjString*)obj;
FREE_ARRAY(char, string->chars, string->length + 1);
FREE(ObjString, obj);
break;
}
case OBJ_UPVALUE: {
FREE(ObjUpvalue, obj);
break;
}
}
}
void freeObjects() {
Obj* obj = vm.objects;
while (obj != NULL) {
Obj* next = obj->next;
freeObject(obj);
obj = next;
}
free(vm.grayStack);
}
void markObject(Obj* obj) {
if (obj == NULL || obj->isMarked)
return;
#ifdef DEBUG_LOG_GC
printf("%p mark ", (void*)obj);
printValue(OBJ_VAL(obj));
printf("\n");
#endif
obj->isMarked = true;
if (vm.grayCapacity < vm.grayCount + 1) {
vm.grayCapacity = GROW_CAPACITY(vm.grayCapacity);
vm.grayStack = realloc(vm.grayStack, sizeof(Obj*) * vm.grayCapacity);
}
vm.grayStack[vm.grayCount++] = obj;
}
void markValue(Value value) {
if (!IS_OBJ(value)) return;
markObject(AS_OBJ(value));
}
static void markArray(ValueArray* array) {
for (int i = 0; i < array->count; ++i) {
markValue(array->values[i]);
}
}
static void blackenObject(Obj* obj) {
#ifdef DEBUG_LOG_GC
printf("%p blacken ", (void*)obj);
printValue(OBJ_VAL(obj));
printf("\n");
#endif
switch (obj->type) {
case OBJ_BOUND_METHOD: {
ObjBoundMethod* boundMethod = (ObjBoundMethod*)obj;
markValue(boundMethod->receiver);
markObject((Obj*)boundMethod->method);
break;
}
case OBJ_CLASS: {
ObjClass* klass = (ObjClass*)obj;
markObject((Obj*)klass->name);
markTable(&klass->methods);
break;
}
case OBJ_CLOSURE: {
ObjClosure* closure = (ObjClosure*)obj;
markObject((Obj*)closure->function);
for (int i = 0; i < closure->upvalueCount; ++i) {
markObject((Obj*)closure->upvalues[i]);
}
break;
}
case OBJ_FUNCTION: {
ObjFunction* function = (ObjFunction*)obj;
markObject((Obj*)function->name);
markArray(&function->chunk.constants);
break;
}
case OBJ_INSTANCE: {
ObjInstance* instance = (ObjInstance*)obj;
markObject((Obj*)instance->klass);
markTable(&instance->fields);
break;
}
case OBJ_UPVALUE: {
markValue(((ObjUpvalue*)obj)->closed);
break;
}
case OBJ_NATIVE:
case OBJ_STRING: break;
}
}
static void traceRefs() {
while (vm.grayCount > 0) {
Obj* obj = vm.grayStack[--vm.grayCount];
blackenObject(obj);
}
}
static void sweep() {
Obj* previous = NULL;
Obj* obj = vm.objects;
while (obj != NULL) {
if (obj->isMarked) {
obj->isMarked = false;
previous = obj;
obj = obj->next;
} else {
Obj* unreached = obj;
obj = obj->next;
if (previous != NULL) {
previous->next = obj;
} else {
vm.objects = obj;
}
freeObject(unreached);
}
}
}
static void markRoots() {
#ifdef DEBUG_LOG_GC
printf("mark roots\n");
#endif
for (Value* slot = vm.stack; slot < vm.stackTop; ++slot) {
markValue(*slot);
}
for (int i = 0; i < vm.frameCount; ++i) {
markObject((Obj*)vm.frames[i].closure);
}
for (ObjUpvalue* upvalue = vm.openUpvalues; upvalue != NULL; upvalue = upvalue->next) {
markObject((Obj*)upvalue);
}
markTable(&vm.globals);
markCompilerRoots();
markObject((Obj*)vm.initString);
}
void collectGarbage() {
#ifdef DEBUG_LOG_GC
printf("-- gc begin\n");
size_t before = vm.bytesAllocated;
#endif
markRoots();
traceRefs();
tableRemoveWhite(&vm.strings);
sweep();
vm.nextGC = vm.bytesAllocated * GC_HEAP_GROW_FACTOR;
#ifdef DEBUG_LOG_GC
printf("-- gc end\n");
printf(" collected %ld bytes (from %ld to %ld) next at %ld\n", before - vm.bytesAllocated, before,
vm.bytesAllocated, vm.nextGC);
#endif
}