-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
alloc: a bunch of safety comments #162289
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -481,7 +481,10 @@ impl<T> [T] { | |
| pub const fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A> { | ||
| let len = self.len(); | ||
| let (b, alloc) = Box::into_raw_with_allocator(self); | ||
| // ignore-tidy-undocumented-unsafe | ||
| // SAFETY: `b` is currently allocated with `alloc` and was allocated with the | ||
| // matching layout for an array of `T * len`, the length is equal to the capacity, | ||
| // and the existence of a `Box<[T]>` is proof that the first `len` elements are | ||
| // valid `T`s. | ||
| unsafe { Vec::from_raw_parts_in(b as *mut T, len, len, alloc) } | ||
| } | ||
|
|
||
|
|
@@ -530,17 +533,24 @@ impl<T> [T] { | |
| // If `m > 0`, there are remaining bits up to the leftmost '1'. | ||
| while m > 0 { | ||
| // `buf.extend(buf)`: | ||
| // ignore-tidy-undocumented-unsafe | ||
| // SAFETY: We're copying `len` elements after offsetting by `len`, | ||
| // with the previous call to `extend` ensuring that the first `len` | ||
| // elements are valid `T`s and the call to `with_capacity` ensuring | ||
| // we have `len * n` space to write the new elements. | ||
|
Comment on lines
+536
to
+539
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This must argue that it's in-bounds of the allocation and that there's no overlap. It's clear that there is no overlap, but this doesn't really explain why No need to explain that the elements are valid because |
||
| // Each iteration of this loop doubles the number of initialised elements, | ||
| // which is tracked via `m` - when `m == 0`, we've written `most_significant_bit(n)` | ||
| // elements to the buffer. | ||
| unsafe { | ||
| ptr::copy_nonoverlapping::<T>( | ||
| buf.as_ptr(), | ||
| (buf.as_mut_ptr()).add(buf.len()), | ||
| buf.len(), | ||
| ); | ||
| // `buf` has capacity of `self.len() * n`. | ||
| let buf_len = buf.len(); | ||
| buf.set_len(buf_len * 2); | ||
| } | ||
| // `buf` has capacity of `self.len() * n`. | ||
| let buf_len = buf.len(); | ||
| // SAFETY: We initialised another `buf_len` elements above. | ||
| unsafe { buf.set_len(buf_len * 2) }; | ||
|
|
||
| m >>= 1; | ||
| } | ||
|
|
@@ -551,7 +561,14 @@ impl<T> [T] { | |
| let rem_len = capacity - buf.len(); // `self.len() * rem` | ||
| if rem_len > 0 { | ||
| // `buf.extend(buf[0 .. rem_len])`: | ||
| // ignore-tidy-undocumented-unsafe | ||
| // SAFETY: We're copying `rem_len` elements after offsetting by `len`. The previous | ||
| // looping `copy_nonoverlapping` always doubled the number of instantiated elements, | ||
| // and so if `rem_len` was greater than `len` it would have allowed for another such | ||
| // doubling, until such time that `rem_len < len`. Thus, the space for these remaining | ||
| // `rem_len` elements must be preceded by more than `rem_len` previously-copied | ||
| // elements. | ||
| // Setting the length is correct since we've initialised the whole `capacity`-length | ||
| // space with copies of the previous `len` elements. | ||
| unsafe { | ||
| // This is non-overlapping since `2^expn > rem`. | ||
| ptr::copy_nonoverlapping::<T>( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should address why this creates a valid
Box. In particular, if I call this and then drop the resultingBox, why does the destructor ofBoxnot violate the safety requirements ofdealloc, in particular the requirements about the length being the same as what it was allocated with?View changes since the review