1 // SPDX-License-Identifier: GPL-2.0 2 3 //! Implementation of [`Vec`]. 4 5 use super::{ 6 allocator::{KVmalloc, Kmalloc, Vmalloc}, 7 layout::ArrayLayout, 8 AllocError, Allocator, Box, Flags, NumaNode, 9 }; 10 use core::{ 11 borrow::{Borrow, BorrowMut}, 12 fmt, 13 marker::PhantomData, 14 mem::{ManuallyDrop, MaybeUninit}, 15 ops::Deref, 16 ops::DerefMut, 17 ops::Index, 18 ops::IndexMut, 19 ptr, 20 ptr::NonNull, 21 slice, 22 slice::SliceIndex, 23 }; 24 25 mod errors; 26 pub use self::errors::{InsertError, PushError, RemoveError}; 27 28 /// Create a [`KVec`] containing the arguments. 29 /// 30 /// New memory is allocated with `GFP_KERNEL`. 31 /// 32 /// # Examples 33 /// 34 /// ``` 35 /// let mut v = kernel::kvec![]; 36 /// v.push(1, GFP_KERNEL)?; 37 /// assert_eq!(v, [1]); 38 /// 39 /// let mut v = kernel::kvec![1; 3]?; 40 /// v.push(4, GFP_KERNEL)?; 41 /// assert_eq!(v, [1, 1, 1, 4]); 42 /// 43 /// let mut v = kernel::kvec![1, 2, 3]?; 44 /// v.push(4, GFP_KERNEL)?; 45 /// assert_eq!(v, [1, 2, 3, 4]); 46 /// 47 /// # Ok::<(), Error>(()) 48 /// ``` 49 #[macro_export] 50 macro_rules! kvec { 51 () => ( 52 $crate::alloc::KVec::new() 53 ); 54 ($elem:expr; $n:expr) => ( 55 $crate::alloc::KVec::from_elem($elem, $n, GFP_KERNEL) 56 ); 57 ($($x:expr),+ $(,)?) => ( 58 match $crate::alloc::KBox::new_uninit(GFP_KERNEL) { 59 Ok(b) => Ok($crate::alloc::KVec::from($crate::alloc::KBox::write(b, [$($x),+]))), 60 Err(e) => Err(e), 61 } 62 ); 63 } 64 65 /// The kernel's [`Vec`] type. 66 /// 67 /// A contiguous growable array type with contents allocated with the kernel's allocators (e.g. 68 /// [`Kmalloc`], [`Vmalloc`] or [`KVmalloc`]), written `Vec<T, A>`. 69 /// 70 /// For non-zero-sized values, a [`Vec`] will use the given allocator `A` for its allocation. For 71 /// the most common allocators the type aliases [`KVec`], [`VVec`] and [`KVVec`] exist. 72 /// 73 /// For zero-sized types the [`Vec`]'s pointer must be `dangling_mut::<T>`; no memory is allocated. 74 /// 75 /// Generally, [`Vec`] consists of a pointer that represents the vector's backing buffer, the 76 /// capacity of the vector (the number of elements that currently fit into the vector), its length 77 /// (the number of elements that are currently stored in the vector) and the `Allocator` type used 78 /// to allocate (and free) the backing buffer. 79 /// 80 /// A [`Vec`] can be deconstructed into and (re-)constructed from its previously named raw parts 81 /// and manually modified. 82 /// 83 /// [`Vec`]'s backing buffer gets, if required, automatically increased (re-allocated) when elements 84 /// are added to the vector. 85 /// 86 /// # Invariants 87 /// 88 /// - `self.ptr` is always properly aligned and either points to memory allocated with `A` or, for 89 /// zero-sized types, is a dangling, well aligned pointer. 90 /// 91 /// - `self.len` always represents the exact number of elements stored in the vector. 92 /// 93 /// - `self.layout` represents the absolute number of elements that can be stored within the vector 94 /// without re-allocation. For ZSTs `self.layout`'s capacity is zero. However, it is legal for the 95 /// backing buffer to be larger than `layout`. 96 /// 97 /// - `self.len()` is always less than or equal to `self.capacity()`. 98 /// 99 /// - The `Allocator` type `A` of the vector is the exact same `Allocator` type the backing buffer 100 /// was allocated with (and must be freed with). 101 pub struct Vec<T, A: Allocator> { 102 ptr: NonNull<T>, 103 /// Represents the actual buffer size as `cap` times `size_of::<T>` bytes. 104 /// 105 /// Note: This isn't quite the same as `Self::capacity`, which in contrast returns the number of 106 /// elements we can still store without reallocating. 107 layout: ArrayLayout<T>, 108 len: usize, 109 _p: PhantomData<A>, 110 } 111 112 /// Type alias for [`Vec`] with a [`Kmalloc`] allocator. 113 /// 114 /// # Examples 115 /// 116 /// ``` 117 /// let mut v = KVec::new(); 118 /// v.push(1, GFP_KERNEL)?; 119 /// assert_eq!(&v, &[1]); 120 /// 121 /// # Ok::<(), Error>(()) 122 /// ``` 123 pub type KVec<T> = Vec<T, Kmalloc>; 124 125 /// Type alias for [`Vec`] with a [`Vmalloc`] allocator. 126 /// 127 /// # Examples 128 /// 129 /// ``` 130 /// let mut v = VVec::new(); 131 /// v.push(1, GFP_KERNEL)?; 132 /// assert_eq!(&v, &[1]); 133 /// 134 /// # Ok::<(), Error>(()) 135 /// ``` 136 pub type VVec<T> = Vec<T, Vmalloc>; 137 138 /// Type alias for [`Vec`] with a [`KVmalloc`] allocator. 139 /// 140 /// # Examples 141 /// 142 /// ``` 143 /// let mut v = KVVec::new(); 144 /// v.push(1, GFP_KERNEL)?; 145 /// assert_eq!(&v, &[1]); 146 /// 147 /// # Ok::<(), Error>(()) 148 /// ``` 149 pub type KVVec<T> = Vec<T, KVmalloc>; 150 151 // SAFETY: `Vec` is `Send` if `T` is `Send` because `Vec` owns its elements. 152 unsafe impl<T, A> Send for Vec<T, A> 153 where 154 T: Send, 155 A: Allocator, 156 { 157 } 158 159 // SAFETY: `Vec` is `Sync` if `T` is `Sync` because `Vec` owns its elements. 160 unsafe impl<T, A> Sync for Vec<T, A> 161 where 162 T: Sync, 163 A: Allocator, 164 { 165 } 166 167 impl<T, A> Vec<T, A> 168 where 169 A: Allocator, 170 { 171 #[inline] 172 const fn is_zst() -> bool { 173 core::mem::size_of::<T>() == 0 174 } 175 176 /// Returns the number of elements that can be stored within the vector without allocating 177 /// additional memory. 178 pub fn capacity(&self) -> usize { 179 if const { Self::is_zst() } { 180 usize::MAX 181 } else { 182 self.layout.len() 183 } 184 } 185 186 /// Returns the number of elements stored within the vector. 187 #[inline] 188 pub fn len(&self) -> usize { 189 self.len 190 } 191 192 /// Increments `self.len` by `additional`. 193 /// 194 /// # Safety 195 /// 196 /// - `additional` must be less than or equal to `self.capacity - self.len`. 197 /// - All elements within the interval [`self.len`,`self.len + additional`) must be initialized. 198 #[inline] 199 pub unsafe fn inc_len(&mut self, additional: usize) { 200 // Guaranteed by the type invariant to never underflow. 201 debug_assert!(additional <= self.capacity() - self.len()); 202 // INVARIANT: By the safety requirements of this method this represents the exact number of 203 // elements stored within `self`. 204 self.len += additional; 205 } 206 207 /// Decreases `self.len` by `count`. 208 /// 209 /// Returns a mutable slice to the elements forgotten by the vector. It is the caller's 210 /// responsibility to drop these elements if necessary. 211 /// 212 /// # Safety 213 /// 214 /// - `count` must be less than or equal to `self.len`. 215 unsafe fn dec_len(&mut self, count: usize) -> &mut [T] { 216 debug_assert!(count <= self.len()); 217 // INVARIANT: We relinquish ownership of the elements within the range `[self.len - count, 218 // self.len)`, hence the updated value of `set.len` represents the exact number of elements 219 // stored within `self`. 220 self.len -= count; 221 // SAFETY: The memory after `self.len()` is guaranteed to contain `count` initialized 222 // elements of type `T`. 223 unsafe { slice::from_raw_parts_mut(self.as_mut_ptr().add(self.len), count) } 224 } 225 226 /// Returns a slice of the entire vector. 227 #[inline] 228 pub fn as_slice(&self) -> &[T] { 229 self 230 } 231 232 /// Returns a mutable slice of the entire vector. 233 #[inline] 234 pub fn as_mut_slice(&mut self) -> &mut [T] { 235 self 236 } 237 238 /// Returns a mutable raw pointer to the vector's backing buffer, or, if `T` is a ZST, a 239 /// dangling raw pointer. 240 #[inline] 241 pub fn as_mut_ptr(&mut self) -> *mut T { 242 self.ptr.as_ptr() 243 } 244 245 /// Returns a raw pointer to the vector's backing buffer, or, if `T` is a ZST, a dangling raw 246 /// pointer. 247 #[inline] 248 pub fn as_ptr(&self) -> *const T { 249 self.ptr.as_ptr() 250 } 251 252 /// Returns `true` if the vector contains no elements, `false` otherwise. 253 /// 254 /// # Examples 255 /// 256 /// ``` 257 /// let mut v = KVec::new(); 258 /// assert!(v.is_empty()); 259 /// 260 /// v.push(1, GFP_KERNEL); 261 /// assert!(!v.is_empty()); 262 /// ``` 263 #[inline] 264 pub fn is_empty(&self) -> bool { 265 self.len() == 0 266 } 267 268 /// Creates a new, empty `Vec<T, A>`. 269 /// 270 /// This method does not allocate by itself. 271 #[inline] 272 pub const fn new() -> Self { 273 // INVARIANT: Since this is a new, empty `Vec` with no backing memory yet, 274 // - `ptr` is a properly aligned dangling pointer for type `T`, 275 // - `layout` is an empty `ArrayLayout` (zero capacity) 276 // - `len` is zero, since no elements can be or have been stored, 277 // - `A` is always valid. 278 Self { 279 ptr: NonNull::dangling(), 280 layout: ArrayLayout::empty(), 281 len: 0, 282 _p: PhantomData::<A>, 283 } 284 } 285 286 /// Returns a slice of `MaybeUninit<T>` for the remaining spare capacity of the vector. 287 pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] { 288 // SAFETY: 289 // - `self.len` is smaller than `self.capacity` by the type invariant and hence, the 290 // resulting pointer is guaranteed to be part of the same allocated object. 291 // - `self.len` can not overflow `isize`. 292 let ptr = unsafe { self.as_mut_ptr().add(self.len) }.cast::<MaybeUninit<T>>(); 293 294 // SAFETY: The memory between `self.len` and `self.capacity` is guaranteed to be allocated 295 // and valid, but uninitialized. 296 unsafe { slice::from_raw_parts_mut(ptr, self.capacity() - self.len) } 297 } 298 299 /// Appends an element to the back of the [`Vec`] instance. 300 /// 301 /// # Examples 302 /// 303 /// ``` 304 /// let mut v = KVec::new(); 305 /// v.push(1, GFP_KERNEL)?; 306 /// assert_eq!(&v, &[1]); 307 /// 308 /// v.push(2, GFP_KERNEL)?; 309 /// assert_eq!(&v, &[1, 2]); 310 /// # Ok::<(), Error>(()) 311 /// ``` 312 pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> { 313 self.reserve(1, flags)?; 314 // SAFETY: The call to `reserve` was successful, so the capacity is at least one greater 315 // than the length. 316 unsafe { self.push_within_capacity_unchecked(v) }; 317 Ok(()) 318 } 319 320 /// Appends an element to the back of the [`Vec`] instance without reallocating. 321 /// 322 /// Fails if the vector does not have capacity for the new element. 323 /// 324 /// # Examples 325 /// 326 /// ``` 327 /// let mut v = KVec::with_capacity(10, GFP_KERNEL)?; 328 /// for i in 0..10 { 329 /// v.push_within_capacity(i)?; 330 /// } 331 /// 332 /// assert!(v.push_within_capacity(10).is_err()); 333 /// # Ok::<(), Error>(()) 334 /// ``` 335 pub fn push_within_capacity(&mut self, v: T) -> Result<(), PushError<T>> { 336 if self.len() < self.capacity() { 337 // SAFETY: The length is less than the capacity. 338 unsafe { self.push_within_capacity_unchecked(v) }; 339 Ok(()) 340 } else { 341 Err(PushError(v)) 342 } 343 } 344 345 /// Appends an element to the back of the [`Vec`] instance without reallocating. 346 /// 347 /// # Safety 348 /// 349 /// The length must be less than the capacity. 350 unsafe fn push_within_capacity_unchecked(&mut self, v: T) { 351 let spare = self.spare_capacity_mut(); 352 353 // SAFETY: By the safety requirements, `spare` is non-empty. 354 unsafe { spare.get_unchecked_mut(0) }.write(v); 355 356 // SAFETY: We just initialised the first spare entry, so it is safe to increase the length 357 // by 1. We also know that the new length is <= capacity because the caller guarantees that 358 // the length is less than the capacity at the beginning of this function. 359 unsafe { self.inc_len(1) }; 360 } 361 362 /// Inserts an element at the given index in the [`Vec`] instance. 363 /// 364 /// Fails if the vector does not have capacity for the new element. Panics if the index is out 365 /// of bounds. 366 /// 367 /// # Examples 368 /// 369 /// ``` 370 /// use kernel::alloc::kvec::InsertError; 371 /// 372 /// let mut v = KVec::with_capacity(5, GFP_KERNEL)?; 373 /// for i in 0..5 { 374 /// v.insert_within_capacity(0, i)?; 375 /// } 376 /// 377 /// assert!(matches!(v.insert_within_capacity(0, 5), Err(InsertError::OutOfCapacity(_)))); 378 /// assert!(matches!(v.insert_within_capacity(1000, 5), Err(InsertError::IndexOutOfBounds(_)))); 379 /// assert_eq!(v, [4, 3, 2, 1, 0]); 380 /// # Ok::<(), Error>(()) 381 /// ``` 382 pub fn insert_within_capacity( 383 &mut self, 384 index: usize, 385 element: T, 386 ) -> Result<(), InsertError<T>> { 387 let len = self.len(); 388 if index > len { 389 return Err(InsertError::IndexOutOfBounds(element)); 390 } 391 392 if len >= self.capacity() { 393 return Err(InsertError::OutOfCapacity(element)); 394 } 395 396 // SAFETY: This is in bounds since `index <= len < capacity`. 397 let p = unsafe { self.as_mut_ptr().add(index) }; 398 // INVARIANT: This breaks the Vec invariants by making `index` contain an invalid element, 399 // but we restore the invariants below. 400 // SAFETY: Both the src and dst ranges end no later than one element after the length. 401 // Since the length is less than the capacity, both ranges are in bounds of the allocation. 402 unsafe { ptr::copy(p, p.add(1), len - index) }; 403 // INVARIANT: This restores the Vec invariants. 404 // SAFETY: The pointer is in-bounds of the allocation. 405 unsafe { ptr::write(p, element) }; 406 // SAFETY: Index `len` contains a valid element due to the above copy and write. 407 unsafe { self.inc_len(1) }; 408 Ok(()) 409 } 410 411 /// Removes the last element from a vector and returns it, or `None` if it is empty. 412 /// 413 /// # Examples 414 /// 415 /// ``` 416 /// let mut v = KVec::new(); 417 /// v.push(1, GFP_KERNEL)?; 418 /// v.push(2, GFP_KERNEL)?; 419 /// assert_eq!(&v, &[1, 2]); 420 /// 421 /// assert_eq!(v.pop(), Some(2)); 422 /// assert_eq!(v.pop(), Some(1)); 423 /// assert_eq!(v.pop(), None); 424 /// # Ok::<(), Error>(()) 425 /// ``` 426 pub fn pop(&mut self) -> Option<T> { 427 if self.is_empty() { 428 return None; 429 } 430 431 let removed: *mut T = { 432 // SAFETY: We just checked that the length is at least one. 433 let slice = unsafe { self.dec_len(1) }; 434 // SAFETY: The argument to `dec_len` was 1 so this returns a slice of length 1. 435 unsafe { slice.get_unchecked_mut(0) } 436 }; 437 438 // SAFETY: The guarantees of `dec_len` allow us to take ownership of this value. 439 Some(unsafe { removed.read() }) 440 } 441 442 /// Removes the element at the given index. 443 /// 444 /// # Examples 445 /// 446 /// ``` 447 /// let mut v = kernel::kvec![1, 2, 3]?; 448 /// assert_eq!(v.remove(1)?, 2); 449 /// assert_eq!(v, [1, 3]); 450 /// # Ok::<(), Error>(()) 451 /// ``` 452 pub fn remove(&mut self, i: usize) -> Result<T, RemoveError> { 453 let value = { 454 let value_ref = self.get(i).ok_or(RemoveError)?; 455 // INVARIANT: This breaks the invariants by invalidating the value at index `i`, but we 456 // restore the invariants below. 457 // SAFETY: The value at index `i` is valid, because otherwise we would have already 458 // failed with `RemoveError`. 459 unsafe { ptr::read(value_ref) } 460 }; 461 462 // SAFETY: We checked that `i` is in-bounds. 463 let p = unsafe { self.as_mut_ptr().add(i) }; 464 465 // INVARIANT: After this call, the invalid value is at the last slot, so the Vec invariants 466 // are restored after the below call to `dec_len(1)`. 467 // SAFETY: `p.add(1).add(self.len - i - 1)` is `i+1+len-i-1 == len` elements after the 468 // beginning of the vector, so this is in-bounds of the vector's allocation. 469 unsafe { ptr::copy(p.add(1), p, self.len - i - 1) }; 470 471 // SAFETY: Since the check at the beginning of this call did not fail with `RemoveError`, 472 // the length is at least one. 473 unsafe { self.dec_len(1) }; 474 475 Ok(value) 476 } 477 478 /// Creates a new [`Vec`] instance with at least the given capacity. 479 /// 480 /// # Examples 481 /// 482 /// ``` 483 /// let v = KVec::<u32>::with_capacity(20, GFP_KERNEL)?; 484 /// 485 /// assert!(v.capacity() >= 20); 486 /// # Ok::<(), Error>(()) 487 /// ``` 488 pub fn with_capacity(capacity: usize, flags: Flags) -> Result<Self, AllocError> { 489 let mut v = Vec::new(); 490 491 v.reserve(capacity, flags)?; 492 493 Ok(v) 494 } 495 496 /// Creates a `Vec<T, A>` from a pointer, a length and a capacity using the allocator `A`. 497 /// 498 /// # Examples 499 /// 500 /// ``` 501 /// let mut v = kernel::kvec![1, 2, 3]?; 502 /// v.reserve(1, GFP_KERNEL)?; 503 /// 504 /// let (mut ptr, mut len, cap) = v.into_raw_parts(); 505 /// 506 /// // SAFETY: We've just reserved memory for another element. 507 /// unsafe { ptr.add(len).write(4) }; 508 /// len += 1; 509 /// 510 /// // SAFETY: We only wrote an additional element at the end of the `KVec`'s buffer and 511 /// // correspondingly increased the length of the `KVec` by one. Otherwise, we construct it 512 /// // from the exact same raw parts. 513 /// let v = unsafe { KVec::from_raw_parts(ptr, len, cap) }; 514 /// 515 /// assert_eq!(v, [1, 2, 3, 4]); 516 /// 517 /// # Ok::<(), Error>(()) 518 /// ``` 519 /// 520 /// # Safety 521 /// 522 /// If `T` is a ZST: 523 /// 524 /// - `ptr` must be a dangling, well aligned pointer. 525 /// 526 /// Otherwise: 527 /// 528 /// - `ptr` must have been allocated with the allocator `A`. 529 /// - `ptr` must satisfy or exceed the alignment requirements of `T`. 530 /// - `ptr` must point to memory with a size of at least `size_of::<T>() * capacity` bytes. 531 /// - The allocated size in bytes must not be larger than `isize::MAX`. 532 /// - `length` must be less than or equal to `capacity`. 533 /// - The first `length` elements must be initialized values of type `T`. 534 /// 535 /// It is also valid to create an empty `Vec` passing a dangling pointer for `ptr` and zero for 536 /// `cap` and `len`. 537 pub unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Self { 538 let layout = if Self::is_zst() { 539 ArrayLayout::empty() 540 } else { 541 // SAFETY: By the safety requirements of this function, `capacity * size_of::<T>()` is 542 // smaller than `isize::MAX`. 543 unsafe { ArrayLayout::new_unchecked(capacity) } 544 }; 545 546 // INVARIANT: For ZSTs, we store an empty `ArrayLayout`, all other type invariants are 547 // covered by the safety requirements of this function. 548 Self { 549 // SAFETY: By the safety requirements, `ptr` is either dangling or pointing to a valid 550 // memory allocation, allocated with `A`. 551 ptr: unsafe { NonNull::new_unchecked(ptr) }, 552 layout, 553 len: length, 554 _p: PhantomData::<A>, 555 } 556 } 557 558 /// Consumes the `Vec<T, A>` and returns its raw components `pointer`, `length` and `capacity`. 559 /// 560 /// This will not run the destructor of the contained elements and for non-ZSTs the allocation 561 /// will stay alive indefinitely. Use [`Vec::from_raw_parts`] to recover the [`Vec`], drop the 562 /// elements and free the allocation, if any. 563 pub fn into_raw_parts(self) -> (*mut T, usize, usize) { 564 let mut me = ManuallyDrop::new(self); 565 let len = me.len(); 566 let capacity = me.capacity(); 567 let ptr = me.as_mut_ptr(); 568 (ptr, len, capacity) 569 } 570 571 /// Clears the vector, removing all values. 572 /// 573 /// Note that this method has no effect on the allocated capacity 574 /// of the vector. 575 /// 576 /// # Examples 577 /// 578 /// ``` 579 /// let mut v = kernel::kvec![1, 2, 3]?; 580 /// 581 /// v.clear(); 582 /// 583 /// assert!(v.is_empty()); 584 /// # Ok::<(), Error>(()) 585 /// ``` 586 #[inline] 587 pub fn clear(&mut self) { 588 self.truncate(0); 589 } 590 591 /// Ensures that the capacity exceeds the length by at least `additional` elements. 592 /// 593 /// # Examples 594 /// 595 /// ``` 596 /// let mut v = KVec::new(); 597 /// v.push(1, GFP_KERNEL)?; 598 /// 599 /// v.reserve(10, GFP_KERNEL)?; 600 /// let cap = v.capacity(); 601 /// assert!(cap >= 10); 602 /// 603 /// v.reserve(10, GFP_KERNEL)?; 604 /// let new_cap = v.capacity(); 605 /// assert_eq!(new_cap, cap); 606 /// 607 /// # Ok::<(), Error>(()) 608 /// ``` 609 pub fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocError> { 610 let len = self.len(); 611 let cap = self.capacity(); 612 613 if cap - len >= additional { 614 return Ok(()); 615 } 616 617 if Self::is_zst() { 618 // The capacity is already `usize::MAX` for ZSTs, we can't go higher. 619 return Err(AllocError); 620 } 621 622 // We know that `cap <= isize::MAX` because of the type invariants of `Self`. So the 623 // multiplication by two won't overflow. 624 let new_cap = core::cmp::max(cap * 2, len.checked_add(additional).ok_or(AllocError)?); 625 let layout = ArrayLayout::new(new_cap).map_err(|_| AllocError)?; 626 627 // SAFETY: 628 // - `ptr` is valid because it's either `None` or comes from a previous call to 629 // `A::realloc`. 630 // - `self.layout` matches the `ArrayLayout` of the preceding allocation. 631 let ptr = unsafe { 632 A::realloc( 633 Some(self.ptr.cast()), 634 layout.into(), 635 self.layout.into(), 636 flags, 637 NumaNode::NO_NODE, 638 )? 639 }; 640 641 // INVARIANT: 642 // - `layout` is some `ArrayLayout::<T>`, 643 // - `ptr` has been created by `A::realloc` from `layout`. 644 self.ptr = ptr.cast(); 645 self.layout = layout; 646 647 Ok(()) 648 } 649 650 /// Shortens the vector, setting the length to `len` and drops the removed values. 651 /// If `len` is greater than or equal to the current length, this does nothing. 652 /// 653 /// This has no effect on the capacity and will not allocate. 654 /// 655 /// # Examples 656 /// 657 /// ``` 658 /// let mut v = kernel::kvec![1, 2, 3]?; 659 /// v.truncate(1); 660 /// assert_eq!(v.len(), 1); 661 /// assert_eq!(&v, &[1]); 662 /// 663 /// # Ok::<(), Error>(()) 664 /// ``` 665 pub fn truncate(&mut self, len: usize) { 666 if let Some(count) = self.len().checked_sub(len) { 667 // SAFETY: `count` is `self.len() - len` so it is guaranteed to be less than or 668 // equal to `self.len()`. 669 let ptr: *mut [T] = unsafe { self.dec_len(count) }; 670 671 // SAFETY: the contract of `dec_len` guarantees that the elements in `ptr` are 672 // valid elements whose ownership has been transferred to the caller. 673 unsafe { ptr::drop_in_place(ptr) }; 674 } 675 } 676 677 /// Takes ownership of all items in this vector without consuming the allocation. 678 /// 679 /// # Examples 680 /// 681 /// ``` 682 /// let mut v = kernel::kvec![0, 1, 2, 3]?; 683 /// 684 /// for (i, j) in v.drain_all().enumerate() { 685 /// assert_eq!(i, j); 686 /// } 687 /// 688 /// assert!(v.capacity() >= 4); 689 /// # Ok::<(), Error>(()) 690 /// ``` 691 pub fn drain_all(&mut self) -> DrainAll<'_, T> { 692 // SAFETY: This does not underflow the length. 693 let elems = unsafe { self.dec_len(self.len()) }; 694 // INVARIANT: The first `len` elements of the spare capacity are valid values, and as we 695 // just set the length to zero, we may transfer ownership to the `DrainAll` object. 696 DrainAll { 697 elements: elems.iter_mut(), 698 } 699 } 700 701 /// Removes all elements that don't match the provided closure. 702 /// 703 /// # Examples 704 /// 705 /// ``` 706 /// let mut v = kernel::kvec![1, 2, 3, 4]?; 707 /// v.retain(|i| *i % 2 == 0); 708 /// assert_eq!(v, [2, 4]); 709 /// # Ok::<(), Error>(()) 710 /// ``` 711 pub fn retain(&mut self, mut f: impl FnMut(&mut T) -> bool) { 712 let mut num_kept = 0; 713 let mut next_to_check = 0; 714 while let Some(to_check) = self.get_mut(next_to_check) { 715 if f(to_check) { 716 self.swap(num_kept, next_to_check); 717 num_kept += 1; 718 } 719 next_to_check += 1; 720 } 721 self.truncate(num_kept); 722 } 723 } 724 725 impl<T: Clone, A: Allocator> Vec<T, A> { 726 /// Extend the vector by `n` clones of `value`. 727 pub fn extend_with(&mut self, n: usize, value: T, flags: Flags) -> Result<(), AllocError> { 728 if n == 0 { 729 return Ok(()); 730 } 731 732 self.reserve(n, flags)?; 733 734 let spare = self.spare_capacity_mut(); 735 736 for item in spare.iter_mut().take(n - 1) { 737 item.write(value.clone()); 738 } 739 740 // We can write the last element directly without cloning needlessly. 741 spare[n - 1].write(value); 742 743 // SAFETY: 744 // - `self.len() + n < self.capacity()` due to the call to reserve above, 745 // - the loop and the line above initialized the next `n` elements. 746 unsafe { self.inc_len(n) }; 747 748 Ok(()) 749 } 750 751 /// Pushes clones of the elements of slice into the [`Vec`] instance. 752 /// 753 /// # Examples 754 /// 755 /// ``` 756 /// let mut v = KVec::new(); 757 /// v.push(1, GFP_KERNEL)?; 758 /// 759 /// v.extend_from_slice(&[20, 30, 40], GFP_KERNEL)?; 760 /// assert_eq!(&v, &[1, 20, 30, 40]); 761 /// 762 /// v.extend_from_slice(&[50, 60], GFP_KERNEL)?; 763 /// assert_eq!(&v, &[1, 20, 30, 40, 50, 60]); 764 /// # Ok::<(), Error>(()) 765 /// ``` 766 pub fn extend_from_slice(&mut self, other: &[T], flags: Flags) -> Result<(), AllocError> { 767 self.reserve(other.len(), flags)?; 768 for (slot, item) in core::iter::zip(self.spare_capacity_mut(), other) { 769 slot.write(item.clone()); 770 } 771 772 // SAFETY: 773 // - `other.len()` spare entries have just been initialized, so it is safe to increase 774 // the length by the same number. 775 // - `self.len() + other.len() <= self.capacity()` is guaranteed by the preceding `reserve` 776 // call. 777 unsafe { self.inc_len(other.len()) }; 778 Ok(()) 779 } 780 781 /// Create a new `Vec<T, A>` and extend it by `n` clones of `value`. 782 pub fn from_elem(value: T, n: usize, flags: Flags) -> Result<Self, AllocError> { 783 let mut v = Self::with_capacity(n, flags)?; 784 785 v.extend_with(n, value, flags)?; 786 787 Ok(v) 788 } 789 790 /// Resizes the [`Vec`] so that `len` is equal to `new_len`. 791 /// 792 /// If `new_len` is smaller than `len`, the `Vec` is [`Vec::truncate`]d. 793 /// If `new_len` is larger, each new slot is filled with clones of `value`. 794 /// 795 /// # Examples 796 /// 797 /// ``` 798 /// let mut v = kernel::kvec![1, 2, 3]?; 799 /// v.resize(1, 42, GFP_KERNEL)?; 800 /// assert_eq!(&v, &[1]); 801 /// 802 /// v.resize(3, 42, GFP_KERNEL)?; 803 /// assert_eq!(&v, &[1, 42, 42]); 804 /// 805 /// # Ok::<(), Error>(()) 806 /// ``` 807 pub fn resize(&mut self, new_len: usize, value: T, flags: Flags) -> Result<(), AllocError> { 808 match new_len.checked_sub(self.len()) { 809 Some(n) => self.extend_with(n, value, flags), 810 None => { 811 self.truncate(new_len); 812 Ok(()) 813 } 814 } 815 } 816 } 817 818 impl<T, A> Drop for Vec<T, A> 819 where 820 A: Allocator, 821 { 822 fn drop(&mut self) { 823 // SAFETY: `self.as_mut_ptr` is guaranteed to be valid by the type invariant. 824 unsafe { 825 ptr::drop_in_place(core::ptr::slice_from_raw_parts_mut( 826 self.as_mut_ptr(), 827 self.len, 828 )) 829 }; 830 831 // SAFETY: 832 // - `self.ptr` was previously allocated with `A`. 833 // - `self.layout` matches the `ArrayLayout` of the preceding allocation. 834 unsafe { A::free(self.ptr.cast(), self.layout.into()) }; 835 } 836 } 837 838 impl<T, A, const N: usize> From<Box<[T; N], A>> for Vec<T, A> 839 where 840 A: Allocator, 841 { 842 fn from(b: Box<[T; N], A>) -> Vec<T, A> { 843 let len = b.len(); 844 let ptr = Box::into_raw(b); 845 846 // SAFETY: 847 // - `b` has been allocated with `A`, 848 // - `ptr` fulfills the alignment requirements for `T`, 849 // - `ptr` points to memory with at least a size of `size_of::<T>() * len`, 850 // - all elements within `b` are initialized values of `T`, 851 // - `len` does not exceed `isize::MAX`. 852 unsafe { Vec::from_raw_parts(ptr.cast(), len, len) } 853 } 854 } 855 856 impl<T, A: Allocator> Default for Vec<T, A> { 857 #[inline] 858 fn default() -> Self { 859 Self::new() 860 } 861 } 862 863 impl<T: fmt::Debug, A: Allocator> fmt::Debug for Vec<T, A> { 864 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 865 fmt::Debug::fmt(&**self, f) 866 } 867 } 868 869 impl<T, A> Deref for Vec<T, A> 870 where 871 A: Allocator, 872 { 873 type Target = [T]; 874 875 #[inline] 876 fn deref(&self) -> &[T] { 877 // SAFETY: The memory behind `self.as_ptr()` is guaranteed to contain `self.len` 878 // initialized elements of type `T`. 879 unsafe { slice::from_raw_parts(self.as_ptr(), self.len) } 880 } 881 } 882 883 impl<T, A> DerefMut for Vec<T, A> 884 where 885 A: Allocator, 886 { 887 #[inline] 888 fn deref_mut(&mut self) -> &mut [T] { 889 // SAFETY: The memory behind `self.as_ptr()` is guaranteed to contain `self.len` 890 // initialized elements of type `T`. 891 unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) } 892 } 893 } 894 895 /// # Examples 896 /// 897 /// ``` 898 /// # use core::borrow::Borrow; 899 /// struct Foo<B: Borrow<[u32]>>(B); 900 /// 901 /// // Owned array. 902 /// let owned_array = Foo([1, 2, 3]); 903 /// 904 /// // Owned vector. 905 /// let owned_vec = Foo(KVec::from_elem(0, 3, GFP_KERNEL)?); 906 /// 907 /// let arr = [1, 2, 3]; 908 /// // Borrowed slice from `arr`. 909 /// let borrowed_slice = Foo(&arr[..]); 910 /// # Ok::<(), Error>(()) 911 /// ``` 912 impl<T, A> Borrow<[T]> for Vec<T, A> 913 where 914 A: Allocator, 915 { 916 fn borrow(&self) -> &[T] { 917 self.as_slice() 918 } 919 } 920 921 /// # Examples 922 /// 923 /// ``` 924 /// # use core::borrow::BorrowMut; 925 /// struct Foo<B: BorrowMut<[u32]>>(B); 926 /// 927 /// // Owned array. 928 /// let owned_array = Foo([1, 2, 3]); 929 /// 930 /// // Owned vector. 931 /// let owned_vec = Foo(KVec::from_elem(0, 3, GFP_KERNEL)?); 932 /// 933 /// let mut arr = [1, 2, 3]; 934 /// // Borrowed slice from `arr`. 935 /// let borrowed_slice = Foo(&mut arr[..]); 936 /// # Ok::<(), Error>(()) 937 /// ``` 938 impl<T, A> BorrowMut<[T]> for Vec<T, A> 939 where 940 A: Allocator, 941 { 942 fn borrow_mut(&mut self) -> &mut [T] { 943 self.as_mut_slice() 944 } 945 } 946 947 impl<T: Eq, A> Eq for Vec<T, A> where A: Allocator {} 948 949 impl<T, I: SliceIndex<[T]>, A> Index<I> for Vec<T, A> 950 where 951 A: Allocator, 952 { 953 type Output = I::Output; 954 955 #[inline] 956 fn index(&self, index: I) -> &Self::Output { 957 Index::index(&**self, index) 958 } 959 } 960 961 impl<T, I: SliceIndex<[T]>, A> IndexMut<I> for Vec<T, A> 962 where 963 A: Allocator, 964 { 965 #[inline] 966 fn index_mut(&mut self, index: I) -> &mut Self::Output { 967 IndexMut::index_mut(&mut **self, index) 968 } 969 } 970 971 macro_rules! impl_slice_eq { 972 ($([$($vars:tt)*] $lhs:ty, $rhs:ty,)*) => { 973 $( 974 impl<T, U, $($vars)*> PartialEq<$rhs> for $lhs 975 where 976 T: PartialEq<U>, 977 { 978 #[inline] 979 fn eq(&self, other: &$rhs) -> bool { self[..] == other[..] } 980 } 981 )* 982 } 983 } 984 985 impl_slice_eq! { 986 [A1: Allocator, A2: Allocator] Vec<T, A1>, Vec<U, A2>, 987 [A: Allocator] Vec<T, A>, &[U], 988 [A: Allocator] Vec<T, A>, &mut [U], 989 [A: Allocator] &[T], Vec<U, A>, 990 [A: Allocator] &mut [T], Vec<U, A>, 991 [A: Allocator] Vec<T, A>, [U], 992 [A: Allocator] [T], Vec<U, A>, 993 [A: Allocator, const N: usize] Vec<T, A>, [U; N], 994 [A: Allocator, const N: usize] Vec<T, A>, &[U; N], 995 } 996 997 impl<'a, T, A> IntoIterator for &'a Vec<T, A> 998 where 999 A: Allocator, 1000 { 1001 type Item = &'a T; 1002 type IntoIter = slice::Iter<'a, T>; 1003 1004 fn into_iter(self) -> Self::IntoIter { 1005 self.iter() 1006 } 1007 } 1008 1009 impl<'a, T, A: Allocator> IntoIterator for &'a mut Vec<T, A> 1010 where 1011 A: Allocator, 1012 { 1013 type Item = &'a mut T; 1014 type IntoIter = slice::IterMut<'a, T>; 1015 1016 fn into_iter(self) -> Self::IntoIter { 1017 self.iter_mut() 1018 } 1019 } 1020 1021 /// An [`Iterator`] implementation for [`Vec`] that moves elements out of a vector. 1022 /// 1023 /// This structure is created by the [`Vec::into_iter`] method on [`Vec`] (provided by the 1024 /// [`IntoIterator`] trait). 1025 /// 1026 /// # Examples 1027 /// 1028 /// ``` 1029 /// let v = kernel::kvec![0, 1, 2]?; 1030 /// let iter = v.into_iter(); 1031 /// 1032 /// # Ok::<(), Error>(()) 1033 /// ``` 1034 pub struct IntoIter<T, A: Allocator> { 1035 ptr: *mut T, 1036 buf: NonNull<T>, 1037 len: usize, 1038 layout: ArrayLayout<T>, 1039 _p: PhantomData<A>, 1040 } 1041 1042 impl<T, A> IntoIter<T, A> 1043 where 1044 A: Allocator, 1045 { 1046 fn into_raw_parts(self) -> (*mut T, NonNull<T>, usize, usize) { 1047 let me = ManuallyDrop::new(self); 1048 let ptr = me.ptr; 1049 let buf = me.buf; 1050 let len = me.len; 1051 let cap = me.layout.len(); 1052 (ptr, buf, len, cap) 1053 } 1054 1055 /// Same as `Iterator::collect` but specialized for `Vec`'s `IntoIter`. 1056 /// 1057 /// # Examples 1058 /// 1059 /// ``` 1060 /// let v = kernel::kvec![1, 2, 3]?; 1061 /// let mut it = v.into_iter(); 1062 /// 1063 /// assert_eq!(it.next(), Some(1)); 1064 /// 1065 /// let v = it.collect(GFP_KERNEL); 1066 /// assert_eq!(v, [2, 3]); 1067 /// 1068 /// # Ok::<(), Error>(()) 1069 /// ``` 1070 /// 1071 /// # Implementation details 1072 /// 1073 /// Currently, we can't implement `FromIterator`. There are a couple of issues with this trait 1074 /// in the kernel, namely: 1075 /// 1076 /// - Rust's specialization feature is unstable. This prevents us to optimize for the special 1077 /// case where `I::IntoIter` equals `Vec`'s `IntoIter` type. 1078 /// - We also can't use `I::IntoIter`'s type ID either to work around this, since `FromIterator` 1079 /// doesn't require this type to be `'static`. 1080 /// - `FromIterator::from_iter` does return `Self` instead of `Result<Self, AllocError>`, hence 1081 /// we can't properly handle allocation failures. 1082 /// - Neither `Iterator::collect` nor `FromIterator::from_iter` can handle additional allocation 1083 /// flags. 1084 /// 1085 /// Instead, provide `IntoIter::collect`, such that we can at least convert a `IntoIter` into a 1086 /// `Vec` again. 1087 /// 1088 /// Note that `IntoIter::collect` doesn't require `Flags`, since it re-uses the existing backing 1089 /// buffer. However, this backing buffer may be shrunk to the actual count of elements. 1090 pub fn collect(self, flags: Flags) -> Vec<T, A> { 1091 let old_layout = self.layout; 1092 let (mut ptr, buf, len, mut cap) = self.into_raw_parts(); 1093 let has_advanced = ptr != buf.as_ptr(); 1094 1095 if has_advanced { 1096 // Copy the contents we have advanced to at the beginning of the buffer. 1097 // 1098 // SAFETY: 1099 // - `ptr` is valid for reads of `len * size_of::<T>()` bytes, 1100 // - `buf.as_ptr()` is valid for writes of `len * size_of::<T>()` bytes, 1101 // - `ptr` and `buf.as_ptr()` are not be subject to aliasing restrictions relative to 1102 // each other, 1103 // - both `ptr` and `buf.ptr()` are properly aligned. 1104 unsafe { ptr::copy(ptr, buf.as_ptr(), len) }; 1105 ptr = buf.as_ptr(); 1106 1107 // SAFETY: `len` is guaranteed to be smaller than `self.layout.len()` by the type 1108 // invariant. 1109 let layout = unsafe { ArrayLayout::<T>::new_unchecked(len) }; 1110 1111 // SAFETY: `buf` points to the start of the backing buffer and `len` is guaranteed by 1112 // the type invariant to be smaller than `cap`. Depending on `realloc` this operation 1113 // may shrink the buffer or leave it as it is. 1114 ptr = match unsafe { 1115 A::realloc( 1116 Some(buf.cast()), 1117 layout.into(), 1118 old_layout.into(), 1119 flags, 1120 NumaNode::NO_NODE, 1121 ) 1122 } { 1123 // If we fail to shrink, which likely can't even happen, continue with the existing 1124 // buffer. 1125 Err(_) => ptr, 1126 Ok(ptr) => { 1127 cap = len; 1128 ptr.as_ptr().cast() 1129 } 1130 }; 1131 } 1132 1133 // SAFETY: If the iterator has been advanced, the advanced elements have been copied to 1134 // the beginning of the buffer and `len` has been adjusted accordingly. 1135 // 1136 // - `ptr` is guaranteed to point to the start of the backing buffer. 1137 // - `cap` is either the original capacity or, after shrinking the buffer, equal to `len`. 1138 // - `alloc` is guaranteed to be unchanged since `into_iter` has been called on the original 1139 // `Vec`. 1140 unsafe { Vec::from_raw_parts(ptr, len, cap) } 1141 } 1142 } 1143 1144 impl<T, A> Iterator for IntoIter<T, A> 1145 where 1146 A: Allocator, 1147 { 1148 type Item = T; 1149 1150 /// # Examples 1151 /// 1152 /// ``` 1153 /// let v = kernel::kvec![1, 2, 3]?; 1154 /// let mut it = v.into_iter(); 1155 /// 1156 /// assert_eq!(it.next(), Some(1)); 1157 /// assert_eq!(it.next(), Some(2)); 1158 /// assert_eq!(it.next(), Some(3)); 1159 /// assert_eq!(it.next(), None); 1160 /// 1161 /// # Ok::<(), Error>(()) 1162 /// ``` 1163 fn next(&mut self) -> Option<T> { 1164 if self.len == 0 { 1165 return None; 1166 } 1167 1168 let current = self.ptr; 1169 1170 // SAFETY: We can't overflow; decreasing `self.len` by one every time we advance `self.ptr` 1171 // by one guarantees that. 1172 unsafe { self.ptr = self.ptr.add(1) }; 1173 1174 self.len -= 1; 1175 1176 // SAFETY: `current` is guaranteed to point at a valid element within the buffer. 1177 Some(unsafe { current.read() }) 1178 } 1179 1180 /// # Examples 1181 /// 1182 /// ``` 1183 /// let v: KVec<u32> = kernel::kvec![1, 2, 3]?; 1184 /// let mut iter = v.into_iter(); 1185 /// let size = iter.size_hint().0; 1186 /// 1187 /// iter.next(); 1188 /// assert_eq!(iter.size_hint().0, size - 1); 1189 /// 1190 /// iter.next(); 1191 /// assert_eq!(iter.size_hint().0, size - 2); 1192 /// 1193 /// iter.next(); 1194 /// assert_eq!(iter.size_hint().0, size - 3); 1195 /// 1196 /// # Ok::<(), Error>(()) 1197 /// ``` 1198 fn size_hint(&self) -> (usize, Option<usize>) { 1199 (self.len, Some(self.len)) 1200 } 1201 } 1202 1203 impl<T, A> Drop for IntoIter<T, A> 1204 where 1205 A: Allocator, 1206 { 1207 fn drop(&mut self) { 1208 // SAFETY: `self.ptr` is guaranteed to be valid by the type invariant. 1209 unsafe { ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.ptr, self.len)) }; 1210 1211 // SAFETY: 1212 // - `self.buf` was previously allocated with `A`. 1213 // - `self.layout` matches the `ArrayLayout` of the preceding allocation. 1214 unsafe { A::free(self.buf.cast(), self.layout.into()) }; 1215 } 1216 } 1217 1218 impl<T, A> IntoIterator for Vec<T, A> 1219 where 1220 A: Allocator, 1221 { 1222 type Item = T; 1223 type IntoIter = IntoIter<T, A>; 1224 1225 /// Consumes the `Vec<T, A>` and creates an `Iterator`, which moves each value out of the 1226 /// vector (from start to end). 1227 /// 1228 /// # Examples 1229 /// 1230 /// ``` 1231 /// let v = kernel::kvec![1, 2]?; 1232 /// let mut v_iter = v.into_iter(); 1233 /// 1234 /// let first_element: Option<u32> = v_iter.next(); 1235 /// 1236 /// assert_eq!(first_element, Some(1)); 1237 /// assert_eq!(v_iter.next(), Some(2)); 1238 /// assert_eq!(v_iter.next(), None); 1239 /// 1240 /// # Ok::<(), Error>(()) 1241 /// ``` 1242 /// 1243 /// ``` 1244 /// let v = kernel::kvec![]; 1245 /// let mut v_iter = v.into_iter(); 1246 /// 1247 /// let first_element: Option<u32> = v_iter.next(); 1248 /// 1249 /// assert_eq!(first_element, None); 1250 /// 1251 /// # Ok::<(), Error>(()) 1252 /// ``` 1253 #[inline] 1254 fn into_iter(self) -> Self::IntoIter { 1255 let buf = self.ptr; 1256 let layout = self.layout; 1257 let (ptr, len, _) = self.into_raw_parts(); 1258 1259 IntoIter { 1260 ptr, 1261 buf, 1262 len, 1263 layout, 1264 _p: PhantomData::<A>, 1265 } 1266 } 1267 } 1268 1269 /// An iterator that owns all items in a vector, but does not own its allocation. 1270 /// 1271 /// # Invariants 1272 /// 1273 /// Every `&mut T` returned by the iterator references a `T` that the iterator may take ownership 1274 /// of. 1275 pub struct DrainAll<'vec, T> { 1276 elements: slice::IterMut<'vec, T>, 1277 } 1278 1279 impl<'vec, T> Iterator for DrainAll<'vec, T> { 1280 type Item = T; 1281 1282 fn next(&mut self) -> Option<T> { 1283 let elem: *mut T = self.elements.next()?; 1284 // SAFETY: By the type invariants, we may take ownership of this value. 1285 Some(unsafe { elem.read() }) 1286 } 1287 1288 fn size_hint(&self) -> (usize, Option<usize>) { 1289 self.elements.size_hint() 1290 } 1291 } 1292 1293 impl<'vec, T> Drop for DrainAll<'vec, T> { 1294 fn drop(&mut self) { 1295 if core::mem::needs_drop::<T>() { 1296 let iter = core::mem::take(&mut self.elements); 1297 let ptr: *mut [T] = iter.into_slice(); 1298 // SAFETY: By the type invariants, we own these values so we may destroy them. 1299 unsafe { ptr::drop_in_place(ptr) }; 1300 } 1301 } 1302 } 1303 1304 #[macros::kunit_tests(rust_kvec_kunit)] 1305 mod tests { 1306 use super::*; 1307 use crate::prelude::*; 1308 1309 #[test] 1310 fn test_kvec_retain() { 1311 /// Verify correctness for one specific function. 1312 #[expect(clippy::needless_range_loop)] 1313 fn verify(c: &[bool]) { 1314 let mut vec1: KVec<usize> = KVec::with_capacity(c.len(), GFP_KERNEL).unwrap(); 1315 let mut vec2: KVec<usize> = KVec::with_capacity(c.len(), GFP_KERNEL).unwrap(); 1316 1317 for i in 0..c.len() { 1318 vec1.push_within_capacity(i).unwrap(); 1319 if c[i] { 1320 vec2.push_within_capacity(i).unwrap(); 1321 } 1322 } 1323 1324 vec1.retain(|i| c[*i]); 1325 1326 assert_eq!(vec1, vec2); 1327 } 1328 1329 /// Add one to a binary integer represented as a boolean array. 1330 fn add(value: &mut [bool]) { 1331 let mut carry = true; 1332 for v in value { 1333 let new_v = carry != *v; 1334 carry = carry && *v; 1335 *v = new_v; 1336 } 1337 } 1338 1339 // This boolean array represents a function from index to boolean. We check that `retain` 1340 // behaves correctly for all possible boolean arrays of every possible length less than 1341 // ten. 1342 let mut func = KVec::with_capacity(10, GFP_KERNEL).unwrap(); 1343 for len in 0..10 { 1344 for _ in 0u32..1u32 << len { 1345 verify(&func); 1346 add(&mut func); 1347 } 1348 func.push_within_capacity(false).unwrap(); 1349 } 1350 } 1351 } 1352