Skip to content
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

Use pointer 'add' instead of 'offset' #265

Merged
merged 1 commit into from
May 2, 2021
Merged
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
4 changes: 2 additions & 2 deletions src/exception-safety.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl<T: Clone> Vec<T> {
self.set_len(self.len() + to_push.len());

for (i, x) in to_push.iter().enumerate() {
self.ptr().offset(i as isize).write(x.clone());
self.ptr().add(i).write(x.clone());
}
}
}
Expand All @@ -58,7 +58,7 @@ impl<T: Clone> Vec<T> {
We bypass `push` in order to avoid redundant capacity and `len` checks on the
Vec that we definitely know has capacity. The logic is totally correct, except
there's a subtle problem with our code: it's not exception-safe! `set_len`,
`offset`, and `write` are all fine; `clone` is the panic bomb we over-looked.
`add`, and `write` are all fine; `clone` is the panic bomb we over-looked.

Clone is completely out of our control, and is totally free to panic. If it
does, our function will exit early with the length of the Vec set too large. If
Expand Down
2 changes: 1 addition & 1 deletion src/vec-drain.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<T> RawValIter<T> {
// information to LLVM via GEP.
slice.as_ptr()
} else {
slice.as_ptr().offset(slice.len() as isize)
slice.as_ptr().add(slice.len())
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/vec-final.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl<T> Vec<T> {
}

unsafe {
ptr::write(self.ptr().offset(self.len as isize), elem);
ptr::write(self.ptr().add(self.len), elem);
}

// Can't overflow, we'll OOM first.
Expand All @@ -123,7 +123,7 @@ impl<T> Vec<T> {
None
} else {
self.len -= 1;
unsafe { Some(ptr::read(self.ptr().offset(self.len as isize))) }
unsafe { Some(ptr::read(self.ptr().add(self.len))) }
}
}

Expand All @@ -135,11 +135,11 @@ impl<T> Vec<T> {

unsafe {
ptr::copy(
self.ptr().offset(index as isize),
self.ptr().offset(index as isize + 1),
self.ptr().add(index),
self.ptr().add(index + 1),
self.len - index,
);
ptr::write(self.ptr().offset(index as isize), elem);
ptr::write(self.ptr().add(index), elem);
self.len += 1;
}
}
Expand All @@ -148,10 +148,10 @@ impl<T> Vec<T> {
assert!(index < self.len, "index out of bounds");
unsafe {
self.len -= 1;
let result = ptr::read(self.ptr().offset(index as isize));
let result = ptr::read(self.ptr().add(index));
ptr::copy(
self.ptr().offset(index as isize + 1),
self.ptr().offset(index as isize),
self.ptr().add(index + 1),
self.ptr().add(index),
self.len - index,
);
result
Expand Down Expand Up @@ -222,7 +222,7 @@ impl<T> RawValIter<T> {
} else if slice.len() == 0 {
slice.as_ptr()
} else {
slice.as_ptr().offset(slice.len() as isize)
slice.as_ptr().add(slice.len())
},
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/vec-insert-remove.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ pub fn insert(&mut self, index: usize, elem: T) {

unsafe {
// ptr::copy(src, dest, len): "copy from src to dest len elems"
ptr::copy(self.ptr.as_ptr().offset(index as isize),
self.ptr.as_ptr().offset(index as isize + 1),
ptr::copy(self.ptr.as_ptr().add(index),
self.ptr.as_ptr().add(index + 1),
self.len - index);
ptr::write(self.ptr.as_ptr().offset(index as isize), elem);
ptr::write(self.ptr.as_ptr().add(index), elem);
self.len += 1;
}
}
Expand All @@ -39,9 +39,9 @@ pub fn remove(&mut self, index: usize) -> T {
assert!(index < self.len, "index out of bounds");
unsafe {
self.len -= 1;
let result = ptr::read(self.ptr.as_ptr().offset(index as isize));
ptr::copy(self.ptr.as_ptr().offset(index as isize + 1),
self.ptr.as_ptr().offset(index as isize),
let result = ptr::read(self.ptr.as_ptr().add(index));
ptr::copy(self.ptr.as_ptr().add(index + 1),
self.ptr.as_ptr().add(index),
self.len - index);
result
}
Expand Down
2 changes: 1 addition & 1 deletion src/vec-into-iter.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<T> Vec<T> {
// can't offset off this pointer, it's not allocated!
ptr.as_ptr()
} else {
ptr.as_ptr().offset(len as isize)
ptr.as_ptr().add(len)
},
_marker: PhantomData,
}
Expand Down
4 changes: 2 additions & 2 deletions src/vec-push-pop.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn push(&mut self, elem: T) {
if self.len == self.cap { self.grow(); }

unsafe {
ptr::write(self.ptr.as_ptr().offset(self.len as isize), elem);
ptr::write(self.ptr.as_ptr().add(self.len), elem);
}

// Can't fail, we'll OOM first.
Expand All @@ -48,7 +48,7 @@ pub fn pop(&mut self) -> Option<T> {
} else {
self.len -= 1;
unsafe {
Some(ptr::read(self.ptr.as_ptr().offset(self.len as isize)))
Some(ptr::read(self.ptr.as_ptr().add(self.len)))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/vec-raw.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl<T> Vec<T> {

IntoIter {
start: buf.ptr.as_ptr(),
end: buf.ptr.as_ptr().offset(len as isize),
end: buf.ptr.as_ptr().add(len),
_buf: buf,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/vec-zsts.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<T> RawValIter<T> {
} else if slice.len() == 0 {
slice.as_ptr()
} else {
slice.as_ptr().offset(slice.len() as isize)
slice.as_ptr().add(slice.len())
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/working-with-unsafe.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl<T> Vec<T> {
self.reallocate();
}
unsafe {
ptr::write(self.ptr.offset(self.len as isize), elem);
ptr::write(self.ptr.add(self.len), elem);
self.len += 1;
}
}
Expand Down