use std::intrinsics;
use std::mem;
use std::num;
-use std::ptr::read;
+use std::ptr;
use std::rc::Rc;
use std::rt::heap::allocate;
let ptr = self.alloc_copy_inner(mem::size_of::<T>(),
mem::min_align_of::<T>());
let ptr = ptr as *mut T;
- mem::overwrite(&mut (*ptr), op());
+ ptr::write(&mut (*ptr), op());
return &*ptr;
}
}
// has *not* been initialized yet.
*ty_ptr = mem::transmute(tydesc);
// Actually initialize it
- mem::overwrite(&mut(*ptr), op());
+ ptr::write(&mut(*ptr), op());
// Now that we are done, update the tydesc to indicate that
// the object is there.
*ty_ptr = bitpack_tydesc_ptr(tydesc, true);
let mut chunk = unsafe {
let chunk = allocate(size, mem::min_align_of::<TypedArenaChunk<T>>());
let mut chunk: Box<TypedArenaChunk<T>> = mem::transmute(chunk);
- mem::overwrite(&mut chunk.next, next);
+ ptr::write(&mut chunk.next, next);
chunk
};
if intrinsics::needs_drop::<T>() {
let mut start = self.start();
for _ in range(0, len) {
- read(start as *T); // run the destructor on the pointer
+ ptr::read(start as *T); // run the destructor on the pointer
start = start.offset(mem::size_of::<T>() as int)
}
}
}
let ptr: &'a mut T = mem::transmute(this.ptr);
- mem::overwrite(ptr, object);
+ ptr::write(ptr, object);
this.ptr = this.ptr.offset(1);
let ptr: &'a T = ptr;
ptr
#![allow(missing_doc)]
use std::clone::Clone;
-use std::mem::{overwrite, zeroed, replace, swap};
+use std::mem::{zeroed, replace, swap};
+use std::ptr;
use std::slice;
/// A priority queue implemented with a binary heap
let parent = (pos - 1) >> 1;
if new > *self.data.get(parent) {
let x = replace(self.data.get_mut(parent), zeroed());
- overwrite(self.data.get_mut(pos), x);
+ ptr::write(self.data.get_mut(pos), x);
pos = parent;
continue
}
break
}
- overwrite(self.data.get_mut(pos), new);
+ ptr::write(self.data.get_mut(pos), new);
}
}
child = right;
}
let x = replace(self.data.get_mut(child), zeroed());
- overwrite(self.data.get_mut(pos), x);
+ ptr::write(self.data.get_mut(pos), x);
pos = child;
child = 2 * pos + 1;
}
- overwrite(self.data.get_mut(pos), new);
+ ptr::write(self.data.get_mut(pos), new);
self.siftup(start, pos);
}
}
#[cfg(not(test))] use cmp::{PartialEq, Eq, PartialOrd, Equiv};
-/// Return the offset of the first null pointer in `buf`.
-#[inline]
-pub unsafe fn buf_len<T>(buf: **T) -> uint {
- position(buf, |i| *i == null())
-}
-
-impl<T> Clone for *T {
- #[inline]
- fn clone(&self) -> *T {
- *self
- }
-}
-
-impl<T> Clone for *mut T {
- #[inline]
- fn clone(&self) -> *mut T {
- *self
- }
-}
-
-/// Return the first offset `i` such that `f(buf[i]) == true`.
-#[inline]
-pub unsafe fn position<T>(buf: *T, f: |&T| -> bool) -> uint {
- let mut i = 0;
- loop {
- if f(&(*buf.offset(i as int))) { return i; }
- else { i += 1; }
- }
-}
-
/// Create a null pointer.
///
/// # Example
/// assert!(p.is_null());
/// ```
#[inline]
+#[unstable = "may need a different name after pending changes to pointer types"]
pub fn null<T>() -> *T { 0 as *T }
/// Create an unsafe mutable null pointer.
/// assert!(p.is_null());
/// ```
#[inline]
+#[unstable = "may need a different name after pending changes to pointer types"]
pub fn mut_null<T>() -> *mut T { 0 as *mut T }
/// Copies data from one location to another.
/// ```
///
#[inline]
+#[unstable]
pub unsafe fn copy_memory<T>(dst: *mut T, src: *T, count: uint) {
intrinsics::copy_memory(dst, src, count)
}
/// If the source and destination overlap then the behavior of this
/// function is undefined.
#[inline]
+#[unstable]
pub unsafe fn copy_nonoverlapping_memory<T>(dst: *mut T,
src: *T,
count: uint) {
/// Invokes memset on the specified pointer, setting `count * size_of::<T>()`
/// bytes of memory starting at `dst` to `c`.
#[inline]
+#[experimental = "uncertain about naming and semantics"]
pub unsafe fn set_memory<T>(dst: *mut T, c: u8, count: uint) {
intrinsics::set_memory(dst, c, count)
}
/// Zeroes out `count * size_of::<T>` bytes of memory at `dst`
#[inline]
+#[experimental = "uncertain about naming and semantics"]
+#[allow(experimental)]
pub unsafe fn zero_memory<T>(dst: *mut T, count: uint) {
set_memory(dst, 0, count);
}
/// Swap the values at two mutable locations of the same type, without
/// deinitialising either. They may overlap.
#[inline]
+#[unstable]
pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
// Give ourselves some scratch space to work with
let mut tmp: T = mem::uninitialized();
/// Replace the value at a mutable location with a new one, returning the old
/// value, without deinitialising either.
#[inline]
+#[unstable]
pub unsafe fn replace<T>(dest: *mut T, mut src: T) -> T {
mem::swap(mem::transmute(dest), &mut src); // cannot overlap
src
/// Reads the value from `*src` and returns it.
#[inline(always)]
+#[unstable]
pub unsafe fn read<T>(src: *T) -> T {
let mut tmp: T = mem::uninitialized();
copy_nonoverlapping_memory(&mut tmp, src, 1);
/// Reads the value from `*src` and nulls it out.
/// This currently prevents destructors from executing.
#[inline(always)]
+#[experimental]
+#[allow(experimental)]
pub unsafe fn read_and_zero<T>(dest: *mut T) -> T {
// Copy the data out from `dest`:
let tmp = read(&*dest);
tmp
}
+/// Unsafely overwrite a memory location with the given value without destroying
+/// the old value.
+///
+/// This operation is unsafe because it does not destroy the previous value
+/// contained at the location `dst`. This could leak allocations or resources,
+/// so care must be taken to previously deallocate the value at `dst`.
+#[inline]
+#[unstable]
+pub unsafe fn write<T>(dst: *mut T, src: T) {
+ intrinsics::move_val_init(&mut *dst, src)
+}
+
/// Given a **T (pointer to an array of pointers),
/// iterate through each *T, up to the provided `len`,
/// passing to the provided callback function
+#[deprecated = "old-style iteration. use a loop and RawPtr::offset"]
pub unsafe fn array_each_with_len<T>(arr: **T, len: uint, cb: |*T|) {
if arr.is_null() {
fail!("ptr::array_each_with_len failure: arr input is null pointer");
///
/// This will only work with a null-terminated
/// pointer array.
+#[deprecated = "old-style iteration. use a loop and RawPtr::offset"]
+#[allow(deprecated)]
pub unsafe fn array_each<T>(arr: **T, cb: |*T|) {
if arr.is_null() {
fail!("ptr::array_each_with_len failure: arr input is null pointer");
array_each_with_len(arr, len, cb);
}
+/// Return the offset of the first null pointer in `buf`.
+#[inline]
+#[deprecated = "use a loop and RawPtr::offset"]
+#[allow(deprecated)]
+pub unsafe fn buf_len<T>(buf: **T) -> uint {
+ position(buf, |i| *i == null())
+}
+
+/// Return the first offset `i` such that `f(buf[i]) == true`.
+#[inline]
+#[deprecated = "old-style iteration. use a loop and RawPtr::offset"]
+pub unsafe fn position<T>(buf: *T, f: |&T| -> bool) -> uint {
+ let mut i = 0;
+ loop {
+ if f(&(*buf.offset(i as int))) { return i; }
+ else { i += 1; }
+ }
+}
+
/// Methods on raw pointers
pub trait RawPtr<T> {
/// Returns the null pointer.
}
}
+impl<T> Clone for *T {
+ #[inline]
+ fn clone(&self) -> *T {
+ *self
+ }
+}
+
+impl<T> Clone for *mut T {
+ #[inline]
+ fn clone(&self) -> *mut T {
+ *self
+ }
+}
+
// Equality for extern "C" fn pointers
#[cfg(not(test))]
mod externfnpointers {