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

时间:2019-05-23 07:43:33

标签: go webassembly

我想提供一个与Java共享的内存。在C和Rust中有malloc()和free()(因为Rust的wbindgen提供了它)。我在Go中找不到类似的内容?我该如何保留内存,获取它的指针并在使用后释放它?

一个用法示例。使用图像位图(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

1 个答案:

答案 0 :(得分:0)

我没有在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);