Skip to content

Commit 7c1548d

Browse files
committed
Place the alloc init in .CRT$XCB
1 parent 7c22d5e commit 7c1548d

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,16 @@ 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+
// This initializer will be executed before `main` or `DllMain`.
93+
//
94+
// The linker will place this initializer before any CRT initializers. However, user code should
95+
// use .CRT$XCU to store their initializers. See comments in `compat` mod.
96+
#[used]
97+
#[link_section = ".CRT$XCB"]
98+
static INIT_TABLE_ENTRY: unsafe extern "C" fn() = init;
99+
92100
// Initialize `HEAP` when the app starts.
93-
pub fn init() {
101+
unsafe extern "C" fn init() {
94102
let heap = unsafe { GetProcessHeap() };
95103
// SAFETY: No locking is needed because within the same process,
96104
// successful calls to `GetProcessHeap` will always return the same value, even on different threads.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ 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();
6261
stack_overflow::init();
6362

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

0 commit comments

Comments
 (0)