-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UI test to ensure no more allocations pre-main
- Loading branch information
Showing
2 changed files
with
39 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//! 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 | ||
|
||
pub struct AbortingAllocator; | ||
|
||
unsafe impl std::alloc::GlobalAlloc for AbortingAllocator { | ||
unsafe fn alloc(&self, _: std::alloc::Layout) -> *mut u8 { | ||
std::process::abort() | ||
} | ||
|
||
unsafe fn dealloc(&self, _: *mut u8, _: std::alloc::Layout) { | ||
std::process::abort() | ||
} | ||
} | ||
|
||
#[global_allocator] | ||
static ALLOCATOR: AbortingAllocator = AbortingAllocator; | ||
|
||
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,13 @@ | ||
//! 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. | ||
//@ run-pass | ||
//@ compile-flags: -Cprefer-dynamic=no | ||
|
||
#[allow(dead_code)] | ||
#[path = "aborting-alloc.rs"] | ||
mod aux; | ||
|
||
fn main() {} |