aboutsummaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-03-08 16:39:46 +0000
committerMichael Williamson <mike@zwobble.org>2025-03-08 16:39:46 +0000
commitc8ffad9a32f18441ff3d6634ad22922b6d723260 (patch)
tree3fba48090d62a84c0d3a349b6edc15c65076dee8 /runtime
parent90efb7b60b3e90729632c5334693ed1ddb1f24df (diff)
Extract array_copy()
Diffstat (limited to 'runtime')
-rw-r--r--runtime/src/shed/namespaces/Std/Lists.c41
1 files changed, 23 insertions, 18 deletions
diff --git a/runtime/src/shed/namespaces/Std/Lists.c b/runtime/src/shed/namespaces/Std/Lists.c
index f7fcf0f8..a7a0ac25 100644
--- a/runtime/src/shed/namespaces/Std/Lists.c
+++ b/runtime/src/shed/namespaces/Std/Lists.c
@@ -16,6 +16,21 @@ struct ShedArray {
ShedObject elements[];
};
+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
+ );
+ array->object_header.mark = 0b110;
+ array->size = size;
+
+ for (PatioInt element_index = 0; element_index < size; element_index++) {
+ array->elements[element_index] = source->elements[element_index];
+ }
+
+ return array;
+}
+
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)
@@ -67,16 +82,11 @@ ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__unsafeAdd
array_builder->is_owner = false;
array = array_builder->array;
} else {
- array = shed__runtime__gc_alloc(
- sizeof(struct ShedArray) + sizeof(ShedObject) * array_builder->capacity,
- 0
+ array = array_copy(
+ array_builder->array,
+ array_builder->size,
+ array_builder->capacity
);
- array->object_header.mark = 0b110;
- array->size = array_builder->size;
-
- for (PatioInt element_index = 0; element_index < array_builder->size; element_index++) {
- array->elements[element_index] = array_builder->array->elements[element_index];
- }
}
array->elements[array_builder->size] = arg1;
@@ -103,16 +113,11 @@ ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__build(She
array_builder->array->size = array_builder->size;
return (ShedObject) array_builder->array;
} else {
- struct ShedArray* array = shed__runtime__gc_alloc(
- sizeof(struct ShedArray) + sizeof(ShedObject) * array_builder->size,
- 0
+ struct ShedArray* array = array_copy(
+ array_builder->array,
+ array_builder->size,
+ array_builder->size
);
- array->object_header.mark = 0b110;
- array->size = array_builder->size;
-
- for (PatioInt element_index = 0; element_index < array_builder->size; element_index++) {
- array->elements[element_index] = array_builder->array->elements[element_index];
- }
return (ShedObject) array;
}