aboutsummaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-03-19 19:10:02 +0000
committerMichael Williamson <mike@zwobble.org>2024-03-19 19:10:02 +0000
commitee724c0764be8f5672462af73d108fcf8f041152 (patch)
tree3f71bb00add5a94efeadf5c40782ddd93d1af0bf /runtime
parenta9a3c3a50b270e21180898d458597a4abfb7804b (diff)
Support bump allocator on Wasm backends
Diffstat (limited to 'runtime')
-rw-r--r--runtime/meson.build1
-rw-r--r--runtime/src/shed/gc/bump/gc.c20
-rw-r--r--runtime/src/shed/memory.c43
-rw-r--r--runtime/src/shed/memory.h25
4 files changed, 75 insertions, 14 deletions
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 <unistd.h>
-
#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 <unistd.h>
+
+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 <stdint.h>
+
+#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