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 #[allow(missing_doc)];
18 use libc::{c_char, size_t};
21 use unstable::intrinsics;
23 /// Returns the size of a type
25 pub fn size_of<T>() -> uint {
26 unsafe { intrinsics::size_of::<T>() }
29 /// Returns the size of the type that `_val` points to
31 pub fn size_of_val<T>(_val: &T) -> uint {
36 * Returns the size of a type, or 1 if the actual size is zero.
38 * Useful for building structures containing variable-length arrays.
41 pub fn nonzero_size_of<T>() -> uint {
42 let s = size_of::<T>();
43 if s == 0 { 1 } else { s }
46 /// Returns the size of the type of the value that `_val` points to
48 pub fn nonzero_size_of_val<T>(_val: &T) -> uint {
49 nonzero_size_of::<T>()
54 * Returns the ABI-required minimum alignment of a type
56 * This is the alignment used for struct fields. It may be smaller
57 * than the preferred alignment.
60 pub fn min_align_of<T>() -> uint {
61 unsafe { intrinsics::min_align_of::<T>() }
64 /// Returns the ABI-required minimum alignment of the type of the value that
67 pub fn min_align_of_val<T>(_val: &T) -> uint {
71 /// Returns the preferred alignment of a type
73 pub fn pref_align_of<T>() -> uint {
74 unsafe { intrinsics::pref_align_of::<T>() }
77 /// Returns the preferred alignment of the type of the value that
80 pub fn pref_align_of_val<T>(_val: &T) -> uint {
84 /// Returns the refcount of a shared box (as just before calling this)
86 pub fn refcount<T>(t: @T) -> uint {
88 let ref_ptr: *uint = cast::transmute_copy(&t);
93 pub fn log_str<T>(t: &T) -> ~str {
95 use rt::io::Decorator;
97 let mut result = io::mem::MemWriter::new();
98 repr::write_repr(&mut result as &mut io::Writer, t);
99 str::from_utf8_owned(result.inner())
102 /// Trait for initiating task failure.
103 pub trait FailWithCause {
104 /// Fail the current task, taking ownership of `cause`
105 fn fail_with(cause: Self, file: &'static str, line: uint) -> !;
108 impl FailWithCause for ~str {
109 fn fail_with(cause: ~str, file: &'static str, line: uint) -> ! {
110 do cause.with_c_str |msg_buf| {
111 do file.with_c_str |file_buf| {
112 begin_unwind_(msg_buf, file_buf, line as libc::size_t)
118 impl FailWithCause for &'static str {
119 fn fail_with(cause: &'static str, file: &'static str, line: uint) -> ! {
120 do cause.with_c_str |msg_buf| {
121 do file.with_c_str |file_buf| {
122 begin_unwind_(msg_buf, file_buf, line as libc::size_t)
128 // FIXME #4427: Temporary until rt::rt_fail_ goes away
129 pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
130 use option::{Some, None};
131 use rt::in_green_task_context;
133 use rt::local::Local;
134 use rt::logging::Logger;
135 use send_str::SendStrOwned;
139 // XXX: Bad re-allocations. fail! needs some refactoring
140 let msg = str::raw::from_c_str(msg);
141 let file = str::raw::from_c_str(file);
143 // XXX: Logging doesn't work correctly in non-task context because it
144 // invokes the local heap
145 if in_green_task_context() {
146 // XXX: Logging doesn't work here - the check to call the log
147 // function never passes - so calling the log function directly.
148 do Local::borrow |task: &mut Task| {
149 let msg = match task.name {
151 fmt!("task '%s' failed at '%s', %s:%i",
152 name.as_slice(), msg, file, line as int),
154 fmt!("task <unnamed> failed at '%s', %s:%i",
155 msg, file, line as int)
158 task.logger.log(SendStrOwned(msg));
161 rterrln!("failed in non-task context at '%s', %s:%i",
162 msg, file, line as int);
165 let task: *mut Task = Local::unsafe_borrow();
166 if (*task).unwinder.unwinding {
167 rtabort!("unwinding again");
169 (*task).unwinder.begin_unwind();
180 assert_eq!(size_of::<u8>(), 1u);
181 assert_eq!(size_of::<u16>(), 2u);
182 assert_eq!(size_of::<u32>(), 4u);
183 assert_eq!(size_of::<u64>(), 8u);
187 #[cfg(target_arch = "x86")]
188 #[cfg(target_arch = "arm")]
189 #[cfg(target_arch = "mips")]
191 assert_eq!(size_of::<uint>(), 4u);
192 assert_eq!(size_of::<*uint>(), 4u);
196 #[cfg(target_arch = "x86_64")]
198 assert_eq!(size_of::<uint>(), 8u);
199 assert_eq!(size_of::<*uint>(), 8u);
203 fn size_of_val_basic() {
204 assert_eq!(size_of_val(&1u8), 1);
205 assert_eq!(size_of_val(&1u16), 2);
206 assert_eq!(size_of_val(&1u32), 4);
207 assert_eq!(size_of_val(&1u64), 8);
211 fn nonzero_size_of_basic() {
213 assert_eq!(size_of::<Z>(), 0u);
214 assert_eq!(nonzero_size_of::<Z>(), 1u);
215 assert_eq!(nonzero_size_of::<uint>(), size_of::<uint>());
219 fn nonzero_size_of_val_basic() {
221 assert_eq!(size_of_val(&z), 0u);
222 assert_eq!(nonzero_size_of_val(&z), 1u);
223 assert_eq!(nonzero_size_of_val(&1u), size_of_val(&1u));
227 fn align_of_basic() {
228 assert_eq!(pref_align_of::<u8>(), 1u);
229 assert_eq!(pref_align_of::<u16>(), 2u);
230 assert_eq!(pref_align_of::<u32>(), 4u);
234 #[cfg(target_arch = "x86")]
235 #[cfg(target_arch = "arm")]
236 #[cfg(target_arch = "mips")]
238 assert_eq!(pref_align_of::<uint>(), 4u);
239 assert_eq!(pref_align_of::<*uint>(), 4u);
243 #[cfg(target_arch = "x86_64")]
245 assert_eq!(pref_align_of::<uint>(), 8u);
246 assert_eq!(pref_align_of::<*uint>(), 8u);
250 fn align_of_val_basic() {
251 assert_eq!(pref_align_of_val(&1u8), 1u);
252 assert_eq!(pref_align_of_val(&1u16), 2u);
253 assert_eq!(pref_align_of_val(&1u32), 4u);
257 fn synthesize_closure() {
258 use unstable::raw::Closure;
261 let f: &fn(int) -> int = |y| x + y;
263 assert_eq!(f(20), 30);
265 let original_closure: Closure = cast::transmute(f);
267 let actual_function_pointer = original_closure.code;
268 let environment = original_closure.env;
270 let new_closure = Closure {
271 code: actual_function_pointer,
275 let new_f: &fn(int) -> int = cast::transmute(new_closure);
276 assert_eq!(new_f(20), 30);
282 fn fail_static() { FailWithCause::fail_with("cause", file!(), line!()) }
286 fn fail_owned() { FailWithCause::fail_with(~"cause", file!(), line!()) }