aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / mem / mod.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! Types, functions, traits, and other helpers to work with memory in UEFI |
| 4 | //! libraries and applications. |
| 5 | |
| 6 | use crate::boot; |
| 7 | use core::ptr::NonNull; |
| 8 | |
| 9 | pub mod memory_map; |
| 10 | |
| 11 | #[cfg(feature = "alloc")] |
| 12 | pub(crate) mod util; |
| 13 | |
| 14 | #[cfg(feature = "alloc")] |
| 15 | pub(crate) use util::*; |
| 16 | |
| 17 | #[cfg(feature = "alloc")] |
| 18 | mod aligned_buffer; |
| 19 | #[cfg(feature = "alloc")] |
| 20 | pub use aligned_buffer::{AlignedBuffer, AlignmentError}; |
| 21 | |
| 22 | /// Wrapper for memory allocated with UEFI's pool allocator. The memory is freed |
| 23 | /// on drop. |
| 24 | #[derive(Debug)] |
| 25 | pub(crate) struct PoolAllocation(NonNull<u8>); |
| 26 | |
| 27 | impl PoolAllocation { |
| 28 | pub(crate) const fn new(ptr: NonNull<u8>) -> Self { |
| 29 | Self(ptr) |
| 30 | } |
| 31 | |
| 32 | pub(crate) const fn as_ptr(&self) -> NonNull<u8> { |
| 33 | self.0 |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl Drop for PoolAllocation { |
| 38 | fn drop(&mut self) { |
| 39 | let active = boot::are_boot_services_active(); |
| 40 | debug_assert!( |
| 41 | active, |
| 42 | "pool allocation dropped after exiting boot services" |
| 43 | ); |
| 44 | if !active { |
| 45 | return; |
| 46 | } |
| 47 | // Ignore errors returned by `free_pool` since we can't propagate them |
| 48 | // from `drop`. |
| 49 | // SAFETY: This pointer was allocated by the matching UEFI allocator. |
| 50 | let _ = unsafe { boot::free_pool(self.0) }; |
| 51 | } |
| 52 | } |