Skip to content

Commit 81b2d33

Browse files
committed
uefi: allocator: implement Allocator trait from allocator_api
No need for this so far, but this provides downstream users more flexibility.
1 parent e3d5afb commit 81b2d33

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

uefi/CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@
4141
- The `Display` impl for `CStr8` now excludes the trailing null character.
4242
- `VariableKeys` initializes with a larger name buffer to work around firmware
4343
bugs on some devices.
44-
- The UEFI `allocator::Allocator` has been optimized for page-aligned
44+
- The UEFI `allocator::Allocator` has been optimized for page-aligned
4545
allocations.
46+
- The UEFI `allocator::Allocator` now implements `core::alloc::Allocator`
47+
(`allocator_api`), when the `unstable` feature is used.
4648

4749

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

uefi/src/allocator.rs

+14
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,17 @@ unsafe impl GlobalAlloc for Allocator {
169169
}
170170
}
171171
}
172+
173+
#[cfg(feature = "unstable")]
174+
unsafe impl core::alloc::Allocator for Allocator {
175+
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, alloc_api::AllocError> {
176+
let ptr = unsafe { <Allocator as GlobalAlloc>::alloc(self, layout) };
177+
NonNull::new(ptr)
178+
.ok_or(alloc_api::AllocError)
179+
.map(|ptr| NonNull::slice_from_raw_parts(ptr, layout.size()))
180+
}
181+
182+
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
183+
unsafe { <Allocator as GlobalAlloc>::dealloc(self, ptr.as_ptr(), layout) }
184+
}
185+
}

uefi/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@
212212
//! [uefi-std-tr-issue]: https://github.com/rust-lang/rust/issues/100499
213213
//! [unstable features]: https://doc.rust-lang.org/unstable-book/
214214
215-
#![cfg_attr(all(feature = "unstable", feature = "alloc"), feature(allocator_api))]
215+
#![cfg_attr(feature = "unstable", feature(allocator_api))]
216216
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
217217
#![no_std]
218218
#![deny(

0 commit comments

Comments
 (0)