Skip to content

Commit 1b269b3

Browse files
committed
Auto merge of #86844 - bjorn3:global_alloc_improvements, r=pnkfelix
Support #[global_allocator] without the allocator shim This makes it possible to use liballoc/libstd in combination with `--emit obj` if you use `#[global_allocator]`. This is what rust-for-linux uses right now and systemd may use in the future. Currently they have to depend on the exact implementation of the allocator shim to create one themself as `--emit obj` doesn't create an allocator shim. Note that currently the allocator shim also defines the oom error handler, which is normally required too. Once `#![feature(default_alloc_error_handler)]` becomes the only option, this can be avoided. In addition when using only fallible allocator methods and either `--cfg no_global_oom_handling` for liballoc (like rust-for-linux) or `--gc-sections` no references to the oom error handler will exist. To avoid this feature being insta-stable, you will have to define `__rust_no_alloc_shim_is_unstable` to avoid linker errors. (Labeling this with both T-compiler and T-lang as it originally involved both an implementation detail and had an insta-stable user facing change. As noted above, the `__rust_no_alloc_shim_is_unstable` symbol requirement should prevent unintended dependence on this unstable feature.)
2 parents 683e52b + 1cec942 commit 1b269b3

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

alloc/src/alloc.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ extern "Rust" {
3737
#[rustc_allocator_zeroed]
3838
#[rustc_nounwind]
3939
fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
40+
41+
#[cfg(not(bootstrap))]
42+
static __rust_no_alloc_shim_is_unstable: u8;
4043
}
4144

4245
/// The global memory allocator.
@@ -90,7 +93,14 @@ pub use std::alloc::Global;
9093
#[must_use = "losing the pointer will leak memory"]
9194
#[inline]
9295
pub unsafe fn alloc(layout: Layout) -> *mut u8 {
93-
unsafe { __rust_alloc(layout.size(), layout.align()) }
96+
unsafe {
97+
// Make sure we don't accidentally allow omitting the allocator shim in
98+
// stable code until it is actually stabilized.
99+
#[cfg(not(bootstrap))]
100+
core::ptr::read_volatile(&__rust_no_alloc_shim_is_unstable);
101+
102+
__rust_alloc(layout.size(), layout.align())
103+
}
94104
}
95105

96106
/// Deallocate memory with the global allocator.

0 commit comments

Comments
 (0)