realloc
Semantically equivalent to realloc(3) or sqlite3_realloc(), this routine reallocates memory allocated via this routine or alloc(). Its first argument is either 0 or a pointer returned by this routine or alloc(). Its second argument is the number of bytes to (re)allocate, or 0 to free the memory specified in the first argument. On allocation error, realloc() throws a WasmAllocError, whereas realloc.impl() will return 0 on allocation error.
Be aware that reassigning the return value of realloc.impl() is poor practice and can lead to leaks of heap memory, as in this contrived example:
let m = wasm.realloc.impl(0, 10); // allocate 10 bytes
m = wasm.realloc.impl(m, 20); // grow m to 20 bytesContent copied to clipboard
If that reallocation fails, it will return 0, overwriting m and effectively leaking the first allocation. Always use an intermediary value for such cases:
let m2 = wasm.realloc.impl(m, 20);
if( m2 ) m = m2;
else { ... error ... }Content copied to clipboard