#![cfg(target_os = "macos")]
use super::PlatformImpl;
use core_foundation::base::{CFAllocatorAllocate, CFAllocatorDeallocate, CFAllocatorReallocate};
use nstd_core::def::NSTDAny;
pub struct PlatformAlloc;
impl PlatformImpl for PlatformAlloc {
#[inline]
unsafe fn allocate(size: usize) -> NSTDAny {
CFAllocatorAllocate(std::ptr::null_mut(), size as isize, 0)
}
unsafe fn allocate_zeroed(size: usize) -> NSTDAny {
let ptr = Self::allocate(size);
if !ptr.is_null() {
let ptr = ptr as *mut u8;
for i in 0..size {
*ptr.add(i) = 0;
}
}
ptr
}
unsafe fn reallocate(ptr: *mut NSTDAny, _: usize, new_size: usize) -> i32 {
let new_mem = CFAllocatorReallocate(std::ptr::null_mut(), *ptr, new_size as isize, 0);
match new_mem.is_null() {
false => {
*ptr = new_mem;
0
}
true => 1,
}
}
#[inline]
unsafe fn deallocate(ptr: *mut NSTDAny, _: usize) -> i32 {
CFAllocatorDeallocate(std::ptr::null_mut(), *ptr);
*ptr = std::ptr::null_mut();
0
}
}