Skip to content

Commit 6a495f7

Browse files
committed
Auto merge of #37492 - japaric:no-atomics-alloc, r=brson
make `alloc` and `collections` compilable for thumbv6m-none-eabi by cfging away `alloc::Arc` and changing OOM to abort for this target r? @alexcrichton cc @thejpster
2 parents 97bfead + abe6fc7 commit 6a495f7

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

src/liballoc/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474

7575
#![feature(allocator)]
7676
#![feature(box_syntax)]
77+
#![feature(cfg_target_has_atomic)]
7778
#![feature(coerce_unsized)]
7879
#![feature(const_fn)]
7980
#![feature(core_intrinsics)]
@@ -122,6 +123,7 @@ mod boxed {
122123
}
123124
#[cfg(test)]
124125
mod boxed_test;
126+
#[cfg(target_has_atomic = "ptr")]
125127
pub mod arc;
126128
pub mod rc;
127129
pub mod raw_vec;

src/liballoc/oom.rs

+34-15
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use core::sync::atomic::{AtomicPtr, Ordering};
12-
use core::mem;
11+
#[cfg(target_has_atomic = "ptr")]
12+
pub use self::imp::set_oom_handler;
1313
use core::intrinsics;
1414

15-
static OOM_HANDLER: AtomicPtr<()> = AtomicPtr::new(default_oom_handler as *mut ());
16-
1715
fn default_oom_handler() -> ! {
1816
// The default handler can't do much more since we can't assume the presence
1917
// of libc or any way of printing an error message.
@@ -26,17 +24,38 @@ fn default_oom_handler() -> ! {
2624
#[unstable(feature = "oom", reason = "not a scrutinized interface",
2725
issue = "27700")]
2826
pub fn oom() -> ! {
29-
let value = OOM_HANDLER.load(Ordering::SeqCst);
30-
let handler: fn() -> ! = unsafe { mem::transmute(value) };
31-
handler();
27+
self::imp::oom()
3228
}
3329

34-
/// Set a custom handler for out-of-memory conditions
35-
///
36-
/// To avoid recursive OOM failures, it is critical that the OOM handler does
37-
/// not allocate any memory itself.
38-
#[unstable(feature = "oom", reason = "not a scrutinized interface",
39-
issue = "27700")]
40-
pub fn set_oom_handler(handler: fn() -> !) {
41-
OOM_HANDLER.store(handler as *mut (), Ordering::SeqCst);
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+
}
54+
55+
#[cfg(not(target_has_atomic = "ptr"))]
56+
mod imp {
57+
#[inline(always)]
58+
pub fn oom() -> ! {
59+
super::default_oom_handler()
60+
}
4261
}

0 commit comments

Comments
 (0)