aboutsummaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-03-19 18:33:07 +0000
committerMichael Williamson <mike@zwobble.org>2024-03-19 18:33:07 +0000
commit3b7a25939f6117ab11084a448e591ffad5380874 (patch)
treeabfeccf683810becba3b2c2a0b919b27b149f777 /runtime
parent55349d1038858afc40f543873aae124d1f6263a8 (diff)
Add bump allocator
Diffstat (limited to 'runtime')
-rw-r--r--runtime/build.py2
-rw-r--r--runtime/meson_options.txt2
-rw-r--r--runtime/src/shed/gc/bump/gc.c53
3 files changed, 55 insertions, 2 deletions
diff --git a/runtime/build.py b/runtime/build.py
index bd55dbf4..927eaf20 100644
--- a/runtime/build.py
+++ b/runtime/build.py
@@ -30,7 +30,7 @@ targets = [
]
-gcs = ["mark-and-sweep"]
+gcs = ["bump", "mark-and-sweep"]
def main():
diff --git a/runtime/meson_options.txt b/runtime/meson_options.txt
index f8e34764..1bec2d79 100644
--- a/runtime/meson_options.txt
+++ b/runtime/meson_options.txt
@@ -1,4 +1,4 @@
-option('gc', type : 'combo', choices : ['mark-and-sweep'], value : 'mark-and-sweep')
+option('gc', type : 'combo', choices : ['bump', 'mark-and-sweep'], value : 'mark-and-sweep')
# In production, we only want to collect periodically, but we currently always
# collect by default to hopefully surface errors more quickly.
diff --git a/runtime/src/shed/gc/bump/gc.c b/runtime/src/shed/gc/bump/gc.c
new file mode 100644
index 00000000..e61e5d89
--- /dev/null
+++ b/runtime/src/shed/gc/bump/gc.c
@@ -0,0 +1,53 @@
+#include <unistd.h>
+
+#include "../../../patio/patio.h"
+#include "../../gc.h"
+
+#define MAX(a,b) (((a)>(b))?(a):(b))
+
+void* ptr = NULL;
+intptr_t remaining = 0;
+
+void shed__runtime__gc_init(void) {
+ ptr = sbrk(0);
+
+ intptr_t remainder = (intptr_t) ptr % sizeof(PatioInt);
+ if (remainder != 0) {
+ intptr_t increment = sizeof(PatioInt) - remainder;
+ // TODO: check for -1 from sbrk
+ sbrk(increment);
+
+ ptr += increment;
+ }
+}
+
+void* shed__runtime__gc_alloc(size_t size, uint32_t runtime_type_tag) {
+ // TODO: call init (once) from generated code
+ if (ptr == NULL) {
+ shed__runtime__gc_init();
+ }
+
+ if (size % sizeof(PatioInt) != 0) {
+ size = size + (sizeof(PatioInt) - size % sizeof(PatioInt));
+ }
+
+ if (remaining < size) {
+ // TODO: what should we grow by?
+ intptr_t increment = MAX(1024 * 1024, size);
+ // TODO: check for -1 from sbrk
+ sbrk(increment);
+
+ remaining += increment;
+ }
+
+ struct ShedGcObjectHeader* object = ptr;
+ ptr += size;
+ remaining -= size;
+
+ object->runtime_type_tag = runtime_type_tag;
+
+ return object;
+}
+
+void shed__runtime__gc_collect() {
+}