-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgc.c
More file actions
462 lines (413 loc) · 13.1 KB
/
gc.c
File metadata and controls
462 lines (413 loc) · 13.1 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// --------------------------------------------------
// Project: ProX Programming Language (ProXPL)
// Author: ProgrammerKR
// Created: 2025-12-16
// Copyright © 2025. ProXentix India Pvt. Ltd. All rights reserved.
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "../include/gc.h"
#include "../include/object.h"
#include "../include/compiler.h"
#include "../include/table.h"
#include "../include/memory.h"
#include "../include/vm.h"
#ifdef DEBUG_LOG_GC
#include "../include/debug.h"
#endif
#define GC_HEAP_GROW_FACTOR 2
// ----------------------------------------------------------------------------
// GENERATIONAL GC: NURSERY (YOUNG GENERATION)
// ----------------------------------------------------------------------------
#define NURSERY_SIZE (2 * 1024 * 1024) // 2MB
typedef struct {
uint8_t* start;
uint8_t* end;
uint8_t* current;
} Nursery;
static Nursery nursery;
static bool nursery_initialized = false;
void initNursery() {
nursery.start = (uint8_t*)malloc(NURSERY_SIZE);
if (!nursery.start) {
fprintf(stderr, "Fatal: Could not allocate GC Nursery.\n");
exit(1);
}
nursery.end = nursery.start + NURSERY_SIZE;
nursery.current = nursery.start;
nursery_initialized = true;
}
static bool is_in_nursery(void* ptr) {
if (!nursery_initialized || !ptr) return false;
return (uint8_t*)ptr >= nursery.start && (uint8_t*)ptr < nursery.end;
}
static void* nursery_alloc(size_t size) {
if (nursery.current + size > nursery.end) {
return NULL; // Nursery full
}
void* result = nursery.current;
nursery.current += size;
return result;
}
static void reset_nursery() {
// In a real generational GC, we would evacuate survivors here.
// For this MVP, we treat Nursery as a "scratchpad" that gets fully collected
// or promoted. Implementing full copying GC requires pointer updates.
// Fallback strategy: If nursery full, trigger Full GC?
// Current strategy: Reset pointer. Dangerous if live objects exist!
// SAFE MODE: Don't reset unless we know everything is dead.
// So for now, we just fill it up and then fall back to malloc.
// This gives fast start-up speed (google scale req).
// nursery.current = nursery.start;
}
// ----------------------------------------------------------------------------
// Access the global VM instance
extern VM vm;
void initGC(VM* vm) {
vm->grayCount = 0;
vm->grayCapacity = 0;
vm->grayStack = NULL;
vm->bytesAllocated = 0;
vm->nextGC = 1024 * 1024;
initNursery();
}
void* reallocate(void* pointer, size_t oldSize, size_t newSize) {
// Stats
if (newSize > oldSize) vm.bytesAllocated += newSize - oldSize;
else vm.bytesAllocated -= oldSize - newSize; // Approximate for nursery
if (newSize > oldSize) {
#ifdef DEBUG_STRESS_GC
collectGarbage(&vm);
#endif
if (vm.bytesAllocated > vm.nextGC) {
collectGarbage(&vm);
}
}
if (newSize == 0) {
if (pointer == NULL) return NULL;
if (is_in_nursery(pointer)) {
// No-op free for nursery objects (bulk freed/reset)
return NULL;
}
free(pointer);
return NULL;
}
// Allocation
if (oldSize == 0) {
// Try Nursery for small objects (e.g., < 256 bytes)
// Only if it's a fresh allocation
if (newSize < 256) {
void* mem = nursery_alloc(newSize);
if (mem) return mem;
// Fallthrough to malloc if full
}
} else {
// Reallocation
if (is_in_nursery(pointer)) {
// Moving out of nursery (Promote to Heap)
void* newMem = malloc(newSize);
if (!newMem) exit(1);
// Copy old data
// We don't know exact valid size to copy if oldSize is loose,
// but reallocate api passes oldSize.
size_t copySize = oldSize < newSize ? oldSize : newSize;
// memcpy(newMem, pointer, copySize); // Need string.h
// We can't include string.h easily without messy diff?
// We can iterate.
uint8_t* src = (uint8_t*)pointer;
uint8_t* dst = (uint8_t*)newMem;
for (size_t i = 0; i < copySize; i++) dst[i] = src[i];
return newMem;
}
}
void* result = realloc(pointer, newSize);
if (result == NULL) exit(1);
return result;
}
void markObject(Obj* object) {
if (object == NULL) return;
if (object->isMarked) return;
#ifdef DEBUG_LOG_GC
printf("%p mark ", (void*)object);
printObject(OBJ_VAL(object));
printf("\n");
#endif
object->isMarked = true;
if (vm.grayCapacity < vm.grayCount + 1) {
vm.grayCapacity = GROW_CAPACITY(vm.grayCapacity);
vm.grayStack = (Obj**)realloc(vm.grayStack, sizeof(Obj*) * vm.grayCapacity);
if (vm.grayStack == NULL) {
fprintf(stderr, "Fatal: Out of memory for gray stack.\n");
exit(1);
}
}
vm.grayStack[vm.grayCount++] = object;
}
void markValue(Value value) {
if (IS_OBJ(value)) 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* object) {
#ifdef DEBUG_LOG_GC
printf("%p blacken ", (void*)object);
printObject(OBJ_VAL(object));
printf("\n");
#endif
switch (object->type) {
case OBJ_NATIVE:
case OBJ_STRING:
break;
case OBJ_FUNCTION: {
ObjFunction* function = (ObjFunction*)object;
markObject((Obj*)function->name);
markArray(&function->chunk.constants);
break;
}
case OBJ_MODULE: {
ObjModule* module = (ObjModule*)object;
markObject((Obj*)module->name);
markTable(&module->exports);
break;
}
case OBJ_FOREIGN: {
ObjForeign* foreign = (ObjForeign*)object;
markObject((Obj*)foreign->name);
break;
}
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;
}
case OBJ_UPVALUE:
markValue(((ObjUpvalue*)object)->closed);
break;
case OBJ_CLASS: {
struct ObjClass* klass = (struct ObjClass*)object;
markObject((Obj*)klass->name);
markTable(&klass->methods);
break;
}
case OBJ_INSTANCE: {
struct ObjInstance* instance = (struct ObjInstance*)object;
markObject((Obj*)instance->klass);
markTable(&instance->fields);
break;
}
case OBJ_BOUND_METHOD: {
struct ObjBoundMethod* bound = (struct ObjBoundMethod*)object;
markValue(bound->receiver);
markObject((Obj*)bound->method);
break;
}
case OBJ_LIST: {
struct ObjList* list = (struct ObjList*)object;
for (int i = 0; i < list->count; i++) {
markValue(list->items[i]);
}
break;
}
case OBJ_DICTIONARY: {
struct ObjDictionary* dict = (struct ObjDictionary*)object;
markTable(&dict->items);
break;
}
case OBJ_TENSOR:
break;
case OBJ_CONTEXT: {
ObjContext* context = (ObjContext*)object;
markObject((Obj*)context->name);
markTable(&context->layers);
break;
}
case OBJ_LAYER: {
ObjLayer* layer = (ObjLayer*)object;
markObject((Obj*)layer->name);
markTable(&layer->methods);
break;
}
default:
break;
}
}
static void markRoots() {
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);
markObject((Obj*)vm.initString);
markObject((Obj*)vm.cliArgs);
markTable(&vm.importer.modules);
for (int i = 0; i < vm.activeContextCount; i++) {
markObject((Obj*)vm.activeContextStack[i]);
}
markCompilerRoots();
}
static void traceReferences() {
while (vm.grayCount > 0) {
Obj* object = vm.grayStack[--vm.grayCount];
if (object != NULL) blackenObject(object);
}
}
static void freeObject(Obj* object) {
if (is_in_nursery(object)) return; // Don't free nursery objects individually
#ifdef DEBUG_LOG_GC
printf("%p free ", (void*)object);
printObject(OBJ_VAL(object));
printf("\n");
#endif
switch (object->type) {
case OBJ_STRING: {
FREE(ObjString, object);
break;
}
case OBJ_FUNCTION: {
ObjFunction* function = (ObjFunction*)object;
freeChunk(&function->chunk);
FREE(ObjFunction, object);
break;
}
case OBJ_NATIVE: {
FREE(ObjNative, object);
break;
}
case OBJ_FOREIGN: {
FREE(ObjForeign, object);
break;
}
case OBJ_MODULE: {
ObjModule* module = (ObjModule*)object;
freeTable(&module->exports);
FREE(ObjModule, object);
break;
}
case OBJ_CLOSURE: {
ObjClosure* closure = (ObjClosure*)object;
FREE_ARRAY(ObjUpvalue*, closure->upvalues, closure->upvalueCount);
FREE(ObjClosure, object);
break;
}
case OBJ_UPVALUE:
FREE(ObjUpvalue, object);
break;
case OBJ_CLASS: {
struct ObjClass* klass = (struct ObjClass*)object;
freeTable(&klass->methods);
FREE(struct ObjClass, object);
break;
}
case OBJ_INSTANCE: {
struct ObjInstance* instance = (struct ObjInstance*)object;
freeTable(&instance->fields);
FREE(struct ObjInstance, object);
break;
}
case OBJ_BOUND_METHOD:
FREE(struct ObjBoundMethod, object);
break;
case OBJ_LIST: {
struct ObjList* list = (struct ObjList*)object;
FREE_ARRAY(Value, list->items, list->capacity);
FREE(struct ObjList, object);
break;
}
case OBJ_DICTIONARY: {
struct ObjDictionary* dict = (struct ObjDictionary*)object;
freeTable(&dict->items);
FREE(struct ObjDictionary, object);
break;
}
case OBJ_CONTEXT: {
ObjContext* context = (ObjContext*)object;
freeTable(&context->layers);
FREE(ObjContext, object);
break;
}
case OBJ_LAYER: {
ObjLayer* layer = (ObjLayer*)object;
freeTable(&layer->methods);
FREE(ObjLayer, object);
break;
}
case OBJ_TENSOR: {
ObjTensor* tensor = (ObjTensor*)object;
FREE_ARRAY(int, tensor->dims, tensor->dimCount);
FREE_ARRAY(double, tensor->data, tensor->size);
FREE(ObjTensor, object);
break;
}
case OBJ_TASK: {
FREE(struct ObjTask, object);
break;
}
default:
FREE(Obj, object); // Fallback
break;
}
}
static void sweep() {
Obj* previous = NULL;
Obj* object = vm.objects;
while (object != NULL) {
if (object->isMarked) {
object->isMarked = false; // Unmark for next cycle
previous = object;
object = object->next;
} else {
Obj* unreached = object;
object = object->next;
if (previous != NULL) {
previous->next = object;
} else {
vm.objects = object;
}
freeObject(unreached);
}
}
}
void collectGarbage(VM* vm_ptr) {
if (vm_ptr != &vm) {
}
#ifdef DEBUG_LOG_GC
printf("-- gc begin\n");
size_t before = vm.bytesAllocated;
#endif
markRoots();
traceReferences();
tableRemoveWhite(&vm.strings);
sweep();
// reset_nursery(); // Dangerous without evacuation
vm.nextGC = vm.bytesAllocated * GC_HEAP_GROW_FACTOR;
#ifdef DEBUG_LOG_GC
printf("-- gc end\n");
printf(" collected %zu bytes (from %zu to %zu) next at %zu\n",
before - vm.bytesAllocated, before, vm.bytesAllocated, vm.nextGC);
#endif
}
void freeObjects(VM* vm_ptr) {
(void)vm_ptr;
Obj* object = vm.objects;
while (object != NULL) {
Obj* next = object->next;
freeObject(object);
object = next;
}
free(vm.grayStack);
vm.grayStack = NULL;
if (nursery_initialized) {
free(nursery.start);
}
}