Conversation

phip1611

Implement the allocator API (of core) for the UEFI allocator.

Checklist

  • Sensible git history (for example, squash "typo" or "fix" commits). See the Rewriting History guide for help.
  • Update the changelog (if necessary)

@phip1611phip1611 marked this pull request as draft April 14, 2025 20:11
@phip1611phip1611 reopened this Apr 14, 2025
@phip1611phip1611 changed the title uefi allocator: implement core allocator api (Global) UEFI Allocator: Implement cores allocator_api Apr 14, 2025
@phip1611phip1611 changed the title (Global) UEFI Allocator: Implement cores allocator_api UEFI Allocator: Implement cores allocator_api Apr 14, 2025
@phip1611phip1611 mentioned this pull request Apr 10, 2025
15 tasks
@phip1611phip1611 marked this pull request as ready for review April 19, 2025 09:52
No need for this so far, but this provides downstream users more
flexibility.
#[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) };

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

@@ -53,6 +51,44 @@ mod bootservices {
}
unsafe { boot::free_pool(ptr) }.unwrap();
}

/// Tests getting the memory map.
pub fn test_memory_map() {
Copy link
Member

Choose a reason for hiding this comment

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

Moving this function seems unrelated to the rest of the PR -- could you drop this change or move it to a separate PR?

unsafe impl core::alloc::Allocator for Allocator {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, core::alloc::AllocError> {
// Stable alternative for Layout::dangling()
fn dangling_for_layout(layout: Layout) -> NonNull<u8> {
Copy link
Member

Choose a reason for hiding this comment

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

Since this is gated by unstable anyway, I think we might as well enable alloc_layout_extra and use Layout::dangling directly.

@phip1611phip1611 mentioned this pull request May 5, 2025
10 tasks
@phip1611

Closing for the same reason as discussed here: #1606 (comment)

Sign up for free to join this conversation on . Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.