aboutsummaryrefslogtreecommitdiff
path: root/runtime/src/shed/namespaces
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-03-16 14:32:10 +0000
committerMichael Williamson <mike@zwobble.org>2026-03-20 10:14:41 +0000
commitb81f97cd01753eabe3722b8a9211e4f3f8d621ef (patch)
tree2638402581a59b19e7174e4ff0d44071b6b9582b /runtime/src/shed/namespaces
parent29e8e5f6823a76e468d980d95934372f2c13c0d6 (diff)
Garbage collect arrays
Diffstat (limited to 'runtime/src/shed/namespaces')
-rw-r--r--runtime/src/shed/namespaces/Std/Lists.c31
1 files changed, 13 insertions, 18 deletions
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;
}