Skip to content

Commit 441068b

Browse files
committed
Use ImageDataType for allocation type
Signed-off-by: Ayush Singh <[email protected]>
1 parent b66fe58 commit 441068b

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

library/std/src/sys/uefi/alloc.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
//! Global Allocator for UEFI.
22
//! Uses [r-efi-alloc](https://crates.io/crates/r-efi-alloc)
33
4-
use crate::alloc::{GlobalAlloc, Layout, System};
4+
use r_efi::protocols::loaded_image;
55

6-
const MEMORY_TYPE: u32 = r_efi::efi::LOADER_DATA;
6+
use crate::alloc::{GlobalAlloc, Layout, System};
7+
use crate::sync::OnceLock;
8+
use crate::sys::uefi::helpers;
79

810
#[stable(feature = "alloc_system_type", since = "1.28.0")]
911
unsafe impl GlobalAlloc for System {
1012
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
13+
static EFI_MEMORY_TYPE: OnceLock<u32> = OnceLock::new();
14+
1115
// Return null pointer if boot services are not available
1216
if crate::os::uefi::env::boot_services().is_none() {
1317
return crate::ptr::null_mut();
1418
}
1519

1620
// If boot services is valid then SystemTable is not null.
1721
let system_table = crate::os::uefi::env::system_table().as_ptr().cast();
22+
23+
// Each loaded image has an image handle that supports `EFI_LOADED_IMAGE_PROTOCOL`. Thus, this
24+
// will never fail.
25+
let mem_type = EFI_MEMORY_TYPE.get_or_init(|| {
26+
let protocol = helpers::image_handle_protocol::<loaded_image::Protocol>(
27+
loaded_image::PROTOCOL_GUID,
28+
)
29+
.unwrap();
30+
// Gives allocations the memory type that the data sections were loaded as.
31+
unsafe { (*protocol.as_ptr()).image_data_type }
32+
});
33+
1834
// The caller must ensure non-0 layout
19-
unsafe { r_efi_alloc::raw::alloc(system_table, layout, MEMORY_TYPE) }
35+
unsafe { r_efi_alloc::raw::alloc(system_table, layout, *mem_type) }
2036
}
2137

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

0 commit comments

Comments
 (0)