Skip to content

Commit b8edf4c

Browse files
committed
uefi: allocator: implement Allocator trait from allocator_api
We have not been requested yet to do this, but this gives downstream users more flexibility.
1 parent c0728be commit b8edf4c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Diff for: uefi/CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
- The `Display` impl for `CStr8` now excludes the trailing null character.
2929
- `VariableKeys` initializes with a larger name buffer to work around firmware
3030
bugs on some devices.
31-
- The UEFI `allocator::Allocator` has been optimized for page-aligned
31+
- The UEFI `allocator::Allocator` has been optimized for page-aligned
3232
allocations.
33+
- The UEFI `allocator::Allocator` now implements `core::alloc::Allocator`
34+
(`allocator_api`), when the `--unstable` feature is used.
3335

3436

3537
# uefi - 0.34.1 (2025-02-07)

Diff for: uefi/src/allocator.rs

+17
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ use core::ptr::{self, NonNull};
2020
use core::sync::atomic::{AtomicU32, Ordering};
2121
use uefi_raw::table::boot::PAGE_SIZE;
2222

23+
#[cfg(feature = "unstable")]
24+
use core::alloc as alloc_api;
25+
2326
/// Get the memory type to use for allocation.
2427
///
2528
/// The first time this is called, the data type of the loaded image will be
@@ -160,3 +163,17 @@ unsafe impl GlobalAlloc for Allocator {
160163
}
161164
}
162165
}
166+
167+
#[cfg(feature = "unstable")]
168+
unsafe impl alloc_api::Allocator for Allocator {
169+
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, alloc_api::AllocError> {
170+
let ptr = unsafe { <Allocator as GlobalAlloc>::alloc(self, layout) };
171+
NonNull::new(ptr)
172+
.ok_or(alloc_api::AllocError)
173+
.map(|ptr| NonNull::slice_from_raw_parts(ptr, layout.size()))
174+
}
175+
176+
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
177+
unsafe { <Allocator as GlobalAlloc>::dealloc(self, ptr.as_ptr(), layout) }
178+
}
179+
}

0 commit comments

Comments
 (0)