842 B
842 B
Reference:
- https://radu-matei.com/blog/practical-guide-to-wasm-memory/ practical-guide-to-wasm-memory.pdf
- https://github.com/alec-deason/wasm_plugin/blob/main/guest/src/lib.rs
- https://github.com/reem/rust-memalloc/blob/master/src/lib.rs
rust-memalloc implements reallocate:
pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, new_size: usize) -> *mut u8 {
if old_size > new_size {
let mut buf = Vec::from_raw_parts(ptr, new_size, old_size);
buf.shrink_to_fit();
ptr_from_vec(buf)
} else if new_size > old_size {
let additional = new_size - old_size;
let mut buf = Vec::from_raw_parts(ptr, 0, old_size);
buf.reserve_exact(additional);
ptr_from_vec(buf)
} else {
ptr
}
}