Skip to content

UEFI Allocator: Implement cores allocator_api #1632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion uefi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
- The `Display` impl for `CStr8` now excludes the trailing null character.
- `VariableKeys` initializes with a larger name buffer to work around firmware
bugs on some devices.
- The UEFI `allocator::Allocator` has been optimized for page-aligned
- The UEFI `allocator::Allocator` has been optimized for page-aligned
allocations.
- The UEFI `allocator::Allocator` now implements `core::alloc::Allocator`
(`allocator_api`), when the `unstable` feature is used.


# uefi - 0.34.1 (2025-02-07)
Expand Down
14 changes: 14 additions & 0 deletions uefi/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,17 @@ unsafe impl GlobalAlloc for Allocator {
}
}
}

#[cfg(feature = "unstable")]
unsafe impl core::alloc::Allocator for Allocator {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, core::alloc::AllocError> {
let ptr = unsafe { <Allocator as GlobalAlloc>::alloc(self, layout) };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to do something here to handle ZSTs? I'm not familiar with the details of the Allocator trait, but I noticed this in the documentation:

In contrast to GlobalAlloc, Allocator allows zero-sized allocations. If an underlying allocator does not support this (like jemalloc) or responds by returning a null pointer (such as libc::malloc), this must be caught by the implementation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm just not sure how to handle it :D but I can figure it out, I guess

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use case but you're right. I added it in a dedicated commit

NonNull::new(ptr)
.ok_or(core::alloc::AllocError)
.map(|ptr| NonNull::slice_from_raw_parts(ptr, layout.size()))
}

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
unsafe { <Allocator as GlobalAlloc>::dealloc(self, ptr.as_ptr(), layout) }
}
}
2 changes: 1 addition & 1 deletion uefi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
//! [uefi-std-tr-issue]: https://github.com/rust-lang/rust/issues/100499
//! [unstable features]: https://doc.rust-lang.org/unstable-book/

#![cfg_attr(all(feature = "unstable", feature = "alloc"), feature(allocator_api))]
#![cfg_attr(feature = "unstable", feature(allocator_api))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![no_std]
#![deny(
Expand Down