@@ -81,69 +81,27 @@ windows_targets::link!("kernel32.dll" "system" fn HeapReAlloc(
8181// See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-heapfree
8282windows_targets:: link!( "kernel32.dll" "system" fn HeapFree ( hheap: c:: HANDLE , dwflags: u32 , lpmem: * const c_void) -> c:: BOOL ) ;
8383
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 ( )
84+ fn get_process_heap ( ) {
85+ /// SAFETY: GetProcessHeap simply returns a valid handle or NULL so is always safe to call.
86+ unsafe {
87+ GetProcessHeap ( )
10588 }
10689}
10790
108- /// This is outlined from `process_heap_alloc` so that `process_heap_alloc`
109- /// does not need any stack allocations.
11091#[ 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`
92+ fn process_heap_alloc (
93+ _heap : MaybeUninit < c:: HANDLE > , // We pass this argument to match the ABI of `HeapAlloc`,
11494 flags : u32 ,
11595 bytes : usize ,
11696) -> * mut c_void {
117- let heap = init_or_get_process_heap ( ) ;
97+ let heap = get_process_heap ( ) ;
11898 if core:: intrinsics:: unlikely ( heap. is_null ( ) ) {
11999 return ptr:: null_mut ( ) ;
120100 }
121101 // SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`.
122102 unsafe { HeapAlloc ( heap, flags, bytes) }
123103}
124104
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-
147105// Header containing a pointer to the start of an allocated block.
148106// SAFETY: Size and alignment must be <= `MIN_ALIGN`.
149107#[ repr( C ) ]
@@ -232,9 +190,9 @@ unsafe impl GlobalAlloc for System {
232190 }
233191 } ;
234192
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 ( ) } ;
193+ // because `ptr` has been successfully allocated with this allocator,
194+ // there must be a valid process heap .
195+ let heap = get_process_heap ( ) ;
238196
239197 // SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`,
240198 // `block` is a pointer to the start of an allocated block.
@@ -244,9 +202,9 @@ unsafe impl GlobalAlloc for System {
244202 #[ inline]
245203 unsafe fn realloc ( & self , ptr : * mut u8 , layout : Layout , new_size : usize ) -> * mut u8 {
246204 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 ( ) } ;
205+ // because `ptr` has been successfully allocated with this allocator,
206+ // there must be a valid process heap .
207+ let heap = get_process_heap ( ) ;
250208
251209 // SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`,
252210 // `ptr` is a pointer to the start of an allocated block.
0 commit comments