From 3b7a25939f6117ab11084a448e591ffad5380874 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 19 Mar 2024 18:33:07 +0000 Subject: Add bump allocator --- runtime/build.py | 2 +- runtime/meson_options.txt | 2 +- runtime/src/shed/gc/bump/gc.c | 53 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 runtime/src/shed/gc/bump/gc.c (limited to 'runtime') 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 + +#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() { +} -- cgit v1.2.3