9 #include "system/error.h"
10 #include "system/lt.h"
12 #define GC_INITIAL_CAPACITY 256
23 static long int value_of_expr(struct Expr expr)
25 if (expr.type == EXPR_CONS) {
26 return (long int) expr.cons;
27 } else if (expr.type == EXPR_ATOM) {
28 return (long int) expr.atom;
34 static int compare_exprs(const void *a, const void *b)
39 const long int ptr_a = value_of_expr(*(const struct Expr *)a);
40 const long int ptr_b = value_of_expr(*(const struct Expr *)b);
41 const long int d = ptr_b - ptr_a;
59 Gc *gc = PUSH_LT(lt, malloc(sizeof(Gc)), free);
61 throw_error(ERROR_TYPE_LIBC);
66 gc->exprs = PUSH_LT(lt, malloc(sizeof(struct Expr) * GC_INITIAL_CAPACITY), free);
67 if (gc->exprs == NULL) {
68 throw_error(ERROR_TYPE_LIBC);
72 gc->visited = PUSH_LT(lt, malloc(sizeof(int) * GC_INITIAL_CAPACITY), free);
73 if (gc->visited == NULL) {
74 throw_error(ERROR_TYPE_LIBC);
79 gc->capacity = GC_INITIAL_CAPACITY;
84 void destroy_gc(Gc *gc)
90 int gc_add_expr(Gc *gc, struct Expr expr)
94 if (gc->size >= gc->capacity) {
95 const size_t new_capacity = gc->capacity * 2;
96 struct Expr *const new_exprs = realloc(
98 sizeof(struct Expr) * new_capacity);
100 if (new_exprs == NULL) {
104 gc->capacity = new_capacity;
105 gc->exprs = REPLACE_LT(gc->lt, gc->exprs, new_exprs);
108 gc->exprs[gc->size++] = expr;
113 static long int gc_find_expr(Gc *gc, struct Expr expr)
118 struct Expr *result =
119 (struct Expr *) bsearch(&expr, gc->exprs, gc->size,
120 sizeof(struct Expr), compare_exprs);
122 if (result == NULL) {
126 return (long int) (result - gc->exprs);
129 static void gc_traverse_expr(Gc *gc, struct Expr root)
132 const long int root_index = gc_find_expr(gc, root);
133 assert(root_index >= 0);
135 if (gc->visited[root_index]) {
140 gc_traverse_expr(gc, root.cons->car);
141 gc_traverse_expr(gc, root.cons->cdr);
144 gc->visited[root_index] = 1;
147 void gc_collect(Gc *gc, struct Expr root)
152 /* Sort gc->exprs O(nlogn) */
153 qsort(gc->exprs, gc->size, sizeof(struct Expr), compare_exprs);
155 /* Defragment O(n) */
156 while(gc->size > 0 && gc->exprs[gc->size - 1].type == EXPR_VOID) {
160 /* Initialize visited array O(n) */
161 memset(gc->visited, 0, sizeof(int) * gc->size);
163 /* Traverse root O(nlogn) */
164 gc_traverse_expr(gc, root);
166 /* Dealloc unvisted O(n) */
167 for (size_t i = 0; i < gc->size; ++i) {
168 if (!gc->visited[i]) {
169 destroy_expr(gc->exprs[i]);
170 gc->exprs[i] = void_expr();