Allow custom wasm malloc implementation (#3245)
Этот коммит содержится в:
родитель
726d74ad1b
коммит
bce0516394
2 изменённых файлов: 58 добавлений и 52 удалений
|
@ -88,55 +88,3 @@ func growHeap() bool {
|
||||||
// Heap has grown successfully.
|
// Heap has grown successfully.
|
||||||
return true
|
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
|
|
||||||
}
|
|
||||||
|
|
58
src/runtime/arch_tinygowasm_malloc.go
Обычный файл
58
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
|
||||||
|
}
|
Загрузка…
Создание таблицы
Сослаться в новой задаче