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