#![no_std]
use core::{
alloc::{GlobalAlloc, Layout},
ffi::c_void,
};
pub use hardened_malloc_sys::{malloc, calloc, realloc, aligned_alloc, free};
pub use hardened_malloc_sys::posix_memalign;
pub use hardened_malloc_sys::{malloc_object_size, malloc_object_size_fast, free_sized};
pub struct HardenedMalloc;
unsafe impl GlobalAlloc for HardenedMalloc {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
malloc(layout.size()) as *mut u8
}
#[inline]
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
calloc(layout.size(), 1) as *mut u8
}
#[inline]
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
free_sized(ptr as *mut c_void, layout.size());
}
#[inline]
unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, size: usize) -> *mut u8 {
realloc(ptr as *mut c_void, size) as *mut u8
}
}