feat: add malloc&free 1
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
Reference:
|
Reference:
|
||||||
* https://radu-matei.com/blog/practical-guide-to-wasm-memory/
|
* https://radu-matei.com/blog/practical-guide-to-wasm-memory/
|
||||||
|
* https://github.com/alec-deason/wasm_plugin/blob/main/guest/src/lib.rs
|
||||||
|
|||||||
@@ -1,5 +1,16 @@
|
|||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe fn malloc_1(len: usize) -> *mut u8 {
|
pub unsafe fn malloc_1(len: usize) -> *mut u8 {
|
||||||
|
let mut buffer: std::mem::ManuallyDrop<Vec<u8>> = std::mem::ManuallyDrop::new(Vec::with_capacity(len as usize));
|
||||||
|
buffer.as_mut_ptr() as *mut u8
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe fn free_1(ptr: *mut u8, len: usize) {
|
||||||
|
std::mem::drop(Vec::from_raw_parts(ptr as *mut u8, 0, len as usize))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe fn malloc_2(len: usize) -> *mut u8 {
|
||||||
let mut buf = Vec::with_capacity(len);
|
let mut buf = Vec::with_capacity(len);
|
||||||
let ptr = buf.as_mut_ptr();
|
let ptr = buf.as_mut_ptr();
|
||||||
std::mem::forget(buf);
|
std::mem::forget(buf);
|
||||||
@@ -7,20 +18,20 @@ pub unsafe fn malloc_1(len: usize) -> *mut u8 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe fn free_1(ptr: *mut u8, len: usize) {
|
pub unsafe fn free_2(ptr: *mut u8, len: usize) {
|
||||||
let data = Vec::from_raw_parts(ptr, len, len);
|
let data = Vec::from_raw_parts(ptr, len, len);
|
||||||
std::mem::drop(data);
|
std::mem::drop(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe fn malloc_2(len: usize) -> *mut u8 {
|
pub unsafe fn malloc_3(len: usize) -> *mut u8 {
|
||||||
let align = std::mem::align_of::<usize>();
|
let align = std::mem::align_of::<usize>();
|
||||||
let layout = std::alloc::Layout::from_size_align_unchecked(len, align);
|
let layout = std::alloc::Layout::from_size_align_unchecked(len, align);
|
||||||
std::alloc::alloc(layout)
|
std::alloc::alloc(layout)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe fn free_2(ptr: *mut u8, len: usize) {
|
pub unsafe fn free_3(ptr: *mut u8, len: usize) {
|
||||||
let align = std::mem::align_of::<usize>();
|
let align = std::mem::align_of::<usize>();
|
||||||
let layout = std::alloc::Layout::from_size_align_unchecked(len, align);
|
let layout = std::alloc::Layout::from_size_align_unchecked(len, align);
|
||||||
std::alloc::dealloc(ptr, layout)
|
std::alloc::dealloc(ptr, layout)
|
||||||
@@ -28,14 +39,16 @@ pub unsafe fn free_2(ptr: *mut u8, len: usize) {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
unsafe {
|
unsafe {
|
||||||
println!("Malloc-1");
|
println!("Malloc & Free - 1");
|
||||||
let p = malloc_1(1024);
|
let p = malloc_1(1024);
|
||||||
println!("Free-1");
|
|
||||||
free_1(p, 1024);
|
free_1(p, 1024);
|
||||||
|
|
||||||
println!("Malloc-2");
|
println!("Malloc & Free - 2");
|
||||||
let p = malloc_2(1024);
|
let p = malloc_2(1024);
|
||||||
println!("Free-2");
|
|
||||||
free_2(p, 1024);
|
free_2(p, 1024);
|
||||||
|
|
||||||
|
println!("Malloc & Free - 3");
|
||||||
|
let p = malloc_3(1024);
|
||||||
|
free_3(p, 1024);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user