Skip to content

Commit

Permalink
Rollup merge of #53329 - frewsxcv:frewsxcv-ptr-add-sub, r=RalfJung
Browse files Browse the repository at this point in the history
Replace usages of ptr::offset with ptr::{add,sub}.

Rust provides these helper methods – so let's use them!
  • Loading branch information
kennytm committed Aug 21, 2018
2 parents 9bbab65 + 993fb93 commit 0dd88c9
Show file tree
Hide file tree
Showing 29 changed files with 148 additions and 148 deletions.
2 changes: 1 addition & 1 deletion src/liballoc/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ mod tests {
.unwrap_or_else(|_| handle_alloc_error(layout));

let mut i = ptr.cast::<u8>().as_ptr();
let end = i.offset(layout.size() as isize);
let end = i.add(layout.size());
while i < end {
assert_eq!(*i, 0);
i = i.offset(1);
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ impl<T: Clone> Clone for Box<[T]> {
impl<T> Drop for BoxBuilder<T> {
fn drop(&mut self) {
let mut data = self.data.ptr();
let max = unsafe { data.offset(self.len as isize) };
let max = unsafe { data.add(self.len) };

while data != max {
unsafe {
Expand Down
48 changes: 24 additions & 24 deletions src/liballoc/collections/btree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,12 +1151,12 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV>
let new_len = self.node.len() - self.idx - 1;

ptr::copy_nonoverlapping(
self.node.keys().as_ptr().offset(self.idx as isize + 1),
self.node.keys().as_ptr().add(self.idx + 1),
new_node.keys.as_mut_ptr(),
new_len
);
ptr::copy_nonoverlapping(
self.node.vals().as_ptr().offset(self.idx as isize + 1),
self.node.vals().as_ptr().add(self.idx + 1),
new_node.vals.as_mut_ptr(),
new_len
);
Expand Down Expand Up @@ -1209,17 +1209,17 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
let new_len = self.node.len() - self.idx - 1;

ptr::copy_nonoverlapping(
self.node.keys().as_ptr().offset(self.idx as isize + 1),
self.node.keys().as_ptr().add(self.idx + 1),
new_node.data.keys.as_mut_ptr(),
new_len
);
ptr::copy_nonoverlapping(
self.node.vals().as_ptr().offset(self.idx as isize + 1),
self.node.vals().as_ptr().add(self.idx + 1),
new_node.data.vals.as_mut_ptr(),
new_len
);
ptr::copy_nonoverlapping(
self.node.as_internal().edges.as_ptr().offset(self.idx as isize + 1),
self.node.as_internal().edges.as_ptr().add(self.idx + 1),
new_node.edges.as_mut_ptr(),
new_len + 1
);
Expand Down Expand Up @@ -1283,14 +1283,14 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
slice_remove(self.node.keys_mut(), self.idx));
ptr::copy_nonoverlapping(
right_node.keys().as_ptr(),
left_node.keys_mut().as_mut_ptr().offset(left_len as isize + 1),
left_node.keys_mut().as_mut_ptr().add(left_len + 1),
right_len
);
ptr::write(left_node.vals_mut().get_unchecked_mut(left_len),
slice_remove(self.node.vals_mut(), self.idx));
ptr::copy_nonoverlapping(
right_node.vals().as_ptr(),
left_node.vals_mut().as_mut_ptr().offset(left_len as isize + 1),
left_node.vals_mut().as_mut_ptr().add(left_len + 1),
right_len
);

Expand All @@ -1309,7 +1309,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
.as_internal_mut()
.edges
.as_mut_ptr()
.offset(left_len as isize + 1),
.add(left_len + 1),
right_len + 1
);

Expand Down Expand Up @@ -1394,10 +1394,10 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::

// Make room for stolen elements in the right child.
ptr::copy(right_kv.0,
right_kv.0.offset(count as isize),
right_kv.0.add(count),
right_len);
ptr::copy(right_kv.1,
right_kv.1.offset(count as isize),
right_kv.1.add(count),
right_len);

// Move elements from the left child to the right one.
Expand All @@ -1418,7 +1418,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
// Make room for stolen edges.
let right_edges = right.reborrow_mut().as_internal_mut().edges.as_mut_ptr();
ptr::copy(right_edges,
right_edges.offset(count as isize),
right_edges.add(count),
right_len + 1);
right.correct_childrens_parent_links(count, count + right_len + 1);

Expand Down Expand Up @@ -1463,10 +1463,10 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
move_kv(right_kv, count - 1, parent_kv, 0, 1);

// Fix right indexing
ptr::copy(right_kv.0.offset(count as isize),
ptr::copy(right_kv.0.add(count),
right_kv.0,
new_right_len);
ptr::copy(right_kv.1.offset(count as isize),
ptr::copy(right_kv.1.add(count),
right_kv.1,
new_right_len);
}
Expand All @@ -1480,7 +1480,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::

// Fix right indexing.
let right_edges = right.reborrow_mut().as_internal_mut().edges.as_mut_ptr();
ptr::copy(right_edges.offset(count as isize),
ptr::copy(right_edges.add(count),
right_edges,
new_right_len + 1);
right.correct_childrens_parent_links(0, new_right_len + 1);
Expand All @@ -1497,11 +1497,11 @@ unsafe fn move_kv<K, V>(
dest: (*mut K, *mut V), dest_offset: usize,
count: usize)
{
ptr::copy_nonoverlapping(source.0.offset(source_offset as isize),
dest.0.offset(dest_offset as isize),
ptr::copy_nonoverlapping(source.0.add(source_offset),
dest.0.add(dest_offset),
count);
ptr::copy_nonoverlapping(source.1.offset(source_offset as isize),
dest.1.offset(dest_offset as isize),
ptr::copy_nonoverlapping(source.1.add(source_offset),
dest.1.add(dest_offset),
count);
}

Expand All @@ -1513,8 +1513,8 @@ unsafe fn move_edges<K, V>(
{
let source_ptr = source.as_internal_mut().edges.as_mut_ptr();
let dest_ptr = dest.as_internal_mut().edges.as_mut_ptr();
ptr::copy_nonoverlapping(source_ptr.offset(source_offset as isize),
dest_ptr.offset(dest_offset as isize),
ptr::copy_nonoverlapping(source_ptr.add(source_offset),
dest_ptr.add(dest_offset),
count);
dest.correct_childrens_parent_links(dest_offset, dest_offset + count);
}
Expand Down Expand Up @@ -1604,8 +1604,8 @@ pub mod marker {

unsafe fn slice_insert<T>(slice: &mut [T], idx: usize, val: T) {
ptr::copy(
slice.as_ptr().offset(idx as isize),
slice.as_mut_ptr().offset(idx as isize + 1),
slice.as_ptr().add(idx),
slice.as_mut_ptr().add(idx + 1),
slice.len() - idx
);
ptr::write(slice.get_unchecked_mut(idx), val);
Expand All @@ -1614,8 +1614,8 @@ unsafe fn slice_insert<T>(slice: &mut [T], idx: usize, val: T) {
unsafe fn slice_remove<T>(slice: &mut [T], idx: usize) -> T {
let ret = ptr::read(slice.get_unchecked(idx));
ptr::copy(
slice.as_ptr().offset(idx as isize + 1),
slice.as_mut_ptr().offset(idx as isize),
slice.as_ptr().add(idx + 1),
slice.as_mut_ptr().add(idx),
slice.len() - idx - 1
);
ret
Expand Down
42 changes: 21 additions & 21 deletions src/liballoc/collections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ impl<T> VecDeque<T> {
/// Moves an element out of the buffer
#[inline]
unsafe fn buffer_read(&mut self, off: usize) -> T {
ptr::read(self.ptr().offset(off as isize))
ptr::read(self.ptr().add(off))
}

/// Writes an element into the buffer, moving it.
#[inline]
unsafe fn buffer_write(&mut self, off: usize, value: T) {
ptr::write(self.ptr().offset(off as isize), value);
ptr::write(self.ptr().add(off), value);
}

/// Returns `true` if and only if the buffer is at full capacity.
Expand Down Expand Up @@ -177,8 +177,8 @@ impl<T> VecDeque<T> {
src,
len,
self.cap());
ptr::copy(self.ptr().offset(src as isize),
self.ptr().offset(dst as isize),
ptr::copy(self.ptr().add(src),
self.ptr().add(dst),
len);
}

Expand All @@ -197,8 +197,8 @@ impl<T> VecDeque<T> {
src,
len,
self.cap());
ptr::copy_nonoverlapping(self.ptr().offset(src as isize),
self.ptr().offset(dst as isize),
ptr::copy_nonoverlapping(self.ptr().add(src),
self.ptr().add(dst),
len);
}

Expand Down Expand Up @@ -436,7 +436,7 @@ impl<T> VecDeque<T> {
pub fn get(&self, index: usize) -> Option<&T> {
if index < self.len() {
let idx = self.wrap_add(self.tail, index);
unsafe { Some(&*self.ptr().offset(idx as isize)) }
unsafe { Some(&*self.ptr().add(idx)) }
} else {
None
}
Expand Down Expand Up @@ -465,7 +465,7 @@ impl<T> VecDeque<T> {
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
if index < self.len() {
let idx = self.wrap_add(self.tail, index);
unsafe { Some(&mut *self.ptr().offset(idx as isize)) }
unsafe { Some(&mut *self.ptr().add(idx)) }
} else {
None
}
Expand Down Expand Up @@ -501,8 +501,8 @@ impl<T> VecDeque<T> {
let ri = self.wrap_add(self.tail, i);
let rj = self.wrap_add(self.tail, j);
unsafe {
ptr::swap(self.ptr().offset(ri as isize),
self.ptr().offset(rj as isize))
ptr::swap(self.ptr().add(ri),
self.ptr().add(rj))
}
}

Expand Down Expand Up @@ -1805,20 +1805,20 @@ impl<T> VecDeque<T> {
// `at` lies in the first half.
let amount_in_first = first_len - at;

ptr::copy_nonoverlapping(first_half.as_ptr().offset(at as isize),
ptr::copy_nonoverlapping(first_half.as_ptr().add(at),
other.ptr(),
amount_in_first);

// just take all of the second half.
ptr::copy_nonoverlapping(second_half.as_ptr(),
other.ptr().offset(amount_in_first as isize),
other.ptr().add(amount_in_first),
second_len);
} else {
// `at` lies in the second half, need to factor in the elements we skipped
// in the first half.
let offset = at - first_len;
let amount_in_second = second_len - offset;
ptr::copy_nonoverlapping(second_half.as_ptr().offset(offset as isize),
ptr::copy_nonoverlapping(second_half.as_ptr().add(offset),
other.ptr(),
amount_in_second);
}
Expand Down Expand Up @@ -2709,24 +2709,24 @@ impl<T> From<VecDeque<T>> for Vec<T> {

// Need to move the ring to the front of the buffer, as vec will expect this.
if other.is_contiguous() {
ptr::copy(buf.offset(tail as isize), buf, len);
ptr::copy(buf.add(tail), buf, len);
} else {
if (tail - head) >= cmp::min(cap - tail, head) {
// There is enough free space in the centre for the shortest block so we can
// do this in at most three copy moves.
if (cap - tail) > head {
// right hand block is the long one; move that enough for the left
ptr::copy(buf.offset(tail as isize),
buf.offset((tail - head) as isize),
ptr::copy(buf.add(tail),
buf.add(tail - head),
cap - tail);
// copy left in the end
ptr::copy(buf, buf.offset((cap - head) as isize), head);
ptr::copy(buf, buf.add(cap - head), head);
// shift the new thing to the start
ptr::copy(buf.offset((tail - head) as isize), buf, len);
ptr::copy(buf.add(tail - head), buf, len);
} else {
// left hand block is the long one, we can do it in two!
ptr::copy(buf, buf.offset((cap - tail) as isize), head);
ptr::copy(buf.offset(tail as isize), buf, cap - tail);
ptr::copy(buf, buf.add(cap - tail), head);
ptr::copy(buf.add(tail), buf, cap - tail);
}
} else {
// Need to use N swaps to move the ring
Expand All @@ -2751,7 +2751,7 @@ impl<T> From<VecDeque<T>> for Vec<T> {
for i in left_edge..right_edge {
right_offset = (i - left_edge) % (cap - right_edge);
let src: isize = (right_edge + right_offset) as isize;
ptr::swap(buf.offset(i as isize), buf.offset(src));
ptr::swap(buf.add(i), buf.offset(src));
}
let n_ops = right_edge - left_edge;
left_edge += n_ops;
Expand Down
4 changes: 2 additions & 2 deletions src/liballoc/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ impl<T, A: Alloc> RawVec<T, A> {
/// // double would have aborted or panicked if the len exceeded
/// // `isize::MAX` so this is safe to do unchecked now.
/// unsafe {
/// ptr::write(self.buf.ptr().offset(self.len as isize), elem);
/// ptr::write(self.buf.ptr().add(self.len), elem);
/// }
/// self.len += 1;
/// }
Expand Down Expand Up @@ -487,7 +487,7 @@ impl<T, A: Alloc> RawVec<T, A> {
/// // `isize::MAX` so this is safe to do unchecked now.
/// for x in elems {
/// unsafe {
/// ptr::write(self.buf.ptr().offset(self.len as isize), x.clone());
/// ptr::write(self.buf.ptr().add(self.len), x.clone());
/// }
/// self.len += 1;
/// }
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ impl<T: Clone> RcFromSlice<T> for Rc<[T]> {
};

for (i, item) in v.iter().enumerate() {
ptr::write(elems.offset(i as isize), item.clone());
ptr::write(elems.add(i), item.clone());
guard.n_elems += 1;
}

Expand Down
8 changes: 4 additions & 4 deletions src/liballoc/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,8 @@ unsafe fn merge<T, F>(v: &mut [T], mid: usize, buf: *mut T, is_less: &mut F)
{
let len = v.len();
let v = v.as_mut_ptr();
let v_mid = v.offset(mid as isize);
let v_end = v.offset(len as isize);
let v_mid = v.add(mid);
let v_end = v.add(len);

// The merge process first copies the shorter run into `buf`. Then it traces the newly copied
// run and the longer run forwards (or backwards), comparing their next unconsumed elements and
Expand All @@ -742,7 +742,7 @@ unsafe fn merge<T, F>(v: &mut [T], mid: usize, buf: *mut T, is_less: &mut F)
ptr::copy_nonoverlapping(v, buf, mid);
hole = MergeHole {
start: buf,
end: buf.offset(mid as isize),
end: buf.add(mid),
dest: v,
};

Expand All @@ -766,7 +766,7 @@ unsafe fn merge<T, F>(v: &mut [T], mid: usize, buf: *mut T, is_less: &mut F)
ptr::copy_nonoverlapping(v_mid, buf, len - mid);
hole = MergeHole {
start: buf,
end: buf.offset((len - mid) as isize),
end: buf.add(len - mid),
dest: v_mid,
};

Expand Down
14 changes: 7 additions & 7 deletions src/liballoc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,8 +1190,8 @@ impl String {
let next = idx + ch.len_utf8();
let len = self.len();
unsafe {
ptr::copy(self.vec.as_ptr().offset(next as isize),
self.vec.as_mut_ptr().offset(idx as isize),
ptr::copy(self.vec.as_ptr().add(next),
self.vec.as_mut_ptr().add(idx),
len - next);
self.vec.set_len(len - (next - idx));
}
Expand Down Expand Up @@ -1232,8 +1232,8 @@ impl String {
del_bytes += ch_len;
} else if del_bytes > 0 {
unsafe {
ptr::copy(self.vec.as_ptr().offset(idx as isize),
self.vec.as_mut_ptr().offset((idx - del_bytes) as isize),
ptr::copy(self.vec.as_ptr().add(idx),
self.vec.as_mut_ptr().add(idx - del_bytes),
ch_len);
}
}
Expand Down Expand Up @@ -1289,11 +1289,11 @@ impl String {
let amt = bytes.len();
self.vec.reserve(amt);

ptr::copy(self.vec.as_ptr().offset(idx as isize),
self.vec.as_mut_ptr().offset((idx + amt) as isize),
ptr::copy(self.vec.as_ptr().add(idx),
self.vec.as_mut_ptr().add(idx + amt),
len - idx);
ptr::copy(bytes.as_ptr(),
self.vec.as_mut_ptr().offset(idx as isize),
self.vec.as_mut_ptr().add(idx),
amt);
self.vec.set_len(len + amt);
}
Expand Down
Loading

0 comments on commit 0dd88c9

Please sign in to comment.