Skip to content

Commit 1918bd3

Browse files
Merge pull request #1176 from phip1611/allocate-pool
uefi: BootServices::allocate_pool now returns NonZero<u8> instead of *mut u8
2 parents 887cd56 + 2f4aece commit 1918bd3

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

uefi-test-runner/src/boot/misc.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ fn test_install_protocol_interface(bt: &BootServices) {
121121
mem::size_of::<TestProtocol>(),
122122
)
123123
.unwrap()
124-
.cast();
124+
.cast()
125+
.as_ptr();
125126
unsafe { alloc.write(TestProtocol { data: 123 }) };
126127

127128
let _ = unsafe {
@@ -187,7 +188,8 @@ fn test_install_configuration_table(st: &SystemTable<Boot>) {
187188
let config = st
188189
.boot_services()
189190
.allocate_pool(MemoryType::ACPI_RECLAIM, 1)
190-
.expect("Failed to allocate config table");
191+
.expect("Failed to allocate config table")
192+
.as_ptr();
191193
unsafe { config.write(42) };
192194

193195
let count = st.config_table().len();

uefi/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
## Changed
1616
- `SystemTable::exit_boot_services` is now `unsafe`. See that method's
1717
documentation for details of obligations for callers.
18+
- `BootServices::allocate_pool` now returns `NonZero<u8>` instead of
19+
`*mut u8`.
1820

1921
## Removed
2022
- Removed the `panic-on-logger-errors` feature of the `uefi` crate. Logger

uefi/src/allocator.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ unsafe impl GlobalAlloc for Allocator {
8686
// within the allocation.
8787
let full_alloc_ptr =
8888
if let Ok(ptr) = boot_services.allocate_pool(memory_type, size + align) {
89-
ptr
89+
ptr.as_ptr()
9090
} else {
9191
return ptr::null_mut();
9292
};
@@ -116,6 +116,7 @@ unsafe impl GlobalAlloc for Allocator {
116116
// use `allocate_pool` directly.
117117
boot_services
118118
.allocate_pool(memory_type, size)
119+
.map(|ptr| ptr.as_ptr())
119120
.unwrap_or(ptr::null_mut())
120121
}
121122
}

uefi/src/table/boot.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,13 @@ impl BootServices {
271271
///
272272
/// * [`uefi::Status::OUT_OF_RESOURCES`]
273273
/// * [`uefi::Status::INVALID_PARAMETER`]
274-
pub fn allocate_pool(&self, mem_ty: MemoryType, size: usize) -> Result<*mut u8> {
274+
pub fn allocate_pool(&self, mem_ty: MemoryType, size: usize) -> Result<NonNull<u8>> {
275275
let mut buffer = ptr::null_mut();
276-
unsafe { (self.0.allocate_pool)(mem_ty, size, &mut buffer) }.to_result_with_val(|| buffer)
276+
let ptr = unsafe { (self.0.allocate_pool)(mem_ty, size, &mut buffer) }
277+
.to_result_with_val(|| buffer)?;
278+
279+
Ok(NonNull::new(ptr)
280+
.expect("UEFI should return error if an allocation failed but never a null pointer"))
277281
}
278282

279283
/// Frees memory allocated from a pool.

uefi/src/table/system.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl SystemTable<Boot> {
265265
// Allocate a byte slice to hold the memory map. If the
266266
// allocation fails treat it as an unrecoverable error.
267267
let buf: *mut u8 = match boot_services.allocate_pool(memory_type, buf_size) {
268-
Ok(buf) => buf,
268+
Ok(buf) => buf.as_ptr(),
269269
Err(err) => reset(err.status()),
270270
};
271271

0 commit comments

Comments
 (0)