Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c3e3819

Browse files
committed
uefi: allocator: implement Allocator trait from allocator_api
1 parent 2fea033 commit c3e3819

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

‎uefi/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
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日)

‎uefi/src/allocator.rs

Lines changed: 17 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /