Commit 6ac4f576 authored by Marcel Hellwig's avatar Marcel Hellwig
Browse files

Changed `*mut ...` to `Option<NonNull<...>>`

Instead of a raw pointer use a combination of Option and NonNull which
is exactly the same size as a raw pointer, expect you cannot access a
nullpointer by accident.
The assembly is completly identical on the hot path (the one that does not
procude a panic). Only one move more on the cold path (because of the
unwrap message (?).
parent 55b67cd7
Pipeline #2485 failed with stages
in 45 seconds
......@@ -7,47 +7,38 @@ extern crate uefi;
use core::alloc::{GlobalAlloc, Layout};
use core::ops::Try;
use core::ptr;
use core::ptr::{self, NonNull};
use uefi::memory::MemoryType;
use uefi::system::SystemTable;
static mut UEFI: *mut SystemTable = 0 as *mut SystemTable;
static mut UEFI: Option<NonNull<SystemTable>> = None;
pub unsafe fn init(table: &'static mut SystemTable) {
UEFI = table;
fn get_uefi() -> Option<&'static mut SystemTable> {
unsafe {
if UEFI as usize == 0 {
} else {
Some(&mut *UEFI)
UEFI = NonNull::new(table);
pub struct Allocator;
unsafe impl GlobalAlloc for Allocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
if let Some(ref mut uefi) = get_uefi() {
let mut ptr = 0;
if let Err(_) = (uefi.BootServices.AllocatePool)(MemoryType::EfiLoaderData, layout.size(), &mut ptr).into_result() {
} else {
ptr as *mut u8
let uefi = UEFI.expect("__rust_allocate: uefi not initialized");
let mut ptr = 0;
if (uefi.as_ref().BootServices.AllocatePool)(
&mut ptr,
ptr as *mut u8
} else {
panic!("__rust_allocate: uefi not initialized");
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
if let Some(ref mut uefi) = get_uefi() {
let _ = (uefi.BootServices.FreePool)(ptr as usize);
} else {
panic!("__rust_deallocate: uefi not initialized");
let uefi = UEFI.expect("__rust_deallocate: uefi not initialized");
let _ = (uefi.as_ref().BootServices.FreePool)(ptr as usize);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment