#![cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
use super::PlatformImpl;
use nstd_core::def::NSTDAny;
use std::alloc::Layout;
pub struct PlatformAlloc;
impl PlatformImpl for PlatformAlloc {
#[inline]
unsafe fn allocate(size: usize) -> NSTDAny {
match Layout::array::<u8>(size) {
Ok(layout) => std::alloc::alloc(layout).cast(),
_ => std::ptr::null_mut(),
}
}
#[inline]
unsafe fn allocate_zeroed(size: usize) -> NSTDAny {
match Layout::array::<u8>(size) {
Ok(layout) => std::alloc::alloc_zeroed(layout).cast(),
_ => std::ptr::null_mut(),
}
}
unsafe fn reallocate(ptr: *mut NSTDAny, size: usize, new_size: usize) -> i32 {
let new_mem = match Layout::array::<u8>(size) {
Ok(layout) => std::alloc::realloc((*ptr).cast(), layout, new_size),
_ => return 1,
};
match new_mem.is_null() {
false => {
*ptr = new_mem.cast();
0
}
true => 1,
}
}
unsafe fn deallocate(ptr: *mut NSTDAny, size: usize) -> i32 {
match Layout::array::<u8>(size) {
Ok(layout) => {
std::alloc::dealloc((*ptr).cast(), layout);
*ptr = std::ptr::null_mut();
0
}
_ => 1,
}
}
}