Skip to content

Commit 4971df8

Browse files
antliljaandrewrk
authored andcommitted
UEFI pool allocator changes
* Changed the interface to align with the new allocator interface. * Fixed bug where not enough memory was allocated for the header or to align the pointer.
1 parent 2a92b04 commit 4971df8

File tree

1 file changed

+41
-55
lines changed

1 file changed

+41
-55
lines changed

lib/std/os/uefi/pool_allocator.zig

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,68 +12,59 @@ const UefiPoolAllocator = struct {
1212
return @intToPtr(*[*]align(8) u8, @ptrToInt(ptr) - @sizeOf(usize));
1313
}
1414

15-
fn alignedAlloc(len: usize, alignment: usize) ?[*]u8 {
16-
var unaligned_ptr: [*]align(8) u8 = undefined;
17-
18-
if (uefi.system_table.boot_services.?.allocatePool(uefi.efi_pool_memory_type, len, &unaligned_ptr) != .Success)
19-
return null;
20-
21-
const unaligned_addr = @ptrToInt(unaligned_ptr);
22-
const aligned_addr = mem.alignForward(unaligned_addr + @sizeOf(usize), alignment);
23-
24-
var aligned_ptr = unaligned_ptr + (aligned_addr - unaligned_addr);
25-
getHeader(aligned_ptr).* = unaligned_ptr;
26-
27-
return aligned_ptr;
28-
}
29-
30-
fn alignedFree(ptr: [*]u8) void {
31-
_ = uefi.system_table.boot_services.?.freePool(getHeader(ptr).*);
32-
}
33-
3415
fn alloc(
3516
_: *anyopaque,
3617
len: usize,
37-
ptr_align: u29,
38-
len_align: u29,
18+
log2_ptr_align: u8,
3919
ret_addr: usize,
40-
) Allocator.Error![]u8 {
20+
) ?[*]u8 {
4121
_ = ret_addr;
4222

4323
assert(len > 0);
44-
assert(std.math.isPowerOfTwo(ptr_align));
4524

46-
var ptr = alignedAlloc(len, ptr_align) orelse return error.OutOfMemory;
25+
const ptr_align = 1 << log2_ptr_align;
26+
27+
const metadata_len = mem.alignForward(@sizeOf(usize), ptr_align);
4728

48-
if (len_align == 0)
49-
return ptr[0..len];
29+
const full_len = metadata_len + len;
5030

51-
return ptr[0..mem.alignBackwardAnyAlign(len, len_align)];
31+
var unaligned_ptr: [*]align(8) u8 = undefined;
32+
if (uefi.system_table.boot_services.?.allocatePool(uefi.efi_pool_memory_type, full_len, &unaligned_ptr) != .Success) return null;
33+
34+
const unaligned_addr = @ptrToInt(unaligned_ptr);
35+
const aligned_addr = mem.alignForward(unaligned_addr + @sizeOf(usize), ptr_align);
36+
37+
var aligned_ptr = unaligned_ptr + (aligned_addr - unaligned_addr);
38+
getHeader(aligned_ptr).* = unaligned_ptr;
39+
40+
return aligned_ptr;
5241
}
5342

5443
fn resize(
5544
_: *anyopaque,
5645
buf: []u8,
57-
buf_align: u29,
46+
log2_old_ptr_align: u8,
5847
new_len: usize,
59-
len_align: u29,
6048
ret_addr: usize,
61-
) ?usize {
62-
_ = buf_align;
49+
) bool {
6350
_ = ret_addr;
6451

65-
return if (new_len <= buf.len) mem.alignAllocLen(buf.len, new_len, len_align) else null;
52+
if (new_len > buf.len) return false;
53+
54+
_ = mem.alignAllocLen(buf.len, new_len, log2_old_ptr_align);
55+
56+
return true;
6657
}
6758

6859
fn free(
6960
_: *anyopaque,
7061
buf: []u8,
71-
buf_align: u29,
62+
log2_old_ptr_align: u8,
7263
ret_addr: usize,
7364
) void {
74-
_ = buf_align;
65+
_ = log2_old_ptr_align;
7566
_ = ret_addr;
76-
alignedFree(buf.ptr);
67+
_ = uefi.system_table.boot_services.?.freePool(getHeader(buf.ptr).*);
7768
}
7869
};
7970

@@ -105,49 +96,44 @@ const raw_pool_allocator_table = Allocator.VTable{
10596
fn uefi_alloc(
10697
_: *anyopaque,
10798
len: usize,
108-
ptr_align: u29,
109-
len_align: u29,
99+
log2_ptr_align: u8,
110100
ret_addr: usize,
111-
) Allocator.Error![]u8 {
112-
_ = len_align;
101+
) ?[*]u8 {
113102
_ = ret_addr;
114103

115-
std.debug.assert(ptr_align <= 8);
104+
std.debug.assert(log2_ptr_align <= 3);
116105

117106
var ptr: [*]align(8) u8 = undefined;
107+
if (uefi.system_table.boot_services.?.allocatePool(uefi.efi_pool_memory_type, len, &ptr) != .Success) return null;
118108

119-
if (uefi.system_table.boot_services.?.allocatePool(uefi.efi_pool_memory_type, len, &ptr) != .Success) {
120-
return error.OutOfMemory;
121-
}
122-
123-
return ptr[0..len];
109+
return ptr;
124110
}
125111

126112
fn uefi_resize(
127113
_: *anyopaque,
128114
buf: []u8,
129-
old_align: u29,
115+
log2_old_ptr_align: u8,
130116
new_len: usize,
131-
len_align: u29,
132117
ret_addr: usize,
133-
) ?usize {
134-
_ = old_align;
118+
) bool {
135119
_ = ret_addr;
136120

137-
if (new_len <= buf.len) {
138-
return mem.alignAllocLen(buf.len, new_len, len_align);
139-
}
121+
std.debug.assert(log2_old_ptr_align <= 3);
122+
123+
if (new_len > buf.len) return false;
124+
125+
_ = mem.alignAllocLen(buf.len, new_len, 8);
140126

141-
return null;
127+
return true;
142128
}
143129

144130
fn uefi_free(
145131
_: *anyopaque,
146132
buf: []u8,
147-
buf_align: u29,
133+
log2_old_ptr_align: u8,
148134
ret_addr: usize,
149135
) void {
150-
_ = buf_align;
136+
_ = log2_old_ptr_align;
151137
_ = ret_addr;
152138
_ = uefi.system_table.boot_services.?.freePool(@alignCast(8, buf.ptr));
153139
}

0 commit comments

Comments
 (0)