sys.rs 7.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

11
//! Misc low level stuff
12

13 14
#[allow(missing_doc)];

15
use c_str::ToCStr;
16 17
use cast;
use libc;
P
Philipp Brüschweiler 已提交
18
use libc::{c_char, size_t};
19 20
use repr;
use str;
21
use unstable::intrinsics;
22

23
/// Returns the size of a type
24
#[inline]
25
pub fn size_of<T>() -> uint {
26
    unsafe { intrinsics::size_of::<T>() }
27 28
}

29
/// Returns the size of the type that `_val` points to
30
#[inline]
31 32 33 34
pub fn size_of_val<T>(_val: &T) -> uint {
    size_of::<T>()
}

35 36 37 38 39
/**
 * Returns the size of a type, or 1 if the actual size is zero.
 *
 * Useful for building structures containing variable-length arrays.
 */
40
#[inline]
41
pub fn nonzero_size_of<T>() -> uint {
42 43 44 45
    let s = size_of::<T>();
    if s == 0 { 1 } else { s }
}

46
/// Returns the size of the type of the value that `_val` points to
47
#[inline]
48 49 50 51 52
pub fn nonzero_size_of_val<T>(_val: &T) -> uint {
    nonzero_size_of::<T>()
}


53 54 55 56 57 58
/**
 * Returns the ABI-required minimum alignment of a type
 *
 * This is the alignment used for struct fields. It may be smaller
 * than the preferred alignment.
 */
59
#[inline]
60
pub fn min_align_of<T>() -> uint {
61
    unsafe { intrinsics::min_align_of::<T>() }
62 63
}

64 65
/// Returns the ABI-required minimum alignment of the type of the value that
/// `_val` points to
66
#[inline]
67 68 69 70
pub fn min_align_of_val<T>(_val: &T) -> uint {
    min_align_of::<T>()
}

71
/// Returns the preferred alignment of a type
72
#[inline]
73
pub fn pref_align_of<T>() -> uint {
74
    unsafe { intrinsics::pref_align_of::<T>() }
75 76
}

77 78
/// Returns the preferred alignment of the type of the value that
/// `_val` points to
79
#[inline]
80 81 82 83
pub fn pref_align_of_val<T>(_val: &T) -> uint {
    pref_align_of::<T>()
}

84
/// Returns the refcount of a shared box (as just before calling this)
85
#[inline]
86
pub fn refcount<T>(t: @T) -> uint {
87
    unsafe {
P
Patrick Walton 已提交
88
        let ref_ptr: *uint = cast::transmute_copy(&t);
89
        *ref_ptr - 1
90
    }
91 92
}

93
pub fn log_str<T>(t: &T) -> ~str {
94 95 96 97 98
    use rt::io;
    use rt::io::Decorator;

    let mut result = io::mem::MemWriter::new();
    repr::write_repr(&mut result as &mut io::Writer, t);
99
    str::from_utf8_owned(result.inner())
100
}
101

102 103 104 105 106 107 108 109
/// Trait for initiating task failure.
pub trait FailWithCause {
    /// Fail the current task, taking ownership of `cause`
    fn fail_with(cause: Self, file: &'static str, line: uint) -> !;
}

impl FailWithCause for ~str {
    fn fail_with(cause: ~str, file: &'static str, line: uint) -> ! {
K
Kevin Ballard 已提交
110 111
        do cause.with_c_str |msg_buf| {
            do file.with_c_str |file_buf| {
112
                begin_unwind_(msg_buf, file_buf, line as libc::size_t)
113 114 115 116 117 118 119
            }
        }
    }
}

impl FailWithCause for &'static str {
    fn fail_with(cause: &'static str, file: &'static str, line: uint) -> ! {
K
Kevin Ballard 已提交
120 121
        do cause.with_c_str |msg_buf| {
            do file.with_c_str |file_buf| {
122
                begin_unwind_(msg_buf, file_buf, line as libc::size_t)
123 124 125 126 127
            }
        }
    }
}

128 129 130 131 132 133
// FIXME #4427: Temporary until rt::rt_fail_ goes away
pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
    use rt::in_green_task_context;
    use rt::task::Task;
    use rt::local::Local;
    use rt::logging::Logger;
B
Ben Blum 已提交
134
    use str::Str;
135

136 137 138 139 140 141
    unsafe {
        // XXX: Bad re-allocations. fail! needs some refactoring
        let msg = str::raw::from_c_str(msg);
        let file = str::raw::from_c_str(file);

        if in_green_task_context() {
142 143
            // Be careful not to allocate in this block, if we're failing we may
            // have been failing due to a lack of memory in the first place...
144
            do Local::borrow |task: &mut Task| {
145 146 147 148
                let n = task.name.map(|n| n.as_slice()).unwrap_or("<unnamed>");
                format_args!(|args| { task.logger.log(args) },
                             "task '{}' failed at '{}', {}:{}",
                             n, msg.as_slice(), file.as_slice(), line);
149
            }
150 151 152
        } else {
            rterrln!("failed in non-task context at '%s', %s:%i",
                     msg, file, line as int);
153
        }
154

155
        let task: *mut Task = Local::unsafe_borrow();
156 157
        if (*task).unwinder.unwinding {
            rtabort!("unwinding again");
158
        }
159
        (*task).unwinder.begin_unwind();
160 161 162
    }
}

163
#[cfg(test)]
164
mod tests {
165
    use cast;
166
    use sys::*;
167 168

    #[test]
169
    fn size_of_basic() {
170 171 172 173
        assert_eq!(size_of::<u8>(), 1u);
        assert_eq!(size_of::<u16>(), 2u);
        assert_eq!(size_of::<u32>(), 4u);
        assert_eq!(size_of::<u64>(), 8u);
174 175 176 177 178
    }

    #[test]
    #[cfg(target_arch = "x86")]
    #[cfg(target_arch = "arm")]
J
Jyun-Yan You 已提交
179
    #[cfg(target_arch = "mips")]
180
    fn size_of_32() {
181 182
        assert_eq!(size_of::<uint>(), 4u);
        assert_eq!(size_of::<*uint>(), 4u);
183 184 185 186
    }

    #[test]
    #[cfg(target_arch = "x86_64")]
187
    fn size_of_64() {
188 189
        assert_eq!(size_of::<uint>(), 8u);
        assert_eq!(size_of::<*uint>(), 8u);
190 191
    }

192 193 194 195 196 197 198 199
    #[test]
    fn size_of_val_basic() {
        assert_eq!(size_of_val(&1u8), 1);
        assert_eq!(size_of_val(&1u16), 2);
        assert_eq!(size_of_val(&1u32), 4);
        assert_eq!(size_of_val(&1u64), 8);
    }

200
    #[test]
201
    fn nonzero_size_of_basic() {
202
        type Z = [i8, ..0];
203 204 205
        assert_eq!(size_of::<Z>(), 0u);
        assert_eq!(nonzero_size_of::<Z>(), 1u);
        assert_eq!(nonzero_size_of::<uint>(), size_of::<uint>());
206 207
    }

208 209 210 211 212 213 214 215
    #[test]
    fn nonzero_size_of_val_basic() {
        let z = [0u8, ..0];
        assert_eq!(size_of_val(&z), 0u);
        assert_eq!(nonzero_size_of_val(&z), 1u);
        assert_eq!(nonzero_size_of_val(&1u), size_of_val(&1u));
    }

216
    #[test]
217
    fn align_of_basic() {
218 219 220
        assert_eq!(pref_align_of::<u8>(), 1u);
        assert_eq!(pref_align_of::<u16>(), 2u);
        assert_eq!(pref_align_of::<u32>(), 4u);
221 222 223 224 225
    }

    #[test]
    #[cfg(target_arch = "x86")]
    #[cfg(target_arch = "arm")]
J
Jyun-Yan You 已提交
226
    #[cfg(target_arch = "mips")]
227
    fn align_of_32() {
228 229
        assert_eq!(pref_align_of::<uint>(), 4u);
        assert_eq!(pref_align_of::<*uint>(), 4u);
230 231 232 233
    }

    #[test]
    #[cfg(target_arch = "x86_64")]
234
    fn align_of_64() {
235 236
        assert_eq!(pref_align_of::<uint>(), 8u);
        assert_eq!(pref_align_of::<*uint>(), 8u);
237
    }
B
Brian Anderson 已提交
238

239 240 241 242 243 244 245
    #[test]
    fn align_of_val_basic() {
        assert_eq!(pref_align_of_val(&1u8), 1u);
        assert_eq!(pref_align_of_val(&1u16), 2u);
        assert_eq!(pref_align_of_val(&1u32), 4u);
    }

B
Brian Anderson 已提交
246
    #[test]
247
    fn synthesize_closure() {
248
        use unstable::raw::Closure;
249 250
        unsafe {
            let x = 10;
251
            let f: &fn(int) -> int = |y| x + y;
B
Brian Anderson 已提交
252

253
            assert_eq!(f(20), 30);
B
Brian Anderson 已提交
254

L
Luqman Aden 已提交
255
            let original_closure: Closure = cast::transmute(f);
B
Brian Anderson 已提交
256

257 258
            let actual_function_pointer = original_closure.code;
            let environment = original_closure.env;
B
Brian Anderson 已提交
259

260 261 262 263
            let new_closure = Closure {
                code: actual_function_pointer,
                env: environment
            };
B
Brian Anderson 已提交
264

265
            let new_f: &fn(int) -> int = cast::transmute(new_closure);
266
            assert_eq!(new_f(20), 30);
267
        }
B
Brian Anderson 已提交
268
    }
269 270 271 272 273 274 275 276

    #[test]
    #[should_fail]
    fn fail_static() { FailWithCause::fail_with("cause", file!(), line!())  }

    #[test]
    #[should_fail]
    fn fail_owned() { FailWithCause::fail_with(~"cause", file!(), line!())  }
277
}