interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / uefi / src / mem / mod.rs

52 lines · 1312 bytes Raw Blame History
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
6use crate::boot;
7use core::ptr::NonNull;
8
9pub mod memory_map;
10
11#[cfg(feature = "alloc")]
12pub(crate) mod util;
13
14#[cfg(feature = "alloc")]
15pub(crate) use util::*;
16
17#[cfg(feature = "alloc")]
18mod aligned_buffer;
19#[cfg(feature = "alloc")]
20pub 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)]
25pub(crate) struct PoolAllocation(NonNull<u8>);
26
27impl 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
37impl 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}