Skip to content

Commit 24dc2bc

Browse files
author
Jorge Aparicio
committed
use an 'imp' module to reduce the amount of cfgs
1 parent 907d2a1 commit 24dc2bc

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

src/liballoc/oom.rs

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,54 @@
99
// except according to those terms.
1010

1111
#[cfg(target_has_atomic = "ptr")]
12-
use core::sync::atomic::{AtomicPtr, Ordering};
13-
#[cfg(target_has_atomic = "ptr")]
14-
use core::mem;
12+
pub use self::imp::set_oom_handler;
1513
use core::intrinsics;
1614

17-
#[cfg(target_has_atomic = "ptr")]
18-
static OOM_HANDLER: AtomicPtr<()> = AtomicPtr::new(default_oom_handler as *mut ());
19-
2015
fn default_oom_handler() -> ! {
2116
// The default handler can't do much more since we can't assume the presence
2217
// of libc or any way of printing an error message.
2318
unsafe { intrinsics::abort() }
2419
}
2520

2621
/// Common out-of-memory routine
27-
#[cfg(target_has_atomic = "ptr")]
2822
#[cold]
2923
#[inline(never)]
3024
#[unstable(feature = "oom", reason = "not a scrutinized interface",
3125
issue = "27700")]
3226
pub fn oom() -> ! {
33-
let value = OOM_HANDLER.load(Ordering::SeqCst);
34-
let handler: fn() -> ! = unsafe { mem::transmute(value) };
35-
handler();
27+
self::imp::oom()
3628
}
3729

38-
/// Common out-of-memory routine
39-
#[cfg(not(target_has_atomic = "ptr"))]
40-
#[cold]
41-
#[inline(never)]
42-
#[unstable(feature = "oom", reason = "not a scrutinized interface",
43-
issue = "27700")]
44-
pub fn oom() -> ! {
45-
default_oom_handler()
30+
#[cfg(target_has_atomic = "ptr")]
31+
mod imp {
32+
use core::mem;
33+
use core::sync::atomic::{AtomicPtr, Ordering};
34+
35+
static OOM_HANDLER: AtomicPtr<()> = AtomicPtr::new(super::default_oom_handler as *mut ());
36+
37+
#[inline(always)]
38+
pub fn oom() -> ! {
39+
let value = OOM_HANDLER.load(Ordering::SeqCst);
40+
let handler: fn() -> ! = unsafe { mem::transmute(value) };
41+
handler();
42+
}
43+
44+
/// Set a custom handler for out-of-memory conditions
45+
///
46+
/// To avoid recursive OOM failures, it is critical that the OOM handler does
47+
/// not allocate any memory itself.
48+
#[unstable(feature = "oom", reason = "not a scrutinized interface",
49+
issue = "27700")]
50+
pub fn set_oom_handler(handler: fn() -> !) {
51+
OOM_HANDLER.store(handler as *mut (), Ordering::SeqCst);
52+
}
53+
4654
}
4755

48-
/// Set a custom handler for out-of-memory conditions
49-
///
50-
/// To avoid recursive OOM failures, it is critical that the OOM handler does
51-
/// not allocate any memory itself.
52-
#[cfg(target_has_atomic = "ptr")]
53-
#[unstable(feature = "oom", reason = "not a scrutinized interface",
54-
issue = "27700")]
55-
pub fn set_oom_handler(handler: fn() -> !) {
56-
OOM_HANDLER.store(handler as *mut (), Ordering::SeqCst);
56+
#[cfg(not(target_has_atomic = "ptr"))]
57+
mod imp {
58+
#[inline(always)]
59+
pub fn oom() -> ! {
60+
super::default_oom_handler()
61+
}
5762
}

0 commit comments

Comments
 (0)