1
Fork 0

std: Add move_val_init to mem. Replace direct intrinsic usage

This commit is contained in:
Brian Anderson 2014-02-08 22:16:42 -08:00
parent d433b80e02
commit c7710cdf45
7 changed files with 24 additions and 18 deletions

View file

@ -178,7 +178,6 @@ 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::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
@ -200,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}
} }
} }

View file

@ -32,10 +32,10 @@ use collections::list;
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 @@ impl Arena {
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 @@ impl Arena {
// 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 @@ impl TypedArenaChunk {
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 @@ impl<T> TypedArena<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

@ -83,6 +83,15 @@ pub unsafe fn uninit<T>() -> T {
intrinsics::uninit() 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(test)] #[cfg(test)]
mod tests { mod tests {
use mem::*; use mem::*;

View file

@ -120,7 +120,6 @@ use mem::size_of;
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 @@ impl<T> OwnedVector<T> for ~[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 @@ impl<T> OwnedVector<T> for ~[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 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [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 clone::Clone;
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

@ -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);
} }
} }