auto merge of #12124 : brson/rust/intrinsics, r=thestinger

As mentioned https://github.com/mozilla/rust/pull/11956#issuecomment-34561655 I've taken some of the most commonly-used intrinsics and put them in a more logical place, reduced the amount of code looking in `unstable::intrinsics`.

r? @thestinger
This commit is contained in:
bors 2014-02-09 15:01:32 -08:00
commit 27f9c7951f
28 changed files with 131 additions and 103 deletions

View File

@ -176,8 +176,8 @@ and `free`:
~~~~ ~~~~
use std::cast; use std::cast;
use std::libc::{c_void, size_t, malloc, free}; use std::libc::{c_void, size_t, malloc, free};
use std::mem;
use std::ptr; use std::ptr;
use std::unstable::intrinsics;
// Define a wrapper around the handle returned by the foreign code. // Define a wrapper around the handle returned by the foreign code.
// Unique<T> has the same semantics as ~T // Unique<T> has the same semantics as ~T
@ -199,7 +199,7 @@ impl<T: Send> Unique<T> {
// `*ptr` is uninitialized, and `*ptr = value` would attempt to destroy it // `*ptr` is uninitialized, and `*ptr = value` would attempt to destroy it
// move_val_init moves a value into this memory without // move_val_init moves a value into this memory without
// attempting to drop the original value. // attempting to drop the original value.
intrinsics::move_val_init(&mut *ptr, value); mem::move_val_init(&mut *ptr, value);
Unique{ptr: ptr} Unique{ptr: ptr}
} }
} }
@ -226,7 +226,7 @@ impl<T: Send> Unique<T> {
impl<T: Send> Drop for Unique<T> { impl<T: Send> Drop for Unique<T> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
let x = intrinsics::uninit(); // dummy value to swap in let x = mem::uninit(); // dummy value to swap in
// We need to move the object out of the box, so that // We need to move the object out of the box, so that
// the destructor is called (at the end of this scope.) // the destructor is called (at the end of this scope.)
ptr::replace_ptr(self.ptr, x); ptr::replace_ptr(self.ptr, x);

View File

@ -32,10 +32,10 @@
use std::cast::{transmute, transmute_mut, transmute_mut_region}; use std::cast::{transmute, transmute_mut, transmute_mut_region};
use std::cast; use std::cast;
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use std::mem;
use std::num; use std::num;
use std::ptr; use std::ptr;
use std::kinds::marker; use std::kinds::marker;
use std::mem;
use std::rc::Rc; use std::rc::Rc;
use std::rt::global_heap; use std::rt::global_heap;
use std::unstable::intrinsics::{TyDesc, get_tydesc}; use std::unstable::intrinsics::{TyDesc, get_tydesc};
@ -216,7 +216,7 @@ fn alloc_pod<'a, T>(&'a mut self, op: || -> T) -> &'a T {
unsafe { unsafe {
let ptr = self.alloc_pod_inner(mem::size_of::<T>(), mem::min_align_of::<T>()); let ptr = self.alloc_pod_inner(mem::size_of::<T>(), mem::min_align_of::<T>());
let ptr: *mut T = transmute(ptr); let ptr: *mut T = transmute(ptr);
intrinsics::move_val_init(&mut (*ptr), op()); mem::move_val_init(&mut (*ptr), op());
return transmute(ptr); return transmute(ptr);
} }
} }
@ -278,7 +278,7 @@ fn alloc_nonpod<'a, T>(&'a mut self, op: || -> T) -> &'a T {
// has *not* been initialized yet. // has *not* been initialized yet.
*ty_ptr = transmute(tydesc); *ty_ptr = transmute(tydesc);
// Actually initialize it // Actually initialize it
intrinsics::move_val_init(&mut(*ptr), op()); mem::move_val_init(&mut(*ptr), op());
// Now that we are done, update the tydesc to indicate that // Now that we are done, update the tydesc to indicate that
// the object is there. // the object is there.
*ty_ptr = bitpack_tydesc_ptr(tydesc, true); *ty_ptr = bitpack_tydesc_ptr(tydesc, true);
@ -379,7 +379,7 @@ fn new<T>(next: Option<~TypedArenaChunk>, capacity: uint)
let mut chunk = unsafe { let mut chunk = unsafe {
let chunk = global_heap::exchange_malloc(size); let chunk = global_heap::exchange_malloc(size);
let mut chunk: ~TypedArenaChunk = cast::transmute(chunk); let mut chunk: ~TypedArenaChunk = cast::transmute(chunk);
intrinsics::move_val_init(&mut chunk.next, next); mem::move_val_init(&mut chunk.next, next);
chunk chunk
}; };
@ -466,7 +466,7 @@ pub fn alloc<'a>(&'a self, object: T) -> &'a T {
} }
let ptr: &'a mut T = cast::transmute(this.ptr); let ptr: &'a mut T = cast::transmute(this.ptr);
intrinsics::move_val_init(ptr, object); mem::move_val_init(ptr, object);
this.ptr = this.ptr.offset(1); this.ptr = this.ptr.offset(1);
let ptr: &'a T = ptr; let ptr: &'a T = ptr;
ptr ptr

View File

@ -13,7 +13,7 @@
#[allow(missing_doc)]; #[allow(missing_doc)];
use std::clone::Clone; use std::clone::Clone;
use std::unstable::intrinsics::{move_val_init, init}; use std::mem::{move_val_init, init};
use std::util::{replace, swap}; use std::util::{replace, swap};
use std::vec; use std::vec;

View File

@ -16,9 +16,9 @@
use std::io; use std::io;
use std::libc::{c_int, c_void}; use std::libc::{c_int, c_void};
use std::libc; use std::libc;
use std::mem;
use std::os; use std::os;
use std::rt::rtio; use std::rt::rtio;
use std::unstable::intrinsics;
use std::vec; use std::vec;
use io::{IoResult, retry}; use io::{IoResult, retry};
@ -147,7 +147,7 @@ fn pread(&mut self, buf: &mut [u8], offset: u64) -> Result<int, IoError> {
#[cfg(windows)] #[cfg(windows)]
fn os_pread(fd: c_int, buf: *u8, amt: uint, offset: u64) -> IoResult<int> { fn os_pread(fd: c_int, buf: *u8, amt: uint, offset: u64) -> IoResult<int> {
unsafe { unsafe {
let mut overlap: libc::OVERLAPPED = intrinsics::init(); let mut overlap: libc::OVERLAPPED = mem::init();
let handle = libc::get_osfhandle(fd) as libc::HANDLE; let handle = libc::get_osfhandle(fd) as libc::HANDLE;
let mut bytes_read = 0; let mut bytes_read = 0;
overlap.Offset = offset as libc::DWORD; overlap.Offset = offset as libc::DWORD;
@ -179,7 +179,7 @@ fn pwrite(&mut self, buf: &[u8], offset: u64) -> Result<(), IoError> {
#[cfg(windows)] #[cfg(windows)]
fn os_pwrite(fd: c_int, buf: *u8, amt: uint, offset: u64) -> IoResult<()> { fn os_pwrite(fd: c_int, buf: *u8, amt: uint, offset: u64) -> IoResult<()> {
unsafe { unsafe {
let mut overlap: libc::OVERLAPPED = intrinsics::init(); let mut overlap: libc::OVERLAPPED = mem::init();
let handle = libc::get_osfhandle(fd) as libc::HANDLE; let handle = libc::get_osfhandle(fd) as libc::HANDLE;
overlap.Offset = offset as libc::DWORD; overlap.Offset = offset as libc::DWORD;
overlap.OffsetHigh = (offset >> 32) as libc::DWORD; overlap.OffsetHigh = (offset >> 32) as libc::DWORD;
@ -867,7 +867,7 @@ pub fn stat(p: &CString) -> IoResult<io::FileStat> {
#[cfg(windows)] #[cfg(windows)]
fn os_stat(p: &CString) -> IoResult<io::FileStat> { fn os_stat(p: &CString) -> IoResult<io::FileStat> {
let mut stat: libc::stat = unsafe { intrinsics::uninit() }; let mut stat: libc::stat = unsafe { mem::uninit() };
as_utf16_p(p.as_str().unwrap(), |up| { as_utf16_p(p.as_str().unwrap(), |up| {
match retry(|| unsafe { libc::wstat(up, &mut stat) }) { match retry(|| unsafe { libc::wstat(up, &mut stat) }) {
0 => Ok(mkstat(&stat, p)), 0 => Ok(mkstat(&stat, p)),
@ -878,7 +878,7 @@ fn os_stat(p: &CString) -> IoResult<io::FileStat> {
#[cfg(unix)] #[cfg(unix)]
fn os_stat(p: &CString) -> IoResult<io::FileStat> { fn os_stat(p: &CString) -> IoResult<io::FileStat> {
let mut stat: libc::stat = unsafe { intrinsics::uninit() }; let mut stat: libc::stat = unsafe { mem::uninit() };
match retry(|| unsafe { libc::stat(p.with_ref(|p| p), &mut stat) }) { match retry(|| unsafe { libc::stat(p.with_ref(|p| p), &mut stat) }) {
0 => Ok(mkstat(&stat, p)), 0 => Ok(mkstat(&stat, p)),
_ => Err(super::last_error()), _ => Err(super::last_error()),
@ -897,7 +897,7 @@ fn os_lstat(_p: &CString) -> IoResult<io::FileStat> {
#[cfg(unix)] #[cfg(unix)]
fn os_lstat(p: &CString) -> IoResult<io::FileStat> { fn os_lstat(p: &CString) -> IoResult<io::FileStat> {
let mut stat: libc::stat = unsafe { intrinsics::uninit() }; let mut stat: libc::stat = unsafe { mem::uninit() };
match retry(|| unsafe { libc::lstat(p.with_ref(|p| p), &mut stat) }) { match retry(|| unsafe { libc::lstat(p.with_ref(|p| p), &mut stat) }) {
0 => Ok(mkstat(&stat, p)), 0 => Ok(mkstat(&stat, p)),
_ => Err(super::last_error()), _ => Err(super::last_error()),

View File

@ -15,7 +15,6 @@
use std::mem; use std::mem;
use std::rt::rtio; use std::rt::rtio;
use std::sync::arc::UnsafeArc; use std::sync::arc::UnsafeArc;
use std::unstable::intrinsics;
use super::{IoResult, retry}; use super::{IoResult, retry};
use super::file::keep_going; use super::file::keep_going;
@ -28,10 +27,10 @@
#[cfg(unix)] pub type sock_t = super::file::fd_t; #[cfg(unix)] pub type sock_t = super::file::fd_t;
pub fn htons(u: u16) -> u16 { pub fn htons(u: u16) -> u16 {
intrinsics::to_be16(u as i16) as u16 mem::to_be16(u as i16) as u16
} }
pub fn ntohs(u: u16) -> u16 { pub fn ntohs(u: u16) -> u16 {
intrinsics::from_be16(u as i16) as u16 mem::from_be16(u as i16) as u16
} }
enum InAddr { enum InAddr {
@ -68,7 +67,7 @@ fn ip_to_inaddr(ip: ip::IpAddr) -> InAddr {
fn addr_to_sockaddr(addr: ip::SocketAddr) -> (libc::sockaddr_storage, uint) { fn addr_to_sockaddr(addr: ip::SocketAddr) -> (libc::sockaddr_storage, uint) {
unsafe { unsafe {
let storage: libc::sockaddr_storage = intrinsics::init(); let storage: libc::sockaddr_storage = mem::init();
let len = match ip_to_inaddr(addr.ip) { let len = match ip_to_inaddr(addr.ip) {
InAddr(inaddr) => { InAddr(inaddr) => {
let storage: *mut libc::sockaddr_in = cast::transmute(&storage); let storage: *mut libc::sockaddr_in = cast::transmute(&storage);
@ -138,7 +137,7 @@ fn sockname(fd: sock_t,
*mut libc::socklen_t) -> libc::c_int) *mut libc::socklen_t) -> libc::c_int)
-> IoResult<ip::SocketAddr> -> IoResult<ip::SocketAddr>
{ {
let mut storage: libc::sockaddr_storage = unsafe { intrinsics::init() }; let mut storage: libc::sockaddr_storage = unsafe { mem::init() };
let mut len = mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t; let mut len = mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t;
unsafe { unsafe {
let storage = &mut storage as *mut libc::sockaddr_storage; let storage = &mut storage as *mut libc::sockaddr_storage;
@ -225,7 +224,7 @@ fn WSAStartup(wVersionRequested: libc::WORD,
LOCK.lock(); LOCK.lock();
if !INITIALIZED { if !INITIALIZED {
let mut data: WSADATA = intrinsics::init(); let mut data: WSADATA = mem::init();
let ret = WSAStartup(0x202, // version 2.2 let ret = WSAStartup(0x202, // version 2.2
&mut data); &mut data);
assert_eq!(ret, 0); assert_eq!(ret, 0);
@ -438,7 +437,7 @@ pub fn fd(&self) -> sock_t { self.listener.fd() }
pub fn native_accept(&mut self) -> IoResult<TcpStream> { pub fn native_accept(&mut self) -> IoResult<TcpStream> {
unsafe { unsafe {
let mut storage: libc::sockaddr_storage = intrinsics::init(); let mut storage: libc::sockaddr_storage = mem::init();
let storagep = &mut storage as *mut libc::sockaddr_storage; let storagep = &mut storage as *mut libc::sockaddr_storage;
let size = mem::size_of::<libc::sockaddr_storage>(); let size = mem::size_of::<libc::sockaddr_storage>();
let mut size = size as libc::socklen_t; let mut size = size as libc::socklen_t;
@ -543,7 +542,7 @@ fn socket_name(&mut self) -> IoResult<ip::SocketAddr> {
impl rtio::RtioUdpSocket for UdpSocket { impl rtio::RtioUdpSocket for UdpSocket {
fn recvfrom(&mut self, buf: &mut [u8]) -> IoResult<(uint, ip::SocketAddr)> { fn recvfrom(&mut self, buf: &mut [u8]) -> IoResult<(uint, ip::SocketAddr)> {
unsafe { unsafe {
let mut storage: libc::sockaddr_storage = intrinsics::init(); let mut storage: libc::sockaddr_storage = mem::init();
let storagep = &mut storage as *mut libc::sockaddr_storage; let storagep = &mut storage as *mut libc::sockaddr_storage;
let mut addrlen: libc::socklen_t = let mut addrlen: libc::socklen_t =
mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t; mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t;

View File

@ -49,11 +49,11 @@
use std::comm::Data; use std::comm::Data;
use std::hashmap::HashMap; use std::hashmap::HashMap;
use std::libc; use std::libc;
use std::mem;
use std::os; use std::os;
use std::ptr; use std::ptr;
use std::rt::rtio; use std::rt::rtio;
use std::sync::atomics; use std::sync::atomics;
use std::unstable::intrinsics;
use io::file::FileDesc; use io::file::FileDesc;
use io::IoResult; use io::IoResult;
@ -87,17 +87,17 @@ pub enum Req {
// returns the current time (in milliseconds) // returns the current time (in milliseconds)
fn now() -> u64 { fn now() -> u64 {
unsafe { unsafe {
let mut now: libc::timeval = intrinsics::init(); let mut now: libc::timeval = mem::init();
assert_eq!(imp::gettimeofday(&mut now, ptr::null()), 0); assert_eq!(imp::gettimeofday(&mut now, ptr::null()), 0);
return (now.tv_sec as u64) * 1000 + (now.tv_usec as u64) / 1000; return (now.tv_sec as u64) * 1000 + (now.tv_usec as u64) / 1000;
} }
} }
fn helper(input: libc::c_int, messages: Port<Req>) { fn helper(input: libc::c_int, messages: Port<Req>) {
let mut set: imp::fd_set = unsafe { intrinsics::init() }; let mut set: imp::fd_set = unsafe { mem::init() };
let mut fd = FileDesc::new(input, true); let mut fd = FileDesc::new(input, true);
let mut timeout: libc::timeval = unsafe { intrinsics::init() }; let mut timeout: libc::timeval = unsafe { mem::init() };
// active timers are those which are able to be selected upon (and it's a // active timers are those which are able to be selected upon (and it's a
// sorted list, and dead timers are those which have expired, but ownership // sorted list, and dead timers are those which have expired, but ownership

View File

@ -34,7 +34,7 @@
use std::os; use std::os;
use std::rt::rtio; use std::rt::rtio;
use std::hashmap::HashMap; use std::hashmap::HashMap;
use std::unstable::intrinsics; use std::mem;
use io::file::FileDesc; use io::file::FileDesc;
use io::IoResult; use io::IoResult;
@ -75,7 +75,7 @@ fn del(efd: libc::c_int, fd: libc::c_int) {
} }
add(efd, input); add(efd, input);
let events: [imp::epoll_event, ..16] = unsafe { intrinsics::init() }; let events: [imp::epoll_event, ..16] = unsafe { mem::init() };
let mut map: HashMap<libc::c_int, (Chan<()>, bool)> = HashMap::new(); let mut map: HashMap<libc::c_int, (Chan<()>, bool)> = HashMap::new();
'outer: loop { 'outer: loop {
let n = match unsafe { let n = match unsafe {

View File

@ -22,7 +22,7 @@
/// format. /// format.
fn write_u32_be(dst: &mut[u8], input: u32) { fn write_u32_be(dst: &mut[u8], input: u32) {
use std::cast::transmute; use std::cast::transmute;
use std::unstable::intrinsics::to_be32; use std::mem::to_be32;
assert!(dst.len() == 4); assert!(dst.len() == 4);
unsafe { unsafe {
let x: *mut i32 = transmute(dst.unsafe_mut_ref(0)); let x: *mut i32 = transmute(dst.unsafe_mut_ref(0));
@ -33,7 +33,7 @@ fn write_u32_be(dst: &mut[u8], input: u32) {
/// Read a vector of bytes into a vector of u32s. The values are read in big-endian format. /// Read a vector of bytes into a vector of u32s. The values are read in big-endian format.
fn read_u32v_be(dst: &mut[u32], input: &[u8]) { fn read_u32v_be(dst: &mut[u32], input: &[u8]) {
use std::cast::transmute; use std::cast::transmute;
use std::unstable::intrinsics::to_be32; use std::mem::to_be32;
assert!(dst.len() * 4 == input.len()); assert!(dst.len() * 4 == input.len());
unsafe { unsafe {
let mut x: *mut i32 = transmute(dst.unsafe_mut_ref(0)); let mut x: *mut i32 = transmute(dst.unsafe_mut_ref(0));

View File

@ -28,6 +28,7 @@
use std::fmt; use std::fmt;
use std::io; use std::io;
use std::libc; use std::libc;
use std::mem;
use std::str; use std::str;
use std::unstable::intrinsics; use std::unstable::intrinsics;
use std::vec; use std::vec;
@ -144,7 +145,7 @@ pub fn render(w: &mut io::Writer, s: &str) -> fmt::Result {
flags: 0, flags: 0,
link_attributes: None, link_attributes: None,
}; };
let mut callbacks: sd_callbacks = intrinsics::init(); let mut callbacks: sd_callbacks = mem::init();
sdhtml_renderer(&callbacks, &options, 0); sdhtml_renderer(&callbacks, &options, 0);
let opaque = my_opaque { let opaque = my_opaque {
@ -197,7 +198,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
MKDEXT_STRIKETHROUGH; MKDEXT_STRIKETHROUGH;
let callbacks = sd_callbacks { let callbacks = sd_callbacks {
blockcode: block, blockcode: block,
other: intrinsics::init() other: mem::init()
}; };
let tests = tests as *mut ::test::Collector as *libc::c_void; let tests = tests as *mut ::test::Collector as *libc::c_void;

View File

@ -17,7 +17,6 @@
use std::ptr; use std::ptr;
use std::rt::rtio; use std::rt::rtio;
use std::rt::task::BlockedTask; use std::rt::task::BlockedTask;
use std::unstable::intrinsics;
use access::Access; use access::Access;
use homing::{HomingIO, HomeHandle}; use homing::{HomingIO, HomeHandle};
@ -33,8 +32,8 @@
/// Generic functions related to dealing with sockaddr things /// Generic functions related to dealing with sockaddr things
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
pub fn htons(u: u16) -> u16 { intrinsics::to_be16(u as i16) as u16 } pub fn htons(u: u16) -> u16 { mem::to_be16(u as i16) as u16 }
pub fn ntohs(u: u16) -> u16 { intrinsics::from_be16(u as i16) as u16 } pub fn ntohs(u: u16) -> u16 { mem::from_be16(u as i16) as u16 }
pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage, pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
len: uint) -> ip::SocketAddr { len: uint) -> ip::SocketAddr {
@ -80,7 +79,7 @@ pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
fn addr_to_sockaddr(addr: ip::SocketAddr) -> (libc::sockaddr_storage, uint) { fn addr_to_sockaddr(addr: ip::SocketAddr) -> (libc::sockaddr_storage, uint) {
unsafe { unsafe {
let mut storage: libc::sockaddr_storage = intrinsics::init(); let mut storage: libc::sockaddr_storage = mem::init();
let len = match addr.ip { let len = match addr.ip {
ip::Ipv4Addr(a, b, c, d) => { ip::Ipv4Addr(a, b, c, d) => {
let storage: &mut libc::sockaddr_in = let storage: &mut libc::sockaddr_in =
@ -134,7 +133,7 @@ fn socket_name(sk: SocketNameKind,
}; };
// Allocate a sockaddr_storage since we don't know if it's ipv4 or ipv6 // Allocate a sockaddr_storage since we don't know if it's ipv4 or ipv6
let mut sockaddr: libc::sockaddr_storage = unsafe { intrinsics::init() }; let mut sockaddr: libc::sockaddr_storage = unsafe { mem::init() };
let mut namelen = mem::size_of::<libc::sockaddr_storage>() as c_int; let mut namelen = mem::size_of::<libc::sockaddr_storage>() as c_int;
let sockaddr_p = &mut sockaddr as *mut libc::sockaddr_storage; let sockaddr_p = &mut sockaddr as *mut libc::sockaddr_storage;

View File

@ -132,7 +132,7 @@ fn vuint_at_slow(data: &[u8], start: uint) -> Res {
pub fn vuint_at(data: &[u8], start: uint) -> Res { pub fn vuint_at(data: &[u8], start: uint) -> Res {
use std::ptr::offset; use std::ptr::offset;
use std::unstable::intrinsics::from_be32; use std::mem::from_be32;
if data.len() - start < 4 { if data.len() - start < 4 {
return vuint_at_slow(data, start); return vuint_at_slow(data, start);

View File

@ -70,6 +70,7 @@
use ops::Drop; use ops::Drop;
use cmp::Eq; use cmp::Eq;
use clone::Clone; use clone::Clone;
use mem;
use option::{Option, Some, None}; use option::{Option, Some, None};
use ptr::RawPtr; use ptr::RawPtr;
use ptr; use ptr;
@ -77,7 +78,6 @@
use str; use str;
use vec::{ImmutableVector, MutableVector}; use vec::{ImmutableVector, MutableVector};
use vec; use vec;
use unstable::intrinsics;
use rt::global_heap::malloc_raw; use rt::global_heap::malloc_raw;
/// The representation of a C String. /// The representation of a C String.
@ -327,7 +327,7 @@ unsafe fn with_c_str_unchecked<T>(&self, f: |*libc::c_char| -> T) -> T {
// Unsafe function that handles possibly copying the &[u8] into a stack array. // Unsafe function that handles possibly copying the &[u8] into a stack array.
unsafe fn with_c_str<T>(v: &[u8], checked: bool, f: |*libc::c_char| -> T) -> T { unsafe fn with_c_str<T>(v: &[u8], checked: bool, f: |*libc::c_char| -> T) -> T {
if v.len() < BUF_LEN { if v.len() < BUF_LEN {
let mut buf: [u8, .. BUF_LEN] = intrinsics::uninit(); let mut buf: [u8, .. BUF_LEN] = mem::uninit();
vec::bytes::copy_memory(buf, v); vec::bytes::copy_memory(buf, v);
buf[v.len()] = 0; buf[v.len()] = 0;

View File

@ -18,7 +18,7 @@
/// Casts the value at `src` to U. The two types must have the same length. /// Casts the value at `src` to U. The two types must have the same length.
#[inline] #[inline]
pub unsafe fn transmute_copy<T, U>(src: &T) -> U { pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
let mut dest: U = intrinsics::uninit(); let mut dest: U = mem::uninit();
let dest_ptr: *mut u8 = transmute(&mut dest); let dest_ptr: *mut u8 = transmute(&mut dest);
let src_ptr: *u8 = transmute(src); let src_ptr: *u8 = transmute(src);
copy_nonoverlapping_memory(dest_ptr, src_ptr, mem::size_of::<U>()); copy_nonoverlapping_memory(dest_ptr, src_ptr, mem::size_of::<U>());

View File

@ -51,7 +51,7 @@ fn next(&mut self) -> Option<u8> {
} }
pub fn u64_to_le_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T { pub fn u64_to_le_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
use unstable::intrinsics::{to_le16, to_le32, to_le64}; use mem::{to_le16, to_le32, to_le64};
use cast::transmute; use cast::transmute;
// LLVM fails to properly optimize this when using shifts instead of the to_le* intrinsics // LLVM fails to properly optimize this when using shifts instead of the to_le* intrinsics
@ -77,7 +77,7 @@ pub fn u64_to_le_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
} }
pub fn u64_to_be_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T { pub fn u64_to_be_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
use unstable::intrinsics::{to_be16, to_be32, to_be64}; use mem::{to_be16, to_be32, to_be64};
use cast::transmute; use cast::transmute;
// LLVM fails to properly optimize this when using shifts instead of the to_be* intrinsics // LLVM fails to properly optimize this when using shifts instead of the to_be* intrinsics
@ -105,7 +105,7 @@ pub fn u64_from_be_bytes(data: &[u8],
size: uint) size: uint)
-> u64 { -> u64 {
use ptr::{copy_nonoverlapping_memory, offset, mut_offset}; use ptr::{copy_nonoverlapping_memory, offset, mut_offset};
use unstable::intrinsics::from_be64; use mem::from_be64;
use vec::MutableVector; use vec::MutableVector;
assert!(size <= 8u); assert!(size <= 8u);

View File

@ -8,9 +8,15 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
//! Functions relating to memory layout //! Basic functions for dealing with memory
//!
//! This module contains functions for querying the size and alignment of
//! types, initializing and manipulating memory.
#[allow(missing_doc)]; // FIXME
use unstable::intrinsics; use unstable::intrinsics;
use unstable::intrinsics::{bswap16, bswap32, bswap64};
/// Returns the size of a type /// Returns the size of a type
#[inline] #[inline]
@ -68,6 +74,58 @@ pub fn pref_align_of_val<T>(_val: &T) -> uint {
pref_align_of::<T>() pref_align_of::<T>()
} }
/// Create a value initialized to zero.
///
/// `init` is unsafe because it returns a zeroed-out datum,
/// which is unsafe unless T is Pod.
#[inline]
pub unsafe fn init<T>() -> T {
intrinsics::init()
}
/// Create an uninitialized value.
#[inline]
pub unsafe fn uninit<T>() -> T {
intrinsics::uninit()
}
/// Move a value to an uninitialized memory location.
///
/// Drop glue is not run on the destination.
#[inline]
pub unsafe fn move_val_init<T>(dst: &mut T, src: T) {
intrinsics::move_val_init(dst, src)
}
#[cfg(target_endian = "little")] #[inline] pub fn to_le16(x: i16) -> i16 { x }
#[cfg(target_endian = "big")] #[inline] pub fn to_le16(x: i16) -> i16 { unsafe { bswap16(x) } }
#[cfg(target_endian = "little")] #[inline] pub fn to_le32(x: i32) -> i32 { x }
#[cfg(target_endian = "big")] #[inline] pub fn to_le32(x: i32) -> i32 { unsafe { bswap32(x) } }
#[cfg(target_endian = "little")] #[inline] pub fn to_le64(x: i64) -> i64 { x }
#[cfg(target_endian = "big")] #[inline] pub fn to_le64(x: i64) -> i64 { unsafe { bswap64(x) } }
#[cfg(target_endian = "little")] #[inline] pub fn to_be16(x: i16) -> i16 { unsafe { bswap16(x) } }
#[cfg(target_endian = "big")] #[inline] pub fn to_be16(x: i16) -> i16 { x }
#[cfg(target_endian = "little")] #[inline] pub fn to_be32(x: i32) -> i32 { unsafe { bswap32(x) } }
#[cfg(target_endian = "big")] #[inline] pub fn to_be32(x: i32) -> i32 { x }
#[cfg(target_endian = "little")] #[inline] pub fn to_be64(x: i64) -> i64 { unsafe { bswap64(x) } }
#[cfg(target_endian = "big")] #[inline] pub fn to_be64(x: i64) -> i64 { x }
#[cfg(target_endian = "little")] #[inline] pub fn from_le16(x: i16) -> i16 { x }
#[cfg(target_endian = "big")] #[inline] pub fn from_le16(x: i16) -> i16 { unsafe { bswap16(x) } }
#[cfg(target_endian = "little")] #[inline] pub fn from_le32(x: i32) -> i32 { x }
#[cfg(target_endian = "big")] #[inline] pub fn from_le32(x: i32) -> i32 { unsafe { bswap32(x) } }
#[cfg(target_endian = "little")] #[inline] pub fn from_le64(x: i64) -> i64 { x }
#[cfg(target_endian = "big")] #[inline] pub fn from_le64(x: i64) -> i64 { unsafe { bswap64(x) } }
#[cfg(target_endian = "little")] #[inline] pub fn from_be16(x: i16) -> i16 { unsafe { bswap16(x) } }
#[cfg(target_endian = "big")] #[inline] pub fn from_be16(x: i16) -> i16 { x }
#[cfg(target_endian = "little")] #[inline] pub fn from_be32(x: i32) -> i32 { unsafe { bswap32(x) } }
#[cfg(target_endian = "big")] #[inline] pub fn from_be32(x: i32) -> i32 { x }
#[cfg(target_endian = "little")] #[inline] pub fn from_be64(x: i64) -> i64 { unsafe { bswap64(x) } }
#[cfg(target_endian = "big")] #[inline] pub fn from_be64(x: i64) -> i64 { x }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use mem::*; use mem::*;

View File

@ -15,6 +15,7 @@
#[cfg(not(test))] #[cfg(not(test))]
use cmp::Equiv; use cmp::Equiv;
use iter::{range, Iterator}; use iter::{range, Iterator};
use mem;
use option::{Option, Some, None}; use option::{Option, Some, None};
use unstable::intrinsics; use unstable::intrinsics;
use util::swap; use util::swap;
@ -132,7 +133,7 @@ pub unsafe fn zero_memory<T>(dst: *mut T, count: uint) {
#[inline] #[inline]
pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) { pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
// Give ourselves some scratch space to work with // Give ourselves some scratch space to work with
let mut tmp: T = intrinsics::uninit(); let mut tmp: T = mem::uninit();
let t: *mut T = &mut tmp; let t: *mut T = &mut tmp;
// Perform the swap // Perform the swap
@ -160,7 +161,7 @@ pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T {
*/ */
#[inline(always)] #[inline(always)]
pub unsafe fn read_ptr<T>(src: *T) -> T { pub unsafe fn read_ptr<T>(src: *T) -> T {
let mut tmp: T = intrinsics::uninit(); let mut tmp: T = mem::uninit();
copy_nonoverlapping_memory(&mut tmp, src, 1); copy_nonoverlapping_memory(&mut tmp, src, 1);
tmp tmp
} }

View File

@ -205,17 +205,17 @@ mod imp {
use cmp; use cmp;
use libc::consts::os::posix01::{PTHREAD_CREATE_JOINABLE, PTHREAD_STACK_MIN}; use libc::consts::os::posix01::{PTHREAD_CREATE_JOINABLE, PTHREAD_STACK_MIN};
use libc; use libc;
use mem;
use os; use os;
use ptr; use ptr;
use unstable::intrinsics;
use unstable::stack::RED_ZONE; use unstable::stack::RED_ZONE;
pub type rust_thread = libc::pthread_t; pub type rust_thread = libc::pthread_t;
pub type rust_thread_return = *u8; pub type rust_thread_return = *u8;
pub unsafe fn create(stack: uint, p: ~proc()) -> rust_thread { pub unsafe fn create(stack: uint, p: ~proc()) -> rust_thread {
let mut native: libc::pthread_t = intrinsics::uninit(); let mut native: libc::pthread_t = mem::uninit();
let mut attr: libc::pthread_attr_t = intrinsics::uninit(); let mut attr: libc::pthread_attr_t = mem::uninit();
assert_eq!(pthread_attr_init(&mut attr), 0); assert_eq!(pthread_attr_init(&mut attr), 0);
assert_eq!(pthread_attr_setdetachstate(&mut attr, assert_eq!(pthread_attr_setdetachstate(&mut attr,
PTHREAD_CREATE_JOINABLE), 0); PTHREAD_CREATE_JOINABLE), 0);

View File

@ -16,7 +16,7 @@
use mem; use mem;
use uint; use uint;
use util::replace; use util::replace;
use unstable::intrinsics::init; use mem::init;
use vec; use vec;
use ptr::RawPtr; use ptr::RawPtr;
use vec::{ImmutableVector, Items, MutableVector, MutItems, OwnedVector}; use vec::{ImmutableVector, Items, MutableVector, MutItems, OwnedVector};

View File

@ -288,8 +288,7 @@ fn visit_leave_fn(&mut self, purity: uint, proto: uint,
/// Create a value initialized to zero. /// Create a value initialized to zero.
/// ///
/// `init` is unsafe because it returns a zeroed-out datum, /// `init` is unsafe because it returns a zeroed-out datum,
/// which is unsafe unless T is POD. We don't have a POD /// which is unsafe unless T is Pod.
/// kind yet. (See #4074).
pub fn init<T>() -> T; pub fn init<T>() -> T;
/// Create an uninitialized value. /// Create an uninitialized value.
@ -440,33 +439,6 @@ fn visit_leave_fn(&mut self, purity: uint, proto: uint,
pub fn u64_mul_with_overflow(x: u64, y: u64) -> (u64, bool); pub fn u64_mul_with_overflow(x: u64, y: u64) -> (u64, bool);
} }
#[cfg(target_endian = "little")] #[inline] pub fn to_le16(x: i16) -> i16 { x }
#[cfg(target_endian = "big")] #[inline] pub fn to_le16(x: i16) -> i16 { unsafe { bswap16(x) } }
#[cfg(target_endian = "little")] #[inline] pub fn to_le32(x: i32) -> i32 { x }
#[cfg(target_endian = "big")] #[inline] pub fn to_le32(x: i32) -> i32 { unsafe { bswap32(x) } }
#[cfg(target_endian = "little")] #[inline] pub fn to_le64(x: i64) -> i64 { x }
#[cfg(target_endian = "big")] #[inline] pub fn to_le64(x: i64) -> i64 { unsafe { bswap64(x) } }
#[cfg(target_endian = "little")] #[inline] pub fn to_be16(x: i16) -> i16 { unsafe { bswap16(x) } }
#[cfg(target_endian = "big")] #[inline] pub fn to_be16(x: i16) -> i16 { x }
#[cfg(target_endian = "little")] #[inline] pub fn to_be32(x: i32) -> i32 { unsafe { bswap32(x) } }
#[cfg(target_endian = "big")] #[inline] pub fn to_be32(x: i32) -> i32 { x }
#[cfg(target_endian = "little")] #[inline] pub fn to_be64(x: i64) -> i64 { unsafe { bswap64(x) } }
#[cfg(target_endian = "big")] #[inline] pub fn to_be64(x: i64) -> i64 { x }
#[cfg(target_endian = "little")] #[inline] pub fn from_le16(x: i16) -> i16 { x }
#[cfg(target_endian = "big")] #[inline] pub fn from_le16(x: i16) -> i16 { unsafe { bswap16(x) } }
#[cfg(target_endian = "little")] #[inline] pub fn from_le32(x: i32) -> i32 { x }
#[cfg(target_endian = "big")] #[inline] pub fn from_le32(x: i32) -> i32 { unsafe { bswap32(x) } }
#[cfg(target_endian = "little")] #[inline] pub fn from_le64(x: i64) -> i64 { x }
#[cfg(target_endian = "big")] #[inline] pub fn from_le64(x: i64) -> i64 { unsafe { bswap64(x) } }
#[cfg(target_endian = "little")] #[inline] pub fn from_be16(x: i16) -> i16 { unsafe { bswap16(x) } }
#[cfg(target_endian = "big")] #[inline] pub fn from_be16(x: i16) -> i16 { x }
#[cfg(target_endian = "little")] #[inline] pub fn from_be32(x: i32) -> i32 { unsafe { bswap32(x) } }
#[cfg(target_endian = "big")] #[inline] pub fn from_be32(x: i32) -> i32 { x }
#[cfg(target_endian = "little")] #[inline] pub fn from_be64(x: i64) -> i64 { unsafe { bswap64(x) } }
#[cfg(target_endian = "big")] #[inline] pub fn from_be64(x: i64) -> i64 { x }
/// `TypeId` represents a globally unique identifier for a type /// `TypeId` represents a globally unique identifier for a type
#[lang="type_id"] // This needs to be kept in lockstep with the code in trans/intrinsic.rs and #[lang="type_id"] // This needs to be kept in lockstep with the code in trans/intrinsic.rs and

View File

@ -92,7 +92,7 @@ mod imp {
use libc; use libc;
use self::os::{PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, use self::os::{PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER,
pthread_mutex_t, pthread_cond_t}; pthread_mutex_t, pthread_cond_t};
use unstable::intrinsics; use mem;
type pthread_mutexattr_t = libc::c_void; type pthread_mutexattr_t = libc::c_void;
type pthread_condattr_t = libc::c_void; type pthread_condattr_t = libc::c_void;
@ -208,8 +208,8 @@ pub struct Mutex {
impl Mutex { impl Mutex {
pub unsafe fn new() -> Mutex { pub unsafe fn new() -> Mutex {
let mut m = Mutex { let mut m = Mutex {
lock: intrinsics::init(), lock: mem::init(),
cond: intrinsics::init(), cond: mem::init(),
}; };
pthread_mutex_init(&mut m.lock, 0 as *libc::c_void); pthread_mutex_init(&mut m.lock, 0 as *libc::c_void);

View File

@ -11,8 +11,8 @@
//! Miscellaneous helpers for common patterns //! Miscellaneous helpers for common patterns
use cast; use cast;
use mem;
use ptr; use ptr;
use unstable::intrinsics;
/// The identity function. /// The identity function.
#[inline] #[inline]
@ -26,7 +26,7 @@ pub fn id<T>(x: T) -> T { x }
pub fn swap<T>(x: &mut T, y: &mut T) { pub fn swap<T>(x: &mut T, y: &mut T) {
unsafe { unsafe {
// Give ourselves some scratch space to work with // Give ourselves some scratch space to work with
let mut tmp: T = intrinsics::uninit(); let mut tmp: T = mem::uninit();
let t: *mut T = &mut tmp; let t: *mut T = &mut tmp;
// Perform the swap, `&mut` pointers never alias // Perform the swap, `&mut` pointers never alias

View File

@ -120,7 +120,6 @@
use kinds::marker; use kinds::marker;
use uint; use uint;
use unstable::finally::Finally; use unstable::finally::Finally;
use unstable::intrinsics;
use unstable::raw::{Repr, Slice, Vec}; use unstable::raw::{Repr, Slice, Vec};
use util; use util;
@ -137,7 +136,7 @@ pub fn from_fn<T>(n_elts: uint, op: |uint| -> T) -> ~[T] {
let mut i: uint = 0u; let mut i: uint = 0u;
(|| { (|| {
while i < n_elts { while i < n_elts {
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i as int)), op(i)); mem::move_val_init(&mut(*ptr::mut_offset(p, i as int)), op(i));
i += 1u; i += 1u;
} }
}).finally(|| { }).finally(|| {
@ -164,7 +163,7 @@ pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> ~[T] {
let mut i = 0u; let mut i = 0u;
(|| { (|| {
while i < n_elts { while i < n_elts {
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i as int)), t.clone()); mem::move_val_init(&mut(*ptr::mut_offset(p, i as int)), t.clone());
i += 1u; i += 1u;
} }
}).finally(|| { }).finally(|| {
@ -1495,7 +1494,7 @@ unsafe fn push_fast<T>(this: &mut ~[T], t: T) {
(**repr).fill += mem::nonzero_size_of::<T>(); (**repr).fill += mem::nonzero_size_of::<T>();
let p = to_unsafe_ptr(&((**repr).data)); let p = to_unsafe_ptr(&((**repr).data));
let p = ptr::offset(p, fill as int) as *mut T; let p = ptr::offset(p, fill as int) as *mut T;
intrinsics::move_val_init(&mut(*p), t); mem::move_val_init(&mut(*p), t);
} }
} }
@ -1552,7 +1551,7 @@ fn insert(&mut self, i: uint, x: T) {
ptr::copy_memory(p.offset(1), p, len - i); ptr::copy_memory(p.offset(1), p, len - i);
// Write it in, overwriting the first copy of the `i`th // Write it in, overwriting the first copy of the `i`th
// element. // element.
intrinsics::move_val_init(&mut *p, x); mem::move_val_init(&mut *p, x);
self.set_len(len + 1); self.set_len(len + 1);
} }
} }
@ -2397,7 +2396,7 @@ unsafe fn unsafe_set(self, index: uint, val: T) {
#[inline] #[inline]
unsafe fn init_elem(self, i: uint, val: T) { unsafe fn init_elem(self, i: uint, val: T) {
intrinsics::move_val_init(&mut (*self.as_mut_ptr().offset(i as int)), val); mem::move_val_init(&mut (*self.as_mut_ptr().offset(i as int)), val);
} }
#[inline] #[inline]

View File

@ -17,14 +17,13 @@
use iter::{DoubleEndedIterator, Iterator}; use iter::{DoubleEndedIterator, Iterator};
use num::CheckedMul; use num::CheckedMul;
use container::Container; use container::Container;
use mem::size_of; use mem::{size_of, move_val_init};
use cast::{forget, transmute}; use cast::{forget, transmute};
use rt::global_heap::{malloc_raw, realloc_raw}; use rt::global_heap::{malloc_raw, realloc_raw};
use vec::{ImmutableVector, Items, MutableVector}; use vec::{ImmutableVector, Items, MutableVector};
use unstable::raw::Slice; use unstable::raw::Slice;
use ptr::{offset, read_ptr}; use ptr::{offset, read_ptr};
use libc::{free, c_void}; use libc::{free, c_void};
use unstable::intrinsics::move_val_init;
pub struct Vec<T> { pub struct Vec<T> {
priv len: uint, priv len: uint,

View File

@ -203,7 +203,7 @@ pub fn new_v4() -> Uuid {
/// * `d3` A 16-bit word /// * `d3` A 16-bit word
/// * `d4` Array of 8 octets /// * `d4` Array of 8 octets
pub fn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8]) -> Uuid { pub fn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8]) -> Uuid {
use std::unstable::intrinsics::{to_be16, to_be32}; use std::mem::{to_be16, to_be32};
// First construct a temporary field-based struct // First construct a temporary field-based struct
let mut fields = UuidFields { let mut fields = UuidFields {
@ -329,7 +329,7 @@ pub fn to_simple_str(&self) -> ~str {
/// ///
/// Example: `550e8400-e29b-41d4-a716-446655440000` /// Example: `550e8400-e29b-41d4-a716-446655440000`
pub fn to_hyphenated_str(&self) -> ~str { pub fn to_hyphenated_str(&self) -> ~str {
use std::unstable::intrinsics::{to_be16, to_be32}; use std::mem::{to_be16, to_be32};
// Convert to field-based struct as it matches groups in output. // Convert to field-based struct as it matches groups in output.
// Ensure fields are in network byte order, as per RFC. // Ensure fields are in network byte order, as per RFC.
let mut uf: UuidFields; let mut uf: UuidFields;

View File

@ -16,8 +16,8 @@
use std::i32::range; use std::i32::range;
use std::libc::{STDIN_FILENO, c_int, fdopen, fgets, fileno, fopen, fstat}; use std::libc::{STDIN_FILENO, c_int, fdopen, fgets, fileno, fopen, fstat};
use std::libc::{stat, strlen}; use std::libc::{stat, strlen};
use std::mem::init;
use std::ptr::null; use std::ptr::null;
use std::unstable::intrinsics::init;
use std::vec::{reverse}; use std::vec::{reverse};
static LINE_LEN: uint = 80; static LINE_LEN: uint = 80;

View File

@ -10,5 +10,5 @@
enum v {} enum v {}
pub fn main() { pub fn main() {
let y: v = unsafe { ::std::unstable::intrinsics::uninit() }; let y: v = unsafe { ::std::mem::uninit() };
} }

View File

@ -8,12 +8,12 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use std::unstable; use std::mem;
pub fn main() { pub fn main() {
unsafe { unsafe {
let mut x: bool = false; let mut x: bool = false;
// this line breaks it // this line breaks it
unstable::intrinsics::move_val_init(&mut x, false); mem::move_val_init(&mut x, false);
} }
} }

View File

@ -11,14 +11,14 @@
// Test the uninit() construct returning various empty types. // Test the uninit() construct returning various empty types.
use std::vec; use std::vec;
use std::unstable::intrinsics; use std::mem;
#[deriving(Clone)] #[deriving(Clone)]
struct Foo; struct Foo;
pub fn main() { pub fn main() {
unsafe { unsafe {
let _x: Foo = intrinsics::uninit(); let _x: Foo = mem::uninit();
let _x: [Foo, ..2] = intrinsics::uninit(); let _x: [Foo, ..2] = mem::uninit();
} }
} }