Skip to content

Commit 5b2bd72

Browse files
uefi: Change MEMORY_TYPE in allocator to an AtomicU32
1 parent 6e3b06c commit 5b2bd72

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

uefi/src/allocator.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
1414
use core::alloc::{GlobalAlloc, Layout};
1515
use core::ptr::{self, NonNull};
16+
use core::sync::atomic::{AtomicU32, Ordering};
1617

1718
use crate::proto::loaded_image::LoadedImage;
1819
use crate::table::boot::{BootServices, MemoryType};
@@ -25,7 +26,7 @@ static mut BOOT_SERVICES: Option<NonNull<BootServices>> = None;
2526

2627
/// The memory type used for pool memory allocations.
2728
/// TODO: Use OnceCell when stablilized.
28-
static mut MEMORY_TYPE: MemoryType = MemoryType::LOADER_DATA;
29+
static MEMORY_TYPE: AtomicU32 = AtomicU32::new(MemoryType::LOADER_DATA.0);
2930

3031
/// Initializes the allocator.
3132
///
@@ -39,7 +40,7 @@ pub unsafe fn init(boot_services: &BootServices) {
3940
if let Ok(loaded_image) =
4041
boot_services.open_protocol_exclusive::<LoadedImage>(boot_services.image_handle())
4142
{
42-
MEMORY_TYPE = loaded_image.data_type()
43+
MEMORY_TYPE.store(loaded_image.data_type().0, Ordering::Release);
4344
}
4445
}
4546

@@ -70,6 +71,7 @@ unsafe impl GlobalAlloc for Allocator {
7071
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
7172
let size = layout.size();
7273
let align = layout.align();
74+
let memory_type = MemoryType(MEMORY_TYPE.load(Ordering::Acquire));
7375

7476
if align > 8 {
7577
// The requested alignment is greater than 8, but `allocate_pool` is
@@ -78,7 +80,7 @@ unsafe impl GlobalAlloc for Allocator {
7880
// within the allocation.
7981
let full_alloc_ptr = if let Ok(ptr) = boot_services()
8082
.as_ref()
81-
.allocate_pool(MEMORY_TYPE, size + align)
83+
.allocate_pool(memory_type, size + align)
8284
{
8385
ptr
8486
} else {
@@ -110,7 +112,7 @@ unsafe impl GlobalAlloc for Allocator {
110112
// use `allocate_pool` directly.
111113
boot_services()
112114
.as_ref()
113-
.allocate_pool(MEMORY_TYPE, size)
115+
.allocate_pool(memory_type, size)
114116
.unwrap_or(ptr::null_mut())
115117
}
116118
}

0 commit comments

Comments
 (0)