| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | use super::{
|
| 6 | allocator::{
|
| 7 | KVmalloc,
|
| 8 | Kmalloc,
|
| 9 | Vmalloc,
|
| 10 | VmallocPageIter, |
| 11 | },
|
| 12 | flags::__GFP_ZERO,
|
| 13 | layout::ArrayLayout,
|
| 14 | AllocError,
|
| 15 | Allocator,
|
| 16 | Box,
|
| 17 | Flags,
|
| 18 | NumaNode, |
| 19 | };
|
| 20 |
|
| 21 | use crate::{
|
| 22 | fmt,
|
| 23 | page::{
|
| 24 | AsPageIter,
|
| 25 | PAGE_SIZE, |
| 26 | }, |
| 27 | };
|
| 28 |
|
| 29 | use core::{
|
| 30 | borrow::{
|
| 31 | Borrow,
|
| 32 | BorrowMut, |
| 33 | },
|
| 34 | marker::PhantomData,
|
| 35 | mem::{
|
| 36 | ManuallyDrop,
|
| 37 | MaybeUninit, |
| 38 | },
|
| 39 | ops::{
|
| 40 | Deref,
|
| 41 | DerefMut,
|
| 42 | Index,
|
| 43 | IndexMut, |
| 44 | },
|
| 45 | ptr::{
|
| 46 | self,
|
| 47 | NonNull, |
| 48 | },
|
| 49 | slice::{
|
| 50 | self,
|
| 51 | SliceIndex, |
| 52 | }, |
| 53 | };
|
| 54 |
|
| 55 | use pin_init::Zeroable;
|
| 56 |
|
| 57 | mod errors;
|
| 58 | pub use self::errors::{InsertError, PushError, RemoveError};
|
| 59 |
|
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | #[macro_export]
|
| 82 | macro_rules! kvec {
|
| 83 | () => (
|
| 84 | $crate::alloc::KVec::new()
|
| 85 | );
|
| 86 | ($elem:expr; $n:expr) => (
|
| 87 | $crate::alloc::KVec::from_elem($elem, $n, GFP_KERNEL)
|
| 88 | );
|
| 89 | ($($x:expr),+ $(,)?) => (
|
| 90 | match $crate::alloc::KBox::new_uninit(GFP_KERNEL) {
|
| 91 | Ok(b) => Ok($crate::alloc::KVec::from($crate::alloc::KBox::write(b, [$($x),+]))),
|
| 92 | Err(e) => Err(e),
|
| 93 | }
|
| 94 | );
|
| 95 | }
|
| 96 |
|
| 97 | /// The kernel's [`Vec`] type.
|
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | pub struct Vec<T, A: Allocator> {
|
| 134 | ptr: NonNull<T>,
|
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | layout: ArrayLayout<T>,
|
| 140 | len: usize,
|
| 141 | _p: PhantomData<A>,
|
| 142 | }
|
| 143 |
|
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | pub type KVec<T> = Vec<T, Kmalloc>;
|
| 156 |
|
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | pub type VVec<T> = Vec<T, Vmalloc>;
|
| 169 |
|
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | pub type KVVec<T> = Vec<T, KVmalloc>;
|
| 182 |
|
| 183 | |
| 184 | unsafe impl<T, A> Send for Vec<T, A>
|
| 185 | where
|
| 186 | T: Send,
|
| 187 | A: Allocator,
|
| 188 | {
|
| 189 | }
|
| 190 |
|
| 191 | |
| 192 | unsafe impl<T, A> Sync for Vec<T, A>
|
| 193 | where
|
| 194 | T: Sync,
|
| 195 | A: Allocator,
|
| 196 | {
|
| 197 | }
|
| 198 |
|
| 199 | impl<T, A> Vec<T, A>
|
| 200 | where
|
| 201 | A: Allocator,
|
| 202 | {
|
| 203 | #[inline]
|
| 204 | const fn is_zst() -> bool {
|
| 205 | core::mem::size_of::<T>() == 0
|
| 206 | }
|
| 207 |
|
| 208 | |
| 209 | |
| 210 | pub const fn capacity(&self) -> usize {
|
| 211 | if const { Self::is_zst() } {
|
| 212 | usize::MAX
|
| 213 | } else {
|
| 214 | self.layout.len()
|
| 215 | }
|
| 216 | }
|
| 217 |
|
| 218 | |
| 219 | #[inline]
|
| 220 | pub const fn len(&self) -> usize {
|
| 221 | self.len
|
| 222 | }
|
| 223 |
|
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | #[inline]
|
| 231 | pub const unsafe fn inc_len(&mut self, additional: usize) {
|
| 232 | |
| 233 | debug_assert!(additional <= self.capacity() - self.len());
|
| 234 | |
| 235 | |
| 236 | self.len += additional;
|
| 237 | }
|
| 238 |
|
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | unsafe fn dec_len(&mut self, count: usize) -> &mut [T] {
|
| 248 | debug_assert!(count <= self.len());
|
| 249 | |
| 250 | |
| 251 | |
| 252 | self.len -= count;
|
| 253 | |
| 254 | |
| 255 | unsafe { slice::from_raw_parts_mut(self.as_mut_ptr().add(self.len), count) }
|
| 256 | }
|
| 257 |
|
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | #[inline]
|
| 270 | pub fn as_slice(&self) -> &[T] {
|
| 271 | self
|
| 272 | }
|
| 273 |
|
| 274 | |
| 275 | #[inline]
|
| 276 | pub fn as_mut_slice(&mut self) -> &mut [T] {
|
| 277 | self
|
| 278 | }
|
| 279 |
|
| 280 | |
| 281 | |
| 282 | #[inline]
|
| 283 | pub fn as_mut_ptr(&mut self) -> *mut T {
|
| 284 | self.ptr.as_ptr()
|
| 285 | }
|
| 286 |
|
| 287 | |
| 288 | |
| 289 | #[inline]
|
| 290 | pub const fn as_ptr(&self) -> *const T {
|
| 291 | self.ptr.as_ptr()
|
| 292 | }
|
| 293 |
|
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | #[inline]
|
| 306 | pub const fn is_empty(&self) -> bool {
|
| 307 | self.len() == 0
|
| 308 | }
|
| 309 |
|
| 310 | |
| 311 | |
| 312 | |
| 313 | #[inline]
|
| 314 | pub const fn new() -> Self {
|
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | Self {
|
| 321 | ptr: NonNull::dangling(),
|
| 322 | layout: ArrayLayout::empty(),
|
| 323 | len: 0,
|
| 324 | _p: PhantomData::<A>,
|
| 325 | }
|
| 326 | }
|
| 327 |
|
| 328 | |
| 329 | pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
|
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | let ptr = unsafe { self.as_mut_ptr().add(self.len) }.cast::<MaybeUninit<T>>();
|
| 335 |
|
| 336 | |
| 337 | |
| 338 | unsafe { slice::from_raw_parts_mut(ptr, self.capacity() - self.len) }
|
| 339 | }
|
| 340 |
|
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> {
|
| 355 | self.reserve(1, flags)?;
|
| 356 | |
| 357 | |
| 358 | unsafe { self.push_within_capacity_unchecked(v) };
|
| 359 | Ok(())
|
| 360 | }
|
| 361 |
|
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | pub fn push_within_capacity(&mut self, v: T) -> Result<(), PushError<T>> {
|
| 378 | if self.len() < self.capacity() {
|
| 379 | |
| 380 | unsafe { self.push_within_capacity_unchecked(v) };
|
| 381 | Ok(())
|
| 382 | } else {
|
| 383 | Err(PushError(v))
|
| 384 | }
|
| 385 | }
|
| 386 |
|
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | unsafe fn push_within_capacity_unchecked(&mut self, v: T) {
|
| 393 | let spare = self.spare_capacity_mut();
|
| 394 |
|
| 395 | |
| 396 | unsafe { spare.get_unchecked_mut(0) }.write(v);
|
| 397 |
|
| 398 | |
| 399 | |
| 400 | |
| 401 | unsafe { self.inc_len(1) };
|
| 402 | }
|
| 403 |
|
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | pub fn insert_within_capacity(
|
| 425 | &mut self,
|
| 426 | index: usize,
|
| 427 | element: T,
|
| 428 | ) -> Result<(), InsertError<T>> {
|
| 429 | let len = self.len();
|
| 430 | if index > len {
|
| 431 | return Err(InsertError::IndexOutOfBounds(element));
|
| 432 | }
|
| 433 |
|
| 434 | if len >= self.capacity() {
|
| 435 | return Err(InsertError::OutOfCapacity(element));
|
| 436 | }
|
| 437 |
|
| 438 | |
| 439 | let p = unsafe { self.as_mut_ptr().add(index) };
|
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | unsafe { ptr::copy(p, p.add(1), len - index) };
|
| 445 | |
| 446 | |
| 447 | unsafe { ptr::write(p, element) };
|
| 448 | |
| 449 | unsafe { self.inc_len(1) };
|
| 450 | Ok(())
|
| 451 | }
|
| 452 |
|
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | pub fn pop(&mut self) -> Option<T> {
|
| 469 | if self.is_empty() {
|
| 470 | return None;
|
| 471 | }
|
| 472 |
|
| 473 | let removed: *mut T = {
|
| 474 | |
| 475 | let slice = unsafe { self.dec_len(1) };
|
| 476 | |
| 477 | unsafe { slice.get_unchecked_mut(0) }
|
| 478 | };
|
| 479 |
|
| 480 | |
| 481 | Some(unsafe { removed.read() })
|
| 482 | }
|
| 483 |
|
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | pub fn remove(&mut self, i: usize) -> Result<T, RemoveError> {
|
| 495 | let value = {
|
| 496 | let value_ref = self.get(i).ok_or(RemoveError)?;
|
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | unsafe { ptr::read(value_ref) }
|
| 502 | };
|
| 503 |
|
| 504 | |
| 505 | let p = unsafe { self.as_mut_ptr().add(i) };
|
| 506 |
|
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | unsafe { ptr::copy(p.add(1), p, self.len - i - 1) };
|
| 512 |
|
| 513 | |
| 514 | |
| 515 | unsafe { self.dec_len(1) };
|
| 516 |
|
| 517 | Ok(value)
|
| 518 | }
|
| 519 |
|
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | pub fn with_capacity(capacity: usize, flags: Flags) -> Result<Self, AllocError> {
|
| 531 | let mut v = Vec::new();
|
| 532 |
|
| 533 | v.reserve(capacity, flags)?;
|
| 534 |
|
| 535 | Ok(v)
|
| 536 | }
|
| 537 |
|
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | pub fn zeroed(n: usize, flags: Flags) -> Result<Self, AllocError>
|
| 549 | where
|
| 550 | T: Zeroable,
|
| 551 | {
|
| 552 | let mut v = Self::with_capacity(n, flags | __GFP_ZERO)?;
|
| 553 |
|
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | unsafe { v.inc_len(n) };
|
| 559 | Ok(v)
|
| 560 | }
|
| 561 |
|
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | pub unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Self {
|
| 604 | let layout = if Self::is_zst() {
|
| 605 | ArrayLayout::empty()
|
| 606 | } else {
|
| 607 | |
| 608 | |
| 609 | unsafe { ArrayLayout::new_unchecked(capacity) }
|
| 610 | };
|
| 611 |
|
| 612 | |
| 613 | |
| 614 | Self {
|
| 615 | |
| 616 | |
| 617 | ptr: unsafe { NonNull::new_unchecked(ptr) },
|
| 618 | layout,
|
| 619 | len: length,
|
| 620 | _p: PhantomData::<A>,
|
| 621 | }
|
| 622 | }
|
| 623 |
|
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | pub fn into_raw_parts(self) -> (*mut T, usize, usize) {
|
| 630 | let mut me = ManuallyDrop::new(self);
|
| 631 | let len = me.len();
|
| 632 | let capacity = me.capacity();
|
| 633 | let ptr = me.as_mut_ptr();
|
| 634 | (ptr, len, capacity)
|
| 635 | }
|
| 636 |
|
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | #[inline]
|
| 653 | pub fn clear(&mut self) {
|
| 654 | self.truncate(0);
|
| 655 | }
|
| 656 |
|
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | pub fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocError> {
|
| 676 | let len = self.len();
|
| 677 | let cap = self.capacity();
|
| 678 |
|
| 679 | if cap - len >= additional {
|
| 680 | return Ok(());
|
| 681 | }
|
| 682 |
|
| 683 | if Self::is_zst() {
|
| 684 | |
| 685 | return Err(AllocError);
|
| 686 | }
|
| 687 |
|
| 688 | |
| 689 | |
| 690 | let new_cap = core::cmp::max(cap * 2, len.checked_add(additional).ok_or(AllocError)?);
|
| 691 | let layout = ArrayLayout::new(new_cap).map_err(|_| AllocError)?;
|
| 692 |
|
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | let ptr = unsafe {
|
| 698 | A::realloc(
|
| 699 | Some(self.ptr.cast()),
|
| 700 | layout.into(),
|
| 701 | self.layout.into(),
|
| 702 | flags,
|
| 703 | NumaNode::NO_NODE,
|
| 704 | )?
|
| 705 | };
|
| 706 |
|
| 707 | |
| 708 | |
| 709 | |
| 710 | self.ptr = ptr.cast();
|
| 711 | self.layout = layout;
|
| 712 |
|
| 713 | Ok(())
|
| 714 | }
|
| 715 |
|
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | pub fn truncate(&mut self, len: usize) {
|
| 732 | if let Some(count) = self.len().checked_sub(len) {
|
| 733 | |
| 734 | |
| 735 | let ptr: *mut [T] = unsafe { self.dec_len(count) };
|
| 736 |
|
| 737 | |
| 738 | |
| 739 | unsafe { ptr::drop_in_place(ptr) };
|
| 740 | }
|
| 741 | }
|
| 742 |
|
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | pub fn drain_all(&mut self) -> DrainAll<'_, T> {
|
| 758 | |
| 759 | let elems = unsafe { self.dec_len(self.len()) };
|
| 760 | |
| 761 | |
| 762 | DrainAll {
|
| 763 | elements: elems.iter_mut(),
|
| 764 | }
|
| 765 | }
|
| 766 |
|
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | pub fn retain(&mut self, mut f: impl FnMut(&mut T) -> bool) {
|
| 778 | let mut num_kept = 0;
|
| 779 | let mut next_to_check = 0;
|
| 780 | while let Some(to_check) = self.get_mut(next_to_check) {
|
| 781 | if f(to_check) {
|
| 782 | self.swap(num_kept, next_to_check);
|
| 783 | num_kept += 1;
|
| 784 | }
|
| 785 | next_to_check += 1;
|
| 786 | }
|
| 787 | self.truncate(num_kept);
|
| 788 | }
|
| 789 | }
|
| 790 | |
| 791 | |
| 792 | |
| 793 | impl<T> Vec<T, KVmalloc> {
|
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | pub fn shrink_to(&mut self, min_capacity: usize, flags: Flags) -> Result<(), AllocError> {
|
| 817 | let target_cap = core::cmp::max(self.len(), min_capacity);
|
| 818 |
|
| 819 | if self.capacity() <= target_cap {
|
| 820 | return Ok(());
|
| 821 | }
|
| 822 |
|
| 823 | if Self::is_zst() {
|
| 824 | return Ok(());
|
| 825 | }
|
| 826 |
|
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | if !unsafe { bindings::is_vmalloc_addr(self.ptr.as_ptr().cast()) } {
|
| 832 | let new_layout = ArrayLayout::<T>::new(target_cap).map_err(|_| AllocError)?;
|
| 833 |
|
| 834 | |
| 835 | |
| 836 | |
| 837 | let ptr = unsafe {
|
| 838 | KVmalloc::realloc(
|
| 839 | Some(self.ptr.cast()),
|
| 840 | new_layout.into(),
|
| 841 | self.layout.into(),
|
| 842 | flags,
|
| 843 | NumaNode::NO_NODE,
|
| 844 | )?
|
| 845 | };
|
| 846 |
|
| 847 | self.ptr = ptr.cast();
|
| 848 | self.layout = new_layout;
|
| 849 | return Ok(());
|
| 850 | }
|
| 851 |
|
| 852 | |
| 853 | let current_size = self.capacity() * core::mem::size_of::<T>();
|
| 854 | let target_size = target_cap * core::mem::size_of::<T>();
|
| 855 | let current_pages = current_size.div_ceil(PAGE_SIZE);
|
| 856 | let target_pages = target_size.div_ceil(PAGE_SIZE);
|
| 857 |
|
| 858 | if current_pages <= target_pages {
|
| 859 | return Ok(());
|
| 860 | }
|
| 861 |
|
| 862 | if target_cap == 0 {
|
| 863 | if !self.layout.is_empty() {
|
| 864 | |
| 865 | |
| 866 | |
| 867 | unsafe { KVmalloc::free(self.ptr.cast(), self.layout.into()) };
|
| 868 | }
|
| 869 | self.ptr = NonNull::dangling();
|
| 870 | self.layout = ArrayLayout::empty();
|
| 871 | return Ok(());
|
| 872 | }
|
| 873 |
|
| 874 | |
| 875 | let new_layout = unsafe { ArrayLayout::<T>::new_unchecked(target_cap) };
|
| 876 |
|
| 877 | let new_ptr = KVmalloc::alloc(new_layout.into(), flags, NumaNode::NO_NODE)?;
|
| 878 |
|
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | unsafe {
|
| 885 | ptr::copy_nonoverlapping(self.as_ptr(), new_ptr.as_ptr().cast::<T>(), self.len())
|
| 886 | };
|
| 887 |
|
| 888 | |
| 889 | |
| 890 | |
| 891 | unsafe { KVmalloc::free(self.ptr.cast(), self.layout.into()) };
|
| 892 |
|
| 893 | self.ptr = new_ptr.cast::<T>();
|
| 894 | self.layout = new_layout;
|
| 895 |
|
| 896 | Ok(())
|
| 897 | }
|
| 898 | }
|
| 899 |
|
| 900 | impl<T: Clone, A: Allocator> Vec<T, A> {
|
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | pub fn extend_with(&mut self, n: usize, value: T, flags: Flags) -> Result<(), AllocError> {
|
| 921 | if n == 0 {
|
| 922 | return Ok(());
|
| 923 | }
|
| 924 |
|
| 925 | self.reserve(n, flags)?;
|
| 926 |
|
| 927 | let spare = self.spare_capacity_mut();
|
| 928 |
|
| 929 | for item in spare.iter_mut().take(n - 1) {
|
| 930 | item.write(value.clone());
|
| 931 | }
|
| 932 |
|
| 933 | |
| 934 | spare[n - 1].write(value);
|
| 935 |
|
| 936 | |
| 937 | |
| 938 | |
| 939 | unsafe { self.inc_len(n) };
|
| 940 |
|
| 941 | Ok(())
|
| 942 | }
|
| 943 |
|
| 944 | |
| 945 | |
| 946 | |
| 947 | |
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | pub fn extend_from_slice(&mut self, other: &[T], flags: Flags) -> Result<(), AllocError> {
|
| 960 | self.reserve(other.len(), flags)?;
|
| 961 | for (slot, item) in core::iter::zip(self.spare_capacity_mut(), other) {
|
| 962 | slot.write(item.clone());
|
| 963 | }
|
| 964 |
|
| 965 | |
| 966 | |
| 967 | |
| 968 | |
| 969 | |
| 970 | unsafe { self.inc_len(other.len()) };
|
| 971 | Ok(())
|
| 972 | }
|
| 973 |
|
| 974 | |
| 975 | pub fn from_elem(value: T, n: usize, flags: Flags) -> Result<Self, AllocError> {
|
| 976 | let mut v = Self::with_capacity(n, flags)?;
|
| 977 |
|
| 978 | v.extend_with(n, value, flags)?;
|
| 979 |
|
| 980 | Ok(v)
|
| 981 | }
|
| 982 |
|
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | |
| 990 | |
| 991 | |
| 992 | |
| 993 | |
| 994 | |
| 995 | |
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | pub fn resize(&mut self, new_len: usize, value: T, flags: Flags) -> Result<(), AllocError> {
|
| 1001 | match new_len.checked_sub(self.len()) {
|
| 1002 | Some(n) => self.extend_with(n, value, flags),
|
| 1003 | None => {
|
| 1004 | self.truncate(new_len);
|
| 1005 | Ok(())
|
| 1006 | }
|
| 1007 | }
|
| 1008 | }
|
| 1009 | }
|
| 1010 |
|
| 1011 | impl<T, A> Drop for Vec<T, A>
|
| 1012 | where
|
| 1013 | A: Allocator,
|
| 1014 | {
|
| 1015 | fn drop(&mut self) {
|
| 1016 | |
| 1017 | unsafe {
|
| 1018 | ptr::drop_in_place(core::ptr::slice_from_raw_parts_mut(
|
| 1019 | self.as_mut_ptr(),
|
| 1020 | self.len,
|
| 1021 | ))
|
| 1022 | };
|
| 1023 |
|
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | unsafe { A::free(self.ptr.cast(), self.layout.into()) };
|
| 1028 | }
|
| 1029 | }
|
| 1030 |
|
| 1031 | impl<T, A, const N: usize> From<Box<[T; N], A>> for Vec<T, A>
|
| 1032 | where
|
| 1033 | A: Allocator,
|
| 1034 | {
|
| 1035 | fn from(b: Box<[T; N], A>) -> Vec<T, A> {
|
| 1036 | let len = b.len();
|
| 1037 | let ptr = Box::into_raw(b);
|
| 1038 |
|
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | |
| 1044 | |
| 1045 | unsafe { Vec::from_raw_parts(ptr.cast(), len, len) }
|
| 1046 | }
|
| 1047 | }
|
| 1048 |
|
| 1049 | impl<T, A: Allocator> Default for Vec<T, A> {
|
| 1050 | #[inline]
|
| 1051 | fn default() -> Self {
|
| 1052 | Self::new()
|
| 1053 | }
|
| 1054 | }
|
| 1055 |
|
| 1056 | impl<T: fmt::Debug, A: Allocator> fmt::Debug for Vec<T, A> {
|
| 1057 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 1058 | fmt::Debug::fmt(&**self, f)
|
| 1059 | }
|
| 1060 | }
|
| 1061 |
|
| 1062 | impl<T, A> Deref for Vec<T, A>
|
| 1063 | where
|
| 1064 | A: Allocator,
|
| 1065 | {
|
| 1066 | type Target = [T];
|
| 1067 |
|
| 1068 | #[inline]
|
| 1069 | fn deref(&self) -> &[T] {
|
| 1070 | |
| 1071 | |
| 1072 | unsafe { slice::from_raw_parts(self.as_ptr(), self.len) }
|
| 1073 | }
|
| 1074 | }
|
| 1075 |
|
| 1076 | impl<T, A> DerefMut for Vec<T, A>
|
| 1077 | where
|
| 1078 | A: Allocator,
|
| 1079 | {
|
| 1080 | #[inline]
|
| 1081 | fn deref_mut(&mut self) -> &mut [T] {
|
| 1082 | |
| 1083 | |
| 1084 | unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
|
| 1085 | }
|
| 1086 | }
|
| 1087 |
|
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | |
| 1102 | |
| 1103 | |
| 1104 | |
| 1105 | impl<T, A> Borrow<[T]> for Vec<T, A>
|
| 1106 | where
|
| 1107 | A: Allocator,
|
| 1108 | {
|
| 1109 | fn borrow(&self) -> &[T] {
|
| 1110 | self.as_slice()
|
| 1111 | }
|
| 1112 | }
|
| 1113 |
|
| 1114 | |
| 1115 | |
| 1116 | |
| 1117 | |
| 1118 | |
| 1119 | |
| 1120 | |
| 1121 | |
| 1122 | |
| 1123 | |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | |
| 1130 | |
| 1131 | impl<T, A> BorrowMut<[T]> for Vec<T, A>
|
| 1132 | where
|
| 1133 | A: Allocator,
|
| 1134 | {
|
| 1135 | fn borrow_mut(&mut self) -> &mut [T] {
|
| 1136 | self.as_mut_slice()
|
| 1137 | }
|
| 1138 | }
|
| 1139 |
|
| 1140 | impl<T: Eq, A> Eq for Vec<T, A> where A: Allocator {}
|
| 1141 |
|
| 1142 | impl<T, I: SliceIndex<[T]>, A> Index<I> for Vec<T, A>
|
| 1143 | where
|
| 1144 | A: Allocator,
|
| 1145 | {
|
| 1146 | type Output = I::Output;
|
| 1147 |
|
| 1148 | #[inline]
|
| 1149 | fn index(&self, index: I) -> &Self::Output {
|
| 1150 | Index::index(&**self, index)
|
| 1151 | }
|
| 1152 | }
|
| 1153 |
|
| 1154 | impl<T, I: SliceIndex<[T]>, A> IndexMut<I> for Vec<T, A>
|
| 1155 | where
|
| 1156 | A: Allocator,
|
| 1157 | {
|
| 1158 | #[inline]
|
| 1159 | fn index_mut(&mut self, index: I) -> &mut Self::Output {
|
| 1160 | IndexMut::index_mut(&mut **self, index)
|
| 1161 | }
|
| 1162 | }
|
| 1163 |
|
| 1164 | macro_rules! impl_slice_eq {
|
| 1165 | ($([$($vars:tt)*] $lhs:ty, $rhs:ty,)*) => {
|
| 1166 | $(
|
| 1167 | impl<T, U, $($vars)*> PartialEq<$rhs> for $lhs
|
| 1168 | where
|
| 1169 | T: PartialEq<U>,
|
| 1170 | {
|
| 1171 | #[inline]
|
| 1172 | fn eq(&self, other: &$rhs) -> bool { self[..] == other[..] }
|
| 1173 | }
|
| 1174 | )*
|
| 1175 | }
|
| 1176 | }
|
| 1177 |
|
| 1178 | impl_slice_eq! {
|
| 1179 | [A1: Allocator, A2: Allocator] Vec<T, A1>, Vec<U, A2>,
|
| 1180 | [A: Allocator] Vec<T, A>, &[U],
|
| 1181 | [A: Allocator] Vec<T, A>, &mut [U],
|
| 1182 | [A: Allocator] &[T], Vec<U, A>,
|
| 1183 | [A: Allocator] &mut [T], Vec<U, A>,
|
| 1184 | [A: Allocator] Vec<T, A>, [U],
|
| 1185 | [A: Allocator] [T], Vec<U, A>,
|
| 1186 | [A: Allocator, const N: usize] Vec<T, A>, [U; N],
|
| 1187 | [A: Allocator, const N: usize] Vec<T, A>, &[U; N],
|
| 1188 | }
|
| 1189 |
|
| 1190 | impl<'a, T, A> IntoIterator for &'a Vec<T, A>
|
| 1191 | where
|
| 1192 | A: Allocator,
|
| 1193 | {
|
| 1194 | type Item = &'a T;
|
| 1195 | type IntoIter = slice::Iter<'a, T>;
|
| 1196 |
|
| 1197 | fn into_iter(self) -> Self::IntoIter {
|
| 1198 | self.iter()
|
| 1199 | }
|
| 1200 | }
|
| 1201 |
|
| 1202 | impl<'a, T, A: Allocator> IntoIterator for &'a mut Vec<T, A>
|
| 1203 | where
|
| 1204 | A: Allocator,
|
| 1205 | {
|
| 1206 | type Item = &'a mut T;
|
| 1207 | type IntoIter = slice::IterMut<'a, T>;
|
| 1208 |
|
| 1209 | fn into_iter(self) -> Self::IntoIter {
|
| 1210 | self.iter_mut()
|
| 1211 | }
|
| 1212 | }
|
| 1213 |
|
| 1214 | |
| 1215 | |
| 1216 | |
| 1217 | |
| 1218 | |
| 1219 | |
| 1220 | |
| 1221 | |
| 1222 | |
| 1223 | |
| 1224 | |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | impl<T> AsPageIter for VVec<T> {
|
| 1238 | type Iter<'a>
|
| 1239 | = VmallocPageIter<'a>
|
| 1240 | where
|
| 1241 | T: 'a;
|
| 1242 |
|
| 1243 | fn page_iter(&mut self) -> Self::Iter<'_> {
|
| 1244 | let ptr = self.ptr.cast();
|
| 1245 | let size = self.layout.size();
|
| 1246 |
|
| 1247 | |
| 1248 | |
| 1249 | |
| 1250 | |
| 1251 | unsafe { VmallocPageIter::new(ptr, size) }
|
| 1252 | }
|
| 1253 | }
|
| 1254 |
|
| 1255 | |
| 1256 | |
| 1257 | |
| 1258 | |
| 1259 | |
| 1260 | |
| 1261 | |
| 1262 | |
| 1263 | |
| 1264 | |
| 1265 | |
| 1266 | |
| 1267 | |
| 1268 | pub struct IntoIter<T, A: Allocator> {
|
| 1269 | ptr: *mut T,
|
| 1270 | buf: NonNull<T>,
|
| 1271 | len: usize,
|
| 1272 | layout: ArrayLayout<T>,
|
| 1273 | _p: PhantomData<A>,
|
| 1274 | }
|
| 1275 |
|
| 1276 | impl<T, A> IntoIter<T, A>
|
| 1277 | where
|
| 1278 | A: Allocator,
|
| 1279 | {
|
| 1280 | fn into_raw_parts(self) -> (*mut T, NonNull<T>, usize, usize) {
|
| 1281 | let me = ManuallyDrop::new(self);
|
| 1282 | let ptr = me.ptr;
|
| 1283 | let buf = me.buf;
|
| 1284 | let len = me.len;
|
| 1285 | let cap = me.layout.len();
|
| 1286 | (ptr, buf, len, cap)
|
| 1287 | }
|
| 1288 |
|
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | |
| 1299 | |
| 1300 | |
| 1301 | |
| 1302 | |
| 1303 | |
| 1304 | |
| 1305 | |
| 1306 | |
| 1307 | |
| 1308 | |
| 1309 | |
| 1310 | |
| 1311 | |
| 1312 | |
| 1313 | |
| 1314 | |
| 1315 | |
| 1316 | |
| 1317 | |
| 1318 | |
| 1319 | |
| 1320 | |
| 1321 | |
| 1322 | |
| 1323 | |
| 1324 | pub fn collect(self, flags: Flags) -> Vec<T, A> {
|
| 1325 | let old_layout = self.layout;
|
| 1326 | let (mut ptr, buf, len, mut cap) = self.into_raw_parts();
|
| 1327 | let has_advanced = ptr != buf.as_ptr();
|
| 1328 |
|
| 1329 | if has_advanced {
|
| 1330 | |
| 1331 | |
| 1332 | |
| 1333 | |
| 1334 | |
| 1335 | |
| 1336 | |
| 1337 | |
| 1338 | unsafe { ptr::copy(ptr, buf.as_ptr(), len) };
|
| 1339 | ptr = buf.as_ptr();
|
| 1340 |
|
| 1341 | |
| 1342 | |
| 1343 | let layout = unsafe { ArrayLayout::<T>::new_unchecked(len) };
|
| 1344 |
|
| 1345 | |
| 1346 | |
| 1347 | |
| 1348 | ptr = match unsafe {
|
| 1349 | A::realloc(
|
| 1350 | Some(buf.cast()),
|
| 1351 | layout.into(),
|
| 1352 | old_layout.into(),
|
| 1353 | flags,
|
| 1354 | NumaNode::NO_NODE,
|
| 1355 | )
|
| 1356 | } {
|
| 1357 | |
| 1358 | |
| 1359 | Err(_) => ptr,
|
| 1360 | Ok(ptr) => {
|
| 1361 | cap = len;
|
| 1362 | ptr.as_ptr().cast()
|
| 1363 | }
|
| 1364 | };
|
| 1365 | }
|
| 1366 |
|
| 1367 | |
| 1368 | |
| 1369 | |
| 1370 | |
| 1371 | |
| 1372 | |
| 1373 | |
| 1374 | unsafe { Vec::from_raw_parts(ptr, len, cap) }
|
| 1375 | }
|
| 1376 | }
|
| 1377 |
|
| 1378 | impl<T, A> Iterator for IntoIter<T, A>
|
| 1379 | where
|
| 1380 | A: Allocator,
|
| 1381 | {
|
| 1382 | type Item = T;
|
| 1383 |
|
| 1384 | |
| 1385 | |
| 1386 | |
| 1387 | |
| 1388 | |
| 1389 | |
| 1390 | |
| 1391 | |
| 1392 | |
| 1393 | |
| 1394 | |
| 1395 | |
| 1396 | |
| 1397 | fn next(&mut self) -> Option<T> {
|
| 1398 | if self.len == 0 {
|
| 1399 | return None;
|
| 1400 | }
|
| 1401 |
|
| 1402 | let current = self.ptr;
|
| 1403 |
|
| 1404 | |
| 1405 | |
| 1406 | unsafe { self.ptr = self.ptr.add(1) };
|
| 1407 |
|
| 1408 | self.len -= 1;
|
| 1409 |
|
| 1410 | |
| 1411 | Some(unsafe { current.read() })
|
| 1412 | }
|
| 1413 |
|
| 1414 | |
| 1415 | |
| 1416 | |
| 1417 | |
| 1418 | |
| 1419 | |
| 1420 | |
| 1421 | |
| 1422 | |
| 1423 | |
| 1424 | |
| 1425 | |
| 1426 | |
| 1427 | |
| 1428 | |
| 1429 | |
| 1430 | |
| 1431 | |
| 1432 | fn size_hint(&self) -> (usize, Option<usize>) {
|
| 1433 | (self.len, Some(self.len))
|
| 1434 | }
|
| 1435 | }
|
| 1436 |
|
| 1437 | impl<T, A> Drop for IntoIter<T, A>
|
| 1438 | where
|
| 1439 | A: Allocator,
|
| 1440 | {
|
| 1441 | fn drop(&mut self) {
|
| 1442 | |
| 1443 | unsafe { ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.ptr, self.len)) };
|
| 1444 |
|
| 1445 | |
| 1446 | |
| 1447 | |
| 1448 | unsafe { A::free(self.buf.cast(), self.layout.into()) };
|
| 1449 | }
|
| 1450 | }
|
| 1451 |
|
| 1452 | impl<T, A> IntoIterator for Vec<T, A>
|
| 1453 | where
|
| 1454 | A: Allocator,
|
| 1455 | {
|
| 1456 | type Item = T;
|
| 1457 | type IntoIter = IntoIter<T, A>;
|
| 1458 |
|
| 1459 | |
| 1460 | |
| 1461 | |
| 1462 | |
| 1463 | |
| 1464 | |
| 1465 | |
| 1466 | |
| 1467 | |
| 1468 | |
| 1469 | |
| 1470 | |
| 1471 | |
| 1472 | |
| 1473 | |
| 1474 | |
| 1475 | |
| 1476 | |
| 1477 | |
| 1478 | |
| 1479 | |
| 1480 | |
| 1481 | |
| 1482 | |
| 1483 | |
| 1484 | |
| 1485 | |
| 1486 | |
| 1487 | #[inline]
|
| 1488 | fn into_iter(self) -> Self::IntoIter {
|
| 1489 | let buf = self.ptr;
|
| 1490 | let layout = self.layout;
|
| 1491 | let (ptr, len, _) = self.into_raw_parts();
|
| 1492 |
|
| 1493 | IntoIter {
|
| 1494 | ptr,
|
| 1495 | buf,
|
| 1496 | len,
|
| 1497 | layout,
|
| 1498 | _p: PhantomData::<A>,
|
| 1499 | }
|
| 1500 | }
|
| 1501 | }
|
| 1502 |
|
| 1503 | |
| 1504 | |
| 1505 | |
| 1506 | |
| 1507 | |
| 1508 | |
| 1509 | pub struct DrainAll<'vec, T> {
|
| 1510 | elements: slice::IterMut<'vec, T>,
|
| 1511 | }
|
| 1512 |
|
| 1513 | impl<'vec, T> Iterator for DrainAll<'vec, T> {
|
| 1514 | type Item = T;
|
| 1515 |
|
| 1516 | fn next(&mut self) -> Option<T> {
|
| 1517 | let elem: *mut T = self.elements.next()?;
|
| 1518 | |
| 1519 | Some(unsafe { elem.read() })
|
| 1520 | }
|
| 1521 |
|
| 1522 | fn size_hint(&self) -> (usize, Option<usize>) {
|
| 1523 | self.elements.size_hint()
|
| 1524 | }
|
| 1525 | }
|
| 1526 |
|
| 1527 | impl<'vec, T> Drop for DrainAll<'vec, T> {
|
| 1528 | fn drop(&mut self) {
|
| 1529 | if core::mem::needs_drop::<T>() {
|
| 1530 | let iter = core::mem::take(&mut self.elements);
|
| 1531 | let ptr: *mut [T] = iter.into_slice();
|
| 1532 | |
| 1533 | unsafe { ptr::drop_in_place(ptr) };
|
| 1534 | }
|
| 1535 | }
|
| 1536 | }
|
| 1537 |
|
| 1538 | #[cfg(CONFIG_RUST_KVEC_KUNIT_TEST)]
|
| 1539 | #[macros::kunit_tests(rust_kvec)]
|
| 1540 | mod tests {
|
| 1541 | use super::*;
|
| 1542 | use crate::prelude::*;
|
| 1543 |
|
| 1544 | #[test]
|
| 1545 | fn test_kvec_retain() {
|
| 1546 | |
| 1547 | #[expect(clippy::needless_range_loop)]
|
| 1548 | fn verify(c: &[bool]) {
|
| 1549 | let mut vec1: KVec<usize> = KVec::with_capacity(c.len(), GFP_KERNEL).unwrap();
|
| 1550 | let mut vec2: KVec<usize> = KVec::with_capacity(c.len(), GFP_KERNEL).unwrap();
|
| 1551 |
|
| 1552 | for i in 0..c.len() {
|
| 1553 | vec1.push_within_capacity(i).unwrap();
|
| 1554 | if c[i] {
|
| 1555 | vec2.push_within_capacity(i).unwrap();
|
| 1556 | }
|
| 1557 | }
|
| 1558 |
|
| 1559 | vec1.retain(|i| c[*i]);
|
| 1560 |
|
| 1561 | assert_eq!(vec1, vec2);
|
| 1562 | }
|
| 1563 |
|
| 1564 | |
| 1565 | fn add(value: &mut [bool]) {
|
| 1566 | let mut carry = true;
|
| 1567 | for v in value {
|
| 1568 | let new_v = carry != *v;
|
| 1569 | carry = carry && *v;
|
| 1570 | *v = new_v;
|
| 1571 | }
|
| 1572 | }
|
| 1573 |
|
| 1574 | |
| 1575 | |
| 1576 | |
| 1577 | let mut func = KVec::with_capacity(10, GFP_KERNEL).unwrap();
|
| 1578 | for len in 0..10 {
|
| 1579 | for _ in 0u32..1u32 << len {
|
| 1580 | verify(&func);
|
| 1581 | add(&mut func);
|
| 1582 | }
|
| 1583 | func.push_within_capacity(false).unwrap();
|
| 1584 | }
|
| 1585 | }
|
| 1586 |
|
| 1587 | #[test]
|
| 1588 | fn test_kvvec_shrink_to() {
|
| 1589 | use crate::page::PAGE_SIZE;
|
| 1590 |
|
| 1591 | |
| 1592 | let mut v = KVVec::<u8>::with_capacity(PAGE_SIZE * 4, GFP_KERNEL).unwrap();
|
| 1593 |
|
| 1594 | |
| 1595 | v.push(1, GFP_KERNEL).unwrap();
|
| 1596 | v.push(2, GFP_KERNEL).unwrap();
|
| 1597 | v.push(3, GFP_KERNEL).unwrap();
|
| 1598 |
|
| 1599 | let initial_capacity = v.capacity();
|
| 1600 | assert!(initial_capacity >= PAGE_SIZE * 4);
|
| 1601 |
|
| 1602 | |
| 1603 | v.shrink_to(PAGE_SIZE, GFP_KERNEL).unwrap();
|
| 1604 |
|
| 1605 | |
| 1606 | assert!(v.capacity() < initial_capacity);
|
| 1607 | assert!(v.capacity() >= PAGE_SIZE);
|
| 1608 |
|
| 1609 | |
| 1610 | assert_eq!(v.len(), 3);
|
| 1611 | assert_eq!(v[0], 1);
|
| 1612 | assert_eq!(v[1], 2);
|
| 1613 | assert_eq!(v[2], 3);
|
| 1614 |
|
| 1615 | |
| 1616 | v.shrink_to(0, GFP_KERNEL).unwrap();
|
| 1617 |
|
| 1618 | |
| 1619 | assert!(v.capacity() >= v.len());
|
| 1620 |
|
| 1621 | |
| 1622 | assert_eq!(v.len(), 3);
|
| 1623 | assert_eq!(v[0], 1);
|
| 1624 | assert_eq!(v[1], 2);
|
| 1625 | assert_eq!(v[2], 3);
|
| 1626 | }
|
| 1627 |
|
| 1628 | #[test]
|
| 1629 | fn test_kvvec_shrink_to_empty() {
|
| 1630 | use crate::page::PAGE_SIZE;
|
| 1631 |
|
| 1632 | |
| 1633 | let mut v = KVVec::<u8>::with_capacity(PAGE_SIZE * 4, GFP_KERNEL).unwrap();
|
| 1634 |
|
| 1635 | assert!(v.is_empty());
|
| 1636 |
|
| 1637 | |
| 1638 | v.shrink_to(0, GFP_KERNEL).unwrap();
|
| 1639 |
|
| 1640 | |
| 1641 | assert_eq!(v.capacity(), 0);
|
| 1642 | assert!(v.is_empty());
|
| 1643 | }
|
| 1644 |
|
| 1645 | #[test]
|
| 1646 | fn test_kvvec_shrink_to_no_op() {
|
| 1647 | use crate::page::PAGE_SIZE;
|
| 1648 |
|
| 1649 | |
| 1650 | let mut v = KVVec::<u8>::with_capacity(PAGE_SIZE, GFP_KERNEL).unwrap();
|
| 1651 | v.push(1, GFP_KERNEL).unwrap();
|
| 1652 |
|
| 1653 | let capacity_before = v.capacity();
|
| 1654 |
|
| 1655 | |
| 1656 | v.shrink_to(capacity_before + 100, GFP_KERNEL).unwrap();
|
| 1657 |
|
| 1658 | assert_eq!(v.capacity(), capacity_before);
|
| 1659 | assert_eq!(v.len(), 1);
|
| 1660 | assert_eq!(v[0], 1);
|
| 1661 | }
|
| 1662 |
|
| 1663 | #[test]
|
| 1664 | fn test_kvvec_shrink_to_respects_min_capacity() {
|
| 1665 | use crate::page::PAGE_SIZE;
|
| 1666 |
|
| 1667 | |
| 1668 | let mut v = KVVec::<u8>::with_capacity(PAGE_SIZE * 4, GFP_KERNEL).unwrap();
|
| 1669 |
|
| 1670 | |
| 1671 | for i in 0..10u8 {
|
| 1672 | v.push(i, GFP_KERNEL).unwrap();
|
| 1673 | }
|
| 1674 |
|
| 1675 | |
| 1676 | let min_cap = PAGE_SIZE * 2;
|
| 1677 | v.shrink_to(min_cap, GFP_KERNEL).unwrap();
|
| 1678 |
|
| 1679 | |
| 1680 | assert!(v.capacity() >= min_cap);
|
| 1681 |
|
| 1682 | |
| 1683 | assert_eq!(v.len(), 10);
|
| 1684 | for i in 0..10u8 {
|
| 1685 | assert_eq!(v[i as usize], i);
|
| 1686 | }
|
| 1687 | }
|
| 1688 | }
|