1#![no_std]
13
14use core::{
15 alloc::{GlobalAlloc, Layout},
16 ffi::c_void,
17};
18
19pub use hardened_malloc_sys::{malloc, calloc, realloc, aligned_alloc, free};
21
22pub use hardened_malloc_sys::posix_memalign;
24
25pub use hardened_malloc_sys::{malloc_object_size, malloc_object_size_fast, free_sized};
27
28pub struct HardenedMalloc;
29
30unsafe impl GlobalAlloc for HardenedMalloc {
31 #[inline]
32 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
33 malloc(layout.size()) as *mut u8
34 }
35
36 #[inline]
37 unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
38 calloc(layout.size(), 1) as *mut u8
39 }
40
41 #[inline]
42 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
43 free_sized(ptr as *mut c_void, layout.size());
46 }
47
48 #[inline]
49 unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, size: usize) -> *mut u8 {
50 realloc(ptr as *mut c_void, size) as *mut u8
51 }
52}