From b81f97cd01753eabe3722b8a9211e4f3f8d621ef Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 16 Mar 2025 14:32:10 +0000 Subject: Garbage collect arrays --- runtime/src/shed/namespaces/Std/Lists.c | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) (limited to 'runtime/src/shed/namespaces') diff --git a/runtime/src/shed/namespaces/Std/Lists.c b/runtime/src/shed/namespaces/Std/Lists.c index a7a0ac25..a803d010 100644 --- a/runtime/src/shed/namespaces/Std/Lists.c +++ b/runtime/src/shed/namespaces/Std/Lists.c @@ -1,27 +1,27 @@ +#include "shed/arrays.h" #include "shed/gc.h" #include "shed/ints.h" #include "./Lists.h" struct ShedArrayBuilder { struct ShedGcObjectHeader object_header; + // TODO: in principle, this should really be a PatioInt since the GC should + // assumes everything is stored in a Patio int. In practice, pointers in + // C are the same size as PatioInt, and indeed the GC assumes this is the + // case (at the time of writing at least), so this should also work. + struct ShedArray* array; PatioInt capacity; PatioInt size; bool is_owner; - struct ShedArray* array; }; -struct ShedArray { - struct ShedGcObjectHeader object_header; - PatioInt size; - ShedObject elements[]; -}; +// TODO: move other array code into `shed/arrays`? static struct ShedArray* array_copy(struct ShedArray* source, PatioInt size, PatioInt capacity) { struct ShedArray* array = shed__runtime__gc_alloc( sizeof(struct ShedArray) + sizeof(ShedObject) * capacity, - 0 + 1 ); - array->object_header.mark = 0b110; array->size = size; for (PatioInt element_index = 0; element_index < size; element_index++) { @@ -32,23 +32,19 @@ static struct ShedArray* array_copy(struct ShedArray* source, PatioInt size, Pat } ShedObject shed__runtime__namespace__Std__Lists__arrayBuilder(ShedClosurePointer closurePointer, ShedObject capacityObject) { - // TODO: runtime type tag / GC support (don't set mark to 0b110, set appropriate runtime type) - PatioInt capacity = shed__runtime__int_value(capacityObject); struct ShedArray* array = shed__runtime__gc_alloc( sizeof(struct ShedArray) + sizeof(ShedObject) * capacity, - 0 + 1 ); - array->object_header.mark = 0b110; array->size = 0; - struct ShedArrayBuilder* array_builder = shed__runtime__gc_alloc(sizeof(struct ShedArrayBuilder), 0); - array_builder->object_header.mark = 0b110; + struct ShedArrayBuilder* array_builder = shed__runtime__gc_alloc(sizeof(struct ShedArrayBuilder), 2); + array_builder->array = array; array_builder->capacity = capacity; array_builder->size = 0; array_builder->is_owner = true; - array_builder->array = array; return (ShedObject) array_builder; } @@ -91,12 +87,11 @@ ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__unsafeAdd array->elements[array_builder->size] = arg1; - struct ShedArrayBuilder* new_array_builder = shed__runtime__gc_alloc(sizeof(struct ShedArrayBuilder), 0); - new_array_builder->object_header.mark = 0b110; + struct ShedArrayBuilder* new_array_builder = shed__runtime__gc_alloc(sizeof(struct ShedArrayBuilder), 2); + new_array_builder->array = array; new_array_builder->capacity = array_builder->capacity; new_array_builder->size = array_builder->size + 1; new_array_builder->is_owner = true; - new_array_builder->array = array; return (ShedObject) new_array_builder; } -- cgit v1.2.3