gpt4 book ai didi

go - Go(WebAssembly)中的malloc()和free()

转载 作者:数据小太阳 更新时间:2023-10-29 03:36:16 26 4
gpt4 key购买 nike

我想提供一个与javascript共享的内存。在c和rust中有malloc()和free()(rust wbindgen提供了它)。我在围棋里找不到类似的东西?如何保留内存、获取指针并在使用后释放内存?
一个使用示例。对图像位图(javascript)执行以下操作:

const context = canvas.getContext("2d");
const size = canvas.width * canvas.height * 4;
// allocate memory in Go
const ptr = window.wasm.go.malloc(size);
const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
const dataGo = new Uint8ClampedArray(window.wasm.goMem.buffer, ptr, size);
const imageDataGo = new ImageData(dataGo, canvas.width, canvas.height);
imageDataGo.data.set(imageData.data);
// run function in Go using the pointer to the memory
window.wasm.go.convolve_mem(ptr, canvas.width, canvas.height);
context.putImageData(imageDataGo, 0, 0);
// free the memory
window.wasm.go.free(size);

我试过了:
exports["malloc"] = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
size := args[0].Int()
mem := make([]byte, size)
ptr := uintptr(unsafe.Pointer(&mem))
return uint64(ptr)
})

make应使用0初始化字节数组的所有元素。但在js中,我得到一个随机数组。因此,我认为这不是预期的工作。我还假设垃圾收集器在函数返回后立即释放内存。
如何在go中保留内存,就像在c中一样?
Go版本GO1.12.1 Linux/AMD64

最佳答案

我在go中找不到解决方案,但我可以使用其他wasm模块和相同的wasm内存。在C语言中,我可以使用malloc,并且有一个WASM模块。请参见代码注释。

const go = new window.Go();

// use the same WASM memory for all Wasm instances
const memory = new WebAssembly.Memory({initial: 1024});

Promise.all([
// The main WASM module with my photo filter
WebAssembly.instantiateStreaming(fetch('some-go-wasm-module.wasm'), {
env: {memory},
...go.importObject
}),
// the memory library written in C provides: abort, calloc, free, malloc, memcoy, memset, sbrk
// source: https://github.com/guybedford/wasm-stdlib-hack/blob/master/dist/memory.wasm
WebAssembly.instantiateStreaming(fetch("memory.wasm"), {
env: {memory}
})
])
.then(module => {
go.run(module[0].instance);
window.wasm.memHelper = {
memory,
...module[1].instance.exports
};
});

现在我可以分配和释放内存,并使用指针在go函数中访问它。此示例来自另一个问题-以显示其工作原理:
const context = canvas.getContext("2d");
const size = canvas.width * canvas.height * 4;

// allocate memory for the image bitmap
const ptr = window.wasm.memHelper.malloc(size);

const imageData = context.getImageData(0, 0, canvas.width, canvas.height);

// create a new ImageData object from this memory
const dataGo = new Uint8ClampedArray(window.wasm.memHelper.memory.buffer, ptr, size);
const imageDataGo = new ImageData(dataGo, canvas.width, canvas.height);

// copy the image from JS context to the WASM context
imageDataGo.data.set(imageData.data);

// run my Go filter
window.wasm.go.convolve_mem(ptr, canvas.width, canvas.height);

// copy the image bitmap from Wasm context back to the canvas
context.putImageData(imageDataGo, 0, 0);

// free memory
window.wasm.memHelper.free(ptr);

关于go - Go(WebAssembly)中的malloc()和free(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56270342/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com