@@ -95,7 +95,7 @@ static HEAP: AtomicPtr<c_void> = AtomicPtr::new(ptr::null_mut());
95
95
#[ inline]
96
96
fn init_or_get_process_heap ( ) -> c:: HANDLE {
97
97
let heap = HEAP . load ( Ordering :: Relaxed ) ;
98
- if heap. is_null ( ) {
98
+ if core :: intrinsics :: unlikely ( heap. is_null ( ) ) {
99
99
// `HEAP` has not yet been successfully initialized
100
100
let heap = unsafe { GetProcessHeap ( ) } ;
101
101
if !heap. is_null ( ) {
@@ -115,6 +115,16 @@ fn init_or_get_process_heap() -> c::HANDLE {
115
115
}
116
116
}
117
117
118
+ #[ inline( never) ]
119
+ fn process_heap_alloc ( flags : c:: DWORD , dwBytes : c:: SIZE_T ) -> c:: LPVOID {
120
+ let heap = init_or_get_process_heap ( ) ;
121
+ if core:: intrinsics:: unlikely ( heap. is_null ( ) ) {
122
+ return ptr:: null_mut ( ) ;
123
+ }
124
+ // SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`.
125
+ unsafe { HeapAlloc ( heap, flags, dwBytes) }
126
+ }
127
+
118
128
// Get a non-null handle to the default heap of the current process.
119
129
// SAFETY: `HEAP` must have been successfully initialized.
120
130
#[ inline]
@@ -133,25 +143,17 @@ struct Header(*mut u8);
133
143
// initialized.
134
144
#[ inline]
135
145
unsafe fn allocate ( layout : Layout , zeroed : bool ) -> * mut u8 {
136
- let heap = init_or_get_process_heap ( ) ;
137
- if heap. is_null ( ) {
138
- // Allocation has failed, could not get the current process heap.
139
- return ptr:: null_mut ( ) ;
140
- }
141
-
142
146
// Allocated memory will be either zeroed or uninitialized.
143
147
let flags = if zeroed { HEAP_ZERO_MEMORY } else { 0 } ;
144
148
145
149
if layout. align ( ) <= MIN_ALIGN {
146
- // SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`.
147
150
// The returned pointer points to the start of an allocated block.
148
- unsafe { HeapAlloc ( heap , flags, layout. size ( ) ) as * mut u8 }
151
+ process_heap_alloc ( flags, layout. size ( ) ) as * mut u8
149
152
} else {
150
153
// Allocate extra padding in order to be able to satisfy the alignment.
151
154
let total = layout. align ( ) + layout. size ( ) ;
152
155
153
- // SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`.
154
- let ptr = unsafe { HeapAlloc ( heap, flags, total) as * mut u8 } ;
156
+ let ptr = process_heap_alloc ( flags, total) as * mut u8 ;
155
157
if ptr. is_null ( ) {
156
158
// Allocation has failed.
157
159
return ptr:: null_mut ( ) ;
0 commit comments