use core::ptr::NonNull;
use crate::mem::MemoryType;
use crate::table::BootServices;
#[global_allocator]
static ALLOCATOR: Allocator = Allocator;
static mut BOOT_SERVICES: Option<NonNull<BootServices>> = None;
pub(crate) fn init(bs: &mut BootServices) {
unsafe {
BOOT_SERVICES = NonNull::new(bs as *mut _);
}
}
unsafe fn boot_services() -> NonNull<BootServices> {
BOOT_SERVICES.expect("boot services not available")
}
pub struct Allocator;
unsafe impl core::alloc::GlobalAlloc for Allocator {
unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 {
boot_services()
.as_ref()
.allocate_pool(MemoryType::LOADER_DATA, layout.size())
.expect("failed to allocate memory")
}
unsafe fn dealloc(&self, ptr: *mut u8, _layout: core::alloc::Layout) {
boot_services().as_ref().free_pool(ptr).expect("failed to free memory");
}
}