cuelang.org/go@v0.10.1/cue/interpreter/wasm/testdata/rust/struct/src/mem.rs (about) 1 extern crate alloc; 2 extern crate core; 3 extern crate wee_alloc; 4 5 use alloc::vec::Vec; 6 use std::mem::MaybeUninit; 7 8 #[global_allocator] 9 static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; 10 11 #[cfg_attr(all(target_arch = "wasm32"), export_name = "allocate")] 12 #[no_mangle] 13 pub extern "C" fn _allocate(size: u32) -> *mut u8 { 14 allocate(size as usize) 15 } 16 17 fn allocate(size: usize) -> *mut u8 { 18 let vec: Vec<MaybeUninit<u8>> = Vec::with_capacity(size); 19 20 Box::into_raw(vec.into_boxed_slice()) as *mut u8 21 } 22 23 #[cfg_attr(all(target_arch = "wasm32"), export_name = "deallocate")] 24 #[no_mangle] 25 pub unsafe extern "C" fn _deallocate(ptr: u32, size: u32) { 26 deallocate(ptr as *mut u8, size as usize); 27 } 28 29 unsafe fn deallocate(ptr: *mut u8, size: usize) { 30 let _ = Vec::from_raw_parts(ptr, 0, size); 31 } 32