use crate::{AllocateType, MemoryDescriptor, MemoryType, Status, SystemTable};
use alloc::vec::Vec;
use core::ops::{Deref, DerefMut};
pub const PAGE_SIZE: usize = 4096; pub fn page_count(bytes: usize) -> usize {
(bytes / PAGE_SIZE) + if bytes % PAGE_SIZE == 0 { 0 } else { 1 }
}
pub fn allocate_pages(
at: AllocateType,
mt: MemoryType,
pages: usize,
addr: u64,
) -> Result<Pages, Status> {
SystemTable::current()
.boot_services()
.allocate_pages(at, mt, pages, addr)
}
pub fn get_memory_map() -> Result<(Vec<MemoryDescriptor>, usize), Status> {
SystemTable::current().boot_services().get_memory_map()
}
pub struct Pages {
ptr: *mut u8,
len: usize, }
impl Pages {
pub unsafe fn new(ptr: *mut u8, len: usize) -> Self {
Self { ptr, len }
}
pub fn addr(&self) -> usize {
self.ptr as _
}
}
impl Drop for Pages {
fn drop(&mut self) {
unsafe {
SystemTable::current()
.boot_services()
.free_pages(self.ptr, self.len / PAGE_SIZE)
.unwrap()
};
}
}
impl Deref for Pages {
type Target = [u8];
fn deref(&self) -> &Self::Target {
unsafe { core::slice::from_raw_parts(self.ptr, self.len) }
}
}
impl DerefMut for Pages {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { core::slice::from_raw_parts_mut(self.ptr, self.len) }
}
}