use core::alloc::{GlobalAlloc, Layout}; pub struct Allocator; #[cfg(all(unix, not(target_os = "macos")))] unsafe impl GlobalAlloc for Allocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { unsafe { libc::memalign(layout.align(), layout.size()) as *mut u8 } } unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { unsafe { libc::free(ptr as *mut libc::c_void) } } unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, new_size: usize) -> *mut u8 { unsafe { libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8 } } } #[cfg(target_os = "macos")] unsafe impl GlobalAlloc for Allocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { let mut ptr: *mut libc::c_void = core::ptr::null_mut(); let align = layout.align().max(8); let size = layout.size(); let result = unsafe { libc::posix_memalign(&mut ptr, align, size) }; if result != 0 { return core::ptr::null_mut(); } ptr as *mut u8 } unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { unsafe { libc::free(ptr as *mut libc::c_void) } } unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, new_size: usize) -> *mut u8 { unsafe { libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8 } } } #[cfg(windows)] unsafe impl GlobalAlloc for Allocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { use windows_sys::Win32::System::Memory::*; unsafe { HeapAlloc(GetProcessHeap(), 0, layout.size()) as *mut u8 } } unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { use windows_sys::Win32::System::Memory::*; unsafe { HeapFree(GetProcessHeap(), 0, ptr as *mut core::ffi::c_void) }; } unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, new_size: usize) -> *mut u8 { use windows_sys::Win32::System::Memory::*; unsafe { HeapReAlloc(GetProcessHeap(), 0, ptr as *mut core::ffi::c_void, new_size) as *mut u8 } } }