Skip to content

Minor change memory copy and swap functions #12143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/libstd/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

//! Unsafe casting functions

use ptr::RawPtr;
use mem;
use unstable::intrinsics;
use ptr::copy_nonoverlapping_memory;
Expand Down Expand Up @@ -72,13 +71,13 @@ pub unsafe fn transmute_region<'a,'b,T>(ptr: &'a T) -> &'b T {

/// Coerce an immutable reference to be mutable.
#[inline]
pub unsafe fn transmute_mut_unsafe<T,P:RawPtr<T>>(ptr: P) -> *mut T {
pub unsafe fn transmute_mut_unsafe<T>(ptr: *T) -> *mut T {
transmute(ptr)
}

/// Coerce an immutable reference to be mutable.
#[inline]
pub unsafe fn transmute_immut_unsafe<T,P:RawPtr<T>>(ptr: P) -> *T {
pub unsafe fn transmute_immut_unsafe<T>(ptr: *mut T) -> *T {
transmute(ptr)
}

Expand Down
18 changes: 9 additions & 9 deletions src/libstd/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ pub fn is_not_null<T,P:RawPtr<T>>(ptr: P) -> bool { ptr.is_not_null() }
* and destination may overlap.
*/
#[inline]
pub unsafe fn copy_memory<T,P:RawPtr<T>>(dst: *mut T, src: P, count: uint) {
intrinsics::copy_memory(dst, cast::transmute_immut_unsafe(src), count)
pub unsafe fn copy_memory<T>(dst: *mut T, src: *T, count: uint) {
intrinsics::copy_memory(dst, src, count)
}

/**
Expand All @@ -103,10 +103,10 @@ pub unsafe fn copy_memory<T,P:RawPtr<T>>(dst: *mut T, src: P, count: uint) {
* and destination may *not* overlap.
*/
#[inline]
pub unsafe fn copy_nonoverlapping_memory<T,P:RawPtr<T>>(dst: *mut T,
src: P,
count: uint) {
intrinsics::copy_nonoverlapping_memory(dst, cast::transmute_immut_unsafe(src), count)
pub unsafe fn copy_nonoverlapping_memory<T>(dst: *mut T,
src: *T,
count: uint) {
intrinsics::copy_nonoverlapping_memory(dst, src, count)
}

/**
Expand Down Expand Up @@ -137,9 +137,9 @@ pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
let t: *mut T = &mut tmp;

// Perform the swap
copy_nonoverlapping_memory(t, x, 1);
copy_memory(x, y, 1); // `x` and `y` may overlap
copy_nonoverlapping_memory(y, t, 1);
copy_nonoverlapping_memory(t, &*x, 1);
copy_memory(x, &*y, 1); // `x` and `y` may overlap
copy_nonoverlapping_memory(y, &*t, 1);

// y and t now point to the same thing, but we need to completely forget `tmp`
// because it's no longer relevant.
Expand Down
13 changes: 5 additions & 8 deletions src/libstd/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,16 @@ 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 = mem::uninit();
let t: *mut T = &mut tmp;
let mut t: T = mem::uninit();

// Perform the swap, `&mut` pointers never alias
let x_raw: *mut T = x;
let y_raw: *mut T = y;
ptr::copy_nonoverlapping_memory(t, x_raw, 1);
ptr::copy_nonoverlapping_memory(x, y_raw, 1);
ptr::copy_nonoverlapping_memory(y, t, 1);
ptr::copy_nonoverlapping_memory(&mut t, &*x, 1);
ptr::copy_nonoverlapping_memory(x, &*y, 1);
ptr::copy_nonoverlapping_memory(y, &t, 1);

// y and t now point to the same thing, but we need to completely forget `tmp`
// because it's no longer relevant.
cast::forget(tmp);
cast::forget(t);
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ impl<T> OwnedVector<T> for ~[T] {
let p = self.as_mut_ptr().offset(i as int);
// Shift everything over to make space. (Duplicating the
// `i`th element into two consecutive places.)
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
// element.
mem::move_val_init(&mut *p, x);
Expand All @@ -1567,7 +1567,7 @@ impl<T> OwnedVector<T> for ~[T] {
let ret = Some(ptr::read_ptr(ptr as *T));

// Shift everything down to fill in that spot.
ptr::copy_memory(ptr, ptr.offset(1), len - i - 1);
ptr::copy_memory(ptr, &*ptr.offset(1), len - i - 1);
self.set_len(len - 1);

ret
Expand Down Expand Up @@ -1842,7 +1842,7 @@ fn insertion_sort<T>(v: &mut [T], compare: |&T, &T| -> Ordering) {
if i != j {
let tmp = ptr::read_ptr(read_ptr);
ptr::copy_memory(buf_v.offset(j + 1),
buf_v.offset(j),
&*buf_v.offset(j),
(i - j) as uint);
ptr::copy_nonoverlapping_memory(buf_v.offset(j),
&tmp as *T,
Expand Down Expand Up @@ -1920,7 +1920,7 @@ fn merge_sort<T>(v: &mut [T], compare: |&T, &T| -> Ordering) {
// that case, `i == j` so we don't copy. The
// `.offset(j)` is always in bounds.
ptr::copy_memory(buf_dat.offset(j + 1),
buf_dat.offset(j),
&*buf_dat.offset(j),
i - j as uint);
ptr::copy_nonoverlapping_memory(buf_dat.offset(j), read_ptr, 1);
}
Expand Down Expand Up @@ -1970,11 +1970,11 @@ fn merge_sort<T>(v: &mut [T], compare: |&T, &T| -> Ordering) {
if left == right_start {
// the number remaining in this run.
let elems = (right_end as uint - right as uint) / mem::size_of::<T>();
ptr::copy_nonoverlapping_memory(out, right, elems);
ptr::copy_nonoverlapping_memory(out, &*right, elems);
break;
} else if right == right_end {
let elems = (right_start as uint - left as uint) / mem::size_of::<T>();
ptr::copy_nonoverlapping_memory(out, left, elems);
ptr::copy_nonoverlapping_memory(out, &*left, elems);
break;
}

Expand All @@ -1988,7 +1988,7 @@ fn merge_sort<T>(v: &mut [T], compare: |&T, &T| -> Ordering) {
} else {
step(&mut left)
};
ptr::copy_nonoverlapping_memory(out, to_copy, 1);
ptr::copy_nonoverlapping_memory(out, &*to_copy, 1);
step(&mut out);
}
}
Expand All @@ -2002,7 +2002,7 @@ fn merge_sort<T>(v: &mut [T], compare: |&T, &T| -> Ordering) {
// write the result to `v` in one go, so that there are never two copies
// of the same object in `v`.
unsafe {
ptr::copy_nonoverlapping_memory(v.as_mut_ptr(), buf_dat, len);
ptr::copy_nonoverlapping_memory(v.as_mut_ptr(), &*buf_dat, len);
}

// increment the pointer, returning the old pointer.
Expand Down