|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 | 11 | #[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; |
15 | 13 | use core::intrinsics;
|
16 | 14 |
|
17 |
| -#[cfg(target_has_atomic = "ptr")] |
18 |
| -static OOM_HANDLER: AtomicPtr<()> = AtomicPtr::new(default_oom_handler as *mut ()); |
19 |
| - |
20 | 15 | fn default_oom_handler() -> ! {
|
21 | 16 | // The default handler can't do much more since we can't assume the presence
|
22 | 17 | // of libc or any way of printing an error message.
|
23 | 18 | unsafe { intrinsics::abort() }
|
24 | 19 | }
|
25 | 20 |
|
26 | 21 | /// Common out-of-memory routine
|
27 |
| -#[cfg(target_has_atomic = "ptr")] |
28 | 22 | #[cold]
|
29 | 23 | #[inline(never)]
|
30 | 24 | #[unstable(feature = "oom", reason = "not a scrutinized interface",
|
31 | 25 | issue = "27700")]
|
32 | 26 | 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() |
36 | 28 | }
|
37 | 29 |
|
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 | + |
46 | 54 | }
|
47 | 55 |
|
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 | + } |
57 | 62 | }
|
0 commit comments