Revert "examples(allocation): free memory after unmarshalling a result from the guest (#1368)"
This reverts commit 5aafcc4836.
This commit is contained in:
@@ -90,15 +90,9 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
}
|
}
|
||||||
|
// Note: This pointer is still owned by TinyGo, so don't try to free it!
|
||||||
greetingPtr := uint32(ptrSize[0] >> 32)
|
greetingPtr := uint32(ptrSize[0] >> 32)
|
||||||
greetingSize := uint32(ptrSize[0])
|
greetingSize := uint32(ptrSize[0])
|
||||||
|
|
||||||
// We don't need the memory after deserialization: make sure it is freed.
|
|
||||||
if greetingPtr != 0 {
|
|
||||||
defer free.Call(ctx, uint64(greetingPtr))
|
|
||||||
}
|
|
||||||
|
|
||||||
// The pointer is a linear memory offset, which is where we write the name.
|
// The pointer is a linear memory offset, which is where we write the name.
|
||||||
if bytes, ok := mod.Memory().Read(greetingPtr, greetingSize); !ok {
|
if bytes, ok := mod.Memory().Read(greetingPtr, greetingSize); !ok {
|
||||||
log.Panicf("Memory.Read(%d, %d) out of range of memory size %d",
|
log.Panicf("Memory.Read(%d, %d) out of range of memory size %d",
|
||||||
|
|||||||
20
examples/allocation/tinygo/testdata/greet.go
vendored
20
examples/allocation/tinygo/testdata/greet.go
vendored
@@ -6,9 +6,6 @@ import (
|
|||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
// #include <stdlib.h>
|
|
||||||
import "C"
|
|
||||||
|
|
||||||
// main is required for TinyGo to compile to Wasm.
|
// main is required for TinyGo to compile to Wasm.
|
||||||
func main() {}
|
func main() {}
|
||||||
|
|
||||||
@@ -73,17 +70,10 @@ func ptrToString(ptr uint32, size uint32) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// stringToPtr returns a pointer and size pair for the given string in a way
|
// stringToPtr returns a pointer and size pair for the given string in a way
|
||||||
// compatible with WebAssembly numeric types. The pointer is not automatically
|
// compatible with WebAssembly numeric types.
|
||||||
// managed by tinygo but must be freed by the host.
|
|
||||||
func stringToPtr(s string) (uint32, uint32) {
|
func stringToPtr(s string) (uint32, uint32) {
|
||||||
if len(s) == 0 {
|
buf := []byte(s)
|
||||||
return 0, 0
|
ptr := &buf[0]
|
||||||
}
|
unsafePtr := uintptr(unsafe.Pointer(ptr))
|
||||||
|
return uint32(unsafePtr), uint32(len(buf))
|
||||||
size := C.ulong(len(s))
|
|
||||||
ptr := unsafe.Pointer(C.malloc(size))
|
|
||||||
|
|
||||||
copy(unsafe.Slice((*byte)(ptr), size), []byte(s))
|
|
||||||
|
|
||||||
return uint32(uintptr(ptr)), uint32(len(s))
|
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
examples/allocation/tinygo/testdata/greet.wasm
vendored
BIN
examples/allocation/tinygo/testdata/greet.wasm
vendored
Binary file not shown.
Reference in New Issue
Block a user