#![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
}
}