1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use core::alloc::{GlobalAlloc, Layout};
struct Alloc;
unsafe impl GlobalAlloc for Alloc {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
libc::memalign(
layout.align().max(core::mem::size_of::<usize>()),
layout.size(),
) as *mut _
}
#[inline]
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
libc::free(ptr as *mut _);
}
}
#[global_allocator]
static ALLOCATOR: Alloc = Alloc;
#[alloc_error_handler]
fn handle(layout: core::alloc::Layout) -> ! {
panic!("memory allocation failed: {:#?}", layout);
}