Uh oh!
There was an error while loading. Please reload this page.
std: rework uefi allocators - #22818
Conversation
There was a problem hiding this comment.
Thank you, this is the right size for a PR like this :)
Two high level concerns:
- All allocators, including platform-specific ones like
WasmAllocatorcurrently live instd.heap. If we're moving these around that should be where. - Why are we adding additional abstractions on top of the UEFI-provided allocator functions? In general that's a good thing if it improves ergonomics of those APIs from a Zig perspective, but in this case no one but the allocator implementation should touch those, and I feel in that case the extra layer is not justified.
RossComputerGuy
commented
Feb 9, 2025
Will do
I'm not 100% sure, this originally came from @truemedian. |
e0ab1e2 to
da5fad6Comparetruemedian
commented
Feb 10, 2025
Which additional abstractions are you referring to?
|
7efc07c to
a9862dbComparelinusg
commented
Feb 10, 2025
The ones in |
truemedian
commented
Feb 10, 2025
Ah, thats because this came out of a branch where the goal was to make |
| /// Allocates memory in pages. | ||
| /// | ||
| /// This allocator is backed by `allocatePages` and is therefore only suitable for usage when Boot Services are available. | ||
| pub const PageAllocator = struct { |
There was a problem hiding this comment.
The namespacing here is awkward, this ought to be a file-as-struct (UefiPageAllocator.zig).
There was a problem hiding this comment.
Why file as a struct when there's multiple allocators?
There was a problem hiding this comment.
Because std.heap.UefiPageAllocator is much nicer than std.heap.uefi_allocators.PageAllocator (which violates https://ziglang.org/documentation/master/#Avoid-Redundant-Names-in-Fully-Qualified-Namespaces). Why do you want both allocators in the same file?
There was a problem hiding this comment.
Because
std.heap.UefiPageAllocatoris much nicer thanstd.heap.uefi_allocators.PageAllocator
Wouldn't std.heap.uefi.Page/Pool be nicer?
Why do you want both allocators in the same file?
Just so there isn't 3 more files that could fit into 1.
There was a problem hiding this comment.
Wouldn't std.heap.uefi.Page/Pool be nicer?
Not really, a "page" and a "page allocator" are different things. Avoiding redundancy does not mean to leave out important bits of information until the name has a different meaning.
Just so there isn't 3 more files that could fit into 1.
That's not a good reason.
There was a problem hiding this comment.
Ok, I've managed to find the time and split the allocators into their own files.
RossComputerGuy
commented
Feb 20, 2025
Already been rebased.
I don't have one unless @truemedian does. I think it just makes the API easier to use and it's already there. Shouldn't need additional maintenance since UEFI's API doesn't really change in that section. I probably will just remove it. |
linusg
commented
Feb 20, 2025
I see at least one reference to |
a9862db to
33af113CompareRossComputerGuy
commented
Feb 20, 2025
Fixed, strange that one didn't get caught when I tried compiling |
linusg
commented
Feb 20, 2025
For the most part Zig follows YAGNI principles. |
RossComputerGuy
commented
Apr 2, 2025
Great, I'll rebase once that lands. Though I'm still wondering about #22818 (comment). |
33af113 to
9611057Compare| /// Deprecated; to be removed after 0.14.0 is tagged. | ||
| pub const GeneralPurposeAllocator = DebugAllocator; | ||
| pub const uefi = @import("heap/uefi_allocators.zig"); |
There was a problem hiding this comment.
i prefer to keep these in std.os.uefi, any reason to move them?
There was a problem hiding this comment.
I forget who mentioned it but someone here mentioned that this is what should be done.
There was a problem hiding this comment.
It was me, but I've changed my mind - the split into file-as-struct is nice but let's keep them in std.os.uefi for now.
| /// Allocates memory pages from the system. | ||
| allocatePages: *const fn (alloc_type: AllocateType, mem_type: MemoryType, pages: usize, memory: *[*]align(4096) u8) callconv(cc) Status, | ||
| _allocatePages: *const fn (alloc_type: AllocateType.Enum, mem_type: MemoryType, pages: usize, memory: PhysicalAddress) callconv(cc) Status, |
There was a problem hiding this comment.
oh that's a good point that i didn't address in my pr: the memory returned is a physical address, which shouldn't be used directly as a pointer
once you rebase i think it might make sense to keep that change (as opposed to *[*]uefi.Page), with the caveat that it should be memory: *PhysicalAddress and to return that PhysicalAddress from allocatePages. idk though, that would imply changing eg convertPointer which could get hairy
alternatively it would be nice if there was std.builtin.AddressSpace.physical but that could come with a cascade of changes that aren't necessarily relevant here.
9611057 to
d5966c8CompareRossComputerGuy
commented
Jul 18, 2025
Rebased, I haven't adjusted much but hopefully I resolved the conflicts well. |
| pages: usize, | ||
| ) AllocatePagesError![]align(4096) Page { | ||
| var ptr: [*]align(4096) Page = switch (location) { | ||
| var ptr: PhysicalAddress = switch (location) { |
There was a problem hiding this comment.
you also have to fix the return type of the function and the .success arm of the switch below
Split off from #22226 as suggested by @linusg. This reworks the allocators to be more friendly.