Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std: Move size/align functions to std::mem. #2240 #9896

Merged
merged 1 commit into from
Oct 18, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/po/ja/rust.md.po
Original file line number Diff line number Diff line change
Expand Up @@ -1842,7 +1842,7 @@ msgid ""
"The type parameters can also be explicitly supplied in a trailing [path]"
"(#paths) component after the function name. This might be necessary if there "
"is not sufficient context to determine the type parameters. For example, "
"`sys::size_of::<u32>() == 4`."
"`mem::size_of::<u32>() == 4`."
msgstr ""

#. type: Plain text
Expand Down
2 changes: 1 addition & 1 deletion doc/po/ja/tutorial-ffi.md.po
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ msgid ""
"impl<T: Send> Unique<T> {\n"
" pub fn new(value: T) -> Unique<T> {\n"
" unsafe {\n"
" let ptr = malloc(std::sys::size_of::<T>() as size_t) as *mut T;\n"
" let ptr = malloc(std::mem::size_of::<T>() as size_t) as *mut T;\n"
" assert!(!ptr::is_null(ptr));\n"
" // `*ptr` is uninitialized, and `*ptr = value` would attempt to destroy it\n"
" intrinsics::move_val_init(&mut *ptr, value);\n"
Expand Down
2 changes: 1 addition & 1 deletion doc/po/rust.md.pot
Original file line number Diff line number Diff line change
Expand Up @@ -1842,7 +1842,7 @@ msgid ""
"The type parameters can also be explicitly supplied in a trailing [path]"
"(#paths) component after the function name. This might be necessary if there "
"is not sufficient context to determine the type parameters. For example, "
"`sys::size_of::<u32>() == 4`."
"`mem::size_of::<u32>() == 4`."
msgstr ""

#. type: Plain text
Expand Down
2 changes: 1 addition & 1 deletion doc/po/tutorial-ffi.md.pot
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ msgid ""
"impl<T: Send> Unique<T> {\n"
" pub fn new(value: T) -> Unique<T> {\n"
" unsafe {\n"
" let ptr = malloc(std::sys::size_of::<T>() as size_t) as *mut T;\n"
" let ptr = malloc(std::mem::size_of::<T>() as size_t) as *mut T;\n"
" assert!(!ptr::is_null(ptr));\n"
" // `*ptr` is uninitialized, and `*ptr = value` would attempt to destroy it\n"
" intrinsics::move_val_init(&mut *ptr, value);\n"
Expand Down
2 changes: 1 addition & 1 deletion doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ with `int`, and require the closure parameter to have type
The type parameters can also be explicitly supplied in a trailing
[path](#paths) component after the function name. This might be necessary
if there is not sufficient context to determine the type parameters. For
example, `sys::size_of::<u32>() == 4`.
example, `mem::size_of::<u32>() == 4`.

Since a parameter type is opaque to the generic function, the set of
operations that can be performed on it is limited. Values of parameter
Expand Down
2 changes: 1 addition & 1 deletion doc/tutorial-ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl<T: Send> Unique<T> {
#[inline(never)];

unsafe {
let ptr = malloc(std::sys::size_of::<T>() as size_t) as *mut T;
let ptr = malloc(std::mem::size_of::<T>() as size_t) as *mut T;
assert!(!ptr::is_null(ptr));
// `*ptr` is uninitialized, and `*ptr = value` would attempt to destroy it
intrinsics::move_val_init(&mut *ptr, value);
Expand Down
10 changes: 5 additions & 5 deletions src/libextra/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use std::cast::{transmute, transmute_mut, transmute_mut_region};
use std::cast;
use std::num;
use std::ptr;
use std::sys;
use std::mem;
use std::uint;
use std::vec;
use std::unstable::intrinsics;
Expand Down Expand Up @@ -123,7 +123,7 @@ unsafe fn destroy_chunk(chunk: &Chunk) {
let (tydesc, is_done) = un_bitpack_tydesc_ptr(*tydesc_data);
let (size, align) = ((*tydesc).size, (*tydesc).align);

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

let start = round_up_to(after_tydesc, align);

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

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

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

tydesc_start = head.fill;
after_tydesc = head.fill + sys::size_of::<*TyDesc>();
after_tydesc = head.fill + mem::size_of::<*TyDesc>();
start = round_up_to(after_tydesc, align);
end = start + n_bytes;
}
Expand All @@ -230,7 +230,7 @@ impl Arena {
}

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

//debug2!("idx = {}, size = {}, align = {}, fill = {}",
// start, n_bytes, align, head.fill);
Expand Down
12 changes: 6 additions & 6 deletions src/libstd/at_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use clone::Clone;
use container::Container;
use iter::Iterator;
use option::{Option, Some, None};
use sys;
use mem;
use unstable::raw::Repr;
use vec::{ImmutableVector, OwnedVector};

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

Expand Down Expand Up @@ -160,7 +160,7 @@ pub mod raw {
use cast::{transmute, transmute_copy};
use libc;
use ptr;
use sys;
use mem;
use uint;
use unstable::intrinsics::{move_val_init, TyDesc};
use unstable::intrinsics;
Expand All @@ -176,7 +176,7 @@ pub mod raw {
#[inline]
pub unsafe fn set_len<T>(v: &mut @[T], new_len: uint) {
let repr: *mut Box<Vec<T>> = cast::transmute_copy(v);
(*repr).data.fill = new_len * sys::size_of::<T>();
(*repr).data.fill = new_len * mem::size_of::<T>();
}

/**
Expand All @@ -199,7 +199,7 @@ pub mod raw {
unsafe fn push_fast<T>(v: &mut @[T], initval: T) {
let repr: *mut Box<Vec<T>> = cast::transmute_copy(v);
let amt = v.len();
(*repr).data.fill += sys::size_of::<T>();
(*repr).data.fill += mem::size_of::<T>();
let p = ptr::offset(&(*repr).data.data as *T, amt as int) as *mut T;
move_val_init(&mut(*p), initval);
}
Expand Down Expand Up @@ -236,7 +236,7 @@ pub mod raw {
unsafe {
if n > (**ptr).data.alloc / (*ty).size {
let alloc = n * (*ty).size;
let total_size = alloc + sys::size_of::<Vec<()>>();
let total_size = alloc + mem::size_of::<Vec<()>>();
if alloc / (*ty).size != n || total_size < alloc {
fail2!("vector size is too large: {}", n);
}
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! Unsafe casting functions

use ptr::RawPtr;
use sys;
use mem;
use unstable::intrinsics;

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/libstd/cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn debug_mem() -> bool {
/// Destroys all managed memory (i.e. @ boxes) held by the current task.
pub unsafe fn annihilate() {
use rt::local_heap::local_free;
use sys;
use mem;
use managed;

let mut stats = AnnihilateStats {
Expand Down Expand Up @@ -115,7 +115,7 @@ pub unsafe fn annihilate() {
if !uniq {
stats.n_bytes_freed +=
(*((*box).type_desc)).size
+ sys::size_of::<raw::Box<()>>();
+ mem::size_of::<raw::Box<()>>();
local_free(box as *i8);
}
true
Expand Down
158 changes: 158 additions & 0 deletions src/libstd/mem.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Functions relating to memory layout

use unstable::intrinsics;

/// Returns the size of a type
#[inline]
pub fn size_of<T>() -> uint {
unsafe { intrinsics::size_of::<T>() }
}

/// Returns the size of the type that `_val` points to
#[inline]
pub fn size_of_val<T>(_val: &T) -> uint {
size_of::<T>()
}

/**
* Returns the size of a type, or 1 if the actual size is zero.
*
* Useful for building structures containing variable-length arrays.
*/
#[inline]
pub fn nonzero_size_of<T>() -> uint {
let s = size_of::<T>();
if s == 0 { 1 } else { s }
}

/// Returns the size of the type of the value that `_val` points to
#[inline]
pub fn nonzero_size_of_val<T>(_val: &T) -> uint {
nonzero_size_of::<T>()
}


/**
* Returns the ABI-required minimum alignment of a type
*
* This is the alignment used for struct fields. It may be smaller
* than the preferred alignment.
*/
#[inline]
pub fn min_align_of<T>() -> uint {
unsafe { intrinsics::min_align_of::<T>() }
}

/// Returns the ABI-required minimum alignment of the type of the value that
/// `_val` points to
#[inline]
pub fn min_align_of_val<T>(_val: &T) -> uint {
min_align_of::<T>()
}

/// Returns the preferred alignment of a type
#[inline]
pub fn pref_align_of<T>() -> uint {
unsafe { intrinsics::pref_align_of::<T>() }
}

/// Returns the preferred alignment of the type of the value that
/// `_val` points to
#[inline]
pub fn pref_align_of_val<T>(_val: &T) -> uint {
pref_align_of::<T>()
}

#[cfg(test)]
mod tests {
use cast;
use mem::*;

#[test]
fn size_of_basic() {
assert_eq!(size_of::<u8>(), 1u);
assert_eq!(size_of::<u16>(), 2u);
assert_eq!(size_of::<u32>(), 4u);
assert_eq!(size_of::<u64>(), 8u);
}

#[test]
#[cfg(target_arch = "x86")]
#[cfg(target_arch = "arm")]
#[cfg(target_arch = "mips")]
fn size_of_32() {
assert_eq!(size_of::<uint>(), 4u);
assert_eq!(size_of::<*uint>(), 4u);
}

#[test]
#[cfg(target_arch = "x86_64")]
fn size_of_64() {
assert_eq!(size_of::<uint>(), 8u);
assert_eq!(size_of::<*uint>(), 8u);
}

#[test]
fn size_of_val_basic() {
assert_eq!(size_of_val(&1u8), 1);
assert_eq!(size_of_val(&1u16), 2);
assert_eq!(size_of_val(&1u32), 4);
assert_eq!(size_of_val(&1u64), 8);
}

#[test]
fn nonzero_size_of_basic() {
type Z = [i8, ..0];
assert_eq!(size_of::<Z>(), 0u);
assert_eq!(nonzero_size_of::<Z>(), 1u);
assert_eq!(nonzero_size_of::<uint>(), size_of::<uint>());
}

#[test]
fn nonzero_size_of_val_basic() {
let z = [0u8, ..0];
assert_eq!(size_of_val(&z), 0u);
assert_eq!(nonzero_size_of_val(&z), 1u);
assert_eq!(nonzero_size_of_val(&1u), size_of_val(&1u));
}

#[test]
fn align_of_basic() {
assert_eq!(pref_align_of::<u8>(), 1u);
assert_eq!(pref_align_of::<u16>(), 2u);
assert_eq!(pref_align_of::<u32>(), 4u);
}

#[test]
#[cfg(target_arch = "x86")]
#[cfg(target_arch = "arm")]
#[cfg(target_arch = "mips")]
fn align_of_32() {
assert_eq!(pref_align_of::<uint>(), 4u);
assert_eq!(pref_align_of::<*uint>(), 4u);
}

#[test]
#[cfg(target_arch = "x86_64")]
fn align_of_64() {
assert_eq!(pref_align_of::<uint>(), 8u);
assert_eq!(pref_align_of::<*uint>(), 8u);
}

#[test]
fn align_of_val_basic() {
assert_eq!(pref_align_of_val(&1u8), 1u);
assert_eq!(pref_align_of_val(&1u16), 2u);
assert_eq!(pref_align_of_val(&1u32), 4u);
}
}
6 changes: 3 additions & 3 deletions src/libstd/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ mod tests {

use num::*;
use num;
use sys;
use mem;

#[test]
fn test_num() {
Expand Down Expand Up @@ -1198,8 +1198,8 @@ mod tests {
#[test]
fn test_primitive() {
let none: Option<f32> = None;
assert_eq!(Primitive::bits(none), sys::size_of::<f32>() * 8);
assert_eq!(Primitive::bytes(none), sys::size_of::<f32>());
assert_eq!(Primitive::bits(none), mem::size_of::<f32>() * 8);
assert_eq!(Primitive::bytes(none), mem::size_of::<f32>());
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ mod tests {

use num::*;
use num;
use sys;
use mem;

#[test]
fn test_num() {
Expand Down Expand Up @@ -1249,8 +1249,8 @@ mod tests {
#[test]
fn test_primitive() {
let none: Option<f64> = None;
assert_eq!(Primitive::bits(none), sys::size_of::<f64>() * 8);
assert_eq!(Primitive::bytes(none), sys::size_of::<f64>());
assert_eq!(Primitive::bits(none), mem::size_of::<f64>() * 8);
assert_eq!(Primitive::bytes(none), mem::size_of::<f64>());
}

#[test]
Expand Down
Loading