1 // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #[cfg(target_has_atomic = "ptr")]
12 pub use self::imp::set_oom_handler;
15 fn default_oom_handler() -> ! {
16 // The default handler can't do much more since we can't assume the presence
17 // of libc or any way of printing an error message.
18 unsafe { intrinsics::abort() }
21 /// Common out-of-memory routine
24 #[unstable(feature = "oom", reason = "not a scrutinized interface",
30 #[cfg(target_has_atomic = "ptr")]
33 use core::sync::atomic::{AtomicPtr, Ordering};
35 static OOM_HANDLER: AtomicPtr<()> = AtomicPtr::new(super::default_oom_handler as *mut ());
39 let value = OOM_HANDLER.load(Ordering::SeqCst);
40 let handler: fn() -> ! = unsafe { mem::transmute(value) };
44 /// Set a custom handler for out-of-memory conditions
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",
50 pub fn set_oom_handler(handler: fn() -> !) {
51 OOM_HANDLER.store(handler as *mut (), Ordering::SeqCst);
55 #[cfg(not(target_has_atomic = "ptr"))]
59 super::default_oom_handler()