From ee724c0764be8f5672462af73d108fcf8f041152 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 19 Mar 2024 19:10:02 +0000 Subject: Support bump allocator on Wasm backends --- runtime/meson.build | 1 + runtime/src/shed/gc/bump/gc.c | 20 ++++++-------------- runtime/src/shed/memory.c | 43 +++++++++++++++++++++++++++++++++++++++++++ runtime/src/shed/memory.h | 25 +++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 runtime/src/shed/memory.c create mode 100644 runtime/src/shed/memory.h (limited to 'runtime') diff --git a/runtime/meson.build b/runtime/meson.build index 04b7c03b..3c1bb383 100644 --- a/runtime/meson.build +++ b/runtime/meson.build @@ -17,6 +17,7 @@ static_library( 'src/shed/effects.c', 'src/shed/gc.c', 'src/shed/ints.c', + 'src/shed/memory.c', 'src/shed/primitives.c', 'src/shed/strings.c', 'src/shed/namespaces/Shed/Builtins.c', diff --git a/runtime/src/shed/gc/bump/gc.c b/runtime/src/shed/gc/bump/gc.c index 298fd3a6..fe859dff 100644 --- a/runtime/src/shed/gc/bump/gc.c +++ b/runtime/src/shed/gc/bump/gc.c @@ -1,7 +1,6 @@ -#include - #include "../../../patio/patio.h" #include "../../gc.h" +#include "../../memory.h" #define MAX(a,b) (((a)>(b))?(a):(b)) @@ -11,16 +10,7 @@ intptr_t remaining = 0; // == Initialization == 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; - } + ptr = shed__runtime__memory_init(sizeof(PatioInt)); } // == Allocation == @@ -33,8 +23,10 @@ void* shed__runtime__gc_alloc(size_t size, uint32_t runtime_type_tag) { if (remaining < size) { // TODO: what should we grow by? intptr_t increment = MAX(1024 * 1024, size); - // TODO: check for -1 from sbrk - sbrk(increment); + // TODO: check for -1 from shed__runtime__memory_grow + // TODO: shed__runtime__memory_grow may grow by more than requested amount. + // Return the amount of allocated memory? + shed__runtime__memory_grow(increment); remaining += increment; } diff --git a/runtime/src/shed/memory.c b/runtime/src/shed/memory.c new file mode 100644 index 00000000..8ed05f38 --- /dev/null +++ b/runtime/src/shed/memory.c @@ -0,0 +1,43 @@ +#include "./memory.h" + +#ifdef __wasm__ + +const intptr_t PAGE_SIZE = 65536; + +void* shed__runtime__memory_init(intptr_t alignment) { + return (void*) (__builtin_wasm_memory_size(0) * PAGE_SIZE); +} + +void* shed__runtime__memory_grow(intptr_t increment) { + if (increment % PAGE_SIZE != 0) { + increment += PAGE_SIZE - (increment % PAGE_SIZE); + } + + return (void*) (__builtin_wasm_memory_grow(0, increment / PAGE_SIZE) * PAGE_SIZE); +} + +#else + +#include + +void* shed__runtime__memory_init(intptr_t alignment) { + void* ptr = sbrk(0); + + intptr_t remainder = (intptr_t) ptr % alignment; + if (remainder != 0) { + intptr_t increment = alignment - remainder; + // TODO: check for -1 from sbrk + sbrk(increment); + + // TODO: assert alignment is correct + ptr += increment; + } + + return ptr; +} + +void* shed__runtime__memory_grow(intptr_t increment) { + return sbrk(increment); +} + +#endif diff --git a/runtime/src/shed/memory.h b/runtime/src/shed/memory.h new file mode 100644 index 00000000..3c057149 --- /dev/null +++ b/runtime/src/shed/memory.h @@ -0,0 +1,25 @@ +#include + +#ifndef SHED_MEMORY_H +#define SHED_MEMORY_H + +/** + * Initialise the heap and return a pointer to the current end of the heap, + * aligned to the given alignment. + * + * Returns -1 on failure. + */ +void* shed__runtime__memory_init(intptr_t alignment); + +/** + * Grow the heap by at least the given increment, and return a pointer to the + * allocated region. + * + * The caller is responsible for ensuring that the increment is a multiple of + * the alignment passed to shed__runtime__memory_init. + * + * Returns -1 on failure. + */ +void* shed__runtime__memory_grow(intptr_t increment); + +#endif -- cgit v1.2.3