Skip to content

Commit 1889b33

Browse files
committed
runtime: temporarily store waspi2 memory allocations from cabi_realloc
This is an initial take at solving the interaction between the TinyGo GC and the Component Model allocation scheme (cabi_realloc).
1 parent 180662f commit 1889b33

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/runtime/runtime_wasip2.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,21 @@ func os_runtime_args() []string {
3030
}
3131

3232
//export cabi_realloc
33-
func cabi_realloc(ptr, oldsize, align, newsize unsafe.Pointer) unsafe.Pointer {
34-
return realloc(ptr, uintptr(newsize))
33+
func cabi_realloc(ptr unsafe.Pointer, oldSize, align, newSize uintptr) unsafe.Pointer {
34+
if newSize == 0 {
35+
return nil
36+
}
37+
newPtr := realloc(ptr, newSize)
38+
if ptr != nil {
39+
for i := range wasmAllocs {
40+
if wasmAllocs[i] == ptr {
41+
wasmAllocs[i] = newPtr
42+
return newPtr
43+
}
44+
}
45+
}
46+
wasmAllocs = append(wasmAllocs, newPtr)
47+
return newPtr
3548
}
3649

3750
func ticksToNanoseconds(ticks timeUnit) int64 {

src/runtime/runtime_wasmentry.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,15 @@ func wasmExportExit() {
9696
// //go:wasmexport function has exited.
9797
schedulerExit = true
9898

99+
// Clear wasm allocations.
100+
wasmAllocs = nil
101+
99102
task.Pause()
100103

101104
// TODO: we could cache the allocated stack so we don't have to keep
102105
// allocating a new stack on every //go:wasmexport call.
103106
}
107+
108+
// wasmAllocs holds memory allocated by the host in guest memory.
109+
// See func cabi_realloc for more information.
110+
var wasmAllocs []unsafe.Pointer

0 commit comments

Comments
 (0)