aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-19 17:29:30 +0200
committerBad Diode <bd@badd10de.dev>2021-10-19 17:29:30 +0200
commit56ddeab2c7d32b2cd8e1e0435d3f006d7985cbe3 (patch)
tree9b3fafd2911b6e9d6e5226ba815e7d0ee32a0e4b
parentf7b5da260fc7b6b73b5ed6c87d3593de372db6ad (diff)
downloadbdl-56ddeab2c7d32b2cd8e1e0435d3f006d7985cbe3.tar.gz
bdl-56ddeab2c7d32b2cd8e1e0435d3f006d7985cbe3.zip
Change gc.roots to use darray.h
-rw-r--r--src/bootstrap/gc.c26
-rw-r--r--src/bootstrap/gc.h9
-rwxr-xr-xsrc/bootstrap/main.c4
3 files changed, 12 insertions, 27 deletions
diff --git a/src/bootstrap/gc.c b/src/bootstrap/gc.c
index 473930a..296ae30 100644
--- a/src/bootstrap/gc.c
+++ b/src/bootstrap/gc.c
@@ -18,16 +18,12 @@ alloc_env(void) {
18 18
19void 19void
20push_root(Object *obj) { 20push_root(Object *obj) {
21 if (gc.roots.size == gc.roots.cap) { 21 array_push(gc.roots, obj);
22 gc.roots.cap *= 2;
23 gc.roots.buf = realloc(gc.roots.buf, gc.roots.cap * sizeof(Object *));
24 }
25 gc.roots.buf[gc.roots.size++] = obj;
26} 22}
27 23
28Object * 24Object *
29pop_root(void) { 25pop_root(void) {
30 return gc.roots.buf[gc.roots.size--]; 26 return array_pop(gc.roots);
31} 27}
32 28
33void 29void
@@ -55,11 +51,6 @@ init_gc(void) {
55 .size = 0, 51 .size = 0,
56 .cap = GC_ENVS_CAP, 52 .cap = GC_ENVS_CAP,
57 }, 53 },
58 .roots = (RootNodes){
59 .buf = malloc(GC_ROOTS_CAP * sizeof(Object*)),
60 .size = 0,
61 .cap = GC_ROOTS_CAP,
62 },
63 .free_objects = (FreeList){ 54 .free_objects = (FreeList){
64 .buf = malloc(GC_OBJS_CAP * sizeof(size_t)), 55 .buf = malloc(GC_OBJS_CAP * sizeof(size_t)),
65 .size = GC_OBJS_CAP, 56 .size = GC_OBJS_CAP,
@@ -76,6 +67,7 @@ init_gc(void) {
76 .cap = GC_ACTIVE_ENVS_CAP, 67 .cap = GC_ACTIVE_ENVS_CAP,
77 }, 68 },
78 }; 69 };
70 array_init(gc.roots, GC_ROOTS_CAP);
79 71
80 // The free list stores the offset from the initial position for all 72 // The free list stores the offset from the initial position for all
81 // available slots. 73 // available slots.
@@ -123,8 +115,8 @@ mark_and_sweep(void) {
123 for (size_t i = 0; i < gc.active_envs.size; i++) { 115 for (size_t i = 0; i < gc.active_envs.size; i++) {
124 mark_environment(gc.active_envs.buf[i]); 116 mark_environment(gc.active_envs.buf[i]);
125 } 117 }
126 for (size_t i = 0; i < gc.roots.size; i++) { 118 for (size_t i = 0; i < array_size(gc.roots); i++) {
127 mark_obj(gc.roots.buf[i]); 119 mark_obj(gc.roots[i]);
128 } 120 }
129 121
130 // Reset the free list. 122 // Reset the free list.
@@ -173,8 +165,8 @@ mark_and_sweep(void) {
173void 165void
174dump_gc(void) { 166dump_gc(void) {
175 printf("-------------- ROOTS -------------- \n"); 167 printf("-------------- ROOTS -------------- \n");
176 for (size_t i = 0; i < gc.roots.size; i++) { 168 for (size_t i = 0; i < array_size(gc.roots); i++) {
177 display(gc.roots.buf[i]); 169 display(gc.roots[i]);
178 printf("\n"); 170 printf("\n");
179 } 171 }
180 printf("--------- OBJECTS (TOP 20) -------- \n"); 172 printf("--------- OBJECTS (TOP 20) -------- \n");
@@ -195,8 +187,8 @@ dump_gc(void) {
195 printf("\n"); 187 printf("\n");
196 } 188 }
197 printf("-------------- MISC --------------- \n"); 189 printf("-------------- MISC --------------- \n");
198 printf("gc.roots.size: %ld\n", gc.roots.size); 190 printf("gc.roots.size: %ld\n", array_size(gc.roots));
199 printf("gc.roots.cap: %ld\n", gc.roots.cap); 191 printf("gc.roots.cap: %ld\n", array_size(gc.roots));
200 printf("gc.active_envs.size: %ld\n", gc.active_envs.size); 192 printf("gc.active_envs.size: %ld\n", gc.active_envs.size);
201 printf("gc.active_envs.cap: %ld\n", gc.active_envs.cap); 193 printf("gc.active_envs.cap: %ld\n", gc.active_envs.cap);
202 printf("gc.obj_cap: %ld\n", gc.obj_cap); 194 printf("gc.obj_cap: %ld\n", gc.obj_cap);
diff --git a/src/bootstrap/gc.h b/src/bootstrap/gc.h
index 038c820..da53a21 100644
--- a/src/bootstrap/gc.h
+++ b/src/bootstrap/gc.h
@@ -4,13 +4,6 @@
4#include "objects.h" 4#include "objects.h"
5#include "environment.h" 5#include "environment.h"
6 6
7// Stack of root nodes.
8typedef struct RootNodes {
9 Object **buf;
10 size_t size;
11 size_t cap;
12} RootNodes;
13
14// Stack of active environments. 7// Stack of active environments.
15typedef struct ActiveEnvs { 8typedef struct ActiveEnvs {
16 Environment **buf; 9 Environment **buf;
@@ -32,7 +25,7 @@ typedef struct FreeList {
32} FreeList; 25} FreeList;
33 26
34typedef struct GC { 27typedef struct GC {
35 RootNodes roots; 28 Object **roots;
36 Environments envs; 29 Environments envs;
37 Object *objects; 30 Object *objects;
38 size_t obj_cap; 31 size_t obj_cap;
diff --git a/src/bootstrap/main.c b/src/bootstrap/main.c
index 22332af..c2c88d6 100755
--- a/src/bootstrap/main.c
+++ b/src/bootstrap/main.c
@@ -108,9 +108,9 @@ process_source(const StringView *source) {
108 }; 108 };
109 while (has_next_token(&visitor) && peek_token(&visitor).type != TOKEN_EOF) { 109 while (has_next_token(&visitor) && peek_token(&visitor).type != TOKEN_EOF) {
110 // Check the root node stack size before parsing 110 // Check the root node stack size before parsing
111 size_t root_stack_size = gc.roots.size; 111 size_t root_stack_size = array_size(gc.roots);
112 Object *root = parse_tree(&visitor); 112 Object *root = parse_tree(&visitor);
113 gc.roots.size = root_stack_size; 113 array_head(gc.roots)->size = root_stack_size;
114 if (root == obj_err || errors_n != 0) { 114 if (root == obj_err || errors_n != 0) {
115 break; 115 break;
116 } 116 }