Skip to content

Commit a9862db

Browse files
std.os.uefi: move allocators to heap
1 parent 1846482 commit a9862db

File tree

6 files changed

+25
-28
lines changed

6 files changed

+25
-28
lines changed

lib/std/debug.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ pub fn defaultPanic(
609609

610610
if (uefi.system_table.boot_services) |bs| {
611611
// ExitData buffer must be allocated using boot_services.allocatePool (spec: page 220)
612-
const exit_data: []u16 = uefi.global_pool_allocator.allocator().alloc(u16, exit_msg.len + 1) catch @trap();
612+
const exit_data: []u16 = std.heap.uefi.global_pool_allocator.allocator().alloc(u16, exit_msg.len + 1) catch @trap();
613613
@memcpy(exit_data, exit_msg[0..exit_data.len]); // Includes null terminator.
614614
_ = bs.exit(uefi.handle, .aborted, exit_data.len, exit_data.ptr);
615615
}

lib/std/heap.zig

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ pub const GeneralPurposeAllocatorConfig = DebugAllocatorConfig;
2424
/// Deprecated; to be removed after 0.14.0 is tagged.
2525
pub const GeneralPurposeAllocator = DebugAllocator;
2626

27+
pub const uefi = @import("heap/uefi_allocators.zig");
28+
2729
const memory_pool = @import("heap/memory_pool.zig");
2830
pub const MemoryPool = memory_pool.MemoryPool;
2931
pub const MemoryPoolAligned = memory_pool.MemoryPoolAligned;
@@ -355,7 +357,7 @@ else if (builtin.target.isWasm()) .{
355357
.ptr = undefined,
356358
.vtable = &SbrkAllocator(std.os.plan9.sbrk).vtable,
357359
} else if (builtin.target.os.tag == .uefi)
358-
std.os.uefi.global_page_allocator.allocator()
360+
uefi.global_page_allocator.allocator()
359361
else
360362
.{
361363
.ptr = undefined,

lib/std/os/uefi/allocator.zig renamed to lib/std/heap/uefi_allocators.zig

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const std = @import("../../std.zig");
1+
const std = @import("../std.zig");
22

33
const mem = std.mem;
44
const uefi = std.os.uefi;
@@ -7,13 +7,16 @@ const Allocator = mem.Allocator;
77

88
const assert = std.debug.assert;
99

10+
pub var global_page_allocator = PageAllocator{};
11+
pub var global_pool_allocator = PoolAllocator{};
12+
1013
/// Allocates memory in pages.
1114
///
1215
/// This allocator is backed by `allocatePages` and is therefore only suitable for usage when Boot Services are available.
13-
pub const Page = struct {
14-
memory_type: uefi.tables.MemoryType = .LoaderData,
16+
pub const PageAllocator = struct {
17+
memory_type: uefi.tables.MemoryType = .loader_data,
1518

16-
pub fn allocator(self: *Page) Allocator {
19+
pub fn allocator(self: *PageAllocator) Allocator {
1720
return Allocator{
1821
.ptr = self,
1922
.vtable = &vtable,
@@ -36,7 +39,7 @@ pub const Page = struct {
3639
_ = alignment;
3740
_ = ret_addr;
3841

39-
const self: *Page = @ptrCast(@alignCast(ctx));
42+
const self: *PageAllocator = @ptrCast(@alignCast(ctx));
4043

4144
assert(len > 0);
4245
const pages = mem.alignForward(usize, len, 4096) / 4096;
@@ -55,7 +58,7 @@ pub const Page = struct {
5558
_ = alignment;
5659
_ = ret_addr;
5760

58-
const self: *Page = @ptrCast(@alignCast(ctx));
61+
const self: *PageAllocator = @ptrCast(@alignCast(ctx));
5962

6063
// If the buffer was originally larger than the new length, we can grow or shrink it in place.
6164
const original_len = mem.alignForward(usize, buf.len, 4096);
@@ -109,10 +112,10 @@ pub const Page = struct {
109112
/// Supports the full std.mem.Allocator interface, including up to page alignment.
110113
///
111114
/// This allocator is backed by `allocatePool` and is therefore only suitable for usage when Boot Services are available.
112-
pub const Pool = struct {
113-
memory_type: uefi.tables.MemoryType = .LoaderData,
115+
pub const PoolAllocator = struct {
116+
memory_type: uefi.tables.MemoryType = .loader_data,
114117

115-
pub fn allocator(self: *Pool) Allocator {
118+
pub fn allocator(self: *PoolAllocator) Allocator {
116119
return Allocator{
117120
.ptr = self,
118121
.vtable = &vtable,
@@ -142,7 +145,7 @@ pub const Pool = struct {
142145
ret_addr: usize,
143146
) ?[*]u8 {
144147
_ = ret_addr;
145-
const self: *Pool = @ptrCast(@alignCast(ctx));
148+
const self: *PoolAllocator = @ptrCast(@alignCast(ctx));
146149

147150
assert(len > 0);
148151

@@ -218,10 +221,10 @@ pub const Pool = struct {
218221
/// Asserts all allocations are at most 8 byte aligned. This is the highest alignment UEFI will give us directly.
219222
///
220223
/// This allocator is backed by `allocatePool` and is therefore only suitable for usage when Boot Services are available.
221-
pub const RawPool = struct {
224+
pub const RawPoolAllocator = struct {
222225
memory_type: uefi.tables.MemoryType = .LoaderData,
223226

224-
pub fn allocator(self: *RawPool) Allocator {
227+
pub fn allocator(self: *RawPoolAllocator) Allocator {
225228
return Allocator{
226229
.ptr = self,
227230
.vtable = &vtable,
@@ -242,7 +245,7 @@ pub const RawPool = struct {
242245
ret_addr: usize,
243246
) ?[*]u8 {
244247
_ = ret_addr;
245-
const self: *RawPool = @ptrCast(@alignCast(ctx));
248+
const self: *RawPoolAllocator = @ptrCast(@alignCast(ctx));
246249

247250
// UEFI pool allocations are 8 byte aligned, so we can't do better than that.
248251
std.debug.assert(@intFromEnum(alignment) <= 3);

lib/std/os/uefi.zig

-8
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ pub const tables = @import("uefi/tables.zig");
1414
/// used by UEFI applications to allocate pool memory.
1515
pub var efi_pool_memory_type: tables.MemoryType = .loader_data;
1616

17-
const allocator = @import("uefi/allocator.zig");
18-
pub const PageAllocator = allocator.Page;
19-
pub const PoolAllocator = allocator.Pool;
20-
pub const RawPoolAllocator = allocator.RawPool;
21-
22-
pub var global_page_allocator = PageAllocator{};
23-
pub var global_pool_allocator = PoolAllocator{};
24-
2517
/// The EFI image's handle that is passed to its entry point.
2618
pub var handle: Handle = undefined;
2719

lib/std/os/uefi/tables.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ pub const AllocateType = union(Enum) {
103103
any: void,
104104

105105
/// Allocate any available range of pages whose uppermost address is less than or equal to a specified address.
106-
max_address: EfiPhysicalAddress,
106+
max_address: PhysicalAddress,
107107

108108
/// Allocate pages at a specified address.
109-
at_address: EfiPhysicalAddress,
109+
at_address: PhysicalAddress,
110110
};
111111

112112
pub const PhysicalAddress = u64;

lib/std/os/uefi/tables/boot_services.zig

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const OpenProtocolAttributes = uefi.tables.OpenProtocolAttributes;
1616
const ProtocolInformationEntry = uefi.tables.ProtocolInformationEntry;
1717
const EventNotify = uefi.tables.EventNotify;
1818
const EfiEventNotify = uefi.tables.EfiEventNotify;
19-
const EfiPhysicalAddress = uefi.tables.EfiPhysicalAddress;
19+
const PhysicalAddress = uefi.tables.PhysicalAddress;
2020
const cc = uefi.cc;
2121

2222
/// Boot services are services provided by the system's firmware until the operating system takes
@@ -40,7 +40,7 @@ pub const BootServices = extern struct {
4040
restoreTpl: *const fn (old_tpl: usize) callconv(cc) void,
4141

4242
/// Allocates memory pages from the system.
43-
_allocatePages: *const fn (alloc_type: AllocateType.Enum, mem_type: MemoryType, pages: usize, memory: EfiPhysicalAddress) callconv(cc) Status,
43+
_allocatePages: *const fn (alloc_type: AllocateType.Enum, mem_type: MemoryType, pages: usize, memory: PhysicalAddress) callconv(cc) Status,
4444

4545
/// Frees memory pages.
4646
_freePages: *const fn (memory: [*]align(4096) u8, pages: usize) callconv(cc) Status,
@@ -209,7 +209,7 @@ pub const BootServices = extern struct {
209209
};
210210

211211
// EFI memory addresses are always 64-bit, even on 32-bit systems
212-
const pointer: EfiPhysicalAddress = @intFromPtr(&buffer_addr);
212+
const pointer: PhysicalAddress = @intFromPtr(&buffer_addr);
213213

214214
try self._allocatePages(alloc_type, mem_type, pages, pointer).err();
215215
const addr: [*]align(4096) u8 = @ptrFromInt(buffer_addr);

0 commit comments

Comments
 (0)