Skip to content

Commit d8d1b8f

Browse files
committed
auto merge of #9896 : brson/rust/stdmem, r=alexcrichton
This is progress toward removing std::sys.
2 parents 51709fc + 34d376f commit d8d1b8f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+288
-269
lines changed

doc/po/ja/rust.md.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1842,7 +1842,7 @@ msgid ""
18421842
"The type parameters can also be explicitly supplied in a trailing [path]"
18431843
"(#paths) component after the function name. This might be necessary if there "
18441844
"is not sufficient context to determine the type parameters. For example, "
1845-
"`sys::size_of::<u32>() == 4`."
1845+
"`mem::size_of::<u32>() == 4`."
18461846
msgstr ""
18471847

18481848
#. type: Plain text

doc/po/ja/tutorial-ffi.md.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ msgid ""
327327
"impl<T: Send> Unique<T> {\n"
328328
" pub fn new(value: T) -> Unique<T> {\n"
329329
" unsafe {\n"
330-
" let ptr = malloc(std::sys::size_of::<T>() as size_t) as *mut T;\n"
330+
" let ptr = malloc(std::mem::size_of::<T>() as size_t) as *mut T;\n"
331331
" assert!(!ptr::is_null(ptr));\n"
332332
" // `*ptr` is uninitialized, and `*ptr = value` would attempt to destroy it\n"
333333
" intrinsics::move_val_init(&mut *ptr, value);\n"

doc/po/rust.md.pot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1842,7 +1842,7 @@ msgid ""
18421842
"The type parameters can also be explicitly supplied in a trailing [path]"
18431843
"(#paths) component after the function name. This might be necessary if there "
18441844
"is not sufficient context to determine the type parameters. For example, "
1845-
"`sys::size_of::<u32>() == 4`."
1845+
"`mem::size_of::<u32>() == 4`."
18461846
msgstr ""
18471847

18481848
#. type: Plain text

doc/po/tutorial-ffi.md.pot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ msgid ""
327327
"impl<T: Send> Unique<T> {\n"
328328
" pub fn new(value: T) -> Unique<T> {\n"
329329
" unsafe {\n"
330-
" let ptr = malloc(std::sys::size_of::<T>() as size_t) as *mut T;\n"
330+
" let ptr = malloc(std::mem::size_of::<T>() as size_t) as *mut T;\n"
331331
" assert!(!ptr::is_null(ptr));\n"
332332
" // `*ptr` is uninitialized, and `*ptr = value` would attempt to destroy it\n"
333333
" intrinsics::move_val_init(&mut *ptr, value);\n"

doc/rust.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ with `int`, and require the closure parameter to have type
975975
The type parameters can also be explicitly supplied in a trailing
976976
[path](#paths) component after the function name. This might be necessary
977977
if there is not sufficient context to determine the type parameters. For
978-
example, `sys::size_of::<u32>() == 4`.
978+
example, `mem::size_of::<u32>() == 4`.
979979

980980
Since a parameter type is opaque to the generic function, the set of
981981
operations that can be performed on it is limited. Values of parameter

doc/tutorial-ffi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ impl<T: Send> Unique<T> {
300300
#[inline(never)];
301301
302302
unsafe {
303-
let ptr = malloc(std::sys::size_of::<T>() as size_t) as *mut T;
303+
let ptr = malloc(std::mem::size_of::<T>() as size_t) as *mut T;
304304
assert!(!ptr::is_null(ptr));
305305
// `*ptr` is uninitialized, and `*ptr = value` would attempt to destroy it
306306
intrinsics::move_val_init(&mut *ptr, value);

src/libextra/arena.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use std::cast::{transmute, transmute_mut, transmute_mut_region};
4242
use std::cast;
4343
use std::num;
4444
use std::ptr;
45-
use std::sys;
45+
use std::mem;
4646
use std::uint;
4747
use std::vec;
4848
use std::unstable::intrinsics;
@@ -123,7 +123,7 @@ unsafe fn destroy_chunk(chunk: &Chunk) {
123123
let (tydesc, is_done) = un_bitpack_tydesc_ptr(*tydesc_data);
124124
let (size, align) = ((*tydesc).size, (*tydesc).align);
125125

126-
let after_tydesc = idx + sys::size_of::<*TyDesc>();
126+
let after_tydesc = idx + mem::size_of::<*TyDesc>();
127127

128128
let start = round_up_to(after_tydesc, align);
129129

@@ -134,7 +134,7 @@ unsafe fn destroy_chunk(chunk: &Chunk) {
134134
}
135135

136136
// Find where the next tydesc lives
137-
idx = round_up_to(start + size, sys::pref_align_of::<*TyDesc>());
137+
idx = round_up_to(start + size, mem::pref_align_of::<*TyDesc>());
138138
}
139139
}
140140

@@ -220,7 +220,7 @@ impl Arena {
220220
let head = transmute_mut_region(&mut self.head);
221221

222222
tydesc_start = head.fill;
223-
after_tydesc = head.fill + sys::size_of::<*TyDesc>();
223+
after_tydesc = head.fill + mem::size_of::<*TyDesc>();
224224
start = round_up_to(after_tydesc, align);
225225
end = start + n_bytes;
226226
}
@@ -230,7 +230,7 @@ impl Arena {
230230
}
231231

232232
let head = transmute_mut_region(&mut self.head);
233-
head.fill = round_up_to(end, sys::pref_align_of::<*TyDesc>());
233+
head.fill = round_up_to(end, mem::pref_align_of::<*TyDesc>());
234234

235235
//debug2!("idx = {}, size = {}, align = {}, fill = {}",
236236
// start, n_bytes, align, head.fill);

src/libstd/at_vec.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use clone::Clone;
1414
use container::Container;
1515
use iter::Iterator;
1616
use option::{Option, Some, None};
17-
use sys;
17+
use mem;
1818
use unstable::raw::Repr;
1919
use vec::{ImmutableVector, OwnedVector};
2020

@@ -26,7 +26,7 @@ use vec::{ImmutableVector, OwnedVector};
2626
pub fn capacity<T>(v: @[T]) -> uint {
2727
unsafe {
2828
let box = v.repr();
29-
(*box).data.alloc / sys::size_of::<T>()
29+
(*box).data.alloc / mem::size_of::<T>()
3030
}
3131
}
3232

@@ -160,7 +160,7 @@ pub mod raw {
160160
use cast::{transmute, transmute_copy};
161161
use libc;
162162
use ptr;
163-
use sys;
163+
use mem;
164164
use uint;
165165
use unstable::intrinsics::{move_val_init, TyDesc};
166166
use unstable::intrinsics;
@@ -176,7 +176,7 @@ pub mod raw {
176176
#[inline]
177177
pub unsafe fn set_len<T>(v: &mut @[T], new_len: uint) {
178178
let repr: *mut Box<Vec<T>> = cast::transmute_copy(v);
179-
(*repr).data.fill = new_len * sys::size_of::<T>();
179+
(*repr).data.fill = new_len * mem::size_of::<T>();
180180
}
181181

182182
/**
@@ -199,7 +199,7 @@ pub mod raw {
199199
unsafe fn push_fast<T>(v: &mut @[T], initval: T) {
200200
let repr: *mut Box<Vec<T>> = cast::transmute_copy(v);
201201
let amt = v.len();
202-
(*repr).data.fill += sys::size_of::<T>();
202+
(*repr).data.fill += mem::size_of::<T>();
203203
let p = ptr::offset(&(*repr).data.data as *T, amt as int) as *mut T;
204204
move_val_init(&mut(*p), initval);
205205
}
@@ -236,7 +236,7 @@ pub mod raw {
236236
unsafe {
237237
if n > (**ptr).data.alloc / (*ty).size {
238238
let alloc = n * (*ty).size;
239-
let total_size = alloc + sys::size_of::<Vec<()>>();
239+
let total_size = alloc + mem::size_of::<Vec<()>>();
240240
if alloc / (*ty).size != n || total_size < alloc {
241241
fail2!("vector size is too large: {}", n);
242242
}

src/libstd/cast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! Unsafe casting functions
1212
1313
use ptr::RawPtr;
14-
use sys;
14+
use mem;
1515
use unstable::intrinsics;
1616

1717
/// Casts the value at `src` to U. The two types must have the same length.
@@ -21,7 +21,7 @@ pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
2121
let mut dest: U = intrinsics::uninit();
2222
let dest_ptr: *mut u8 = transmute(&mut dest);
2323
let src_ptr: *u8 = transmute(src);
24-
intrinsics::memcpy32(dest_ptr, src_ptr, sys::size_of::<U>() as u32);
24+
intrinsics::memcpy32(dest_ptr, src_ptr, mem::size_of::<U>() as u32);
2525
dest
2626
}
2727

@@ -32,7 +32,7 @@ pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
3232
let mut dest: U = intrinsics::uninit();
3333
let dest_ptr: *mut u8 = transmute(&mut dest);
3434
let src_ptr: *u8 = transmute(src);
35-
intrinsics::memcpy64(dest_ptr, src_ptr, sys::size_of::<U>() as u64);
35+
intrinsics::memcpy64(dest_ptr, src_ptr, mem::size_of::<U>() as u64);
3636
dest
3737
}
3838

src/libstd/cleanup.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn debug_mem() -> bool {
6868
/// Destroys all managed memory (i.e. @ boxes) held by the current task.
6969
pub unsafe fn annihilate() {
7070
use rt::local_heap::local_free;
71-
use sys;
71+
use mem;
7272
use managed;
7373

7474
let mut stats = AnnihilateStats {
@@ -115,7 +115,7 @@ pub unsafe fn annihilate() {
115115
if !uniq {
116116
stats.n_bytes_freed +=
117117
(*((*box).type_desc)).size
118-
+ sys::size_of::<raw::Box<()>>();
118+
+ mem::size_of::<raw::Box<()>>();
119119
local_free(box as *i8);
120120
}
121121
true

src/libstd/mem.rs

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
/**
28+
* Returns the size of a type, or 1 if the actual size is zero.
29+
*
30+
* Useful for building structures containing variable-length arrays.
31+
*/
32+
#[inline]
33+
pub fn nonzero_size_of<T>() -> uint {
34+
let s = size_of::<T>();
35+
if s == 0 { 1 } else { s }
36+
}
37+
38+
/// Returns the size of the type of the value that `_val` points to
39+
#[inline]
40+
pub fn nonzero_size_of_val<T>(_val: &T) -> uint {
41+
nonzero_size_of::<T>()
42+
}
43+
44+
45+
/**
46+
* Returns the ABI-required minimum alignment of a type
47+
*
48+
* This is the alignment used for struct fields. It may be smaller
49+
* than the preferred alignment.
50+
*/
51+
#[inline]
52+
pub fn min_align_of<T>() -> uint {
53+
unsafe { intrinsics::min_align_of::<T>() }
54+
}
55+
56+
/// Returns the ABI-required minimum alignment of the type of the value that
57+
/// `_val` points to
58+
#[inline]
59+
pub fn min_align_of_val<T>(_val: &T) -> uint {
60+
min_align_of::<T>()
61+
}
62+
63+
/// Returns the preferred alignment of a type
64+
#[inline]
65+
pub fn pref_align_of<T>() -> uint {
66+
unsafe { intrinsics::pref_align_of::<T>() }
67+
}
68+
69+
/// Returns the preferred alignment of the type of the value that
70+
/// `_val` points to
71+
#[inline]
72+
pub fn pref_align_of_val<T>(_val: &T) -> uint {
73+
pref_align_of::<T>()
74+
}
75+
76+
#[cfg(test)]
77+
mod tests {
78+
use cast;
79+
use mem::*;
80+
81+
#[test]
82+
fn size_of_basic() {
83+
assert_eq!(size_of::<u8>(), 1u);
84+
assert_eq!(size_of::<u16>(), 2u);
85+
assert_eq!(size_of::<u32>(), 4u);
86+
assert_eq!(size_of::<u64>(), 8u);
87+
}
88+
89+
#[test]
90+
#[cfg(target_arch = "x86")]
91+
#[cfg(target_arch = "arm")]
92+
#[cfg(target_arch = "mips")]
93+
fn size_of_32() {
94+
assert_eq!(size_of::<uint>(), 4u);
95+
assert_eq!(size_of::<*uint>(), 4u);
96+
}
97+
98+
#[test]
99+
#[cfg(target_arch = "x86_64")]
100+
fn size_of_64() {
101+
assert_eq!(size_of::<uint>(), 8u);
102+
assert_eq!(size_of::<*uint>(), 8u);
103+
}
104+
105+
#[test]
106+
fn size_of_val_basic() {
107+
assert_eq!(size_of_val(&1u8), 1);
108+
assert_eq!(size_of_val(&1u16), 2);
109+
assert_eq!(size_of_val(&1u32), 4);
110+
assert_eq!(size_of_val(&1u64), 8);
111+
}
112+
113+
#[test]
114+
fn nonzero_size_of_basic() {
115+
type Z = [i8, ..0];
116+
assert_eq!(size_of::<Z>(), 0u);
117+
assert_eq!(nonzero_size_of::<Z>(), 1u);
118+
assert_eq!(nonzero_size_of::<uint>(), size_of::<uint>());
119+
}
120+
121+
#[test]
122+
fn nonzero_size_of_val_basic() {
123+
let z = [0u8, ..0];
124+
assert_eq!(size_of_val(&z), 0u);
125+
assert_eq!(nonzero_size_of_val(&z), 1u);
126+
assert_eq!(nonzero_size_of_val(&1u), size_of_val(&1u));
127+
}
128+
129+
#[test]
130+
fn align_of_basic() {
131+
assert_eq!(pref_align_of::<u8>(), 1u);
132+
assert_eq!(pref_align_of::<u16>(), 2u);
133+
assert_eq!(pref_align_of::<u32>(), 4u);
134+
}
135+
136+
#[test]
137+
#[cfg(target_arch = "x86")]
138+
#[cfg(target_arch = "arm")]
139+
#[cfg(target_arch = "mips")]
140+
fn align_of_32() {
141+
assert_eq!(pref_align_of::<uint>(), 4u);
142+
assert_eq!(pref_align_of::<*uint>(), 4u);
143+
}
144+
145+
#[test]
146+
#[cfg(target_arch = "x86_64")]
147+
fn align_of_64() {
148+
assert_eq!(pref_align_of::<uint>(), 8u);
149+
assert_eq!(pref_align_of::<*uint>(), 8u);
150+
}
151+
152+
#[test]
153+
fn align_of_val_basic() {
154+
assert_eq!(pref_align_of_val(&1u8), 1u);
155+
assert_eq!(pref_align_of_val(&1u16), 2u);
156+
assert_eq!(pref_align_of_val(&1u32), 4u);
157+
}
158+
}

src/libstd/num/f32.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ mod tests {
928928

929929
use num::*;
930930
use num;
931-
use sys;
931+
use mem;
932932

933933
#[test]
934934
fn test_num() {
@@ -1198,8 +1198,8 @@ mod tests {
11981198
#[test]
11991199
fn test_primitive() {
12001200
let none: Option<f32> = None;
1201-
assert_eq!(Primitive::bits(none), sys::size_of::<f32>() * 8);
1202-
assert_eq!(Primitive::bytes(none), sys::size_of::<f32>());
1201+
assert_eq!(Primitive::bits(none), mem::size_of::<f32>() * 8);
1202+
assert_eq!(Primitive::bytes(none), mem::size_of::<f32>());
12031203
}
12041204

12051205
#[test]

src/libstd/num/f64.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ mod tests {
976976

977977
use num::*;
978978
use num;
979-
use sys;
979+
use mem;
980980

981981
#[test]
982982
fn test_num() {
@@ -1249,8 +1249,8 @@ mod tests {
12491249
#[test]
12501250
fn test_primitive() {
12511251
let none: Option<f64> = None;
1252-
assert_eq!(Primitive::bits(none), sys::size_of::<f64>() * 8);
1253-
assert_eq!(Primitive::bytes(none), sys::size_of::<f64>());
1252+
assert_eq!(Primitive::bits(none), mem::size_of::<f64>() * 8);
1253+
assert_eq!(Primitive::bytes(none), mem::size_of::<f64>());
12541254
}
12551255

12561256
#[test]

0 commit comments

Comments
 (0)