1#![no_std]
13
14use core::{
15 alloc::{GlobalAlloc, Layout},
16 ffi::c_void,
17};
18
19use hardened_malloc_sys::{calloc, free_sized, malloc, realloc};
20
21pub struct HardenedMalloc;
22
23unsafe impl GlobalAlloc for HardenedMalloc {
24 #[inline]
25 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
26 malloc(layout.size()) as *mut u8
27 }
28
29 #[inline]
30 unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
31 calloc(layout.size(), 1) as *mut u8
32 }
33
34 #[inline]
35 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
36 free_sized(ptr as *mut c_void, layout.size());
39 }
40
41 #[inline]
42 unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, size: usize) -> *mut u8 {
43 realloc(ptr as *mut c_void, size) as *mut u8
44 }
45}