Skip to content
Merged
Changes from all commits
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
55 changes: 43 additions & 12 deletions lib/std/os/uefi/pool_allocator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ const UefiPoolAllocator = struct {
fn alloc(
_: *anyopaque,
len: usize,
log2_ptr_align: u8,
alignment: mem.Alignment,
ret_addr: usize,
) ?[*]u8 {
_ = ret_addr;

assert(len > 0);

const ptr_align = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_ptr_align));
const ptr_align = alignment.toByteUnits();

const metadata_len = mem.alignForward(usize, @sizeOf(usize), ptr_align);

Expand All @@ -43,24 +43,38 @@ const UefiPoolAllocator = struct {
fn resize(
_: *anyopaque,
buf: []u8,
log2_old_ptr_align: u8,
alignment: mem.Alignment,
new_len: usize,
ret_addr: usize,
) bool {
_ = ret_addr;
_ = log2_old_ptr_align;
_ = alignment;

if (new_len > buf.len) return false;
return true;
}

fn remap(
_: *anyopaque,
buf: []u8,
alignment: mem.Alignment,
new_len: usize,
ret_addr: usize,
) ?[*]u8 {
_ = alignment;
_ = ret_addr;

if (new_len > buf.len) return null;
return buf.ptr;
}

fn free(
_: *anyopaque,
buf: []u8,
log2_old_ptr_align: u8,
alignment: mem.Alignment,
ret_addr: usize,
) void {
_ = log2_old_ptr_align;
_ = alignment;
_ = ret_addr;
_ = uefi.system_table.boot_services.?.freePool(getHeader(buf.ptr).*);
}
Expand All @@ -76,6 +90,7 @@ pub const pool_allocator = Allocator{
const pool_allocator_vtable = Allocator.VTable{
.alloc = UefiPoolAllocator.alloc,
.resize = UefiPoolAllocator.resize,
.remap = UefiPoolAllocator.remap,
.free = UefiPoolAllocator.free,
};

Expand All @@ -88,18 +103,19 @@ pub const raw_pool_allocator = Allocator{
const raw_pool_allocator_table = Allocator.VTable{
.alloc = uefi_alloc,
.resize = uefi_resize,
.remap = uefi_remap,
.free = uefi_free,
};

fn uefi_alloc(
_: *anyopaque,
len: usize,
log2_ptr_align: u8,
alignment: mem.Alignment,
ret_addr: usize,
) ?[*]u8 {
_ = ret_addr;

std.debug.assert(log2_ptr_align <= 3);
std.debug.assert(@intFromEnum(alignment) <= 3);

var ptr: [*]align(8) u8 = undefined;
if (uefi.system_table.boot_services.?.allocatePool(uefi.efi_pool_memory_type, len, &ptr) != .Success) return null;
Expand All @@ -110,25 +126,40 @@ fn uefi_alloc(
fn uefi_resize(
_: *anyopaque,
buf: []u8,
log2_old_ptr_align: u8,
alignment: mem.Alignment,
new_len: usize,
ret_addr: usize,
) bool {
_ = ret_addr;

std.debug.assert(log2_old_ptr_align <= 3);
std.debug.assert(@intFromEnum(alignment) <= 3);

if (new_len > buf.len) return false;
return true;
}

fn uefi_remap(
_: *anyopaque,
buf: []u8,
alignment: mem.Alignment,
new_len: usize,
ret_addr: usize,
) ?[*]u8 {
_ = ret_addr;

std.debug.assert(@intFromEnum(alignment) <= 3);

if (new_len > buf.len) return null;
return buf.ptr;
}

fn uefi_free(
_: *anyopaque,
buf: []u8,
log2_old_ptr_align: u8,
alignment: mem.Alignment,
ret_addr: usize,
) void {
_ = log2_old_ptr_align;
_ = alignment;
_ = ret_addr;
_ = uefi.system_table.boot_services.?.freePool(@alignCast(buf.ptr));
}
Loading