hardened_malloc_rs/
lib.rs

1#![no_std]
2
3mod bindings;
4use core::{
5	alloc::{GlobalAlloc, Layout},
6	ffi::c_void,
7};
8
9pub use bindings::{calloc, free, malloc, realloc};
10
11pub struct HardenedMalloc;
12
13unsafe impl GlobalAlloc for HardenedMalloc {
14	#[inline]
15	unsafe fn alloc(&self, layout: Layout) -> *mut u8 { malloc(layout.size()) as *mut u8 }
16
17	#[inline]
18	unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { calloc(layout.size(), 1) as *mut u8 }
19
20	#[inline]
21	unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { free(ptr as *mut c_void); }
22
23	#[inline]
24	unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, size: usize) -> *mut u8 {
25		realloc(ptr as *mut c_void, size) as *mut u8
26	}
27}