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 //! Functions relating to memory layout
13 use unstable::intrinsics;
15 /// Returns the size of a type
17 pub fn size_of<T>() -> uint {
18 unsafe { intrinsics::size_of::<T>() }
21 /// Returns the size of the type that `_val` points to
23 pub fn size_of_val<T>(_val: &T) -> uint {
27 /// Returns the size of a type, or 1 if the actual size is zero.
29 /// Useful for building structures containing variable-length arrays.
31 pub fn nonzero_size_of<T>() -> uint {
32 let s = size_of::<T>();
33 if s == 0 { 1 } else { s }
36 /// Returns the size of the type of the value that `_val` points to
38 pub fn nonzero_size_of_val<T>(_val: &T) -> uint {
39 nonzero_size_of::<T>()
42 /// Returns the ABI-required minimum alignment of a type
44 /// This is the alignment used for struct fields. It may be smaller
45 /// than the preferred alignment.
47 pub fn min_align_of<T>() -> uint {
48 unsafe { intrinsics::min_align_of::<T>() }
51 /// Returns the ABI-required minimum alignment of the type of the value that
54 pub fn min_align_of_val<T>(_val: &T) -> uint {
58 /// Returns the preferred alignment of a type
60 pub fn pref_align_of<T>() -> uint {
61 unsafe { intrinsics::pref_align_of::<T>() }
64 /// Returns the preferred alignment of the type of the value that
67 pub fn pref_align_of_val<T>(_val: &T) -> uint {
77 assert_eq!(size_of::<u8>(), 1u);
78 assert_eq!(size_of::<u16>(), 2u);
79 assert_eq!(size_of::<u32>(), 4u);
80 assert_eq!(size_of::<u64>(), 8u);
84 #[cfg(target_arch = "x86")]
85 #[cfg(target_arch = "arm")]
86 #[cfg(target_arch = "mips")]
88 assert_eq!(size_of::<uint>(), 4u);
89 assert_eq!(size_of::<*uint>(), 4u);
93 #[cfg(target_arch = "x86_64")]
95 assert_eq!(size_of::<uint>(), 8u);
96 assert_eq!(size_of::<*uint>(), 8u);
100 fn size_of_val_basic() {
101 assert_eq!(size_of_val(&1u8), 1);
102 assert_eq!(size_of_val(&1u16), 2);
103 assert_eq!(size_of_val(&1u32), 4);
104 assert_eq!(size_of_val(&1u64), 8);
108 fn nonzero_size_of_basic() {
110 assert_eq!(size_of::<Z>(), 0u);
111 assert_eq!(nonzero_size_of::<Z>(), 1u);
112 assert_eq!(nonzero_size_of::<uint>(), size_of::<uint>());
116 fn nonzero_size_of_val_basic() {
118 assert_eq!(size_of_val(&z), 0u);
119 assert_eq!(nonzero_size_of_val(&z), 1u);
120 assert_eq!(nonzero_size_of_val(&1u), size_of_val(&1u));
124 fn align_of_basic() {
125 assert_eq!(pref_align_of::<u8>(), 1u);
126 assert_eq!(pref_align_of::<u16>(), 2u);
127 assert_eq!(pref_align_of::<u32>(), 4u);
131 #[cfg(target_arch = "x86")]
132 #[cfg(target_arch = "arm")]
133 #[cfg(target_arch = "mips")]
135 assert_eq!(pref_align_of::<uint>(), 4u);
136 assert_eq!(pref_align_of::<*uint>(), 4u);
140 #[cfg(target_arch = "x86_64")]
142 assert_eq!(pref_align_of::<uint>(), 8u);
143 assert_eq!(pref_align_of::<*uint>(), 8u);
147 fn align_of_val_basic() {
148 assert_eq!(pref_align_of_val(&1u8), 1u);
149 assert_eq!(pref_align_of_val(&1u16), 2u);
150 assert_eq!(pref_align_of_val(&1u32), 4u);