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.
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 //! Misc low level stuff
13 use option::{Some, None};
19 use libc::{c_void, c_char, size_t};
23 pub type FreeGlue<'self> = &'self fn(*TypeDesc, *c_void);
25 // Corresponds to runtime type_desc type
32 // Remaining fields not listed
35 /// The representation of a Rust closure
42 #[abi = "rust-intrinsic"]
43 pub extern "rust-intrinsic" {
44 fn get_tydesc<T>() -> *();
45 fn size_of<T>() -> uint;
46 fn pref_align_of<T>() -> uint;
47 fn min_align_of<T>() -> uint;
52 use libc::{c_char, size_t};
56 unsafe fn rust_upcall_fail(expr: *c_char,
62 /// Compares contents of two pointers using the default method.
63 /// Equivalent to `*x1 == *x2`. Useful for hashtables.
64 pub fn shape_eq<T:Eq>(x1: &T, x2: &T) -> bool {
68 pub fn shape_lt<T:Ord>(x1: &T, x2: &T) -> bool {
72 pub fn shape_le<T:Ord>(x1: &T, x2: &T) -> bool {
77 * Returns a pointer to a type descriptor.
79 * Useful for calling certain function in the Rust runtime or otherwise
80 * performing dark magick.
83 pub fn get_type_desc<T>() -> *TypeDesc {
84 unsafe { rusti::get_tydesc::<T>() as *TypeDesc }
87 /// Returns a pointer to a type descriptor.
89 pub fn get_type_desc_val<T>(_val: &T) -> *TypeDesc {
93 /// Returns the size of a type
95 pub fn size_of<T>() -> uint {
96 unsafe { rusti::size_of::<T>() }
99 /// Returns the size of the type that `_val` points to
101 pub fn size_of_val<T>(_val: &T) -> uint {
106 * Returns the size of a type, or 1 if the actual size is zero.
108 * Useful for building structures containing variable-length arrays.
111 pub fn nonzero_size_of<T>() -> uint {
112 let s = size_of::<T>();
113 if s == 0 { 1 } else { s }
116 /// Returns the size of the type of the value that `_val` points to
118 pub fn nonzero_size_of_val<T>(_val: &T) -> uint {
119 nonzero_size_of::<T>()
124 * Returns the ABI-required minimum alignment of a type
126 * This is the alignment used for struct fields. It may be smaller
127 * than the preferred alignment.
130 pub fn min_align_of<T>() -> uint {
131 unsafe { rusti::min_align_of::<T>() }
134 /// Returns the ABI-required minimum alignment of the type of the value that
137 pub fn min_align_of_val<T>(_val: &T) -> uint {
141 /// Returns the preferred alignment of a type
143 pub fn pref_align_of<T>() -> uint {
144 unsafe { rusti::pref_align_of::<T>() }
147 /// Returns the preferred alignment of the type of the value that
150 pub fn pref_align_of_val<T>(_val: &T) -> uint {
154 /// Returns the refcount of a shared box (as just before calling this)
156 pub fn refcount<T>(t: @T) -> uint {
158 let ref_ptr: *uint = cast::reinterpret_cast(&t);
163 pub fn log_str<T>(t: &T) -> ~str {
164 do io::with_str_writer |wr| {
165 repr::write_repr(wr, t)
169 /** Initiate task failure */
170 pub fn begin_unwind(msg: ~str, file: ~str, line: uint) -> ! {
172 use rt::{context, OldTaskContext};
173 use rt::local_services::unsafe_borrow_local_services;
177 do str::as_buf(msg) |msg_buf, _msg_len| {
178 do str::as_buf(file) |file_buf, _file_len| {
180 let msg_buf = cast::transmute(msg_buf);
181 let file_buf = cast::transmute(file_buf);
182 begin_unwind_(msg_buf, file_buf, line as libc::size_t)
188 gc::cleanup_stack_for_failure();
190 let local_services = unsafe_borrow_local_services();
191 match local_services.unwinder {
192 Some(ref mut unwinder) => unwinder.begin_unwind(),
193 None => abort!("failure without unwinder. aborting process")
200 // FIXME #4427: Temporary until rt::rt_fail_ goes away
201 pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
203 gc::cleanup_stack_for_failure();
204 rustrt::rust_upcall_fail(msg, file, line);
209 pub fn fail_assert(msg: &str, file: &str, line: uint) -> ! {
210 let (msg, file) = (msg.to_owned(), file.to_owned());
211 begin_unwind(~"assertion failed: " + msg, file, line)
221 assert!(size_of::<u8>() == 1u);
222 assert!(size_of::<u16>() == 2u);
223 assert!(size_of::<u32>() == 4u);
224 assert!(size_of::<u64>() == 8u);
228 #[cfg(target_arch = "x86")]
229 #[cfg(target_arch = "arm")]
230 #[cfg(target_arch = "mips")]
232 assert!(size_of::<uint>() == 4u);
233 assert!(size_of::<*uint>() == 4u);
237 #[cfg(target_arch = "x86_64")]
239 assert!(size_of::<uint>() == 8u);
240 assert!(size_of::<*uint>() == 8u);
244 fn size_of_val_basic() {
245 assert_eq!(size_of_val(&1u8), 1);
246 assert_eq!(size_of_val(&1u16), 2);
247 assert_eq!(size_of_val(&1u32), 4);
248 assert_eq!(size_of_val(&1u64), 8);
252 fn nonzero_size_of_basic() {
254 assert!(size_of::<Z>() == 0u);
255 assert!(nonzero_size_of::<Z>() == 1u);
256 assert!(nonzero_size_of::<uint>() == size_of::<uint>());
260 fn nonzero_size_of_val_basic() {
262 assert_eq!(size_of_val(&z), 0u);
263 assert_eq!(nonzero_size_of_val(&z), 1u);
264 assert_eq!(nonzero_size_of_val(&1u), size_of_val(&1u));
268 fn align_of_basic() {
269 assert!(pref_align_of::<u8>() == 1u);
270 assert!(pref_align_of::<u16>() == 2u);
271 assert!(pref_align_of::<u32>() == 4u);
275 #[cfg(target_arch = "x86")]
276 #[cfg(target_arch = "arm")]
277 #[cfg(target_arch = "mips")]
279 assert!(pref_align_of::<uint>() == 4u);
280 assert!(pref_align_of::<*uint>() == 4u);
284 #[cfg(target_arch = "x86_64")]
286 assert!(pref_align_of::<uint>() == 8u);
287 assert!(pref_align_of::<*uint>() == 8u);
291 fn align_of_val_basic() {
292 assert_eq!(pref_align_of_val(&1u8), 1u);
293 assert_eq!(pref_align_of_val(&1u16), 2u);
294 assert_eq!(pref_align_of_val(&1u32), 4u);
298 fn synthesize_closure() {
301 let f: &fn(int) -> int = |y| x + y;
303 assert!(f(20) == 30);
305 let original_closure: Closure = cast::transmute(f);
307 let actual_function_pointer = original_closure.code;
308 let environment = original_closure.env;
310 let new_closure = Closure {
311 code: actual_function_pointer,
315 let new_f: &fn(int) -> int = cast::transmute(new_closure);
316 assert!(new_f(20) == 30);
324 // indent-tabs-mode: nil
326 // buffer-file-coding-system: utf-8-unix