#![allow(non_camel_case_types)]
use std::alloc::{GlobalAlloc, Layout, handle_alloc_error};
use self::c_ffi::{arch_type, c_void};
mod c_ffi {
#[cfg(target_pointer_width = "64")]
pub type arch_type = u64;
#[cfg(target_pointer_width = "32")]
pub type arch_type = u32;
#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
compile_error!("Unsupported target pointer width. Only 32-bit and 64-bit architectures are supported.");
pub type c_void = std::ffi::c_void;
}
#[link(name = "allocator", kind = "static")]
extern "C" {
fn c_global_allocator(bytes: arch_type) -> *mut c_void;
fn c_global_deallocator(ptr: *mut u8) -> c_void;
}
pub(self) struct C_GLOBAL_ALLOCATOR;
unsafe impl GlobalAlloc for C_GLOBAL_ALLOCATOR {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let ptr: *mut u8 = c_global_allocator(layout.size() as arch_type) as *mut u8;
if ptr.is_null() {
handle_alloc_error(layout);
}
ptr
}
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
if !ptr.is_null() {
c_global_deallocator(ptr);
}
}
}
#[global_allocator]
pub(self) static GLOBAL: C_GLOBAL_ALLOCATOR = C_GLOBAL_ALLOCATOR;