Skip to content

Commit

Permalink
Wrap GetProcessHeap and make it never inline
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft committed Jan 22, 2024
1 parent d9d89fd commit 936e0fd
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions library/std/src/sys/pal/windows/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ static HEAP: AtomicPtr<c_void> = AtomicPtr::new(ptr::null_mut());
// a non-null handle returned by `GetProcessHeap`.
#[inline]
fn init_or_get_process_heap() -> c::HANDLE {
let heap = HEAP.load(Ordering::Relaxed);
if heap.is_null() {
// `HEAP` has not yet been successfully initialized
// The method is marked never inline to shrink the binary size. It won't be called many times.
#[cold]
#[inline(never)]
fn init_process_heap() -> c::HANDLE {
let heap = unsafe { GetProcessHeap() };
if !heap.is_null() {
// SAFETY: No locking is needed because within the same process,
Expand All @@ -109,6 +110,11 @@ fn init_or_get_process_heap() -> c::HANDLE {
// Could not get the current process heap.
ptr::null_mut()
}
}

let heap = HEAP.load(Ordering::Relaxed);
if heap.is_null() {
init_process_heap()
} else {
// SAFETY: `HEAP` contains a non-null handle returned by `GetProcessHeap`
heap
Expand Down

0 comments on commit 936e0fd

Please sign in to comment.