extern crate libc;
use self::libc::{_exit, EXIT_SUCCESS};
use super::util::align_diff;
use super::TOTAL_BYTES;
use core::alloc::{GlobalAlloc, Layout};
use core::cell::UnsafeCell;
use core::ptr;
static mut HEAP: [u8; TOTAL_BYTES] = [0; TOTAL_BYTES];
pub struct BumpAlloc {
memblk: UnsafeCell<*mut u8>,
offset: UnsafeCell<usize>,
}
unsafe impl Sync for BumpAlloc {}
trait ConstInit {
#[allow(clippy::declare_interior_mutable_const)]
const INIT: Self;
}
impl ConstInit for BumpAlloc {
const INIT: Self = Self {
memblk: UnsafeCell::new(ptr::null_mut()),
offset: UnsafeCell::new(0),
};
}
impl BumpAlloc {
#[allow(clippy::declare_interior_mutable_const)]
pub const INIT: Self = <Self as ConstInit>::INIT;
}
unsafe impl GlobalAlloc for BumpAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let offset = self.offset.get();
if (*self.memblk.get()).is_null() {
let ptr = libc::mmap(
0 as *mut libc::c_void,
TOTAL_BYTES as libc::size_t,
libc::PROT_READ | libc::PROT_WRITE,
libc::MAP_ANON | libc::MAP_PRIVATE,
-1,
0 as libc::off_t,
);
if ptr == libc::MAP_FAILED {
return ptr::null_mut();
}
*self.memblk.get() = ptr as *mut u8;
}
let diff = align_diff(HEAP.as_mut_ptr().add(*offset) as usize, layout.align());
let start = *offset + diff;
let end = start.saturating_add(layout.size());
if end >= TOTAL_BYTES {
_exit(EXIT_SUCCESS);
} else {
let p = HEAP[*offset..end].as_mut_ptr() as *mut u8;
*offset = end;
p
}
}
unsafe fn dealloc(&self, _: *mut u8, _: Layout) {
}
}