-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.c
More file actions
269 lines (251 loc) · 7.27 KB
/
memory.c
File metadata and controls
269 lines (251 loc) · 7.27 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
#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
//tune this later
#define GC_HEAP_GROW_FACTOR 2
/*
* Start off with all objects white.
* Find all the roots and mark them gray.
* As long as there are still gray objects:
- Pick a gray object. Turn any white objects that the object mentions to gray.
- Mark the original gray object black.
*/
void *reallocate(RotoVM* vm, void* prev, size_t old_size, size_t new_size){
vm->bytes_alocated += new_size - old_size;
if (new_size > old_size){
#ifdef DEBUG_STRESS_GC
collect_garbage();
#endif
if (vm->bytes_alocated > vm->next_gc){
collect_garbage(vm);
}
}
if(new_size == 0){
free(prev);
return NULL;
}
return realloc(prev, new_size);
}
void mark_object(RotoVM* vm,Obj* object){
if (object == NULL) return;
if(object->is_marked) return;
#ifdef DEBUG_LOG_GC
//for readability
// printf("\x1B[31m");
//
// printf("%p mark ",(void*)object);
__print_with_color(_RED,"%p mark ",(void*)object);
print_value(OBJ_VAL(object));
printf("\n");
printf(_RESET);
#endif
object->is_marked = true;
if(vm->gray_capacity < vm->gray_count + 1){
vm->gray_capacity = GROW_CAPACITY(vm->gray_capacity);
vm->gray_stack = realloc(vm->gray_stack,sizeof(Obj*) * vm->gray_capacity);
if (vm->gray_stack == NULL) exit(1);
}
vm->gray_stack[vm->gray_count++] = object;
}
void mark_value(RotoVM* vm,Value value){
if(!IS_OBJ(value)) return;
mark_object(vm,AS_OBJ(value));
}
static void mark_array(RotoVM* vm,ValueArray* array){
for (int i = 0; i < array->count; i++) {
mark_value(vm,array->values[i]);
}
}
static void blacken_object(RotoVM* vm,Obj* object){
#ifdef DEBUG_LOG_GC
__print_with_color(_YELLOW, "%p blacken ", (void*)object);
print_value(OBJ_VAL(object));
printf("\n");
printf(_RESET);
#endif
switch (object->type) {
case OBJ_LIST:{
ObjList* list = (ObjList*)object;
for (int i = 0; i < list->values.count; i++) {
mark_value(vm,list->values.values[i]);
}
break;
}
case OBJ_BOUND_METHOD:{
ObjBoundMethod* bound = (ObjBoundMethod*)object;
mark_value(vm,bound->receiver);
mark_object(vm,(Obj*)bound->method);
break;
}
case OBJ_CLASS:{
ObjClass* klass = (ObjClass*)object;
mark_object(vm,(Obj*)klass->name);
mark_table(vm,&klass->methods);
break;
}
case OBJ_CLOSURE:{
ObjClosure* closure = (ObjClosure*)object;
mark_object(vm,(Obj*)closure->function);
for (int i = 0; i < closure->upvalue_count; i++) {
mark_object(vm,(Obj*)closure->upvalues[i]);
}
break;
}
case OBJ_FUNCTION:{
ObjFunction* function = (ObjFunction*)object;
mark_object(vm,(Obj*)function->name);
mark_array(vm,&function->chunk.constants);
break;
}
case OBJ_INSTANCE:{
ObjInstance* instance = (ObjInstance*)object;
mark_object(vm,(Obj*)instance->klass);
mark_table(vm,&instance->fields);
break;
}
case OBJ_UPVALUE:
mark_value(vm,((ObjUpvalue*)object)->closed);
break;
case OBJ_NATIVE:
case OBJ_STRING:
break;
}
}
static void free_object(RotoVM* vm,Obj *object) {
#ifdef DEBUG_LOG_GC
// printf("\x1B[34m");
// printf("%p free type %d\n", (void*)object,object->type);
__print_with_color(_BLUE, "%p free type %d\n", (void*)object,object->type);
printf(_RESET);
#endif
switch (object->type) {
case OBJ_LIST:{
ObjList* list = (ObjList*)object;
free_val_array(vm,&list->values);
FREE(vm, ObjList, object);
break;
}
case OBJ_BOUND_METHOD:
FREE(vm,ObjBoundMethod, object);
break;
case OBJ_CLASS:{
ObjClass* klass = (ObjClass*)object;
free_table(vm,&klass->methods);
FREE(vm,ObjClass, object);
break;
}
case OBJ_CLOSURE:{
ObjClosure* closure = (ObjClosure*)object;
FREE_ARRAY(vm,ObjUpvalue*, closure->upvalues, closure->upvalue_count);
FREE(vm,ObjClosure, object);
break;
}
case OBJ_FUNCTION:{
ObjFunction* function = (ObjFunction*)object;
free_chunk(vm,&function->chunk);
FREE(vm,ObjFunction, object);
break;
}
case OBJ_INSTANCE:{
ObjInstance* instance = (ObjInstance*)object;
free_table(vm,&instance->fields);
FREE(vm,ObjInstance, object);
break;
}
case OBJ_NATIVE:{
FREE(vm,ObjNative,object);
break;
}
case OBJ_STRING:{
ObjString* string = (ObjString*)object;
FREE_ARRAY(vm,char, string->chars, string->length + 1);
FREE(vm,ObjString, object);
break;
}
case OBJ_UPVALUE:
FREE(vm,ObjUpvalue, object);
break;
}
}
static void mark_roots(RotoVM* vm){
for(Value* slot = vm->stack; slot < vm->stack_top; slot++){
mark_value(vm,*slot);
}
for (int i = 0; i < vm->frameCount; i++) {
mark_object(vm,(Obj*)vm->frames[i].closure);
}
for (ObjUpvalue* upvalue = vm->open_upvalues; upvalue != NULL; upvalue = upvalue->next) {
mark_object(vm,(Obj*)upvalue);
}
mark_table(vm,&vm->globals);
mark_compiler_roots(vm);
mark_object(vm,(Obj*)vm->init_string);
}
static void trace_references(RotoVM* vm){
while (vm->gray_count > 0){
Obj* object = vm->gray_stack[--vm->gray_count];
blacken_object(vm,object);
}
}
static void sweep(RotoVM* vm){
Obj* previous = NULL;
Obj* object = vm->objects;
while(object != NULL){
if(object->is_marked){
object->is_marked = false;
previous = object;
object = object->next;
} else{
Obj* unreached = object;
object = object->next;
if (previous != NULL){
previous->next = object;
} else{
vm->objects = object;
}
free_object(vm,unreached);
}
}
}
void free_objects(RotoVM* vm) {
/* code */
Obj* object = vm->objects;
while (object != NULL) {
Obj* next = object->next;
free_object(vm,object);
object = next;
}
free(vm->gray_stack);
}
void collect_garbage(RotoVM* vm){
#ifdef DEBUG_LOG_GC
// printf("\x1B[32m");
// printf("-- gc begin\n");
__print_with_color(_GREEN,"-- gc begin\n");
size_t before = vm.bytes_alocated;
printf(_RESET);
#endif
//mark
mark_roots(vm);
//trace
trace_references(vm);
//weak references
table_remove_white(&vm->strings);
//sweep
sweep(vm);
vm->next_gc = vm->bytes_alocated * GC_HEAP_GROW_FACTOR;
#ifdef DEBUG_LOG_GC
// printf("\x1B[32m");
// printf("-- gc end\n");
__print_with_color(_GREEN,"-- gc end\n");
printf(" collected %zd bytes (from %zd to %zd) next at %zd\n",
before - vm.bytes_alocated, before, vm.bytes_alocated,vm.next_gc);
printf(_RESET);
#endif
}