1pub struct FreeFn;
9
10impl smart_ptr::Deleter for FreeFn {
11 fn delete<T>(&mut self, ptr: *mut u8) {
12 unsafe {
13 libc::free(ptr as *mut _);
14 }
15 }
16}
17
18pub type Box = smart_ptr::Unique<libc::c_void, FreeFn>;
20
21pub trait Malloc: Sized {
23 fn malloc(size: usize) -> Self;
29}
30
31impl Malloc for Box {
32 fn malloc(size: usize) -> Self {
33 let ptr = unsafe {
34 libc::malloc(size)
35 };
36
37 if ptr.is_null() {
38 panic!("libc::malloc failed");
39 }
40
41
42 unsafe {
43 Self::new(ptr, FreeFn)
44 }
45 }
46}