1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use prelude::v1::*;
use cmp;
use env;
use fmt;
use intrinsics;
use libc::{self, uintptr_t};
use os;
use slice;
use str;
use sync::atomic::{self, Ordering};
pub fn running_on_valgrind() -> bool {
extern {
fn rust_running_on_valgrind() -> uintptr_t;
}
unsafe { rust_running_on_valgrind() != 0 }
}
pub fn limit_thread_creation_due_to_osx_and_valgrind() -> bool {
(cfg!(target_os="macos")) && running_on_valgrind()
}
pub fn min_stack() -> uint {
static MIN: atomic::AtomicUsize = atomic::ATOMIC_USIZE_INIT;
match MIN.load(Ordering::SeqCst) {
0 => {}
n => return n - 1,
}
let amt = env::var_string("RUST_MIN_STACK").ok().and_then(|s| s.parse().ok());
let amt = amt.unwrap_or(2 * 1024 * 1024);
MIN.store(amt + 1, Ordering::SeqCst);
return amt;
}
pub fn default_sched_threads() -> uint {
match env::var_string("RUST_THREADS") {
Ok(nstr) => {
let opt_n: Option<uint> = nstr.parse().ok();
match opt_n {
Some(n) if n > 0 => n,
_ => panic!("`RUST_THREADS` is `{}`, should be a positive integer", nstr)
}
}
Err(..) => {
if limit_thread_creation_due_to_osx_and_valgrind() {
1
} else {
os::num_cpus()
}
}
}
}
pub const ENFORCE_SANITY: bool = true || !cfg!(rtopt) || cfg!(rtdebug) ||
cfg!(rtassert);
#[allow(missing_copy_implementations)]
pub struct Stdio(libc::c_int);
#[allow(non_upper_case_globals)]
pub const Stdout: Stdio = Stdio(libc::STDOUT_FILENO);
#[allow(non_upper_case_globals)]
pub const Stderr: Stdio = Stdio(libc::STDERR_FILENO);
impl Stdio {
pub fn write_bytes(&mut self, data: &[u8]) {
#[cfg(unix)]
type WriteLen = libc::size_t;
#[cfg(windows)]
type WriteLen = libc::c_uint;
unsafe {
let Stdio(fd) = *self;
libc::write(fd,
data.as_ptr() as *const libc::c_void,
data.len() as WriteLen);
}
}
}
impl fmt::Writer for Stdio {
fn write_str(&mut self, data: &str) -> fmt::Result {
self.write_bytes(data.as_bytes());
Ok(())
}
}
pub fn dumb_print(args: fmt::Arguments) {
let _ = Stderr.write_fmt(args);
}
pub fn abort(args: fmt::Arguments) -> ! {
use fmt::Writer;
struct BufWriter<'a> {
buf: &'a mut [u8],
pos: uint,
}
impl<'a> fmt::Writer for BufWriter<'a> {
fn write_str(&mut self, bytes: &str) -> fmt::Result {
let left = &mut self.buf[self.pos..];
let to_write = &bytes.as_bytes()[..cmp::min(bytes.len(), left.len())];
slice::bytes::copy_memory(left, to_write);
self.pos += to_write.len();
Ok(())
}
}
let mut msg = [0u8; 512];
let mut w = BufWriter { buf: &mut msg, pos: 0 };
let _ = write!(&mut w, "{}", args);
let msg = str::from_utf8(&w.buf[..w.pos]).unwrap_or("aborted");
let msg = if msg.is_empty() {"aborted"} else {msg};
rterrln!("fatal runtime error: {}", msg);
unsafe { intrinsics::abort(); }
}
pub unsafe fn report_overflow() {
use thread::Thread;
rterrln!("\nthread '{}' has overflowed its stack",
Thread::current().name().unwrap_or("<unknown>"));
}