Skip to content

Commit d433b80

Browse files
committed
std: Add init and uninit to mem. Replace direct intrinsic usage
1 parent 49ac48d commit d433b80

File tree

19 files changed

+60
-43
lines changed

19 files changed

+60
-43
lines changed

src/doc/guide-ffi.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ and `free`:
176176
~~~~
177177
use std::cast;
178178
use std::libc::{c_void, size_t, malloc, free};
179+
use std::mem;
179180
use std::ptr;
180181
use std::unstable::intrinsics;
181182
@@ -226,7 +227,7 @@ impl<T: Send> Unique<T> {
226227
impl<T: Send> Drop for Unique<T> {
227228
fn drop(&mut self) {
228229
unsafe {
229-
let x = intrinsics::uninit(); // dummy value to swap in
230+
let x = mem::uninit(); // dummy value to swap in
230231
// We need to move the object out of the box, so that
231232
// the destructor is called (at the end of this scope.)
232233
ptr::replace_ptr(self.ptr, x);

src/libnative/io/file.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ use std::io::IoError;
1616
use std::io;
1717
use std::libc::{c_int, c_void};
1818
use std::libc;
19+
use std::mem;
1920
use std::os;
2021
use std::rt::rtio;
21-
use std::unstable::intrinsics;
2222
use std::vec;
2323

2424
use io::{IoResult, retry};
@@ -147,7 +147,7 @@ impl rtio::RtioFileStream for FileDesc {
147147
#[cfg(windows)]
148148
fn os_pread(fd: c_int, buf: *u8, amt: uint, offset: u64) -> IoResult<int> {
149149
unsafe {
150-
let mut overlap: libc::OVERLAPPED = intrinsics::init();
150+
let mut overlap: libc::OVERLAPPED = mem::init();
151151
let handle = libc::get_osfhandle(fd) as libc::HANDLE;
152152
let mut bytes_read = 0;
153153
overlap.Offset = offset as libc::DWORD;
@@ -179,7 +179,7 @@ impl rtio::RtioFileStream for FileDesc {
179179
#[cfg(windows)]
180180
fn os_pwrite(fd: c_int, buf: *u8, amt: uint, offset: u64) -> IoResult<()> {
181181
unsafe {
182-
let mut overlap: libc::OVERLAPPED = intrinsics::init();
182+
let mut overlap: libc::OVERLAPPED = mem::init();
183183
let handle = libc::get_osfhandle(fd) as libc::HANDLE;
184184
overlap.Offset = offset as libc::DWORD;
185185
overlap.OffsetHigh = (offset >> 32) as libc::DWORD;
@@ -867,7 +867,7 @@ pub fn stat(p: &CString) -> IoResult<io::FileStat> {
867867

868868
#[cfg(windows)]
869869
fn os_stat(p: &CString) -> IoResult<io::FileStat> {
870-
let mut stat: libc::stat = unsafe { intrinsics::uninit() };
870+
let mut stat: libc::stat = unsafe { mem::uninit() };
871871
as_utf16_p(p.as_str().unwrap(), |up| {
872872
match retry(|| unsafe { libc::wstat(up, &mut stat) }) {
873873
0 => Ok(mkstat(&stat, p)),
@@ -878,7 +878,7 @@ pub fn stat(p: &CString) -> IoResult<io::FileStat> {
878878

879879
#[cfg(unix)]
880880
fn os_stat(p: &CString) -> IoResult<io::FileStat> {
881-
let mut stat: libc::stat = unsafe { intrinsics::uninit() };
881+
let mut stat: libc::stat = unsafe { mem::uninit() };
882882
match retry(|| unsafe { libc::stat(p.with_ref(|p| p), &mut stat) }) {
883883
0 => Ok(mkstat(&stat, p)),
884884
_ => Err(super::last_error()),
@@ -897,7 +897,7 @@ pub fn lstat(p: &CString) -> IoResult<io::FileStat> {
897897

898898
#[cfg(unix)]
899899
fn os_lstat(p: &CString) -> IoResult<io::FileStat> {
900-
let mut stat: libc::stat = unsafe { intrinsics::uninit() };
900+
let mut stat: libc::stat = unsafe { mem::uninit() };
901901
match retry(|| unsafe { libc::lstat(p.with_ref(|p| p), &mut stat) }) {
902902
0 => Ok(mkstat(&stat, p)),
903903
_ => Err(super::last_error()),

src/libnative/io/net.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn ip_to_inaddr(ip: ip::IpAddr) -> InAddr {
6868

6969
fn addr_to_sockaddr(addr: ip::SocketAddr) -> (libc::sockaddr_storage, uint) {
7070
unsafe {
71-
let storage: libc::sockaddr_storage = intrinsics::init();
71+
let storage: libc::sockaddr_storage = mem::init();
7272
let len = match ip_to_inaddr(addr.ip) {
7373
InAddr(inaddr) => {
7474
let storage: *mut libc::sockaddr_in = cast::transmute(&storage);
@@ -138,7 +138,7 @@ fn sockname(fd: sock_t,
138138
*mut libc::socklen_t) -> libc::c_int)
139139
-> IoResult<ip::SocketAddr>
140140
{
141-
let mut storage: libc::sockaddr_storage = unsafe { intrinsics::init() };
141+
let mut storage: libc::sockaddr_storage = unsafe { mem::init() };
142142
let mut len = mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t;
143143
unsafe {
144144
let storage = &mut storage as *mut libc::sockaddr_storage;
@@ -225,7 +225,7 @@ pub fn init() {
225225

226226
LOCK.lock();
227227
if !INITIALIZED {
228-
let mut data: WSADATA = intrinsics::init();
228+
let mut data: WSADATA = mem::init();
229229
let ret = WSAStartup(0x202, // version 2.2
230230
&mut data);
231231
assert_eq!(ret, 0);
@@ -438,7 +438,7 @@ impl TcpAcceptor {
438438

439439
pub fn native_accept(&mut self) -> IoResult<TcpStream> {
440440
unsafe {
441-
let mut storage: libc::sockaddr_storage = intrinsics::init();
441+
let mut storage: libc::sockaddr_storage = mem::init();
442442
let storagep = &mut storage as *mut libc::sockaddr_storage;
443443
let size = mem::size_of::<libc::sockaddr_storage>();
444444
let mut size = size as libc::socklen_t;
@@ -543,7 +543,7 @@ impl rtio::RtioSocket for UdpSocket {
543543
impl rtio::RtioUdpSocket for UdpSocket {
544544
fn recvfrom(&mut self, buf: &mut [u8]) -> IoResult<(uint, ip::SocketAddr)> {
545545
unsafe {
546-
let mut storage: libc::sockaddr_storage = intrinsics::init();
546+
let mut storage: libc::sockaddr_storage = mem::init();
547547
let storagep = &mut storage as *mut libc::sockaddr_storage;
548548
let mut addrlen: libc::socklen_t =
549549
mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t;

src/libnative/io/timer_other.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@
4949
use std::comm::Data;
5050
use std::hashmap::HashMap;
5151
use std::libc;
52+
use std::mem;
5253
use std::os;
5354
use std::ptr;
5455
use std::rt::rtio;
5556
use std::sync::atomics;
56-
use std::unstable::intrinsics;
5757

5858
use io::file::FileDesc;
5959
use io::IoResult;
@@ -87,17 +87,17 @@ pub enum Req {
8787
// returns the current time (in milliseconds)
8888
fn now() -> u64 {
8989
unsafe {
90-
let mut now: libc::timeval = intrinsics::init();
90+
let mut now: libc::timeval = mem::init();
9191
assert_eq!(imp::gettimeofday(&mut now, ptr::null()), 0);
9292
return (now.tv_sec as u64) * 1000 + (now.tv_usec as u64) / 1000;
9393
}
9494
}
9595

9696
fn helper(input: libc::c_int, messages: Port<Req>) {
97-
let mut set: imp::fd_set = unsafe { intrinsics::init() };
97+
let mut set: imp::fd_set = unsafe { mem::init() };
9898

9999
let mut fd = FileDesc::new(input, true);
100-
let mut timeout: libc::timeval = unsafe { intrinsics::init() };
100+
let mut timeout: libc::timeval = unsafe { mem::init() };
101101

102102
// active timers are those which are able to be selected upon (and it's a
103103
// sorted list, and dead timers are those which have expired, but ownership

src/libnative/io/timer_timerfd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use std::ptr;
3434
use std::os;
3535
use std::rt::rtio;
3636
use std::hashmap::HashMap;
37-
use std::unstable::intrinsics;
37+
use std::mem;
3838

3939
use io::file::FileDesc;
4040
use io::IoResult;
@@ -75,7 +75,7 @@ fn helper(input: libc::c_int, messages: Port<Req>) {
7575
}
7676

7777
add(efd, input);
78-
let events: [imp::epoll_event, ..16] = unsafe { intrinsics::init() };
78+
let events: [imp::epoll_event, ..16] = unsafe { mem::init() };
7979
let mut map: HashMap<libc::c_int, (Chan<()>, bool)> = HashMap::new();
8080
'outer: loop {
8181
let n = match unsafe {

src/librustdoc/html/markdown.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use std::cast;
2828
use std::fmt;
2929
use std::io;
3030
use std::libc;
31+
use std::mem;
3132
use std::str;
3233
use std::unstable::intrinsics;
3334
use std::vec;
@@ -144,7 +145,7 @@ pub fn render(w: &mut io::Writer, s: &str) -> fmt::Result {
144145
flags: 0,
145146
link_attributes: None,
146147
};
147-
let mut callbacks: sd_callbacks = intrinsics::init();
148+
let mut callbacks: sd_callbacks = mem::init();
148149

149150
sdhtml_renderer(&callbacks, &options, 0);
150151
let opaque = my_opaque {
@@ -197,7 +198,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
197198
MKDEXT_STRIKETHROUGH;
198199
let callbacks = sd_callbacks {
199200
blockcode: block,
200-
other: intrinsics::init()
201+
other: mem::init()
201202
};
202203

203204
let tests = tests as *mut ::test::Collector as *libc::c_void;

src/librustuv/net.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
8080

8181
fn addr_to_sockaddr(addr: ip::SocketAddr) -> (libc::sockaddr_storage, uint) {
8282
unsafe {
83-
let mut storage: libc::sockaddr_storage = intrinsics::init();
83+
let mut storage: libc::sockaddr_storage = mem::init();
8484
let len = match addr.ip {
8585
ip::Ipv4Addr(a, b, c, d) => {
8686
let storage: &mut libc::sockaddr_in =
@@ -134,7 +134,7 @@ fn socket_name(sk: SocketNameKind,
134134
};
135135

136136
// Allocate a sockaddr_storage since we don't know if it's ipv4 or ipv6
137-
let mut sockaddr: libc::sockaddr_storage = unsafe { intrinsics::init() };
137+
let mut sockaddr: libc::sockaddr_storage = unsafe { mem::init() };
138138
let mut namelen = mem::size_of::<libc::sockaddr_storage>() as c_int;
139139

140140
let sockaddr_p = &mut sockaddr as *mut libc::sockaddr_storage;

src/libstd/c_str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ use kinds::marker;
7070
use ops::Drop;
7171
use cmp::Eq;
7272
use clone::Clone;
73+
use mem;
7374
use option::{Option, Some, None};
7475
use ptr::RawPtr;
7576
use ptr;
7677
use str::StrSlice;
7778
use str;
7879
use vec::{ImmutableVector, MutableVector};
7980
use vec;
80-
use unstable::intrinsics;
8181
use rt::global_heap::malloc_raw;
8282

8383
/// The representation of a C String.
@@ -327,7 +327,7 @@ impl<'a> ToCStr for &'a [u8] {
327327
// Unsafe function that handles possibly copying the &[u8] into a stack array.
328328
unsafe fn with_c_str<T>(v: &[u8], checked: bool, f: |*libc::c_char| -> T) -> T {
329329
if v.len() < BUF_LEN {
330-
let mut buf: [u8, .. BUF_LEN] = intrinsics::uninit();
330+
let mut buf: [u8, .. BUF_LEN] = mem::uninit();
331331
vec::bytes::copy_memory(buf, v);
332332
buf[v.len()] = 0;
333333

src/libstd/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use ptr::copy_nonoverlapping_memory;
1818
/// Casts the value at `src` to U. The two types must have the same length.
1919
#[inline]
2020
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
21-
let mut dest: U = intrinsics::uninit();
21+
let mut dest: U = mem::uninit();
2222
let dest_ptr: *mut u8 = transmute(&mut dest);
2323
let src_ptr: *u8 = transmute(src);
2424
copy_nonoverlapping_memory(dest_ptr, src_ptr, mem::size_of::<U>());

src/libstd/mem.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ pub fn pref_align_of_val<T>(_val: &T) -> uint {
6868
pref_align_of::<T>()
6969
}
7070

71+
/// Create a value initialized to zero.
72+
///
73+
/// `init` is unsafe because it returns a zeroed-out datum,
74+
/// which is unsafe unless T is Pod.
75+
#[inline]
76+
pub unsafe fn init<T>() -> T {
77+
intrinsics::init()
78+
}
79+
80+
/// Create an uninitialized value.
81+
#[inline]
82+
pub unsafe fn uninit<T>() -> T {
83+
intrinsics::uninit()
84+
}
85+
7186
#[cfg(test)]
7287
mod tests {
7388
use mem::*;

src/libstd/ptr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use clone::Clone;
1515
#[cfg(not(test))]
1616
use cmp::Equiv;
1717
use iter::{range, Iterator};
18+
use mem;
1819
use option::{Option, Some, None};
1920
use unstable::intrinsics;
2021
use util::swap;
@@ -132,7 +133,7 @@ pub unsafe fn zero_memory<T>(dst: *mut T, count: uint) {
132133
#[inline]
133134
pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
134135
// Give ourselves some scratch space to work with
135-
let mut tmp: T = intrinsics::uninit();
136+
let mut tmp: T = mem::uninit();
136137
let t: *mut T = &mut tmp;
137138

138139
// Perform the swap
@@ -160,7 +161,7 @@ pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T {
160161
*/
161162
#[inline(always)]
162163
pub unsafe fn read_ptr<T>(src: *T) -> T {
163-
let mut tmp: T = intrinsics::uninit();
164+
let mut tmp: T = mem::uninit();
164165
copy_nonoverlapping_memory(&mut tmp, src, 1);
165166
tmp
166167
}

src/libstd/rt/thread.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,17 @@ mod imp {
205205
use cmp;
206206
use libc::consts::os::posix01::{PTHREAD_CREATE_JOINABLE, PTHREAD_STACK_MIN};
207207
use libc;
208+
use mem;
208209
use os;
209210
use ptr;
210-
use unstable::intrinsics;
211211
use unstable::stack::RED_ZONE;
212212

213213
pub type rust_thread = libc::pthread_t;
214214
pub type rust_thread_return = *u8;
215215

216216
pub unsafe fn create(stack: uint, p: ~proc()) -> rust_thread {
217-
let mut native: libc::pthread_t = intrinsics::uninit();
218-
let mut attr: libc::pthread_attr_t = intrinsics::uninit();
217+
let mut native: libc::pthread_t = mem::uninit();
218+
let mut attr: libc::pthread_attr_t = mem::uninit();
219219
assert_eq!(pthread_attr_init(&mut attr), 0);
220220
assert_eq!(pthread_attr_setdetachstate(&mut attr,
221221
PTHREAD_CREATE_JOINABLE), 0);

src/libstd/trie.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use iter::{Extendable, FromIterator, Iterator};
1616
use mem;
1717
use uint;
1818
use util::replace;
19-
use unstable::intrinsics::init;
19+
use mem::init;
2020
use vec;
2121
use ptr::RawPtr;
2222
use vec::{ImmutableVector, Items, MutableVector, MutItems, OwnedVector};

src/libstd/unstable/intrinsics.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,7 @@ extern "rust-intrinsic" {
288288
/// Create a value initialized to zero.
289289
///
290290
/// `init` is unsafe because it returns a zeroed-out datum,
291-
/// which is unsafe unless T is POD. We don't have a POD
292-
/// kind yet. (See #4074).
291+
/// which is unsafe unless T is Pod.
293292
pub fn init<T>() -> T;
294293

295294
/// Create an uninitialized value.

src/libstd/unstable/mutex.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ mod imp {
9292
use libc;
9393
use self::os::{PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER,
9494
pthread_mutex_t, pthread_cond_t};
95-
use unstable::intrinsics;
95+
use mem;
9696

9797
type pthread_mutexattr_t = libc::c_void;
9898
type pthread_condattr_t = libc::c_void;
@@ -208,8 +208,8 @@ mod imp {
208208
impl Mutex {
209209
pub unsafe fn new() -> Mutex {
210210
let mut m = Mutex {
211-
lock: intrinsics::init(),
212-
cond: intrinsics::init(),
211+
lock: mem::init(),
212+
cond: mem::init(),
213213
};
214214

215215
pthread_mutex_init(&mut m.lock, 0 as *libc::c_void);

src/libstd/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
//! Miscellaneous helpers for common patterns
1212
1313
use cast;
14+
use mem;
1415
use ptr;
15-
use unstable::intrinsics;
1616

1717
/// The identity function.
1818
#[inline]
@@ -26,7 +26,7 @@ pub fn id<T>(x: T) -> T { x }
2626
pub fn swap<T>(x: &mut T, y: &mut T) {
2727
unsafe {
2828
// Give ourselves some scratch space to work with
29-
let mut tmp: T = intrinsics::uninit();
29+
let mut tmp: T = mem::uninit();
3030
let t: *mut T = &mut tmp;
3131

3232
// Perform the swap, `&mut` pointers never alias

src/test/bench/shootout-k-nucleotide.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use std::cast::transmute;
1616
use std::i32::range;
1717
use std::libc::{STDIN_FILENO, c_int, fdopen, fgets, fileno, fopen, fstat};
1818
use std::libc::{stat, strlen};
19+
use std::mem::init;
1920
use std::ptr::null;
20-
use std::unstable::intrinsics::init;
2121
use std::vec::{reverse};
2222

2323
static LINE_LEN: uint = 80;

src/test/run-pass/issue-10714.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
enum v {}
1212
pub fn main() {
13-
let y: v = unsafe { ::std::unstable::intrinsics::uninit() };
13+
let y: v = unsafe { ::std::mem::uninit() };
1414
}

0 commit comments

Comments
 (0)