Skip to content

Commit 3e25d5a

Browse files
committed
Use cfg(bootstrap) as necessary
1 parent abd406c commit 3e25d5a

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

library/alloc/src/alloc.rs

+24
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ pub use std::alloc::handle_alloc_error;
421421
#[doc(hidden)]
422422
#[allow(unused_attributes)]
423423
#[unstable(feature = "alloc_internals", issue = "none")]
424+
#[cfg(not(bootstrap))]
424425
pub mod __alloc_error_handler {
425426
#[rustc_std_internal_symbol]
426427
#[linkage = "weak"]
@@ -444,3 +445,26 @@ pub mod __alloc_error_handler {
444445
}
445446
}
446447
}
448+
449+
#[cfg(all(not(no_global_oom_handling), not(test)))]
450+
#[doc(hidden)]
451+
#[allow(unused_attributes)]
452+
#[unstable(feature = "alloc_internals", issue = "none")]
453+
#[cfg(bootstrap)]
454+
pub mod __alloc_error_handler {
455+
#[rustc_std_internal_symbol]
456+
pub unsafe fn __rdl_oom(size: usize, _align: usize) -> ! {
457+
extern "Rust" {
458+
static __rust_alloc_error_handler_should_panic: u8;
459+
}
460+
461+
if unsafe { __rust_alloc_error_handler_should_panic != 0 } {
462+
panic!("memory allocation of {size} bytes failed")
463+
} else {
464+
core::panicking::panic_nounwind_fmt(
465+
format_args!("memory allocation of {size} bytes failed"),
466+
/* force_no_backtrace */ false,
467+
)
468+
}
469+
}
470+
}

library/std/src/alloc.rs

+52
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ pub fn rust_oom(layout: Layout) -> ! {
381381
#[doc(hidden)]
382382
#[allow(unused_attributes)]
383383
#[unstable(feature = "alloc_internals", issue = "none")]
384+
#[cfg(not(bootstrap))]
384385
pub mod __default_lib_allocator {
385386
use super::{GlobalAlloc, Layout, System};
386387
// These are used as a fallback for implementing the `__rust_alloc`, etc symbols
@@ -437,3 +438,54 @@ pub mod __default_lib_allocator {
437438
}
438439
}
439440
}
441+
442+
#[cfg(not(test))]
443+
#[doc(hidden)]
444+
#[allow(unused_attributes)]
445+
#[unstable(feature = "alloc_internals", issue = "none")]
446+
#[cfg(bootstrap)]
447+
pub mod __default_lib_allocator {
448+
use super::{GlobalAlloc, Layout, System};
449+
450+
#[rustc_std_internal_symbol]
451+
pub unsafe extern "C" fn __rdl_alloc(size: usize, align: usize) -> *mut u8 {
452+
// SAFETY: see the guarantees expected by `Layout::from_size_align` and
453+
// `GlobalAlloc::alloc`.
454+
unsafe {
455+
let layout = Layout::from_size_align_unchecked(size, align);
456+
System.alloc(layout)
457+
}
458+
}
459+
460+
#[rustc_std_internal_symbol]
461+
pub unsafe extern "C" fn __rdl_dealloc(ptr: *mut u8, size: usize, align: usize) {
462+
// SAFETY: see the guarantees expected by `Layout::from_size_align` and
463+
// `GlobalAlloc::dealloc`.
464+
unsafe { System.dealloc(ptr, Layout::from_size_align_unchecked(size, align)) }
465+
}
466+
467+
#[rustc_std_internal_symbol]
468+
pub unsafe extern "C" fn __rdl_realloc(
469+
ptr: *mut u8,
470+
old_size: usize,
471+
align: usize,
472+
new_size: usize,
473+
) -> *mut u8 {
474+
// SAFETY: see the guarantees expected by `Layout::from_size_align` and
475+
// `GlobalAlloc::realloc`.
476+
unsafe {
477+
let old_layout = Layout::from_size_align_unchecked(old_size, align);
478+
System.realloc(ptr, old_layout, new_size)
479+
}
480+
}
481+
482+
#[rustc_std_internal_symbol]
483+
pub unsafe extern "C" fn __rdl_alloc_zeroed(size: usize, align: usize) -> *mut u8 {
484+
// SAFETY: see the guarantees expected by `Layout::from_size_align` and
485+
// `GlobalAlloc::alloc_zeroed`.
486+
unsafe {
487+
let layout = Layout::from_size_align_unchecked(size, align);
488+
System.alloc_zeroed(layout)
489+
}
490+
}
491+
}

0 commit comments

Comments
 (0)