1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
const std = @import("std");
const mem = std.mem;
const uefi = std.os.uefi;
const assert = std.debug.assert;
const Allocator = mem.Allocator;
const UefiPoolAllocator = struct {
fn getHeader(ptr: [*]u8) *[*]align(8) u8 {
return @as(*[*]align(8) u8, @ptrFromInt(@intFromPtr(ptr) - @sizeOf(usize)));
}
fn alloc(
_: *anyopaque,
len: usize,
alignment: mem.Alignment,
ret_addr: usize,
) ?[*]u8 {
_ = ret_addr;
assert(len > 0);
const ptr_align = alignment.toByteUnits();
const metadata_len = mem.alignForward(usize, @sizeOf(usize), ptr_align);
const full_len = metadata_len + len;
const unaligned_slice = uefi.system_table.boot_services.?.allocatePool(
uefi.efi_pool_memory_type,
full_len,
) catch return null;
const unaligned_addr = @intFromPtr(unaligned_slice.ptr);
const aligned_addr = mem.alignForward(usize, unaligned_addr + @sizeOf(usize), ptr_align);
const aligned_ptr = unaligned_slice.ptr + (aligned_addr - unaligned_addr);
getHeader(aligned_ptr).* = unaligned_slice.ptr;
return aligned_ptr;
}
fn resize(
_: *anyopaque,
buf: []u8,
alignment: mem.Alignment,
new_len: usize,
ret_addr: usize,
) bool {
_ = ret_addr;
_ = 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,
alignment: mem.Alignment,
ret_addr: usize,
) void {
_ = alignment;
_ = ret_addr;
uefi.system_table.boot_services.?.freePool(getHeader(buf.ptr).*) catch unreachable;
}
};
/// Supports the full Allocator interface, including alignment.
/// For a direct call of `allocatePool`, see `raw_pool_allocator`.
pub const pool_allocator = Allocator{
.ptr = undefined,
.vtable = &pool_allocator_vtable,
};
const pool_allocator_vtable = Allocator.VTable{
.alloc = UefiPoolAllocator.alloc,
.resize = UefiPoolAllocator.resize,
.remap = UefiPoolAllocator.remap,
.free = UefiPoolAllocator.free,
};
/// Asserts allocations are 8 byte aligned and calls `boot_services.allocatePool`.
pub const raw_pool_allocator = Allocator{
.ptr = undefined,
.vtable = &raw_pool_allocator_table,
};
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,
alignment: mem.Alignment,
ret_addr: usize,
) ?[*]u8 {
_ = ret_addr;
std.debug.assert(@intFromEnum(alignment) <= 3);
const slice = uefi.system_table.boot_services.?.allocatePool(
uefi.efi_pool_memory_type,
len,
) catch return null;
return slice.ptr;
}
fn uefi_resize(
_: *anyopaque,
buf: []u8,
alignment: mem.Alignment,
new_len: usize,
ret_addr: usize,
) bool {
_ = ret_addr;
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,
alignment: mem.Alignment,
ret_addr: usize,
) void {
_ = alignment;
_ = ret_addr;
uefi.system_table.boot_services.?.freePool(@alignCast(buf.ptr)) catch unreachable;
}
|