use alloc::allocator::{Alloc, AllocErr, Layout};
use linked_list_allocator::LockedHeap;
use arch::interrupts::disable_interrupts_and_then;
pub const HEAP_START: usize = 0o_000_001_000_000_0000;
pub const HEAP_SIZE: usize = 500 * 1024;
pub struct HeapAllocator {
inner: LockedHeap,
}
impl HeapAllocator {
pub const fn new() -> Self {
HeapAllocator {
inner: LockedHeap::empty(),
}
}
pub unsafe fn init(&self, heap_bottom: usize, heap_size: usize) {
self.inner.lock().init(heap_bottom, heap_size);
}
}
unsafe impl<'a> Alloc for &'a HeapAllocator {
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
disable_interrupts_and_then(|| -> Result<*mut u8, AllocErr> {
self.inner.lock().alloc(layout)
})
}
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
disable_interrupts_and_then(|| {
self.inner.lock().dealloc(ptr, layout);
});
}
fn oom(&mut self, _: AllocErr) -> ! {
panic!("Out of memory");
}
}