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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#![no_std]
use core::alloc::{GlobalAlloc, Layout};
use core::ffi::c_void;
use core::ptr;
mod libc;
pub struct LibcAlloc;
unsafe impl GlobalAlloc for LibcAlloc {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let mut ptr = ptr::null_mut();
let ret = libc::posix_memalign(
&mut ptr,
layout.align().max(core::mem::size_of::<usize>()),
layout.size(),
);
if ret == 0 {
ptr as *mut u8
} else {
ptr::null_mut()
}
}
#[inline]
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
let ptr = self.alloc(layout);
if !ptr.is_null() {
ptr::write_bytes(ptr, 0, layout.size());
}
ptr
}
#[inline]
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
libc::free(ptr as *mut c_void);
}
#[inline]
unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, new_size: usize) -> *mut u8 {
libc::realloc(ptr as *mut c_void, new_size) as *mut u8
}
}