@@ -3,7 +3,6 @@ use crate::alloc::{GlobalAlloc, Layout, System};
3
3
use crate :: ffi:: c_void;
4
4
use crate :: mem:: MaybeUninit ;
5
5
use crate :: ptr;
6
- use crate :: sync:: atomic:: { AtomicPtr , Ordering } ;
7
6
use crate :: sys:: c;
8
7
9
8
#[ cfg( test) ]
@@ -81,69 +80,25 @@ windows_targets::link!("kernel32.dll" "system" fn HeapReAlloc(
81
80
// See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-heapfree
82
81
windows_targets:: link!( "kernel32.dll" "system" fn HeapFree ( hheap: c:: HANDLE , dwflags: u32 , lpmem: * const c_void) -> c:: BOOL ) ;
83
82
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 ( ) }
106
86
}
107
87
108
- /// This is outlined from `process_heap_alloc` so that `process_heap_alloc`
109
- /// does not need any stack allocations.
110
88
#[ 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`,
114
91
flags : u32 ,
115
92
bytes : usize ,
116
93
) -> * mut c_void {
117
- let heap = init_or_get_process_heap ( ) ;
94
+ let heap = get_process_heap ( ) ;
118
95
if core:: intrinsics:: unlikely ( heap. is_null ( ) ) {
119
96
return ptr:: null_mut ( ) ;
120
97
}
121
98
// SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`.
122
99
unsafe { HeapAlloc ( heap, flags, bytes) }
123
100
}
124
101
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
-
147
102
// Header containing a pointer to the start of an allocated block.
148
103
// SAFETY: Size and alignment must be <= `MIN_ALIGN`.
149
104
#[ repr( C ) ]
@@ -232,9 +187,9 @@ unsafe impl GlobalAlloc for System {
232
187
}
233
188
} ;
234
189
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 ( ) ;
238
193
239
194
// SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`,
240
195
// `block` is a pointer to the start of an allocated block.
@@ -244,9 +199,9 @@ unsafe impl GlobalAlloc for System {
244
199
#[ inline]
245
200
unsafe fn realloc ( & self , ptr : * mut u8 , layout : Layout , new_size : usize ) -> * mut u8 {
246
201
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 ( ) ;
250
205
251
206
// SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`,
252
207
// `ptr` is a pointer to the start of an allocated block.
0 commit comments