use core::alloc::{GlobalAlloc, Layout};
pub use slab_allocator_rs::LockedHeap;
use spin::Lazy;
pub struct LazyHeap(Lazy<LockedHeap>);
impl LazyHeap {
pub const fn new(init: fn() -> LockedHeap) -> Self {
Self(Lazy::new(init))
}
pub const fn empty() -> Self {
Self(Lazy::new(|| LockedHeap::empty()))
}
pub fn init(&self, begin: usize, len: usize) {
unsafe {
self.0.init(begin, len);
}
}
}
unsafe impl GlobalAlloc for LazyHeap {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
unsafe { self.0.alloc(layout) }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe { self.0.dealloc(ptr, layout) }
}
}