Skip to content

Commit 8cfbde1

Browse files
tamirdjannau
authored andcommitted
rust: alloc: add Vec::len() <= Vec::capacity invariant
Document the invariant that the vector's length is always less than or equal to its capacity. This is already implied by these other invariants: - `self.len` always represents the exact number of elements stored in the vector. - `self.layout` represents the absolute number of elements that can be stored within the vector without re-allocation. but it doesn't hurt to spell it out. Note that the language references `self.capacity` rather than `self.layout.len` as the latter is zero for a vector of ZSTs. Update a safety comment touched by this patch to correctly reference `realloc` rather than `alloc` and replace "leaves" with "leave" to improve grammar. Reviewed-by: Alice Ryhl <[email protected]> Signed-off-by: Tamir Duberstein <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Danilo Krummrich <[email protected]>
1 parent f2f4a6f commit 8cfbde1

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

rust/kernel/alloc/kvec.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ macro_rules! kvec {
9393
/// without re-allocation. For ZSTs `self.layout`'s capacity is zero. However, it is legal for the
9494
/// backing buffer to be larger than `layout`.
9595
///
96+
/// - `self.len()` is always less than or equal to `self.capacity()`.
97+
///
9698
/// - The `Allocator` type `A` of the vector is the exact same `Allocator` type the backing buffer
9799
/// was allocated with (and must be freed with).
98100
pub struct Vec<T, A: Allocator> {
@@ -265,8 +267,8 @@ where
265267
/// Returns a slice of `MaybeUninit<T>` for the remaining spare capacity of the vector.
266268
pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
267269
// SAFETY:
268-
// - `self.len` is smaller than `self.capacity` and hence, the resulting pointer is
269-
// guaranteed to be part of the same allocated object.
270+
// - `self.len` is smaller than `self.capacity` by the type invariant and hence, the
271+
// resulting pointer is guaranteed to be part of the same allocated object.
270272
// - `self.len` can not overflow `isize`.
271273
let ptr = unsafe { self.as_mut_ptr().add(self.len) } as *mut MaybeUninit<T>;
272274

@@ -820,12 +822,13 @@ where
820822
unsafe { ptr::copy(ptr, buf.as_ptr(), len) };
821823
ptr = buf.as_ptr();
822824

823-
// SAFETY: `len` is guaranteed to be smaller than `self.layout.len()`.
825+
// SAFETY: `len` is guaranteed to be smaller than `self.layout.len()` by the type
826+
// invariant.
824827
let layout = unsafe { ArrayLayout::<T>::new_unchecked(len) };
825828

826-
// SAFETY: `buf` points to the start of the backing buffer and `len` is guaranteed to be
827-
// smaller than `cap`. Depending on `alloc` this operation may shrink the buffer or leaves
828-
// it as it is.
829+
// SAFETY: `buf` points to the start of the backing buffer and `len` is guaranteed by
830+
// the type invariant to be smaller than `cap`. Depending on `realloc` this operation
831+
// may shrink the buffer or leave it as it is.
829832
ptr = match unsafe {
830833
A::realloc(Some(buf.cast()), layout.into(), old_layout.into(), flags)
831834
} {

0 commit comments

Comments
 (0)