Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ImageDataType for allocation type #117177

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions library/std/src/sys/uefi/alloc.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
//! Global Allocator for UEFI.
//! Uses [r-efi-alloc](https://crates.io/crates/r-efi-alloc)

use crate::alloc::{GlobalAlloc, Layout, System};
use r_efi::protocols::loaded_image;

const MEMORY_TYPE: u32 = r_efi::efi::LOADER_DATA;
use crate::alloc::{GlobalAlloc, Layout, System};
use crate::sync::OnceLock;
use crate::sys::uefi::helpers;

#[stable(feature = "alloc_system_type", since = "1.28.0")]
unsafe impl GlobalAlloc for System {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
static EFI_MEMORY_TYPE: OnceLock<u32> = OnceLock::new();

// Return null pointer if boot services are not available
if crate::os::uefi::env::boot_services().is_none() {
return crate::ptr::null_mut();
}

// If boot services is valid then SystemTable is not null.
let system_table = crate::os::uefi::env::system_table().as_ptr().cast();

// Each loaded image has an image handle that supports `EFI_LOADED_IMAGE_PROTOCOL`. Thus, this
// will never fail.
let mem_type = EFI_MEMORY_TYPE.get_or_init(|| {
let protocol = helpers::image_handle_protocol::<loaded_image::Protocol>(
loaded_image::PROTOCOL_GUID,
)
.unwrap();
// Gives allocations the memory type that the data sections were loaded as.
unsafe { (*protocol.as_ptr()).image_data_type }
Ayush1325 marked this conversation as resolved.
Show resolved Hide resolved
});

// The caller must ensure non-0 layout
unsafe { r_efi_alloc::raw::alloc(system_table, layout, MEMORY_TYPE) }
unsafe { r_efi_alloc::raw::alloc(system_table, layout, *mem_type) }
}

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
Expand Down
Loading