std: Add init and uninit to mem. Replace direct intrinsic usage

This commit is contained in:
Brian Anderson 2014-02-08 02:46:55 -08:00
parent 49ac48db3f
commit d433b80e02
19 changed files with 60 additions and 43 deletions

View File

@ -176,6 +176,7 @@ and `free`:
~~~~
use std::cast;
use std::libc::{c_void, size_t, malloc, free};
use std::mem;
use std::ptr;
use std::unstable::intrinsics;
@ -226,7 +227,7 @@ impl<T: Send> Unique<T> {
impl<T: Send> Drop for Unique<T> {
fn drop(&mut self) {
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
// the destructor is called (at the end of this scope.)
ptr::replace_ptr(self.ptr, x);

View File

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

View File

@ -68,7 +68,7 @@ fn ip_to_inaddr(ip: ip::IpAddr) -> InAddr {
fn addr_to_sockaddr(addr: ip::SocketAddr) -> (libc::sockaddr_storage, uint) {
unsafe {
let storage: libc::sockaddr_storage = intrinsics::init();
let storage: libc::sockaddr_storage = mem::init();
let len = match ip_to_inaddr(addr.ip) {
InAddr(inaddr) => {
let storage: *mut libc::sockaddr_in = cast::transmute(&storage);
@ -138,7 +138,7 @@ fn sockname(fd: sock_t,
*mut libc::socklen_t) -> libc::c_int)
-> 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;
unsafe {
let storage = &mut storage as *mut libc::sockaddr_storage;
@ -225,7 +225,7 @@ pub fn init() {
LOCK.lock();
if !INITIALIZED {
let mut data: WSADATA = intrinsics::init();
let mut data: WSADATA = mem::init();
let ret = WSAStartup(0x202, // version 2.2
&mut data);
assert_eq!(ret, 0);
@ -438,7 +438,7 @@ impl TcpAcceptor {
pub fn native_accept(&mut self) -> IoResult<TcpStream> {
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 size = mem::size_of::<libc::sockaddr_storage>();
let mut size = size as libc::socklen_t;
@ -543,7 +543,7 @@ impl rtio::RtioSocket for UdpSocket {
impl rtio::RtioUdpSocket for UdpSocket {
fn recvfrom(&mut self, buf: &mut [u8]) -> IoResult<(uint, ip::SocketAddr)> {
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 mut addrlen: 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::hashmap::HashMap;
use std::libc;
use std::mem;
use std::os;
use std::ptr;
use std::rt::rtio;
use std::sync::atomics;
use std::unstable::intrinsics;
use io::file::FileDesc;
use io::IoResult;
@ -87,17 +87,17 @@ pub enum Req {
// returns the current time (in milliseconds)
fn now() -> u64 {
unsafe {
let mut now: libc::timeval = intrinsics::init();
let mut now: libc::timeval = mem::init();
assert_eq!(imp::gettimeofday(&mut now, ptr::null()), 0);
return (now.tv_sec as u64) * 1000 + (now.tv_usec as u64) / 1000;
}
}
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 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
// sorted list, and dead timers are those which have expired, but ownership

View File

@ -34,7 +34,7 @@ use std::ptr;
use std::os;
use std::rt::rtio;
use std::hashmap::HashMap;
use std::unstable::intrinsics;
use std::mem;
use io::file::FileDesc;
use io::IoResult;
@ -75,7 +75,7 @@ fn helper(input: libc::c_int, messages: Port<Req>) {
}
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();
'outer: loop {
let n = match unsafe {

View File

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

View File

@ -80,7 +80,7 @@ pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
fn addr_to_sockaddr(addr: ip::SocketAddr) -> (libc::sockaddr_storage, uint) {
unsafe {
let mut storage: libc::sockaddr_storage = intrinsics::init();
let mut storage: libc::sockaddr_storage = mem::init();
let len = match addr.ip {
ip::Ipv4Addr(a, b, c, d) => {
let storage: &mut libc::sockaddr_in =
@ -134,7 +134,7 @@ fn socket_name(sk: SocketNameKind,
};
// 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 sockaddr_p = &mut sockaddr as *mut libc::sockaddr_storage;

View File

@ -70,6 +70,7 @@ use kinds::marker;
use ops::Drop;
use cmp::Eq;
use clone::Clone;
use mem;
use option::{Option, Some, None};
use ptr::RawPtr;
use ptr;
@ -77,7 +78,6 @@ use str::StrSlice;
use str;
use vec::{ImmutableVector, MutableVector};
use vec;
use unstable::intrinsics;
use rt::global_heap::malloc_raw;
/// The representation of a C String.
@ -327,7 +327,7 @@ impl<'a> ToCStr for &'a [u8] {
// 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 {
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);
buf[v.len()] = 0;

View File

@ -18,7 +18,7 @@ use ptr::copy_nonoverlapping_memory;
/// Casts the value at `src` to U. The two types must have the same length.
#[inline]
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 src_ptr: *u8 = transmute(src);
copy_nonoverlapping_memory(dest_ptr, src_ptr, mem::size_of::<U>());

View File

@ -68,6 +68,21 @@ pub fn pref_align_of_val<T>(_val: &T) -> uint {
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()
}
#[cfg(test)]
mod tests {
use mem::*;

View File

@ -15,6 +15,7 @@ use clone::Clone;
#[cfg(not(test))]
use cmp::Equiv;
use iter::{range, Iterator};
use mem;
use option::{Option, Some, None};
use unstable::intrinsics;
use util::swap;
@ -132,7 +133,7 @@ pub unsafe fn zero_memory<T>(dst: *mut T, count: uint) {
#[inline]
pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
// 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;
// Perform the swap
@ -160,7 +161,7 @@ pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T {
*/
#[inline(always)]
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);
tmp
}

View File

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

View File

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

View File

@ -288,8 +288,7 @@ extern "rust-intrinsic" {
/// Create a value initialized to zero.
///
/// `init` is unsafe because it returns a zeroed-out datum,
/// which is unsafe unless T is POD. We don't have a POD
/// kind yet. (See #4074).
/// which is unsafe unless T is Pod.
pub fn init<T>() -> T;
/// Create an uninitialized value.

View File

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

View File

@ -11,8 +11,8 @@
//! Miscellaneous helpers for common patterns
use cast;
use mem;
use ptr;
use unstable::intrinsics;
/// The identity function.
#[inline]
@ -26,7 +26,7 @@ pub fn id<T>(x: T) -> T { x }
pub fn swap<T>(x: &mut T, y: &mut T) {
unsafe {
// 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;
// Perform the swap, `&mut` pointers never alias

View File

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

View File

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

View File

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