@@ -81,69 +81,27 @@ windows_targets::link!("kernel32.dll" "system" fn HeapReAlloc(
81
81
// See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-heapfree
82
82
windows_targets:: link!( "kernel32.dll" "system" fn HeapFree ( hheap: c:: HANDLE , dwflags: u32 , lpmem: * const c_void) -> c:: BOOL ) ;
83
83
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 ( )
105
88
}
106
89
}
107
90
108
- /// This is outlined from `process_heap_alloc` so that `process_heap_alloc`
109
- /// does not need any stack allocations.
110
91
#[ 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`,
114
94
flags : u32 ,
115
95
bytes : usize ,
116
96
) -> * mut c_void {
117
- let heap = init_or_get_process_heap ( ) ;
97
+ let heap = get_process_heap ( ) ;
118
98
if core:: intrinsics:: unlikely ( heap. is_null ( ) ) {
119
99
return ptr:: null_mut ( ) ;
120
100
}
121
101
// SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`.
122
102
unsafe { HeapAlloc ( heap, flags, bytes) }
123
103
}
124
104
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
105
// Header containing a pointer to the start of an allocated block.
148
106
// SAFETY: Size and alignment must be <= `MIN_ALIGN`.
149
107
#[ repr( C ) ]
@@ -232,9 +190,9 @@ unsafe impl GlobalAlloc for System {
232
190
}
233
191
} ;
234
192
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 ( ) ;
238
196
239
197
// SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`,
240
198
// `block` is a pointer to the start of an allocated block.
@@ -244,9 +202,9 @@ unsafe impl GlobalAlloc for System {
244
202
#[ inline]
245
203
unsafe fn realloc ( & self , ptr : * mut u8 , layout : Layout , new_size : usize ) -> * mut u8 {
246
204
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 ( ) ;
250
208
251
209
// SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`,
252
210
// `ptr` is a pointer to the start of an allocated block.
0 commit comments