@@ -89,30 +89,12 @@ extern "system" {
89
89
// Either a non-null handle returned by `GetProcessHeap`, or null when not yet initialized or `GetProcessHeap` failed.
90
90
static HEAP : AtomicPtr < c_void > = AtomicPtr :: new ( ptr:: null_mut ( ) ) ;
91
91
92
- // Get a handle to the default heap of the current process, or null if the operation fails.
93
- // If this operation is successful, `HEAP` will be successfully initialized and contain
94
- // a non-null handle returned by `GetProcessHeap`.
95
- #[ inline]
96
- fn init_or_get_process_heap ( ) -> c:: HANDLE {
97
- let heap = HEAP . load ( Ordering :: Relaxed ) ;
98
- if heap. is_null ( ) {
99
- // `HEAP` has not yet been successfully initialized
100
- let heap = unsafe { GetProcessHeap ( ) } ;
101
- if !heap. is_null ( ) {
102
- // SAFETY: No locking is needed because within the same process,
103
- // successful calls to `GetProcessHeap` will always return the same value, even on different threads.
104
- HEAP . store ( heap, Ordering :: Release ) ;
105
-
106
- // SAFETY: `HEAP` contains a non-null handle returned by `GetProcessHeap`
107
- heap
108
- } else {
109
- // Could not get the current process heap.
110
- ptr:: null_mut ( )
111
- }
112
- } else {
113
- // SAFETY: `HEAP` contains a non-null handle returned by `GetProcessHeap`
114
- heap
115
- }
92
+ // Initialize `HEAP` when the app starts.
93
+ pub fn init ( ) {
94
+ let heap = unsafe { GetProcessHeap ( ) } ;
95
+ // SAFETY: No locking is needed because within the same process,
96
+ // successful calls to `GetProcessHeap` will always return the same value, even on different threads.
97
+ HEAP . store ( heap, Ordering :: Release ) ;
116
98
}
117
99
118
100
// Get a non-null handle to the default heap of the current process.
@@ -133,7 +115,8 @@ struct Header(*mut u8);
133
115
// initialized.
134
116
#[ inline]
135
117
unsafe fn allocate ( layout : Layout , zeroed : bool ) -> * mut u8 {
136
- let heap = init_or_get_process_heap ( ) ;
118
+ // SAFETY: Check the pointer here.
119
+ let heap = unsafe { get_process_heap ( ) } ;
137
120
if heap. is_null ( ) {
138
121
// Allocation has failed, could not get the current process heap.
139
122
return ptr:: null_mut ( ) ;
0 commit comments