raw_vec.rs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. // SPDX-License-Identifier: Apache-2.0 OR MIT
  2. #![unstable(feature = "raw_vec_internals", reason = "unstable const warnings", issue = "none")]
  3. use core::alloc::LayoutError;
  4. use core::cmp;
  5. use core::intrinsics;
  6. use core::mem::{self, ManuallyDrop, MaybeUninit};
  7. use core::ops::Drop;
  8. use core::ptr::{self, NonNull, Unique};
  9. use core::slice;
  10. #[cfg(not(no_global_oom_handling))]
  11. use crate::alloc::handle_alloc_error;
  12. use crate::alloc::{Allocator, Global, Layout};
  13. use crate::boxed::Box;
  14. use crate::collections::TryReserveError;
  15. use crate::collections::TryReserveErrorKind::*;
  16. #[cfg(test)]
  17. mod tests;
  18. #[cfg(not(no_global_oom_handling))]
  19. enum AllocInit {
  20. /// The contents of the new memory are uninitialized.
  21. Uninitialized,
  22. /// The new memory is guaranteed to be zeroed.
  23. Zeroed,
  24. }
  25. /// A low-level utility for more ergonomically allocating, reallocating, and deallocating
  26. /// a buffer of memory on the heap without having to worry about all the corner cases
  27. /// involved. This type is excellent for building your own data structures like Vec and VecDeque.
  28. /// In particular:
  29. ///
  30. /// * Produces `Unique::dangling()` on zero-sized types.
  31. /// * Produces `Unique::dangling()` on zero-length allocations.
  32. /// * Avoids freeing `Unique::dangling()`.
  33. /// * Catches all overflows in capacity computations (promotes them to "capacity overflow" panics).
  34. /// * Guards against 32-bit systems allocating more than isize::MAX bytes.
  35. /// * Guards against overflowing your length.
  36. /// * Calls `handle_alloc_error` for fallible allocations.
  37. /// * Contains a `ptr::Unique` and thus endows the user with all related benefits.
  38. /// * Uses the excess returned from the allocator to use the largest available capacity.
  39. ///
  40. /// This type does not in anyway inspect the memory that it manages. When dropped it *will*
  41. /// free its memory, but it *won't* try to drop its contents. It is up to the user of `RawVec`
  42. /// to handle the actual things *stored* inside of a `RawVec`.
  43. ///
  44. /// Note that the excess of a zero-sized types is always infinite, so `capacity()` always returns
  45. /// `usize::MAX`. This means that you need to be careful when round-tripping this type with a
  46. /// `Box<[T]>`, since `capacity()` won't yield the length.
  47. #[allow(missing_debug_implementations)]
  48. pub(crate) struct RawVec<T, A: Allocator = Global> {
  49. ptr: Unique<T>,
  50. cap: usize,
  51. alloc: A,
  52. }
  53. impl<T> RawVec<T, Global> {
  54. /// HACK(Centril): This exists because stable `const fn` can only call stable `const fn`, so
  55. /// they cannot call `Self::new()`.
  56. ///
  57. /// If you change `RawVec<T>::new` or dependencies, please take care to not introduce anything
  58. /// that would truly const-call something unstable.
  59. pub const NEW: Self = Self::new();
  60. /// Creates the biggest possible `RawVec` (on the system heap)
  61. /// without allocating. If `T` has positive size, then this makes a
  62. /// `RawVec` with capacity `0`. If `T` is zero-sized, then it makes a
  63. /// `RawVec` with capacity `usize::MAX`. Useful for implementing
  64. /// delayed allocation.
  65. #[must_use]
  66. pub const fn new() -> Self {
  67. Self::new_in(Global)
  68. }
  69. /// Creates a `RawVec` (on the system heap) with exactly the
  70. /// capacity and alignment requirements for a `[T; capacity]`. This is
  71. /// equivalent to calling `RawVec::new` when `capacity` is `0` or `T` is
  72. /// zero-sized. Note that if `T` is zero-sized this means you will
  73. /// *not* get a `RawVec` with the requested capacity.
  74. ///
  75. /// # Panics
  76. ///
  77. /// Panics if the requested capacity exceeds `isize::MAX` bytes.
  78. ///
  79. /// # Aborts
  80. ///
  81. /// Aborts on OOM.
  82. #[cfg(not(any(no_global_oom_handling, test)))]
  83. #[must_use]
  84. #[inline]
  85. pub fn with_capacity(capacity: usize) -> Self {
  86. Self::with_capacity_in(capacity, Global)
  87. }
  88. /// Like `with_capacity`, but guarantees the buffer is zeroed.
  89. #[cfg(not(any(no_global_oom_handling, test)))]
  90. #[must_use]
  91. #[inline]
  92. pub fn with_capacity_zeroed(capacity: usize) -> Self {
  93. Self::with_capacity_zeroed_in(capacity, Global)
  94. }
  95. }
  96. impl<T, A: Allocator> RawVec<T, A> {
  97. // Tiny Vecs are dumb. Skip to:
  98. // - 8 if the element size is 1, because any heap allocators is likely
  99. // to round up a request of less than 8 bytes to at least 8 bytes.
  100. // - 4 if elements are moderate-sized (<= 1 KiB).
  101. // - 1 otherwise, to avoid wasting too much space for very short Vecs.
  102. pub(crate) const MIN_NON_ZERO_CAP: usize = if mem::size_of::<T>() == 1 {
  103. 8
  104. } else if mem::size_of::<T>() <= 1024 {
  105. 4
  106. } else {
  107. 1
  108. };
  109. /// Like `new`, but parameterized over the choice of allocator for
  110. /// the returned `RawVec`.
  111. pub const fn new_in(alloc: A) -> Self {
  112. // `cap: 0` means "unallocated". zero-sized types are ignored.
  113. Self { ptr: Unique::dangling(), cap: 0, alloc }
  114. }
  115. /// Like `with_capacity`, but parameterized over the choice of
  116. /// allocator for the returned `RawVec`.
  117. #[cfg(not(no_global_oom_handling))]
  118. #[inline]
  119. pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
  120. Self::allocate_in(capacity, AllocInit::Uninitialized, alloc)
  121. }
  122. /// Like `with_capacity_zeroed`, but parameterized over the choice
  123. /// of allocator for the returned `RawVec`.
  124. #[cfg(not(no_global_oom_handling))]
  125. #[inline]
  126. pub fn with_capacity_zeroed_in(capacity: usize, alloc: A) -> Self {
  127. Self::allocate_in(capacity, AllocInit::Zeroed, alloc)
  128. }
  129. /// Converts the entire buffer into `Box<[MaybeUninit<T>]>` with the specified `len`.
  130. ///
  131. /// Note that this will correctly reconstitute any `cap` changes
  132. /// that may have been performed. (See description of type for details.)
  133. ///
  134. /// # Safety
  135. ///
  136. /// * `len` must be greater than or equal to the most recently requested capacity, and
  137. /// * `len` must be less than or equal to `self.capacity()`.
  138. ///
  139. /// Note, that the requested capacity and `self.capacity()` could differ, as
  140. /// an allocator could overallocate and return a greater memory block than requested.
  141. pub unsafe fn into_box(self, len: usize) -> Box<[MaybeUninit<T>], A> {
  142. // Sanity-check one half of the safety requirement (we cannot check the other half).
  143. debug_assert!(
  144. len <= self.capacity(),
  145. "`len` must be smaller than or equal to `self.capacity()`"
  146. );
  147. let me = ManuallyDrop::new(self);
  148. unsafe {
  149. let slice = slice::from_raw_parts_mut(me.ptr() as *mut MaybeUninit<T>, len);
  150. Box::from_raw_in(slice, ptr::read(&me.alloc))
  151. }
  152. }
  153. #[cfg(not(no_global_oom_handling))]
  154. fn allocate_in(capacity: usize, init: AllocInit, alloc: A) -> Self {
  155. // Don't allocate here because `Drop` will not deallocate when `capacity` is 0.
  156. if mem::size_of::<T>() == 0 || capacity == 0 {
  157. Self::new_in(alloc)
  158. } else {
  159. // We avoid `unwrap_or_else` here because it bloats the amount of
  160. // LLVM IR generated.
  161. let layout = match Layout::array::<T>(capacity) {
  162. Ok(layout) => layout,
  163. Err(_) => capacity_overflow(),
  164. };
  165. match alloc_guard(layout.size()) {
  166. Ok(_) => {}
  167. Err(_) => capacity_overflow(),
  168. }
  169. let result = match init {
  170. AllocInit::Uninitialized => alloc.allocate(layout),
  171. AllocInit::Zeroed => alloc.allocate_zeroed(layout),
  172. };
  173. let ptr = match result {
  174. Ok(ptr) => ptr,
  175. Err(_) => handle_alloc_error(layout),
  176. };
  177. // Allocators currently return a `NonNull<[u8]>` whose length
  178. // matches the size requested. If that ever changes, the capacity
  179. // here should change to `ptr.len() / mem::size_of::<T>()`.
  180. Self {
  181. ptr: unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) },
  182. cap: capacity,
  183. alloc,
  184. }
  185. }
  186. }
  187. /// Reconstitutes a `RawVec` from a pointer, capacity, and allocator.
  188. ///
  189. /// # Safety
  190. ///
  191. /// The `ptr` must be allocated (via the given allocator `alloc`), and with the given
  192. /// `capacity`.
  193. /// The `capacity` cannot exceed `isize::MAX` for sized types. (only a concern on 32-bit
  194. /// systems). ZST vectors may have a capacity up to `usize::MAX`.
  195. /// If the `ptr` and `capacity` come from a `RawVec` created via `alloc`, then this is
  196. /// guaranteed.
  197. #[inline]
  198. pub unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, alloc: A) -> Self {
  199. Self { ptr: unsafe { Unique::new_unchecked(ptr) }, cap: capacity, alloc }
  200. }
  201. /// Gets a raw pointer to the start of the allocation. Note that this is
  202. /// `Unique::dangling()` if `capacity == 0` or `T` is zero-sized. In the former case, you must
  203. /// be careful.
  204. #[inline]
  205. pub fn ptr(&self) -> *mut T {
  206. self.ptr.as_ptr()
  207. }
  208. /// Gets the capacity of the allocation.
  209. ///
  210. /// This will always be `usize::MAX` if `T` is zero-sized.
  211. #[inline(always)]
  212. pub fn capacity(&self) -> usize {
  213. if mem::size_of::<T>() == 0 { usize::MAX } else { self.cap }
  214. }
  215. /// Returns a shared reference to the allocator backing this `RawVec`.
  216. pub fn allocator(&self) -> &A {
  217. &self.alloc
  218. }
  219. fn current_memory(&self) -> Option<(NonNull<u8>, Layout)> {
  220. if mem::size_of::<T>() == 0 || self.cap == 0 {
  221. None
  222. } else {
  223. // We have an allocated chunk of memory, so we can bypass runtime
  224. // checks to get our current layout.
  225. unsafe {
  226. let layout = Layout::array::<T>(self.cap).unwrap_unchecked();
  227. Some((self.ptr.cast().into(), layout))
  228. }
  229. }
  230. }
  231. /// Ensures that the buffer contains at least enough space to hold `len +
  232. /// additional` elements. If it doesn't already have enough capacity, will
  233. /// reallocate enough space plus comfortable slack space to get amortized
  234. /// *O*(1) behavior. Will limit this behavior if it would needlessly cause
  235. /// itself to panic.
  236. ///
  237. /// If `len` exceeds `self.capacity()`, this may fail to actually allocate
  238. /// the requested space. This is not really unsafe, but the unsafe
  239. /// code *you* write that relies on the behavior of this function may break.
  240. ///
  241. /// This is ideal for implementing a bulk-push operation like `extend`.
  242. ///
  243. /// # Panics
  244. ///
  245. /// Panics if the new capacity exceeds `isize::MAX` bytes.
  246. ///
  247. /// # Aborts
  248. ///
  249. /// Aborts on OOM.
  250. #[cfg(not(no_global_oom_handling))]
  251. #[inline]
  252. pub fn reserve(&mut self, len: usize, additional: usize) {
  253. // Callers expect this function to be very cheap when there is already sufficient capacity.
  254. // Therefore, we move all the resizing and error-handling logic from grow_amortized and
  255. // handle_reserve behind a call, while making sure that this function is likely to be
  256. // inlined as just a comparison and a call if the comparison fails.
  257. #[cold]
  258. fn do_reserve_and_handle<T, A: Allocator>(
  259. slf: &mut RawVec<T, A>,
  260. len: usize,
  261. additional: usize,
  262. ) {
  263. handle_reserve(slf.grow_amortized(len, additional));
  264. }
  265. if self.needs_to_grow(len, additional) {
  266. do_reserve_and_handle(self, len, additional);
  267. }
  268. }
  269. /// A specialized version of `reserve()` used only by the hot and
  270. /// oft-instantiated `Vec::push()`, which does its own capacity check.
  271. #[cfg(not(no_global_oom_handling))]
  272. #[inline(never)]
  273. pub fn reserve_for_push(&mut self, len: usize) {
  274. handle_reserve(self.grow_amortized(len, 1));
  275. }
  276. /// The same as `reserve`, but returns on errors instead of panicking or aborting.
  277. pub fn try_reserve(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> {
  278. if self.needs_to_grow(len, additional) {
  279. self.grow_amortized(len, additional)
  280. } else {
  281. Ok(())
  282. }
  283. }
  284. /// The same as `reserve_for_push`, but returns on errors instead of panicking or aborting.
  285. #[inline(never)]
  286. pub fn try_reserve_for_push(&mut self, len: usize) -> Result<(), TryReserveError> {
  287. self.grow_amortized(len, 1)
  288. }
  289. /// Ensures that the buffer contains at least enough space to hold `len +
  290. /// additional` elements. If it doesn't already, will reallocate the
  291. /// minimum possible amount of memory necessary. Generally this will be
  292. /// exactly the amount of memory necessary, but in principle the allocator
  293. /// is free to give back more than we asked for.
  294. ///
  295. /// If `len` exceeds `self.capacity()`, this may fail to actually allocate
  296. /// the requested space. This is not really unsafe, but the unsafe code
  297. /// *you* write that relies on the behavior of this function may break.
  298. ///
  299. /// # Panics
  300. ///
  301. /// Panics if the new capacity exceeds `isize::MAX` bytes.
  302. ///
  303. /// # Aborts
  304. ///
  305. /// Aborts on OOM.
  306. #[cfg(not(no_global_oom_handling))]
  307. pub fn reserve_exact(&mut self, len: usize, additional: usize) {
  308. handle_reserve(self.try_reserve_exact(len, additional));
  309. }
  310. /// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
  311. pub fn try_reserve_exact(
  312. &mut self,
  313. len: usize,
  314. additional: usize,
  315. ) -> Result<(), TryReserveError> {
  316. if self.needs_to_grow(len, additional) { self.grow_exact(len, additional) } else { Ok(()) }
  317. }
  318. /// Shrinks the buffer down to the specified capacity. If the given amount
  319. /// is 0, actually completely deallocates.
  320. ///
  321. /// # Panics
  322. ///
  323. /// Panics if the given amount is *larger* than the current capacity.
  324. ///
  325. /// # Aborts
  326. ///
  327. /// Aborts on OOM.
  328. #[cfg(not(no_global_oom_handling))]
  329. pub fn shrink_to_fit(&mut self, cap: usize) {
  330. handle_reserve(self.shrink(cap));
  331. }
  332. }
  333. impl<T, A: Allocator> RawVec<T, A> {
  334. /// Returns if the buffer needs to grow to fulfill the needed extra capacity.
  335. /// Mainly used to make inlining reserve-calls possible without inlining `grow`.
  336. fn needs_to_grow(&self, len: usize, additional: usize) -> bool {
  337. additional > self.capacity().wrapping_sub(len)
  338. }
  339. fn set_ptr_and_cap(&mut self, ptr: NonNull<[u8]>, cap: usize) {
  340. // Allocators currently return a `NonNull<[u8]>` whose length matches
  341. // the size requested. If that ever changes, the capacity here should
  342. // change to `ptr.len() / mem::size_of::<T>()`.
  343. self.ptr = unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) };
  344. self.cap = cap;
  345. }
  346. // This method is usually instantiated many times. So we want it to be as
  347. // small as possible, to improve compile times. But we also want as much of
  348. // its contents to be statically computable as possible, to make the
  349. // generated code run faster. Therefore, this method is carefully written
  350. // so that all of the code that depends on `T` is within it, while as much
  351. // of the code that doesn't depend on `T` as possible is in functions that
  352. // are non-generic over `T`.
  353. fn grow_amortized(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> {
  354. // This is ensured by the calling contexts.
  355. debug_assert!(additional > 0);
  356. if mem::size_of::<T>() == 0 {
  357. // Since we return a capacity of `usize::MAX` when `elem_size` is
  358. // 0, getting to here necessarily means the `RawVec` is overfull.
  359. return Err(CapacityOverflow.into());
  360. }
  361. // Nothing we can really do about these checks, sadly.
  362. let required_cap = len.checked_add(additional).ok_or(CapacityOverflow)?;
  363. // This guarantees exponential growth. The doubling cannot overflow
  364. // because `cap <= isize::MAX` and the type of `cap` is `usize`.
  365. let cap = cmp::max(self.cap * 2, required_cap);
  366. let cap = cmp::max(Self::MIN_NON_ZERO_CAP, cap);
  367. let new_layout = Layout::array::<T>(cap);
  368. // `finish_grow` is non-generic over `T`.
  369. let ptr = finish_grow(new_layout, self.current_memory(), &mut self.alloc)?;
  370. self.set_ptr_and_cap(ptr, cap);
  371. Ok(())
  372. }
  373. // The constraints on this method are much the same as those on
  374. // `grow_amortized`, but this method is usually instantiated less often so
  375. // it's less critical.
  376. fn grow_exact(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> {
  377. if mem::size_of::<T>() == 0 {
  378. // Since we return a capacity of `usize::MAX` when the type size is
  379. // 0, getting to here necessarily means the `RawVec` is overfull.
  380. return Err(CapacityOverflow.into());
  381. }
  382. let cap = len.checked_add(additional).ok_or(CapacityOverflow)?;
  383. let new_layout = Layout::array::<T>(cap);
  384. // `finish_grow` is non-generic over `T`.
  385. let ptr = finish_grow(new_layout, self.current_memory(), &mut self.alloc)?;
  386. self.set_ptr_and_cap(ptr, cap);
  387. Ok(())
  388. }
  389. #[allow(dead_code)]
  390. fn shrink(&mut self, cap: usize) -> Result<(), TryReserveError> {
  391. assert!(cap <= self.capacity(), "Tried to shrink to a larger capacity");
  392. let (ptr, layout) = if let Some(mem) = self.current_memory() { mem } else { return Ok(()) };
  393. let ptr = unsafe {
  394. // `Layout::array` cannot overflow here because it would have
  395. // overflowed earlier when capacity was larger.
  396. let new_layout = Layout::array::<T>(cap).unwrap_unchecked();
  397. self.alloc
  398. .shrink(ptr, layout, new_layout)
  399. .map_err(|_| AllocError { layout: new_layout, non_exhaustive: () })?
  400. };
  401. self.set_ptr_and_cap(ptr, cap);
  402. Ok(())
  403. }
  404. }
  405. // This function is outside `RawVec` to minimize compile times. See the comment
  406. // above `RawVec::grow_amortized` for details. (The `A` parameter isn't
  407. // significant, because the number of different `A` types seen in practice is
  408. // much smaller than the number of `T` types.)
  409. #[inline(never)]
  410. fn finish_grow<A>(
  411. new_layout: Result<Layout, LayoutError>,
  412. current_memory: Option<(NonNull<u8>, Layout)>,
  413. alloc: &mut A,
  414. ) -> Result<NonNull<[u8]>, TryReserveError>
  415. where
  416. A: Allocator,
  417. {
  418. // Check for the error here to minimize the size of `RawVec::grow_*`.
  419. let new_layout = new_layout.map_err(|_| CapacityOverflow)?;
  420. alloc_guard(new_layout.size())?;
  421. let memory = if let Some((ptr, old_layout)) = current_memory {
  422. debug_assert_eq!(old_layout.align(), new_layout.align());
  423. unsafe {
  424. // The allocator checks for alignment equality
  425. intrinsics::assume(old_layout.align() == new_layout.align());
  426. alloc.grow(ptr, old_layout, new_layout)
  427. }
  428. } else {
  429. alloc.allocate(new_layout)
  430. };
  431. memory.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () }.into())
  432. }
  433. unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A> {
  434. /// Frees the memory owned by the `RawVec` *without* trying to drop its contents.
  435. fn drop(&mut self) {
  436. if let Some((ptr, layout)) = self.current_memory() {
  437. unsafe { self.alloc.deallocate(ptr, layout) }
  438. }
  439. }
  440. }
  441. // Central function for reserve error handling.
  442. #[cfg(not(no_global_oom_handling))]
  443. #[inline]
  444. fn handle_reserve(result: Result<(), TryReserveError>) {
  445. match result.map_err(|e| e.kind()) {
  446. Err(CapacityOverflow) => capacity_overflow(),
  447. Err(AllocError { layout, .. }) => handle_alloc_error(layout),
  448. Ok(()) => { /* yay */ }
  449. }
  450. }
  451. // We need to guarantee the following:
  452. // * We don't ever allocate `> isize::MAX` byte-size objects.
  453. // * We don't overflow `usize::MAX` and actually allocate too little.
  454. //
  455. // On 64-bit we just need to check for overflow since trying to allocate
  456. // `> isize::MAX` bytes will surely fail. On 32-bit and 16-bit we need to add
  457. // an extra guard for this in case we're running on a platform which can use
  458. // all 4GB in user-space, e.g., PAE or x32.
  459. #[inline]
  460. fn alloc_guard(alloc_size: usize) -> Result<(), TryReserveError> {
  461. if usize::BITS < 64 && alloc_size > isize::MAX as usize {
  462. Err(CapacityOverflow.into())
  463. } else {
  464. Ok(())
  465. }
  466. }
  467. // One central function responsible for reporting capacity overflows. This'll
  468. // ensure that the code generation related to these panics is minimal as there's
  469. // only one location which panics rather than a bunch throughout the module.
  470. #[cfg(not(no_global_oom_handling))]
  471. fn capacity_overflow() -> ! {
  472. panic!("capacity overflow");
  473. }