Commit 4a69eba2 authored by Jeremy Soller's avatar Jeremy Soller

Merge branch 'master' into 'master'

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

See merge request !1
parents 497ce915 6ac4f576
......@@ -5,47 +5,38 @@
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 {
None
} 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() {
ptr::null_mut()
} else {
ptr as *mut u8
}
let uefi = UEFI.expect("__rust_allocate: uefi not initialized");
let mut ptr = 0;
if (uefi.as_ref().BootServices.AllocatePool)(
MemoryType::EfiLoaderData,
layout.size(),
&mut ptr,
)
.into_result()
.is_ok()
{
ptr as *mut u8
} else {
panic!("__rust_allocate: uefi not initialized");
ptr::null_mut()
}
}
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