-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(uefi): add post-boot-services allocator
Signed-off-by: Martin Kröning <[email protected]>
- Loading branch information
Showing
6 changed files
with
75 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use alloc::vec; | ||
use core::alloc::{GlobalAlloc, Layout}; | ||
use core::mem::MaybeUninit; | ||
use core::ptr::{self, NonNull}; | ||
|
||
use allocator_api2::alloc::Allocator; | ||
use one_shot_mutex::OneShotMutex; | ||
|
||
use crate::bump_allocator::BumpAllocator; | ||
|
||
pub enum GlobalAllocator { | ||
Uefi, | ||
Bump(BumpAllocator), | ||
} | ||
|
||
pub struct LockedAllocator(OneShotMutex<GlobalAllocator>); | ||
|
||
impl LockedAllocator { | ||
pub const fn uefi() -> Self { | ||
Self(OneShotMutex::new(GlobalAllocator::Uefi)) | ||
} | ||
} | ||
|
||
unsafe impl GlobalAlloc for LockedAllocator { | ||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
match &*self.0.lock() { | ||
GlobalAllocator::Uefi => unsafe { uefi::allocator::Allocator.alloc(layout) }, | ||
GlobalAllocator::Bump(bump) => bump | ||
.allocate(layout) | ||
// FIXME: Use NonNull::as_mut_ptr once `slice_ptr_get` is stabilized | ||
// https://github.com/rust-lang/rust/issues/74265 | ||
.map_or(ptr::null_mut(), |ptr| ptr.as_ptr().cast()), | ||
} | ||
} | ||
|
||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { | ||
match &*self.0.lock() { | ||
GlobalAllocator::Uefi => unsafe { uefi::allocator::Allocator.dealloc(ptr, layout) }, | ||
GlobalAllocator::Bump(bump) => unsafe { | ||
bump.deallocate(NonNull::new(ptr).unwrap(), layout) | ||
}, | ||
} | ||
} | ||
} | ||
|
||
#[global_allocator] | ||
static ALLOCATOR: LockedAllocator = LockedAllocator::uefi(); | ||
|
||
pub fn exit_boot_services() { | ||
assert!(matches!(*ALLOCATOR.0.lock(), GlobalAllocator::Uefi)); | ||
|
||
let mem = vec![MaybeUninit::uninit(); 4096].leak(); | ||
|
||
let bump = BumpAllocator::from(mem); | ||
|
||
*ALLOCATOR.0.lock() = GlobalAllocator::Bump(bump); | ||
|
||
uefi::allocator::exit_boot_services(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters