13
13
14
14
use core:: alloc:: { GlobalAlloc , Layout } ;
15
15
use core:: ptr:: { self , NonNull } ;
16
+ use core:: sync:: atomic:: { AtomicU32 , Ordering } ;
16
17
17
18
use crate :: proto:: loaded_image:: LoadedImage ;
18
19
use crate :: table:: boot:: { BootServices , MemoryType } ;
@@ -25,7 +26,7 @@ static mut BOOT_SERVICES: Option<NonNull<BootServices>> = None;
25
26
26
27
/// The memory type used for pool memory allocations.
27
28
/// 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 ) ;
29
30
30
31
/// Initializes the allocator.
31
32
///
@@ -39,7 +40,7 @@ pub unsafe fn init(boot_services: &BootServices) {
39
40
if let Ok ( loaded_image) =
40
41
boot_services. open_protocol_exclusive :: < LoadedImage > ( boot_services. image_handle ( ) )
41
42
{
42
- MEMORY_TYPE = loaded_image. data_type ( )
43
+ MEMORY_TYPE . store ( loaded_image. data_type ( ) . 0 , Ordering :: Release ) ;
43
44
}
44
45
}
45
46
@@ -70,6 +71,7 @@ unsafe impl GlobalAlloc for Allocator {
70
71
unsafe fn alloc ( & self , layout : Layout ) -> * mut u8 {
71
72
let size = layout. size ( ) ;
72
73
let align = layout. align ( ) ;
74
+ let memory_type = MemoryType ( MEMORY_TYPE . load ( Ordering :: Acquire ) ) ;
73
75
74
76
if align > 8 {
75
77
// The requested alignment is greater than 8, but `allocate_pool` is
@@ -78,7 +80,7 @@ unsafe impl GlobalAlloc for Allocator {
78
80
// within the allocation.
79
81
let full_alloc_ptr = if let Ok ( ptr) = boot_services ( )
80
82
. as_ref ( )
81
- . allocate_pool ( MEMORY_TYPE , size + align)
83
+ . allocate_pool ( memory_type , size + align)
82
84
{
83
85
ptr
84
86
} else {
@@ -110,7 +112,7 @@ unsafe impl GlobalAlloc for Allocator {
110
112
// use `allocate_pool` directly.
111
113
boot_services ( )
112
114
. as_ref ( )
113
- . allocate_pool ( MEMORY_TYPE , size)
115
+ . allocate_pool ( memory_type , size)
114
116
. unwrap_or ( ptr:: null_mut ( ) )
115
117
}
116
118
}
0 commit comments