Skip to content

Commit f913231

Browse files
committed
Auto merge of #51264 - glandium:oom, r=alexcrichton
Make the OOM hook return `()` rather than `!` Per discussion in #51245 (comment) This allows more flexibility in what can be done with the API. This also splits `rtabort!` into `dumb_print` happening in the default hook and `abort_internal`, happening in the actual oom handler after calling the hook. Registering an empty function thus makes the oom handler not print anything but still abort. Cc: @alexcrichton
2 parents 577a5b2 + b945be7 commit f913231

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/libstd/alloc.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,22 @@
1919

2020
use core::sync::atomic::{AtomicPtr, Ordering};
2121
use core::{mem, ptr};
22+
use sys_common::util::dumb_print;
2223

2324
static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());
2425

2526
/// Registers a custom OOM hook, replacing any that was previously registered.
2627
///
27-
/// The OOM hook is invoked when an infallible memory allocation fails.
28-
/// The default hook prints a message to standard error and aborts the
29-
/// execution, but this behavior can be customized with the [`set_oom_hook`]
30-
/// and [`take_oom_hook`] functions.
28+
/// The OOM hook is invoked when an infallible memory allocation fails, before
29+
/// the runtime aborts. The default hook prints a message to standard error,
30+
/// but this behavior can be customized with the [`set_oom_hook`] and
31+
/// [`take_oom_hook`] functions.
3132
///
3233
/// The hook is provided with a `Layout` struct which contains information
3334
/// about the allocation that failed.
3435
///
3536
/// The OOM hook is a global resource.
36-
pub fn set_oom_hook(hook: fn(Layout) -> !) {
37+
pub fn set_oom_hook(hook: fn(Layout)) {
3738
HOOK.store(hook as *mut (), Ordering::SeqCst);
3839
}
3940

@@ -42,7 +43,7 @@ pub fn set_oom_hook(hook: fn(Layout) -> !) {
4243
/// *See also the function [`set_oom_hook`].*
4344
///
4445
/// If no custom hook is registered, the default hook will be returned.
45-
pub fn take_oom_hook() -> fn(Layout) -> ! {
46+
pub fn take_oom_hook() -> fn(Layout) {
4647
let hook = HOOK.swap(ptr::null_mut(), Ordering::SeqCst);
4748
if hook.is_null() {
4849
default_oom_hook
@@ -51,21 +52,22 @@ pub fn take_oom_hook() -> fn(Layout) -> ! {
5152
}
5253
}
5354

54-
fn default_oom_hook(layout: Layout) -> ! {
55-
rtabort!("memory allocation of {} bytes failed", layout.size())
55+
fn default_oom_hook(layout: Layout) {
56+
dumb_print(format_args!("memory allocation of {} bytes failed", layout.size()));
5657
}
5758

5859
#[cfg(not(test))]
5960
#[doc(hidden)]
6061
#[lang = "oom"]
6162
pub extern fn rust_oom(layout: Layout) -> ! {
6263
let hook = HOOK.load(Ordering::SeqCst);
63-
let hook: fn(Layout) -> ! = if hook.is_null() {
64+
let hook: fn(Layout) = if hook.is_null() {
6465
default_oom_hook
6566
} else {
6667
unsafe { mem::transmute(hook) }
6768
};
68-
hook(layout)
69+
hook(layout);
70+
unsafe { ::sys::abort_internal(); }
6971
}
7072

7173
#[cfg(not(test))]

0 commit comments

Comments
 (0)