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 use core::sync::atomic::{AtomicPtr, Ordering};
15 static OOM_HANDLER: AtomicPtr<()> = AtomicPtr::new(default_oom_handler as *mut ());
17 fn default_oom_handler() -> ! {
18 // The default handler can't do much more since we can't assume the presence
19 // of libc or any way of printing an error message.
20 unsafe { intrinsics::abort() }
23 /// Common out-of-memory routine
26 #[unstable(feature = "oom", reason = "not a scrutinized interface",
29 let value = OOM_HANDLER.load(Ordering::SeqCst);
30 let handler: fn() -> ! = unsafe { mem::transmute(value) };
34 /// Set a custom handler for out-of-memory conditions
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",
40 pub fn set_oom_handler(handler: fn() -> !) {
41 OOM_HANDLER.store(handler as *mut (), Ordering::SeqCst);