-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #123550 - GnomedDev:remove-initial-arc, r=Nilstrieb
Remove last rt::init allocation for thread info Removes the last allocation pre-main by just not storing anything in std::thread::Thread for the main thread. - The thread name can just be a hard coded literal, as was done in #123433. - The ThreadId is always the `1` value, so `ThreadId::new` now starts at `2` and can fabricate the `1` value when needed. - Storing Parker in a static that is initialized once at startup. This uses SyncUnsafeCell and MaybeUninit as this is quite performance critical and we don't need synchronization or to store a tag value and possibly leave in a panic. This also adds a UI test to make sure that allocations do not occur before main ever again. try-job: dist-x86_64-linux
- Loading branch information
Showing
6 changed files
with
151 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ | |
//! } | ||
//! | ||
//! fn main() { | ||
//! drop(String::from("An allocation")); | ||
//! assert!(unsafe { HIT }); | ||
//! } | ||
//! ``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//! Helper for 'no-allocation-before-main'. | ||
//! | ||
//! This also contains a meta-test to make sure that the AbortingAllocator does indeed abort. | ||
//! | ||
//! -Cprefer-dynamic=no is required as otherwise #[global_allocator] does nothing. | ||
//@ run-fail | ||
//@ compile-flags: -Cprefer-dynamic=no | ||
|
||
use std::{sync::atomic::{AtomicBool, Ordering}, alloc::System}; | ||
|
||
static ABORT: AtomicBool = AtomicBool::new(true); | ||
|
||
pub struct AbortingAllocator(System); | ||
|
||
unsafe impl std::alloc::GlobalAlloc for AbortingAllocator { | ||
unsafe fn alloc(&self, layout: std::alloc::Layout) -> *mut u8 { | ||
if ABORT.swap(false, Ordering::SeqCst) { | ||
println!("{}", std::backtrace::Backtrace::force_capture()); | ||
std::process::abort(); | ||
} | ||
|
||
self.0.alloc(layout) | ||
} | ||
|
||
unsafe fn dealloc(&self, ptr: *mut u8, layout: std::alloc::Layout) { | ||
if ABORT.swap(false, Ordering::SeqCst) { | ||
println!("{}", std::backtrace::Backtrace::force_capture()); | ||
std::process::abort(); | ||
} | ||
|
||
self.0.dealloc(ptr, layout) | ||
} | ||
} | ||
|
||
#[global_allocator] | ||
static ALLOCATOR: AbortingAllocator = AbortingAllocator(System); | ||
|
||
fn main() { | ||
std::hint::black_box(String::from("An allocation")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//! Tests that a program with no body does not allocate. | ||
//! | ||
//! The initial runtime should not allocate for performance/binary size reasons. | ||
//! | ||
//! -Cprefer-dynamic=no is required as otherwise #[global_allocator] does nothing. | ||
//! We only test linux-gnu as other targets currently need allocation for thread dtors. | ||
//@ run-pass | ||
//@ compile-flags: -Cprefer-dynamic=no -Cdebuginfo=full | ||
//@ only-linux | ||
//@ only-gnu | ||
|
||
#[allow(dead_code)] | ||
#[path = "aborting-alloc.rs"] | ||
mod aux; | ||
|
||
fn main() {} |