aboutsummaryrefslogtreecommitdiffstats
path: root/src/bootstrap/gc.c
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-15 17:58:05 +0200
committerBad Diode <bd@badd10de.dev>2021-10-15 17:58:05 +0200
commit87b1a0d4a833dd0b2164481be45f7d1c59375040 (patch)
tree87828c2149c7db2880b22a306b2c5d3511185cae /src/bootstrap/gc.c
parent14814ecbf53760654aab34e0613abf347a54113f (diff)
downloadbdl-87b1a0d4a833dd0b2164481be45f7d1c59375040.tar.gz
bdl-87b1a0d4a833dd0b2164481be45f7d1c59375040.zip
Add boilerplate for GC allocator
Diffstat (limited to 'src/bootstrap/gc.c')
-rw-r--r--src/bootstrap/gc.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/bootstrap/gc.c b/src/bootstrap/gc.c
new file mode 100644
index 0000000..fff4201
--- /dev/null
+++ b/src/bootstrap/gc.c
@@ -0,0 +1,48 @@
1typedef struct GC {
2 Object *obj_list;
3 // Free list keeps track of the offset numbers from the obj_list
4 size_t *free_list;
5 size_t obj_size;
6 size_t obj_cap;
7 size_t fl_pos;
8 size_t available_slots;
9} GC;
10
11// FIXME: small value for testing purposes
12#define GC_INITIAL_HEAP 100000
13
14static GC gc;
15
16void
17init_gc(void) {
18 gc = (GC){
19 .obj_cap = GC_INITIAL_HEAP,
20 .available_slots = GC_INITIAL_HEAP,
21 };
22 gc.free_list = malloc(GC_INITIAL_HEAP * sizeof(size_t *));
23 gc.obj_list = malloc(GC_INITIAL_HEAP * sizeof(Object *));
24
25 // The free list stores the offset from the initial position for all
26 // available slots.
27 for (size_t i = 0; i < gc.obj_cap; i++) {
28 gc.free_list[i] = i;
29 }
30}
31
32Object *
33alloc_object(ObjectType type) {
34 if (gc.available_slots == 0) {
35 // TODO: trigger garbage collection.
36 if (gc.available_slots == 0) {
37 // TODO: grow heap tables.
38 // NOTE: When growing the tables, we might lose the pointer
39 // references! Should we work with offsets all the way? That is for
40 // cdr and car? Should we have a utility function?
41 }
42 }
43 size_t slot = gc.free_list[gc.fl_pos++];
44 gc.available_slots--;
45 Object *obj = gc.obj_list + slot;
46 obj->type = type;
47 return obj;
48}