wasm: fix C realloc and optimize it a bit

- Do not use make([]byte, ...) to allocate, instead call the allocator
    directly with a nil (undefined) layout. This makes sure the precise
    GC will scan the contents of the allocation, since C could very well
    put pointers in there.
  - Simplify the map to use the pointer as the key and the size as the
    value, instead of storing the slices directly in the map.
This commit is contained in:
Ayke van Laethem
2025-05-21 12:33:44 +02:00
committed by Ron Evans
parent 8872d5ebfc
commit 26ee3fb93e
2 changed files with 22 additions and 12 deletions
+3
View File
@@ -250,6 +250,9 @@ func (b *builder) createMapIteratorNext(rangeVal ssa.Value, llvmRangeVal, it llv
func hashmapIsBinaryKey(keyType types.Type) bool { func hashmapIsBinaryKey(keyType types.Type) bool {
switch keyType := keyType.Underlying().(type) { switch keyType := keyType.Underlying().(type) {
case *types.Basic: case *types.Basic:
// TODO: unsafe.Pointer is also a binary key, but to support that we
// need to fix an issue with interp first (see
// https://github.com/tinygo-org/tinygo/pull/4898).
return keyType.Info()&(types.IsBoolean|types.IsInteger) != 0 return keyType.Info()&(types.IsBoolean|types.IsInteger) != 0
case *types.Pointer: case *types.Pointer:
return true return true
+19 -12
View File
@@ -8,16 +8,21 @@ import "unsafe"
// code linked from other languages can allocate memory without colliding with // code linked from other languages can allocate memory without colliding with
// our GC allocations. // our GC allocations.
var allocs = make(map[uintptr][]byte) // Map of allocations, where the key is the allocated pointer and the value is
// the size of the allocation.
// TODO: make this a map[unsafe.Pointer]uintptr, since that results in slightly
// smaller binaries. But for that to work, unsafe.Pointer needs to be seen as a
// binary key (which it is not at the moment).
// See https://github.com/tinygo-org/tinygo/pull/4898 for details.
var allocs = make(map[*byte]uintptr)
//export malloc //export malloc
func libc_malloc(size uintptr) unsafe.Pointer { func libc_malloc(size uintptr) unsafe.Pointer {
if size == 0 { if size == 0 {
return nil return nil
} }
buf := make([]byte, size) ptr := alloc(size, nil)
ptr := unsafe.Pointer(&buf[0]) allocs[(*byte)(ptr)] = size
allocs[uintptr(ptr)] = buf
return ptr return ptr
} }
@@ -26,8 +31,8 @@ func libc_free(ptr unsafe.Pointer) {
if ptr == nil { if ptr == nil {
return return
} }
if _, ok := allocs[uintptr(ptr)]; ok { if _, ok := allocs[(*byte)(ptr)]; ok {
delete(allocs, uintptr(ptr)) delete(allocs, (*byte)(ptr))
} else { } else {
panic("free: invalid pointer") panic("free: invalid pointer")
} }
@@ -48,18 +53,20 @@ 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's hard to optimize this to expand the current buffer with our GC, but
// it is theoretically possible. For now, just always allocate fresh. // it is theoretically possible. For now, just always allocate fresh.
buf := make([]byte, size) // TODO: we could skip this if the new allocation is smaller than the old.
ptr := alloc(size, nil)
if oldPtr != nil { if oldPtr != nil {
if oldBuf, ok := allocs[uintptr(oldPtr)]; ok { if oldSize, ok := allocs[(*byte)(oldPtr)]; ok {
copy(buf, oldBuf) oldBuf := unsafe.Slice((*byte)(oldPtr), oldSize)
delete(allocs, uintptr(oldPtr)) newBuf := unsafe.Slice((*byte)(ptr), size)
copy(newBuf, oldBuf)
delete(allocs, (*byte)(oldPtr))
} else { } else {
panic("realloc: invalid pointer") panic("realloc: invalid pointer")
} }
} }
ptr := unsafe.Pointer(&buf[0]) allocs[(*byte)(ptr)] = size
allocs[uintptr(ptr)] = buf
return ptr return ptr
} }