@@ -3,7 +3,6 @@ use crate::alloc::{GlobalAlloc, Layout, System};
33use crate :: ffi:: c_void;
44use crate :: mem:: MaybeUninit ;
55use crate :: ptr;
6- use crate :: sync:: atomic:: { AtomicPtr , Ordering } ;
76use crate :: sys:: c;
87
98#[ cfg( test) ]
@@ -81,69 +80,25 @@ windows_targets::link!("kernel32.dll" "system" fn HeapReAlloc(
8180// See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-heapfree
8281windows_targets:: link!( "kernel32.dll" "system" fn HeapFree ( hheap: c:: HANDLE , dwflags: u32 , lpmem: * const c_void) -> c:: BOOL ) ;
8382
84- // Cached handle to the default heap of the current process.
85- // Either a non-null handle returned by `GetProcessHeap`, or null when not yet initialized or `GetProcessHeap` failed.
86- static HEAP : AtomicPtr < c_void > = AtomicPtr :: new ( ptr:: null_mut ( ) ) ;
87-
88- // Get a handle to the default heap of the current process, or null if the operation fails.
89- // If this operation is successful, `HEAP` will be successfully initialized and contain
90- // a non-null handle returned by `GetProcessHeap`.
91- #[ inline]
92- fn init_or_get_process_heap ( ) -> c:: HANDLE {
93- // `HEAP` has not yet been successfully initialized
94- let heap = unsafe { GetProcessHeap ( ) } ;
95- if !heap. is_null ( ) {
96- // SAFETY: No locking is needed because within the same process,
97- // successful calls to `GetProcessHeap` will always return the same value, even on different threads.
98- HEAP . store ( heap, Ordering :: Release ) ;
99-
100- // SAFETY: `HEAP` contains a non-null handle returned by `GetProcessHeap`
101- heap
102- } else {
103- // Could not get the current process heap.
104- ptr:: null_mut ( )
105- }
83+ fn get_process_heap ( ) -> * mut c_void {
84+ // SAFETY: GetProcessHeap simply returns a valid handle or NULL so is always safe to call.
85+ unsafe { GetProcessHeap ( ) }
10686}
10787
108- /// This is outlined from `process_heap_alloc` so that `process_heap_alloc`
109- /// does not need any stack allocations.
11088#[ inline( never) ]
111- #[ cold]
112- extern "C" fn process_heap_init_and_alloc (
113- _heap : MaybeUninit < c:: HANDLE > , // We pass this argument to match the ABI of `HeapAlloc`
89+ fn process_heap_alloc (
90+ _heap : MaybeUninit < c:: HANDLE > , // We pass this argument to match the ABI of `HeapAlloc`,
11491 flags : u32 ,
11592 bytes : usize ,
11693) -> * mut c_void {
117- let heap = init_or_get_process_heap ( ) ;
94+ let heap = get_process_heap ( ) ;
11895 if core:: intrinsics:: unlikely ( heap. is_null ( ) ) {
11996 return ptr:: null_mut ( ) ;
12097 }
12198 // SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`.
12299 unsafe { HeapAlloc ( heap, flags, bytes) }
123100}
124101
125- #[ inline( never) ]
126- fn process_heap_alloc (
127- _heap : MaybeUninit < c:: HANDLE > , // We pass this argument to match the ABI of `HeapAlloc`,
128- flags : u32 ,
129- bytes : usize ,
130- ) -> * mut c_void {
131- let heap = HEAP . load ( Ordering :: Relaxed ) ;
132- if core:: intrinsics:: likely ( !heap. is_null ( ) ) {
133- // SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`.
134- unsafe { HeapAlloc ( heap, flags, bytes) }
135- } else {
136- process_heap_init_and_alloc ( MaybeUninit :: uninit ( ) , flags, bytes)
137- }
138- }
139-
140- // Get a non-null handle to the default heap of the current process.
141- // SAFETY: `HEAP` must have been successfully initialized.
142- #[ inline]
143- unsafe fn get_process_heap ( ) -> c:: HANDLE {
144- HEAP . load ( Ordering :: Acquire )
145- }
146-
147102// Header containing a pointer to the start of an allocated block.
148103// SAFETY: Size and alignment must be <= `MIN_ALIGN`.
149104#[ repr( C ) ]
@@ -232,9 +187,9 @@ unsafe impl GlobalAlloc for System {
232187 }
233188 } ;
234189
235- // SAFETY: because `ptr` has been successfully allocated with this allocator,
236- // `HEAP` must have been successfully initialized .
237- let heap = unsafe { get_process_heap ( ) } ;
190+ // because `ptr` has been successfully allocated with this allocator,
191+ // there must be a valid process heap .
192+ let heap = get_process_heap ( ) ;
238193
239194 // SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`,
240195 // `block` is a pointer to the start of an allocated block.
@@ -244,9 +199,9 @@ unsafe impl GlobalAlloc for System {
244199 #[ inline]
245200 unsafe fn realloc ( & self , ptr : * mut u8 , layout : Layout , new_size : usize ) -> * mut u8 {
246201 if layout. align ( ) <= MIN_ALIGN {
247- // SAFETY: because `ptr` has been successfully allocated with this allocator,
248- // `HEAP` must have been successfully initialized .
249- let heap = unsafe { get_process_heap ( ) } ;
202+ // because `ptr` has been successfully allocated with this allocator,
203+ // there must be a valid process heap .
204+ let heap = get_process_heap ( ) ;
250205
251206 // SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`,
252207 // `ptr` is a pointer to the start of an allocated block.
0 commit comments