diff options
author | Anuraag Agrawal <[email protected]> | 2022-11-02 16:52:48 +0900 |
---|---|---|
committer | GitHub <[email protected]> | 2022-11-02 08:52:48 +0100 |
commit | bce0516394f7446598d169867c31a708443dd2a4 (patch) | |
tree | 43a1f0f14393fde0aa4c56a5c7671c03cfdae28a | |
parent | 726d74ad1b81bacedc7e0d9873811e2cbe89b38a (diff) | |
download | tinygo-bce0516394f7446598d169867c31a708443dd2a4.tar.gz tinygo-bce0516394f7446598d169867c31a708443dd2a4.zip |
Allow custom wasm malloc implementation (#3245)
-rw-r--r-- | src/runtime/arch_tinygowasm.go | 52 | ||||
-rw-r--r-- | src/runtime/arch_tinygowasm_malloc.go | 58 |
2 files changed, 58 insertions, 52 deletions
diff --git a/src/runtime/arch_tinygowasm.go b/src/runtime/arch_tinygowasm.go index f1893d913..a4a701a8e 100644 --- a/src/runtime/arch_tinygowasm.go +++ b/src/runtime/arch_tinygowasm.go @@ -88,55 +88,3 @@ func growHeap() bool { // Heap has grown successfully. return true } - -// The below functions override the default allocator of wasi-libc. This ensures -// code linked from other languages can allocate memory without colliding with -// our GC allocations. - -var allocs = make(map[uintptr][]byte) - -//export malloc -func libc_malloc(size uintptr) unsafe.Pointer { - buf := make([]byte, size) - ptr := unsafe.Pointer(&buf[0]) - allocs[uintptr(ptr)] = buf - return ptr -} - -//export free -func libc_free(ptr unsafe.Pointer) { - if ptr == nil { - return - } - if _, ok := allocs[uintptr(ptr)]; ok { - delete(allocs, uintptr(ptr)) - } else { - panic("free: invalid pointer") - } -} - -//export calloc -func libc_calloc(nmemb, size uintptr) unsafe.Pointer { - // No difference between calloc and malloc. - return libc_malloc(nmemb * size) -} - -//export realloc -func libc_realloc(oldPtr unsafe.Pointer, size uintptr) unsafe.Pointer { - // It's hard to optimize this to expand the current buffer with our GC, but - // it is theoretically possible. For now, just always allocate fresh. - buf := make([]byte, size) - - if oldPtr != nil { - if oldBuf, ok := allocs[uintptr(oldPtr)]; ok { - copy(buf, oldBuf) - delete(allocs, uintptr(oldPtr)) - } else { - panic("realloc: invalid pointer") - } - } - - ptr := unsafe.Pointer(&buf[0]) - allocs[uintptr(ptr)] = buf - return ptr -} diff --git a/src/runtime/arch_tinygowasm_malloc.go b/src/runtime/arch_tinygowasm_malloc.go new file mode 100644 index 000000000..f7f0c5a2c --- /dev/null +++ b/src/runtime/arch_tinygowasm_malloc.go @@ -0,0 +1,58 @@ +//go:build tinygo.wasm && !custommalloc +// +build tinygo.wasm,!custommalloc + +package runtime + +import "unsafe" + +// The below functions override the default allocator of wasi-libc. This ensures +// code linked from other languages can allocate memory without colliding with +// our GC allocations. + +var allocs = make(map[uintptr][]byte) + +//export malloc +func libc_malloc(size uintptr) unsafe.Pointer { + buf := make([]byte, size) + ptr := unsafe.Pointer(&buf[0]) + allocs[uintptr(ptr)] = buf + return ptr +} + +//export free +func libc_free(ptr unsafe.Pointer) { + if ptr == nil { + return + } + if _, ok := allocs[uintptr(ptr)]; ok { + delete(allocs, uintptr(ptr)) + } else { + panic("free: invalid pointer") + } +} + +//export calloc +func libc_calloc(nmemb, size uintptr) unsafe.Pointer { + // No difference between calloc and malloc. + return libc_malloc(nmemb * size) +} + +//export realloc +func libc_realloc(oldPtr unsafe.Pointer, size uintptr) unsafe.Pointer { + // It's hard to optimize this to expand the current buffer with our GC, but + // it is theoretically possible. For now, just always allocate fresh. + buf := make([]byte, size) + + if oldPtr != nil { + if oldBuf, ok := allocs[uintptr(oldPtr)]; ok { + copy(buf, oldBuf) + delete(allocs, uintptr(oldPtr)) + } else { + panic("realloc: invalid pointer") + } + } + + ptr := unsafe.Pointer(&buf[0]) + allocs[uintptr(ptr)] = buf + return ptr +} |