]> git.lizzy.rs Git - rust.git/blob - src/libstd/mem.rs
auto merge of #10977 : brson/rust/androidtest, r=brson
[rust.git] / src / libstd / mem.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 //! Functions relating to memory layout
12
13 use unstable::intrinsics;
14
15 /// Returns the size of a type
16 #[inline]
17 pub fn size_of<T>() -> uint {
18     unsafe { intrinsics::size_of::<T>() }
19 }
20
21 /// Returns the size of the type that `_val` points to
22 #[inline]
23 pub fn size_of_val<T>(_val: &T) -> uint {
24     size_of::<T>()
25 }
26
27 /// Returns the size of a type, or 1 if the actual size is zero.
28 ///
29 /// Useful for building structures containing variable-length arrays.
30 #[inline]
31 pub fn nonzero_size_of<T>() -> uint {
32     let s = size_of::<T>();
33     if s == 0 { 1 } else { s }
34 }
35
36 /// Returns the size of the type of the value that `_val` points to
37 #[inline]
38 pub fn nonzero_size_of_val<T>(_val: &T) -> uint {
39     nonzero_size_of::<T>()
40 }
41
42 /// Returns the ABI-required minimum alignment of a type
43 ///
44 /// This is the alignment used for struct fields. It may be smaller
45 /// than the preferred alignment.
46 #[inline]
47 pub fn min_align_of<T>() -> uint {
48     unsafe { intrinsics::min_align_of::<T>() }
49 }
50
51 /// Returns the ABI-required minimum alignment of the type of the value that
52 /// `_val` points to
53 #[inline]
54 pub fn min_align_of_val<T>(_val: &T) -> uint {
55     min_align_of::<T>()
56 }
57
58 /// Returns the preferred alignment of a type
59 #[inline]
60 pub fn pref_align_of<T>() -> uint {
61     unsafe { intrinsics::pref_align_of::<T>() }
62 }
63
64 /// Returns the preferred alignment of the type of the value that
65 /// `_val` points to
66 #[inline]
67 pub fn pref_align_of_val<T>(_val: &T) -> uint {
68     pref_align_of::<T>()
69 }
70
71 #[cfg(test)]
72 mod tests {
73     use mem::*;
74
75     #[test]
76     fn size_of_basic() {
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);
81     }
82
83     #[test]
84     #[cfg(target_arch = "x86")]
85     #[cfg(target_arch = "arm")]
86     #[cfg(target_arch = "mips")]
87     fn size_of_32() {
88         assert_eq!(size_of::<uint>(), 4u);
89         assert_eq!(size_of::<*uint>(), 4u);
90     }
91
92     #[test]
93     #[cfg(target_arch = "x86_64")]
94     fn size_of_64() {
95         assert_eq!(size_of::<uint>(), 8u);
96         assert_eq!(size_of::<*uint>(), 8u);
97     }
98
99     #[test]
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);
105     }
106
107     #[test]
108     fn nonzero_size_of_basic() {
109         type Z = [i8, ..0];
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>());
113     }
114
115     #[test]
116     fn nonzero_size_of_val_basic() {
117         let z = [0u8, ..0];
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));
121     }
122
123     #[test]
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);
128     }
129
130     #[test]
131     #[cfg(target_arch = "x86")]
132     #[cfg(target_arch = "arm")]
133     #[cfg(target_arch = "mips")]
134     fn align_of_32() {
135         assert_eq!(pref_align_of::<uint>(), 4u);
136         assert_eq!(pref_align_of::<*uint>(), 4u);
137     }
138
139     #[test]
140     #[cfg(target_arch = "x86_64")]
141     fn align_of_64() {
142         assert_eq!(pref_align_of::<uint>(), 8u);
143         assert_eq!(pref_align_of::<*uint>(), 8u);
144     }
145
146     #[test]
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);
151     }
152 }