I am learning WebAssembly
now and I find that the external function (libc or third party function) can't be accessed by Wasm world easily (using import is a general solution).
I'm trying to use emcc
to compile my source code into wasm but malloc
and free
are widely used in my source code. I don't think that importing malloc
and free
from real world is practicable.
So, I'm pretty curious about how to implement malloc
in wasm world. I have already known how the malloc works in glibc
:using brk() or sbrk() to extend heap and some memory management for the liner address. But in wasm world I think it impossible to call brk()
or sbrk()
to get the liner address.
Is it reasonable to use global var to implement malloc
like this ?
u_char mem[10240];
void *wasm_malloc(size_t num)
{
/*get the free mem idx*/
return &mem[idx];
}