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