]> git.lizzy.rs Git - rust.git/blob - src/libcore/sys.rs
8cad0a22886424be926e7f213d0474e12e691b41
[rust.git] / src / libcore / sys.rs
1 // Copyright 2012 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.
4 //
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.
10
11 //! Misc low level stuff
12
13 use cast;
14 use cmp::{Eq, Ord};
15 use gc;
16 use io;
17 use libc;
18 use libc::{c_void, c_char, size_t};
19 use repr;
20 use str;
21
22 pub type FreeGlue<'self> = &'self fn(*TypeDesc, *c_void);
23
24 // Corresponds to runtime type_desc type
25 pub struct TypeDesc {
26     size: uint,
27     align: uint,
28     take_glue: uint,
29     drop_glue: uint,
30     free_glue: uint
31     // Remaining fields not listed
32 }
33
34 /// The representation of a Rust closure
35 pub struct Closure {
36     code: *(),
37     env: *(),
38 }
39
40 pub mod rusti {
41     #[abi = "rust-intrinsic"]
42     pub extern "rust-intrinsic" {
43         fn get_tydesc<T>() -> *();
44         fn size_of<T>() -> uint;
45         fn pref_align_of<T>() -> uint;
46         fn min_align_of<T>() -> uint;
47     }
48 }
49
50 pub mod rustrt {
51     use libc::{c_char, size_t};
52
53     pub extern {
54         #[rust_stack]
55         unsafe fn rust_upcall_fail(expr: *c_char,
56                                    file: *c_char,
57                                    line: size_t);
58     }
59 }
60
61 /// Compares contents of two pointers using the default method.
62 /// Equivalent to `*x1 == *x2`.  Useful for hashtables.
63 pub fn shape_eq<T:Eq>(x1: &T, x2: &T) -> bool {
64     *x1 == *x2
65 }
66
67 pub fn shape_lt<T:Ord>(x1: &T, x2: &T) -> bool {
68     *x1 < *x2
69 }
70
71 pub fn shape_le<T:Ord>(x1: &T, x2: &T) -> bool {
72     *x1 <= *x2
73 }
74
75 /**
76  * Returns a pointer to a type descriptor.
77  *
78  * Useful for calling certain function in the Rust runtime or otherwise
79  * performing dark magick.
80  */
81 #[inline(always)]
82 pub fn get_type_desc<T>() -> *TypeDesc {
83     unsafe { rusti::get_tydesc::<T>() as *TypeDesc }
84 }
85
86 /// Returns a pointer to a type descriptor.
87 #[inline(always)]
88 pub fn get_type_desc_val<T>(_val: &T) -> *TypeDesc {
89     get_type_desc::<T>()
90 }
91
92 /// Returns the size of a type
93 #[inline(always)]
94 pub fn size_of<T>() -> uint {
95     unsafe { rusti::size_of::<T>() }
96 }
97
98 /// Returns the size of the type that `_val` points to
99 #[inline(always)]
100 pub fn size_of_val<T>(_val: &T) -> uint {
101     size_of::<T>()
102 }
103
104 /**
105  * Returns the size of a type, or 1 if the actual size is zero.
106  *
107  * Useful for building structures containing variable-length arrays.
108  */
109 #[inline(always)]
110 pub fn nonzero_size_of<T>() -> uint {
111     let s = size_of::<T>();
112     if s == 0 { 1 } else { s }
113 }
114
115 /// Returns the size of the type of the value that `_val` points to
116 #[inline(always)]
117 pub fn nonzero_size_of_val<T>(_val: &T) -> uint {
118     nonzero_size_of::<T>()
119 }
120
121
122 /**
123  * Returns the ABI-required minimum alignment of a type
124  *
125  * This is the alignment used for struct fields. It may be smaller
126  * than the preferred alignment.
127  */
128 #[inline(always)]
129 pub fn min_align_of<T>() -> uint {
130     unsafe { rusti::min_align_of::<T>() }
131 }
132
133 /// Returns the ABI-required minimum alignment of the type of the value that
134 /// `_val` points to
135 #[inline(always)]
136 pub fn min_align_of_val<T>(_val: &T) -> uint {
137     min_align_of::<T>()
138 }
139
140 /// Returns the preferred alignment of a type
141 #[inline(always)]
142 pub fn pref_align_of<T>() -> uint {
143     unsafe { rusti::pref_align_of::<T>() }
144 }
145
146 /// Returns the preferred alignment of the type of the value that
147 /// `_val` points to
148 #[inline(always)]
149 pub fn pref_align_of_val<T>(_val: &T) -> uint {
150     pref_align_of::<T>()
151 }
152
153 /// Returns the refcount of a shared box (as just before calling this)
154 #[inline(always)]
155 pub fn refcount<T>(t: @T) -> uint {
156     unsafe {
157         let ref_ptr: *uint = cast::transmute_copy(&t);
158         *ref_ptr - 1
159     }
160 }
161
162 pub fn log_str<T>(t: &T) -> ~str {
163     do io::with_str_writer |wr| {
164         repr::write_repr(wr, t)
165     }
166 }
167
168 /// Trait for initiating task failure.
169 pub trait FailWithCause {
170     /// Fail the current task, taking ownership of `cause`
171     fn fail_with(cause: Self, file: &'static str, line: uint) -> !;
172 }
173
174 impl FailWithCause for ~str {
175     fn fail_with(cause: ~str, file: &'static str, line: uint) -> ! {
176         do str::as_buf(cause) |msg_buf, _msg_len| {
177             do str::as_buf(file) |file_buf, _file_len| {
178                 unsafe {
179                     let msg_buf = cast::transmute(msg_buf);
180                     let file_buf = cast::transmute(file_buf);
181                     begin_unwind_(msg_buf, file_buf, line as libc::size_t)
182                 }
183             }
184         }
185     }
186 }
187
188 impl FailWithCause for &'static str {
189     fn fail_with(cause: &'static str, file: &'static str, line: uint) -> ! {
190         do str::as_buf(cause) |msg_buf, _msg_len| {
191             do str::as_buf(file) |file_buf, _file_len| {
192                 unsafe {
193                     let msg_buf = cast::transmute(msg_buf);
194                     let file_buf = cast::transmute(file_buf);
195                     begin_unwind_(msg_buf, file_buf, line as libc::size_t)
196                 }
197             }
198         }
199     }
200 }
201
202 // NOTE: remove function after snapshot
203 #[cfg(stage0)]
204 pub fn begin_unwind(msg: ~str, file: ~str, line: uint) -> ! {
205     do str::as_buf(msg) |msg_buf, _msg_len| {
206         do str::as_buf(file) |file_buf, _file_len| {
207             unsafe {
208                 let msg_buf = cast::transmute(msg_buf);
209                 let file_buf = cast::transmute(file_buf);
210                 begin_unwind_(msg_buf, file_buf, line as libc::size_t)
211             }
212         }
213     }
214 }
215
216 // FIXME #4427: Temporary until rt::rt_fail_ goes away
217 pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
218     unsafe {
219         gc::cleanup_stack_for_failure();
220         rustrt::rust_upcall_fail(msg, file, line);
221         cast::transmute(())
222     }
223 }
224
225 // NOTE: remove function after snapshot
226 #[cfg(stage0)]
227 pub fn fail_assert(msg: &str, file: &str, line: uint) -> ! {
228     let (msg, file) = (msg.to_owned(), file.to_owned());
229     begin_unwind(~"assertion failed: " + msg, file, line)
230 }
231
232 #[cfg(test)]
233 mod tests {
234     use cast;
235     use sys::*;
236
237     #[test]
238     fn size_of_basic() {
239         assert!(size_of::<u8>() == 1u);
240         assert!(size_of::<u16>() == 2u);
241         assert!(size_of::<u32>() == 4u);
242         assert!(size_of::<u64>() == 8u);
243     }
244
245     #[test]
246     #[cfg(target_arch = "x86")]
247     #[cfg(target_arch = "arm")]
248     #[cfg(target_arch = "mips")]
249     fn size_of_32() {
250         assert!(size_of::<uint>() == 4u);
251         assert!(size_of::<*uint>() == 4u);
252     }
253
254     #[test]
255     #[cfg(target_arch = "x86_64")]
256     fn size_of_64() {
257         assert!(size_of::<uint>() == 8u);
258         assert!(size_of::<*uint>() == 8u);
259     }
260
261     #[test]
262     fn size_of_val_basic() {
263         assert_eq!(size_of_val(&1u8), 1);
264         assert_eq!(size_of_val(&1u16), 2);
265         assert_eq!(size_of_val(&1u32), 4);
266         assert_eq!(size_of_val(&1u64), 8);
267     }
268
269     #[test]
270     fn nonzero_size_of_basic() {
271         type Z = [i8, ..0];
272         assert!(size_of::<Z>() == 0u);
273         assert!(nonzero_size_of::<Z>() == 1u);
274         assert!(nonzero_size_of::<uint>() == size_of::<uint>());
275     }
276
277     #[test]
278     fn nonzero_size_of_val_basic() {
279         let z = [0u8, ..0];
280         assert_eq!(size_of_val(&z), 0u);
281         assert_eq!(nonzero_size_of_val(&z), 1u);
282         assert_eq!(nonzero_size_of_val(&1u), size_of_val(&1u));
283     }
284
285     #[test]
286     fn align_of_basic() {
287         assert!(pref_align_of::<u8>() == 1u);
288         assert!(pref_align_of::<u16>() == 2u);
289         assert!(pref_align_of::<u32>() == 4u);
290     }
291
292     #[test]
293     #[cfg(target_arch = "x86")]
294     #[cfg(target_arch = "arm")]
295     #[cfg(target_arch = "mips")]
296     fn align_of_32() {
297         assert!(pref_align_of::<uint>() == 4u);
298         assert!(pref_align_of::<*uint>() == 4u);
299     }
300
301     #[test]
302     #[cfg(target_arch = "x86_64")]
303     fn align_of_64() {
304         assert!(pref_align_of::<uint>() == 8u);
305         assert!(pref_align_of::<*uint>() == 8u);
306     }
307
308     #[test]
309     fn align_of_val_basic() {
310         assert_eq!(pref_align_of_val(&1u8), 1u);
311         assert_eq!(pref_align_of_val(&1u16), 2u);
312         assert_eq!(pref_align_of_val(&1u32), 4u);
313     }
314
315     #[test]
316     fn synthesize_closure() {
317         unsafe {
318             let x = 10;
319             let f: &fn(int) -> int = |y| x + y;
320
321             assert!(f(20) == 30);
322
323             let original_closure: Closure = cast::transmute(f);
324
325             let actual_function_pointer = original_closure.code;
326             let environment = original_closure.env;
327
328             let new_closure = Closure {
329                 code: actual_function_pointer,
330                 env: environment
331             };
332
333             let new_f: &fn(int) -> int = cast::transmute(new_closure);
334             assert!(new_f(20) == 30);
335         }
336     }
337
338     #[test]
339     #[should_fail]
340     fn fail_static() { FailWithCause::fail_with("cause", file!(), line!())  }
341
342     #[test]
343     #[should_fail]
344     fn fail_owned() { FailWithCause::fail_with(~"cause", file!(), line!())  }
345 }
346
347 // Local Variables:
348 // mode: rust;
349 // fill-column: 78;
350 // indent-tabs-mode: nil
351 // c-basic-offset: 4
352 // buffer-file-coding-system: utf-8-unix
353 // End: