| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | use super::{AllocError, Flags};
|
| 6 | use alloc::vec::Vec;
|
| 7 |
|
| 8 | |
| 9 | pub trait VecExt<T>: Sized {
|
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | fn with_capacity(capacity: usize, flags: Flags) -> Result<Self, AllocError>;
|
| 21 |
|
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError>;
|
| 36 |
|
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | fn extend_from_slice(&mut self, other: &[T], flags: Flags) -> Result<(), AllocError>
|
| 53 | where
|
| 54 | T: Clone;
|
| 55 |
|
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocError>;
|
| 75 | }
|
| 76 |
|
| 77 | impl<T> VecExt<T> for Vec<T> {
|
| 78 | fn with_capacity(capacity: usize, flags: Flags) -> Result<Self, AllocError> {
|
| 79 | let mut v = Vec::new();
|
| 80 | <Self as VecExt<_>>::reserve(&mut v, capacity, flags)?;
|
| 81 | Ok(v)
|
| 82 | }
|
| 83 |
|
| 84 | fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> {
|
| 85 | <Self as VecExt<_>>::reserve(self, 1, flags)?;
|
| 86 | let s = self.spare_capacity_mut();
|
| 87 | s[0].write(v);
|
| 88 |
|
| 89 | |
| 90 | |
| 91 | |
| 92 | unsafe { self.set_len(self.len() + 1) };
|
| 93 | Ok(())
|
| 94 | }
|
| 95 |
|
| 96 | fn extend_from_slice(&mut self, other: &[T], flags: Flags) -> Result<(), AllocError>
|
| 97 | where
|
| 98 | T: Clone,
|
| 99 | {
|
| 100 | <Self as VecExt<_>>::reserve(self, other.len(), flags)?;
|
| 101 | for (slot, item) in core::iter::zip(self.spare_capacity_mut(), other) {
|
| 102 | slot.write(item.clone());
|
| 103 | }
|
| 104 |
|
| 105 | |
| 106 | |
| 107 | |
| 108 | unsafe { self.set_len(self.len() + other.len()) };
|
| 109 | Ok(())
|
| 110 | }
|
| 111 |
|
| 112 | #[cfg(any(test, testlib))]
|
| 113 | fn reserve(&mut self, additional: usize, _flags: Flags) -> Result<(), AllocError> {
|
| 114 | Vec::reserve(self, additional);
|
| 115 | Ok(())
|
| 116 | }
|
| 117 |
|
| 118 | #[cfg(not(any(test, testlib)))]
|
| 119 | fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocError> {
|
| 120 | let len = self.len();
|
| 121 | let cap = self.capacity();
|
| 122 |
|
| 123 | if cap - len >= additional {
|
| 124 | return Ok(());
|
| 125 | }
|
| 126 |
|
| 127 | if core::mem::size_of::<T>() == 0 {
|
| 128 | |
| 129 | return Err(AllocError);
|
| 130 | }
|
| 131 |
|
| 132 | |
| 133 | |
| 134 | let new_cap = core::cmp::max(cap * 2, len.checked_add(additional).ok_or(AllocError)?);
|
| 135 | let layout = core::alloc::Layout::array::<T>(new_cap).map_err(|_| AllocError)?;
|
| 136 |
|
| 137 | let (old_ptr, len, cap) = destructure(self);
|
| 138 |
|
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | let ptr = if cap == 0 {
|
| 144 | core::ptr::null_mut()
|
| 145 | } else {
|
| 146 | old_ptr
|
| 147 | };
|
| 148 |
|
| 149 | |
| 150 | |
| 151 | let new_ptr = unsafe { super::allocator::krealloc_aligned(ptr.cast(), layout, flags) };
|
| 152 | if new_ptr.is_null() {
|
| 153 | |
| 154 | unsafe { rebuild(self, old_ptr, len, cap) };
|
| 155 | Err(AllocError)
|
| 156 | } else {
|
| 157 | |
| 158 | |
| 159 | unsafe { rebuild(self, new_ptr.cast::<T>(), len, new_cap) };
|
| 160 | Ok(())
|
| 161 | }
|
| 162 | }
|
| 163 | }
|
| 164 |
|
| 165 | #[cfg(not(any(test, testlib)))]
|
| 166 | fn destructure<T>(v: &mut Vec<T>) -> (*mut T, usize, usize) {
|
| 167 | let mut tmp = Vec::new();
|
| 168 | core::mem::swap(&mut tmp, v);
|
| 169 | let mut tmp = core::mem::ManuallyDrop::new(tmp);
|
| 170 | let len = tmp.len();
|
| 171 | let cap = tmp.capacity();
|
| 172 | (tmp.as_mut_ptr(), len, cap)
|
| 173 | }
|
| 174 |
|
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | #[cfg(not(any(test, testlib)))]
|
| 181 | unsafe fn rebuild<T>(v: &mut Vec<T>, ptr: *mut T, len: usize, cap: usize) {
|
| 182 | |
| 183 | let mut tmp = unsafe { Vec::from_raw_parts(ptr, len, cap) };
|
| 184 | core::mem::swap(&mut tmp, v);
|
| 185 | }
|