github.com/tetratelabs/tinymem@v0.1.0/exports.go (about) 1 package tinymem 2 3 import "unsafe" 4 5 var alivePointers = map[uintptr][]byte{} 6 7 // malloc is a WebAssembly export named "_malloc" that allocates a pointer 8 // (linear memory offset) that can be used for the given size in bytes. 9 // 10 // Note: This is an ownership transfer, which means the caller must call free 11 // when finished. 12 // 13 //export _malloc 14 func malloc(size uint32) uintptr { 15 buf := make([]byte, size) 16 ptr := &buf[0] 17 unsafePtr := uintptr(unsafe.Pointer(ptr)) 18 alivePointers[unsafePtr] = buf 19 return unsafePtr 20 } 21 22 // free is a WebAssembly export named "_free" that deallocates a pointer 23 // allocated by malloc. 24 // 25 //export _free 26 func free(ptr uintptr) { 27 delete(alivePointers, ptr) 28 }