Skip to content

Commit 7c22d5e

Browse files
committed
std: init HEAP only once
1 parent d9d89fd commit 7c22d5e

File tree

2 files changed

+9
-25
lines changed

2 files changed

+9
-25
lines changed

library/std/src/sys/pal/windows/alloc.rs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -89,30 +89,12 @@ extern "system" {
8989
// Either a non-null handle returned by `GetProcessHeap`, or null when not yet initialized or `GetProcessHeap` failed.
9090
static HEAP: AtomicPtr<c_void> = AtomicPtr::new(ptr::null_mut());
9191

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);
11698
}
11799

118100
// Get a non-null handle to the default heap of the current process.
@@ -133,7 +115,8 @@ struct Header(*mut u8);
133115
// initialized.
134116
#[inline]
135117
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() };
137120
if heap.is_null() {
138121
// Allocation has failed, could not get the current process heap.
139122
return ptr::null_mut();

library/std/src/sys/pal/windows/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ impl<T> IoResult<T> for Result<T, api::WinError> {
5858
// SAFETY: must be called only once during runtime initialization.
5959
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
6060
pub unsafe fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) {
61+
alloc::init();
6162
stack_overflow::init();
6263

6364
// Normally, `thread::spawn` will call `Thread::set_name` but since this thread already

0 commit comments

Comments
 (0)