fix macOS build
This commit is contained in:
parent
359657e174
commit
0c0aa792c1
5 changed files with 107 additions and 9 deletions
|
|
@ -2,7 +2,7 @@ use core::alloc::{GlobalAlloc, Layout};
|
|||
|
||||
pub struct Allocator;
|
||||
|
||||
#[cfg(unix)]
|
||||
#[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 }
|
||||
|
|
@ -17,6 +17,28 @@ unsafe impl GlobalAlloc for Allocator {
|
|||
}
|
||||
}
|
||||
|
||||
#[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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue