rkgk/crates/haku2/src/lib.rs

38 lines
924 B
Rust
Raw Normal View History

use std::{
alloc::{self, Layout},
ptr,
};
#[unsafe(no_mangle)]
unsafe extern "C" fn __haku2_alloc(size: usize, align: usize) -> *mut u8 {
if let Ok(layout) = Layout::from_size_align(size, align) {
alloc::alloc(layout)
} else {
ptr::null_mut()
}
}
#[unsafe(no_mangle)]
unsafe extern "C" fn __haku2_realloc(
ptr: *mut u8,
size: usize,
align: usize,
new_size: usize,
) -> *mut u8 {
if let Ok(layout) = Layout::from_size_align(size, align) {
alloc::realloc(ptr, layout, new_size)
} else {
ptr::null_mut()
}
}
#[unsafe(no_mangle)]
unsafe extern "C" fn __haku2_dealloc(ptr: *mut u8, size: usize, align: usize) {
match Layout::from_size_align(size, align) {
Ok(layout) => alloc::dealloc(ptr, layout),
Err(_) => {
log::error!("__haku2_dealloc: invalid layout size={size} align={align} ptr={ptr:?}")
}
}
}