1 #![unstable(feature = "raw_vec_internals", reason = "implementation detail", issue = "0")]
7 use core
::ptr
::{self, NonNull, Unique}
;
10 use crate::alloc
::{Alloc, Layout, Global, handle_alloc_error}
;
11 use crate::collections
::CollectionAllocErr
::{self, *}
;
12 use crate::boxed
::Box
;
14 /// A low-level utility for more ergonomically allocating, reallocating, and deallocating
15 /// a buffer of memory on the heap without having to worry about all the corner cases
16 /// involved. This type is excellent for building your own data structures like Vec and VecDeque.
19 /// * Produces Unique::empty() on zero-sized types
20 /// * Produces Unique::empty() on zero-length allocations
21 /// * Catches all overflows in capacity computations (promotes them to "capacity overflow" panics)
22 /// * Guards against 32-bit systems allocating more than isize::MAX bytes
23 /// * Guards against overflowing your length
24 /// * Aborts on OOM or calls handle_alloc_error as applicable
25 /// * Avoids freeing Unique::empty()
26 /// * Contains a ptr::Unique and thus endows the user with all related benefits
28 /// This type does not in anyway inspect the memory that it manages. When dropped it *will*
29 /// free its memory, but it *won't* try to Drop its contents. It is up to the user of RawVec
30 /// to handle the actual things *stored* inside of a RawVec.
32 /// Note that a RawVec always forces its capacity to be usize::MAX for zero-sized types.
33 /// This enables you to use capacity growing logic catch the overflows in your length
34 /// that might occur with zero-sized types.
36 /// However this means that you need to be careful when round-tripping this type
37 /// with a `Box<[T]>`: `cap()` won't yield the len. However `with_capacity`,
38 /// `shrink_to_fit`, and `from_box` will actually set RawVec's private capacity
39 /// field. This allows zero-sized types to not be special-cased by consumers of
41 #[allow(missing_debug_implementations)]
42 pub struct RawVec
<T
, A
: Alloc
= Global
> {
48 impl<T
, A
: Alloc
> RawVec
<T
, A
> {
49 /// Like `new` but parameterized over the choice of allocator for
50 /// the returned RawVec.
51 pub const fn new_in(a
: A
) -> Self {
52 // !0 is usize::MAX. This branch should be stripped at compile time.
53 // FIXME(mark-i-m): use this line when `if`s are allowed in `const`
54 //let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
56 // Unique::empty() doubles as "unallocated" and "zero-sized allocation"
59 // FIXME(mark-i-m): use `cap` when ifs are allowed in const
60 cap
: [0, !0][(mem
::size_of
::<T
>() == 0) as usize],
65 /// Like `with_capacity` but parameterized over the choice of
66 /// allocator for the returned RawVec.
68 pub fn with_capacity_in(cap
: usize, a
: A
) -> Self {
69 RawVec
::allocate_in(cap
, false, a
)
72 /// Like `with_capacity_zeroed` but parameterized over the choice
73 /// of allocator for the returned RawVec.
75 pub fn with_capacity_zeroed_in(cap
: usize, a
: A
) -> Self {
76 RawVec
::allocate_in(cap
, true, a
)
79 fn allocate_in(cap
: usize, zeroed
: bool
, mut a
: A
) -> Self {
81 let elem_size
= mem
::size_of
::<T
>();
83 let alloc_size
= cap
.checked_mul(elem_size
).unwrap_or_else(|| capacity_overflow());
84 alloc_guard(alloc_size
).unwrap_or_else(|_
| capacity_overflow());
86 // handles ZSTs and `cap = 0` alike
87 let ptr
= if alloc_size
== 0 {
88 NonNull
::<T
>::dangling()
90 let align
= mem
::align_of
::<T
>();
91 let layout
= Layout
::from_size_align(alloc_size
, align
).unwrap();
92 let result
= if zeroed
{
93 a
.alloc_zeroed(layout
)
98 Ok(ptr
) => ptr
.cast(),
99 Err(_
) => handle_alloc_error(layout
),
112 impl<T
> RawVec
<T
, Global
> {
113 /// Creates the biggest possible RawVec (on the system heap)
114 /// without allocating. If T has positive size, then this makes a
115 /// RawVec with capacity 0. If T has 0 size, then it makes a
116 /// RawVec with capacity `usize::MAX`. Useful for implementing
117 /// delayed allocation.
118 pub const fn new() -> Self {
122 /// Creates a RawVec (on the system heap) with exactly the
123 /// capacity and alignment requirements for a `[T; cap]`. This is
124 /// equivalent to calling RawVec::new when `cap` is 0 or T is
125 /// zero-sized. Note that if `T` is zero-sized this means you will
126 /// *not* get a RawVec with the requested capacity!
130 /// * Panics if the requested capacity exceeds `usize::MAX` bytes.
131 /// * Panics on 32-bit platforms if the requested capacity exceeds
132 /// `isize::MAX` bytes.
138 pub fn with_capacity(cap
: usize) -> Self {
139 RawVec
::allocate_in(cap
, false, Global
)
142 /// Like `with_capacity` but guarantees the buffer is zeroed.
144 pub fn with_capacity_zeroed(cap
: usize) -> Self {
145 RawVec
::allocate_in(cap
, true, Global
)
149 impl<T
, A
: Alloc
> RawVec
<T
, A
> {
150 /// Reconstitutes a RawVec from a pointer, capacity, and allocator.
152 /// # Undefined Behavior
154 /// The ptr must be allocated (via the given allocator `a`), and with the given capacity. The
155 /// capacity cannot exceed `isize::MAX` (only a concern on 32-bit systems).
156 /// If the ptr and capacity come from a RawVec created via `a`, then this is guaranteed.
157 pub unsafe fn from_raw_parts_in(ptr
: *mut T
, cap
: usize, a
: A
) -> Self {
159 ptr
: Unique
::new_unchecked(ptr
),
166 impl<T
> RawVec
<T
, Global
> {
167 /// Reconstitutes a RawVec from a pointer, capacity.
169 /// # Undefined Behavior
171 /// The ptr must be allocated (on the system heap), and with the given capacity. The
172 /// capacity cannot exceed `isize::MAX` (only a concern on 32-bit systems).
173 /// If the ptr and capacity come from a RawVec, then this is guaranteed.
174 pub unsafe fn from_raw_parts(ptr
: *mut T
, cap
: usize) -> Self {
176 ptr
: Unique
::new_unchecked(ptr
),
182 /// Converts a `Box<[T]>` into a `RawVec<T>`.
183 pub fn from_box(mut slice
: Box
<[T
]>) -> Self {
185 let result
= RawVec
::from_raw_parts(slice
.as_mut_ptr(), slice
.len());
192 impl<T
, A
: Alloc
> RawVec
<T
, A
> {
193 /// Gets a raw pointer to the start of the allocation. Note that this is
194 /// Unique::empty() if `cap = 0` or T is zero-sized. In the former case, you must
196 pub fn ptr(&self) -> *mut T
{
200 /// Gets the capacity of the allocation.
202 /// This will always be `usize::MAX` if `T` is zero-sized.
204 pub fn cap(&self) -> usize {
205 if mem
::size_of
::<T
>() == 0 {
212 /// Returns a shared reference to the allocator backing this RawVec.
213 pub fn alloc(&self) -> &A
{
217 /// Returns a mutable reference to the allocator backing this RawVec.
218 pub fn alloc_mut(&mut self) -> &mut A
{
222 fn current_layout(&self) -> Option
<Layout
> {
226 // We have an allocated chunk of memory, so we can bypass runtime
227 // checks to get our current layout.
229 let align
= mem
::align_of
::<T
>();
230 let size
= mem
::size_of
::<T
>() * self.cap
;
231 Some(Layout
::from_size_align_unchecked(size
, align
))
236 /// Doubles the size of the type's backing allocation. This is common enough
237 /// to want to do that it's easiest to just have a dedicated method. Slightly
238 /// more efficient logic can be provided for this than the general case.
240 /// This function is ideal for when pushing elements one-at-a-time because
241 /// you don't need to incur the costs of the more general computations
242 /// reserve needs to do to guard against overflow. You do however need to
243 /// manually check if your `len == cap`.
247 /// * Panics if T is zero-sized on the assumption that you managed to exhaust
248 /// all `usize::MAX` slots in your imaginary buffer.
249 /// * Panics on 32-bit platforms if the requested capacity exceeds
250 /// `isize::MAX` bytes.
259 /// # #![feature(alloc, raw_vec_internals)]
260 /// # extern crate alloc;
262 /// # use alloc::raw_vec::RawVec;
263 /// struct MyVec<T> {
268 /// impl<T> MyVec<T> {
269 /// pub fn push(&mut self, elem: T) {
270 /// if self.len == self.buf.cap() { self.buf.double(); }
271 /// // double would have aborted or panicked if the len exceeded
272 /// // `isize::MAX` so this is safe to do unchecked now.
274 /// ptr::write(self.buf.ptr().add(self.len), elem);
280 /// # let mut vec = MyVec { buf: RawVec::new(), len: 0 };
286 pub fn double(&mut self) {
288 let elem_size
= mem
::size_of
::<T
>();
290 // since we set the capacity to usize::MAX when elem_size is
291 // 0, getting to here necessarily means the RawVec is overfull.
292 assert
!(elem_size
!= 0, "capacity overflow");
294 let (new_cap
, uniq
) = match self.current_layout() {
296 // Since we guarantee that we never allocate more than
297 // isize::MAX bytes, `elem_size * self.cap <= isize::MAX` as
298 // a precondition, so this can't overflow. Additionally the
299 // alignment will never be too large as to "not be
300 // satisfiable", so `Layout::from_size_align` will always
303 // tl;dr; we bypass runtime checks due to dynamic assertions
304 // in this module, allowing us to use
305 // `from_size_align_unchecked`.
306 let new_cap
= 2 * self.cap
;
307 let new_size
= new_cap
* elem_size
;
308 alloc_guard(new_size
).unwrap_or_else(|_
| capacity_overflow());
309 let ptr_res
= self.a
.realloc(NonNull
::from(self.ptr
).cast(),
313 Ok(ptr
) => (new_cap
, ptr
.cast().into()),
314 Err(_
) => handle_alloc_error(
315 Layout
::from_size_align_unchecked(new_size
, cur
.align())
320 // skip to 4 because tiny Vec's are dumb; but not if that
321 // would cause overflow
322 let new_cap
= if elem_size
> (!0) / 8 { 1 }
else { 4 }
;
323 match self.a
.alloc_array
::<T
>(new_cap
) {
324 Ok(ptr
) => (new_cap
, ptr
.into()),
325 Err(_
) => handle_alloc_error(Layout
::array
::<T
>(new_cap
).unwrap()),
334 /// Attempts to double the size of the type's backing allocation in place. This is common
335 /// enough to want to do that it's easiest to just have a dedicated method. Slightly
336 /// more efficient logic can be provided for this than the general case.
338 /// Returns `true` if the reallocation attempt has succeeded.
342 /// * Panics if T is zero-sized on the assumption that you managed to exhaust
343 /// all `usize::MAX` slots in your imaginary buffer.
344 /// * Panics on 32-bit platforms if the requested capacity exceeds
345 /// `isize::MAX` bytes.
348 pub fn double_in_place(&mut self) -> bool
{
350 let elem_size
= mem
::size_of
::<T
>();
351 let old_layout
= match self.current_layout() {
352 Some(layout
) => layout
,
353 None
=> return false, // nothing to double
356 // since we set the capacity to usize::MAX when elem_size is
357 // 0, getting to here necessarily means the RawVec is overfull.
358 assert
!(elem_size
!= 0, "capacity overflow");
360 // Since we guarantee that we never allocate more than isize::MAX
361 // bytes, `elem_size * self.cap <= isize::MAX` as a precondition, so
362 // this can't overflow.
364 // Similarly like with `double` above we can go straight to
365 // `Layout::from_size_align_unchecked` as we know this won't
366 // overflow and the alignment is sufficiently small.
367 let new_cap
= 2 * self.cap
;
368 let new_size
= new_cap
* elem_size
;
369 alloc_guard(new_size
).unwrap_or_else(|_
| capacity_overflow());
370 match self.a
.grow_in_place(NonNull
::from(self.ptr
).cast(), old_layout
, new_size
) {
372 // We can't directly divide `size`.
383 /// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
384 pub fn try_reserve_exact(&mut self, used_cap
: usize, needed_extra_cap
: usize)
385 -> Result
<(), CollectionAllocErr
> {
387 self.reserve_internal(used_cap
, needed_extra_cap
, Fallible
, Exact
)
390 /// Ensures that the buffer contains at least enough space to hold
391 /// `used_cap + needed_extra_cap` elements. If it doesn't already,
392 /// will reallocate the minimum possible amount of memory necessary.
393 /// Generally this will be exactly the amount of memory necessary,
394 /// but in principle the allocator is free to give back more than
397 /// If `used_cap` exceeds `self.cap()`, this may fail to actually allocate
398 /// the requested space. This is not really unsafe, but the unsafe
399 /// code *you* write that relies on the behavior of this function may break.
403 /// * Panics if the requested capacity exceeds `usize::MAX` bytes.
404 /// * Panics on 32-bit platforms if the requested capacity exceeds
405 /// `isize::MAX` bytes.
410 pub fn reserve_exact(&mut self, used_cap
: usize, needed_extra_cap
: usize) {
411 match self.reserve_internal(used_cap
, needed_extra_cap
, Infallible
, Exact
) {
412 Err(CapacityOverflow
) => capacity_overflow(),
413 Err(AllocErr
) => unreachable
!(),
414 Ok(()) => { /* yay */ }
418 /// Calculates the buffer's new size given that it'll hold `used_cap +
419 /// needed_extra_cap` elements. This logic is used in amortized reserve methods.
420 /// Returns `(new_capacity, new_alloc_size)`.
421 fn amortized_new_size(&self, used_cap
: usize, needed_extra_cap
: usize)
422 -> Result
<usize, CollectionAllocErr
> {
424 // Nothing we can really do about these checks :(
425 let required_cap
= used_cap
.checked_add(needed_extra_cap
).ok_or(CapacityOverflow
)?
;
426 // Cannot overflow, because `cap <= isize::MAX`, and type of `cap` is `usize`.
427 let double_cap
= self.cap
* 2;
428 // `double_cap` guarantees exponential growth.
429 Ok(cmp
::max(double_cap
, required_cap
))
432 /// The same as `reserve`, but returns on errors instead of panicking or aborting.
433 pub fn try_reserve(&mut self, used_cap
: usize, needed_extra_cap
: usize)
434 -> Result
<(), CollectionAllocErr
> {
435 self.reserve_internal(used_cap
, needed_extra_cap
, Fallible
, Amortized
)
438 /// Ensures that the buffer contains at least enough space to hold
439 /// `used_cap + needed_extra_cap` elements. If it doesn't already have
440 /// enough capacity, will reallocate enough space plus comfortable slack
441 /// space to get amortized `O(1)` behavior. Will limit this behavior
442 /// if it would needlessly cause itself to panic.
444 /// If `used_cap` exceeds `self.cap()`, this may fail to actually allocate
445 /// the requested space. This is not really unsafe, but the unsafe
446 /// code *you* write that relies on the behavior of this function may break.
448 /// This is ideal for implementing a bulk-push operation like `extend`.
452 /// * Panics if the requested capacity exceeds `usize::MAX` bytes.
453 /// * Panics on 32-bit platforms if the requested capacity exceeds
454 /// `isize::MAX` bytes.
463 /// # #![feature(alloc, raw_vec_internals)]
464 /// # extern crate alloc;
466 /// # use alloc::raw_vec::RawVec;
467 /// struct MyVec<T> {
472 /// impl<T: Clone> MyVec<T> {
473 /// pub fn push_all(&mut self, elems: &[T]) {
474 /// self.buf.reserve(self.len, elems.len());
475 /// // reserve would have aborted or panicked if the len exceeded
476 /// // `isize::MAX` so this is safe to do unchecked now.
479 /// ptr::write(self.buf.ptr().add(self.len), x.clone());
486 /// # let mut vector = MyVec { buf: RawVec::new(), len: 0 };
487 /// # vector.push_all(&[1, 3, 5, 7, 9]);
490 pub fn reserve(&mut self, used_cap
: usize, needed_extra_cap
: usize) {
491 match self.reserve_internal(used_cap
, needed_extra_cap
, Infallible
, Amortized
) {
492 Err(CapacityOverflow
) => capacity_overflow(),
493 Err(AllocErr
) => unreachable
!(),
494 Ok(()) => { /* yay */ }
497 /// Attempts to ensure that the buffer contains at least enough space to hold
498 /// `used_cap + needed_extra_cap` elements. If it doesn't already have
499 /// enough capacity, will reallocate in place enough space plus comfortable slack
500 /// space to get amortized `O(1)` behavior. Will limit this behaviour
501 /// if it would needlessly cause itself to panic.
503 /// If `used_cap` exceeds `self.cap()`, this may fail to actually allocate
504 /// the requested space. This is not really unsafe, but the unsafe
505 /// code *you* write that relies on the behavior of this function may break.
507 /// Returns `true` if the reallocation attempt has succeeded.
511 /// * Panics if the requested capacity exceeds `usize::MAX` bytes.
512 /// * Panics on 32-bit platforms if the requested capacity exceeds
513 /// `isize::MAX` bytes.
514 pub fn reserve_in_place(&mut self, used_cap
: usize, needed_extra_cap
: usize) -> bool
{
516 // NOTE: we don't early branch on ZSTs here because we want this
517 // to actually catch "asking for more than usize::MAX" in that case.
518 // If we make it past the first branch then we are guaranteed to
521 // Don't actually need any more capacity. If the current `cap` is 0, we can't
522 // reallocate in place.
523 // Wrapping in case they give a bad `used_cap`
524 let old_layout
= match self.current_layout() {
525 Some(layout
) => layout
,
526 None
=> return false,
528 if self.cap().wrapping_sub(used_cap
) >= needed_extra_cap
{
532 let new_cap
= self.amortized_new_size(used_cap
, needed_extra_cap
)
533 .unwrap_or_else(|_
| capacity_overflow());
535 // Here, `cap < used_cap + needed_extra_cap <= new_cap`
536 // (regardless of whether `self.cap - used_cap` wrapped).
537 // Therefore we can safely call grow_in_place.
539 let new_layout
= Layout
::new
::<T
>().repeat(new_cap
).unwrap().0;
540 // FIXME: may crash and burn on over-reserve
541 alloc_guard(new_layout
.size()).unwrap_or_else(|_
| capacity_overflow());
542 match self.a
.grow_in_place(
543 NonNull
::from(self.ptr
).cast(), old_layout
, new_layout
.size(),
556 /// Shrinks the allocation down to the specified amount. If the given amount
557 /// is 0, actually completely deallocates.
561 /// Panics if the given amount is *larger* than the current capacity.
566 pub fn shrink_to_fit(&mut self, amount
: usize) {
567 let elem_size
= mem
::size_of
::<T
>();
569 // Set the `cap` because they might be about to promote to a `Box<[T]>`
575 // This check is my waterloo; it's the only thing Vec wouldn't have to do.
576 assert
!(self.cap
>= amount
, "Tried to shrink to a larger capacity");
579 // We want to create a new zero-length vector within the
580 // same allocator. We use ptr::write to avoid an
581 // erroneous attempt to drop the contents, and we use
582 // ptr::read to sidestep condition against destructuring
583 // types that implement Drop.
586 let a
= ptr
::read(&self.a
as *const A
);
587 self.dealloc_buffer();
588 ptr
::write(self, RawVec
::new_in(a
));
590 } else if self.cap
!= amount
{
592 // We know here that our `amount` is greater than zero. This
593 // implies, via the assert above, that capacity is also greater
594 // than zero, which means that we've got a current layout that
597 // We also know that `self.cap` is greater than `amount`, and
598 // consequently we don't need runtime checks for creating either
600 let old_size
= elem_size
* self.cap
;
601 let new_size
= elem_size
* amount
;
602 let align
= mem
::align_of
::<T
>();
603 let old_layout
= Layout
::from_size_align_unchecked(old_size
, align
);
604 match self.a
.realloc(NonNull
::from(self.ptr
).cast(),
607 Ok(p
) => self.ptr
= p
.cast().into(),
608 Err(_
) => handle_alloc_error(
609 Layout
::from_size_align_unchecked(new_size
, align
)
625 enum ReserveStrategy
{
630 use ReserveStrategy
::*;
632 impl<T
, A
: Alloc
> RawVec
<T
, A
> {
636 needed_extra_cap
: usize,
637 fallibility
: Fallibility
,
638 strategy
: ReserveStrategy
,
639 ) -> Result
<(), CollectionAllocErr
> {
641 use crate::alloc
::AllocErr
;
643 // NOTE: we don't early branch on ZSTs here because we want this
644 // to actually catch "asking for more than usize::MAX" in that case.
645 // If we make it past the first branch then we are guaranteed to
648 // Don't actually need any more capacity.
649 // Wrapping in case they gave a bad `used_cap`.
650 if self.cap().wrapping_sub(used_cap
) >= needed_extra_cap
{
654 // Nothing we can really do about these checks :(
655 let new_cap
= match strategy
{
656 Exact
=> used_cap
.checked_add(needed_extra_cap
).ok_or(CapacityOverflow
)?
,
657 Amortized
=> self.amortized_new_size(used_cap
, needed_extra_cap
)?
,
659 let new_layout
= Layout
::array
::<T
>(new_cap
).map_err(|_
| CapacityOverflow
)?
;
661 alloc_guard(new_layout
.size())?
;
663 let res
= match self.current_layout() {
665 debug_assert
!(new_layout
.align() == layout
.align());
666 self.a
.realloc(NonNull
::from(self.ptr
).cast(), layout
, new_layout
.size())
668 None
=> self.a
.alloc(new_layout
),
671 match (&res
, fallibility
) {
672 (Err(AllocErr
), Infallible
) => handle_alloc_error(new_layout
),
676 self.ptr
= res?
.cast().into();
685 impl<T
> RawVec
<T
, Global
> {
686 /// Converts the entire buffer into `Box<[T]>`.
688 /// While it is not *strictly* Undefined Behavior to call
689 /// this procedure while some of the RawVec is uninitialized,
690 /// it certainly makes it trivial to trigger it.
692 /// Note that this will correctly reconstitute any `cap` changes
693 /// that may have been performed. (see description of type for details)
694 pub unsafe fn into_box(self) -> Box
<[T
]> {
695 // NOTE: not calling `cap()` here, actually using the real `cap` field!
696 let slice
= slice
::from_raw_parts_mut(self.ptr(), self.cap
);
697 let output
: Box
<[T
]> = Box
::from_raw(slice
);
703 impl<T
, A
: Alloc
> RawVec
<T
, A
> {
704 /// Frees the memory owned by the RawVec *without* trying to Drop its contents.
705 pub unsafe fn dealloc_buffer(&mut self) {
706 let elem_size
= mem
::size_of
::<T
>();
708 if let Some(layout
) = self.current_layout() {
709 self.a
.dealloc(NonNull
::from(self.ptr
).cast(), layout
);
715 unsafe impl<#[may_dangle] T, A: Alloc> Drop for RawVec<T, A> {
716 /// Frees the memory owned by the RawVec *without* trying to Drop its contents.
718 unsafe { self.dealloc_buffer(); }
724 // We need to guarantee the following:
725 // * We don't ever allocate `> isize::MAX` byte-size objects
726 // * We don't overflow `usize::MAX` and actually allocate too little
728 // On 64-bit we just need to check for overflow since trying to allocate
729 // `> isize::MAX` bytes will surely fail. On 32-bit and 16-bit we need to add
730 // an extra guard for this in case we're running on a platform which can use
731 // all 4GB in user-space. e.g., PAE or x32
734 fn alloc_guard(alloc_size
: usize) -> Result
<(), CollectionAllocErr
> {
735 if mem
::size_of
::<usize>() < 8 && alloc_size
> core
::isize::MAX
as usize {
736 Err(CapacityOverflow
)
742 // One central function responsible for reporting capacity overflows. This'll
743 // ensure that the code generation related to these panics is minimal as there's
744 // only one location which panics rather than a bunch throughout the module.
745 fn capacity_overflow() -> ! {
746 panic
!("capacity overflow")
754 fn allocator_param() {
755 use crate::alloc
::AllocErr
;
757 // Writing a test of integration between third-party
758 // allocators and RawVec is a little tricky because the RawVec
759 // API does not expose fallible allocation methods, so we
760 // cannot check what happens when allocator is exhausted
761 // (beyond detecting a panic).
763 // Instead, this just checks that the RawVec methods do at
764 // least go through the Allocator API when it reserves
767 // A dumb allocator that consumes a fixed amount of fuel
768 // before allocation attempts start failing.
769 struct BoundedAlloc { fuel: usize }
770 unsafe impl Alloc
for BoundedAlloc
{
771 unsafe fn alloc(&mut self, layout
: Layout
) -> Result
<NonNull
<u8>, AllocErr
> {
772 let size
= layout
.size();
773 if size
> self.fuel
{
774 return Err(AllocErr
);
776 match Global
.alloc(layout
) {
777 ok @
Ok(_
) => { self.fuel -= size; ok }
781 unsafe fn dealloc(&mut self, ptr
: NonNull
<u8>, layout
: Layout
) {
782 Global
.dealloc(ptr
, layout
)
786 let a
= BoundedAlloc { fuel: 500 }
;
787 let mut v
: RawVec
<u8, _
> = RawVec
::with_capacity_in(50, a
);
788 assert_eq
!(v
.a
.fuel
, 450);
789 v
.reserve(50, 150); // (causes a realloc, thus using 50 + 150 = 200 units of fuel)
790 assert_eq
!(v
.a
.fuel
, 250);
794 fn reserve_does_not_overallocate() {
796 let mut v
: RawVec
<u32> = RawVec
::new();
797 // First `reserve` allocates like `reserve_exact`
799 assert_eq
!(9, v
.cap());
803 let mut v
: RawVec
<u32> = RawVec
::new();
805 assert_eq
!(7, v
.cap());
806 // 97 if more than double of 7, so `reserve` should work
807 // like `reserve_exact`.
809 assert_eq
!(97, v
.cap());
813 let mut v
: RawVec
<u32> = RawVec
::new();
815 assert_eq
!(12, v
.cap());
817 // 3 is less than half of 12, so `reserve` must grow
818 // exponentially. At the time of writing this test grow
819 // factor is 2, so new capacity is 24, however, grow factor
820 // of 1.5 is OK too. Hence `>= 18` in assert.
821 assert
!(v
.cap() >= 12 + 12 / 2);