-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.c
More file actions
356 lines (320 loc) · 8.5 KB
/
memory.c
File metadata and controls
356 lines (320 loc) · 8.5 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//> Chunks of Bytecode memory-c
#include "common.h"
//> Garbage Collection memory-include-compiler
#include "compiler.h"
//< Garbage Collection memory-include-compiler
#include "memory.h"
//> Strings memory-include-vm
#include "vm.h"
//< Strings memory-include-vm
//> Garbage Collection debug-log-includes
#ifdef DEBUG_LOG_GC
#include <stdio.h>
#include "debug.h"
#endif
//< Garbage Collection debug-log-includes
//> Garbage Collection heap-grow-factor
#define GC_HEAP_GROW_FACTOR 2
//< Garbage Collection heap-grow-factor
void* reallocate(void* previous, usize oldSize, usize newSize) {
//> Garbage Collection updated-bytes-allocated
vm.bytesAllocated += newSize - oldSize;
//< Garbage Collection updated-bytes-allocated
//> Garbage Collection call-collect
if (newSize > oldSize) {
#ifdef DEBUG_STRESS_GC
collectGarbage();
#endif
//> collect-on-next
if (vm.bytesAllocated > vm.nextGC) {
collectGarbage();
}
//< collect-on-next
}
//< Garbage Collection call-collect
if (newSize == 0) {
free(previous);
return nil;
}
return realloc(previous, newSize);
}
//> Garbage Collection mark-object
void markObject(Obj* object) {
if (object == nil) return;
//> check-is-marked
if (object->isMarked) return;
//< check-is-marked
//> log-mark-object
#ifdef DEBUG_LOG_GC
print("%p mark ", (void*)object);
printValue(OBJ_VAL(object));
print("\n");
#endif
//< log-mark-object
object->isMarked = true;
//> add-to-gray-stack
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++] = object;
//< add-to-gray-stack
}
//< Garbage Collection mark-object
//> Garbage Collection mark-value
void markValue(Value value) {
if (!IS_OBJ(value)) return;
markObject(AS_OBJ(value));
}
//< Garbage Collection mark-value
//> Garbage Collection mark-array
static void markArray(ValueArray* array) {
for (int i = 0; i < array->count; i++) {
markValue(array->values[i]);
}
}
//< Garbage Collection mark-array
//> Garbage Collection blacken-object
static void blackenObject(Obj* object) {
//> log-blacken-object
#ifdef DEBUG_LOG_GC
print("%p blacken ", (void*)object);
printValue(OBJ_VAL(object));
print("\n");
#endif
//< log-blacken-object
switch (object->type) {
//> Methods and Initializers blacken-bound-method
case OBJ_BOUND_METHOD: {
ObjBoundMethod* bound = (ObjBoundMethod*)object;
markValue(bound->receiver);
markObject((Obj*)bound->method);
break;
}
//< Methods and Initializers blacken-bound-method
//> Classes and Instances blacken-class
case OBJ_CLASS: {
ObjClass* klass = (ObjClass*)object;
markObject((Obj*)klass->name);
//> Methods and Initializers mark-methods
markTable(&klass->methods);
//< Methods and Initializers mark-methods
break;
}
//< Classes and Instances blacken-class
//> blacken-closure
case OBJ_CLOSURE: {
ObjClosure* closure = (ObjClosure*)object;
markObject((Obj*)closure->function);
for (int i = 0; i < closure->upvalueCount; i++) {
markObject((Obj*)closure->upvalues[i]);
}
break;
}
//< blacken-closure
//> blacken-function
case OBJ_FUNCTION: {
ObjFunction* function = (ObjFunction*)object;
markObject((Obj*)function->name);
markArray(&function->chunk.constants);
break;
}
//< blacken-function
//> Classes and Instances blacken-instance
case OBJ_INSTANCE: {
ObjInstance* instance = (ObjInstance*)object;
markObject((Obj*)instance->klass);
markTable(&instance->fields);
break;
}
//< Classes and Instances blacken-instance
//> blacken-upvalue
case OBJ_UPVALUE:
markValue(((ObjUpvalue*)object)->closed);
break;
//< blacken-upvalue
case OBJ_NATIVE:
case OBJ_STRING:
break;
}
}
//< Garbage Collection blacken-object
//> Strings free-object
static void freeObject(Obj* object) {
//> Garbage Collection log-free-object
#ifdef DEBUG_LOG_GC
print("%p free type %d\n", (void*)object, object->type);
#endif
//< Garbage Collection log-free-object
switch (object->type) {
//> Methods and Initializers free-bound-method
case OBJ_BOUND_METHOD:
FREE(ObjBoundMethod, object);
break;
//< Methods and Initializers free-bound-method
//> Classes and Instances free-class
case OBJ_CLASS: {
//> Methods and Initializers free-methods
ObjClass* klass = (ObjClass*)object;
freeTable(&klass->methods);
//< Methods and Initializers free-methods
FREE(ObjClass, object);
break;
} // [braces]
//< Classes and Instances free-class
//> Closures free-closure
case OBJ_CLOSURE: {
//> free-upvalues
ObjClosure* closure = (ObjClosure*)object;
FREE_ARRAY(ObjUpvalue*, closure->upvalues, closure->upvalueCount);
//< free-upvalues
FREE(ObjClosure, object);
break;
}
//< Closures free-closure
//> Calls and Functions free-function
case OBJ_FUNCTION: {
ObjFunction* function = (ObjFunction*)object;
freeChunk(&function->chunk);
FREE(ObjFunction, object);
break;
}
//< Calls and Functions free-function
//> Classes and Instances free-instance
case OBJ_INSTANCE: {
ObjInstance* instance = (ObjInstance*)object;
freeTable(&instance->fields);
FREE(ObjInstance, object);
break;
}
//< Classes and Instances free-instance
//> Calls and Functions free-native
case OBJ_NATIVE:
FREE(ObjNative, object);
break;
//< Calls and Functions free-native
case OBJ_STRING: {
ObjString* string = (ObjString*)object;
FREE_ARRAY(char, string->chars, string->length + 1);
FREE(ObjString, object);
break;
}
//> Closures free-upvalue
case OBJ_UPVALUE:
FREE(ObjUpvalue, object);
break;
//< Closures free-upvalue
}
}
//< Strings free-object
//> Garbage Collection mark-roots
static void markRoots(void) {
for (Value* slot = vm.stack; slot < vm.stackTop; slot++) {
markValue(*slot);
}
//> mark-closures
for (int i = 0; i < vm.frameCount; i++) {
markObject((Obj*)vm.frames[i].closure);
}
//< mark-closures
//> mark-open-upvalues
for (ObjUpvalue* upvalue = vm.openUpvalues;
upvalue != nil;
upvalue = upvalue->next) {
markObject((Obj*)upvalue);
}
//< mark-open-upvalues
//> mark-globals
markTable(&vm.globals);
//< mark-globals
//> call-mark-compiler-roots
markCompilerRoots();
//< call-mark-compiler-roots
//> Methods and Initializers mark-init-string
markObject((Obj*)vm.initString);
//< Methods and Initializers mark-init-string
}
//< Garbage Collection mark-roots
//> Garbage Collection trace-references
static void traceReferences(void) {
while (vm.grayCount > 0) {
Obj* object = vm.grayStack[--vm.grayCount];
blackenObject(object);
}
}
//< Garbage Collection trace-references
//> Garbage Collection sweep
static void sweep(void) {
Obj* previous = nil;
Obj* object = vm.objects;
while (object != nil) {
if (object->isMarked) {
//> unmark
object->isMarked = false;
//< unmark
previous = object;
object = object->next;
} else {
Obj* unreached = object;
object = object->next;
if (previous != nil) {
previous->next = object;
} else {
vm.objects = object;
}
freeObject(unreached);
}
}
}
//< Garbage Collection sweep
//> Garbage Collection collect-garbage
void collectGarbage(void) {
//> log-before-collect
#ifdef DEBUG_LOG_GC
print("-- gc begin\n");
//> log-before-size
usize before = vm.bytesAllocated;
//< log-before-size
#endif
//< log-before-collect
//> call-mark-roots
markRoots();
//< call-mark-roots
//> call-trace-references
traceReferences();
//< call-trace-references
//> sweep-strings
tableRemoveWhite(&vm.strings);
//< sweep-strings
//> call-sweep
sweep();
//< call-sweep
//> update-next-gc
vm.nextGC = vm.bytesAllocated * GC_HEAP_GROW_FACTOR;
//< update-next-gc
//> log-after-collect
#ifdef DEBUG_LOG_GC
print("-- gc end\n");
//> log-collected-amount
print(" collected %ld bytes (from %ld to %ld) next at %ld\n",
before - vm.bytesAllocated, before, vm.bytesAllocated,
vm.nextGC);
//< log-collected-amount
#endif
//< log-after-collect
}
//< Garbage Collection collect-garbage
//> Strings free-objects
void freeObjects() {
Obj* object = vm.objects;
while (object != nil) {
Obj* next = object->next;
freeObject(object);
object = next;
}
//> Garbage Collection free-gray-stack
free(vm.grayStack);
//< Garbage Collection free-gray-stack
}
//< Strings free-objects