pub unsafe trait BumpAllocator: Allocator + Sealed {
// Required methods
fn any_stats(&self) -> AnyStats<'_>;
fn checkpoint(&self) -> Checkpoint;
unsafe fn reset_to(&self, checkpoint: Checkpoint);
fn prepare_allocation(
&self,
layout: Layout,
) -> Result<Range<NonNull<u8>>, AllocError>;
unsafe fn allocate_prepared(
&self,
layout: Layout,
range: Range<NonNull<u8>>,
) -> NonNull<u8>;
unsafe fn allocate_prepared_rev(
&self,
layout: Layout,
range: Range<NonNull<u8>>,
) -> NonNull<u8>;
}Expand description
A bump allocator.
This trait provides additional methods and guarantees on top of an Allocator.
A BumpAllocator has laxer safety conditions when using Allocator methods:
- You can call
grow*,shrinkanddeallocatewith pointers that did not come from this allocator. In this case:grow*will always allocate a new memory block.deallocatewill do nothingshrinkwill either do nothing or allocate iff the alignment increases
- Memory blocks can be split.
deallocatecan be called with any pointer or alignment when the size is0.shrinknever errors unless the new alignment is greater
Those invariants are used here:
- Handling of foreign pointers is necessary for implementing
BumpVec::from_parts,BumpBox::into_boxandBump(Scope)::dealloc. - Memory block splitting is necessary for
split_offandsplit_at. - Deallocate with a size of
0is used in the drop implementation ofBumpVec. - The non-erroring behavior of
shrinkis necessary forBumpAllocatorExt::shrink_slice
§Safety
An implementor must support the conditions described above.
Required Methods§
Sourcefn any_stats(&self) -> AnyStats<'_>
fn any_stats(&self) -> AnyStats<'_>
Returns a type which provides statistics about the memory usage of the bump allocator.
Sourcefn checkpoint(&self) -> Checkpoint
fn checkpoint(&self) -> Checkpoint
Creates a checkpoint of the current bump position.
The bump position can be reset to this checkpoint with reset_to.
Sourceunsafe fn reset_to(&self, checkpoint: Checkpoint)
unsafe fn reset_to(&self, checkpoint: Checkpoint)
Resets the bump position to a previously created checkpoint. The memory that has been allocated since then will be reused by future allocations.
§Safety
- the checkpoint must have been created by this bump allocator
- the bump allocator must not have been
resetsince creation of this checkpoint - there must be no references to allocations made since creation of this checkpoint
- the checkpoint must not have been created by an
!GUARANTEED_ALLOCATEDwhen self isGUARANTEED_ALLOCATED
§Examples
fn test(bump: impl BumpAllocator) {
let checkpoint = bump.checkpoint();
{
let hello = bump.allocate(Layout::new::<[u8;5]>()).unwrap();
assert_eq!(bump.any_stats().allocated(), 5);
}
unsafe { bump.reset_to(checkpoint); }
assert_eq!(bump.any_stats().allocated(), 0);
}
test(<Bump>::new());
test(Bump::<Global, 1, true, false>::unallocated());Sourcefn prepare_allocation(
&self,
layout: Layout,
) -> Result<Range<NonNull<u8>>, AllocError>
fn prepare_allocation( &self, layout: Layout, ) -> Result<Range<NonNull<u8>>, AllocError>
Returns a pointer range of free space in the bump allocator with a size of at least layout.size().
Both the start and the end of the range is aligned to layout.align().
The pointer range takes up as much of the free space of the chunk as possible while satisfying the other conditions.
§Errors
Errors if the allocation fails.
Sourceunsafe fn allocate_prepared(
&self,
layout: Layout,
range: Range<NonNull<u8>>,
) -> NonNull<u8>
unsafe fn allocate_prepared( &self, layout: Layout, range: Range<NonNull<u8>>, ) -> NonNull<u8>
Allocate part of the free space returned from a prepare_allocation call.
§Safety
rangemust have been returned from a call toprepare_allocation- no allocation, grow, shrink or deallocate must have taken place since then
- no resets must have taken place since then
layoutmust be less than or equal to thelayoutused when callingprepare_allocation, both in size and alignment
Sourceunsafe fn allocate_prepared_rev(
&self,
layout: Layout,
range: Range<NonNull<u8>>,
) -> NonNull<u8>
unsafe fn allocate_prepared_rev( &self, layout: Layout, range: Range<NonNull<u8>>, ) -> NonNull<u8>
Allocate part of the free space returned from a prepare_allocation call starting at the end.
§Safety
rangemust have been returned from a call toprepare_allocation- no allocation, grow, shrink or deallocate must have taken place since then
- no resets must have taken place since then
layoutmust be less than or equal to thelayoutused when callingprepare_allocation, both in size and alignment
Trait Implementations§
Source§impl BumpAllocatorExt for dyn BumpAllocator + '_
impl BumpAllocatorExt for dyn BumpAllocator + '_
Source§fn stats(&self) -> AnyStats<'_>
fn stats(&self) -> AnyStats<'_>
Source§fn try_allocate_layout(&self, layout: Layout) -> Result<NonNull<u8>, AllocError>
fn try_allocate_layout(&self, layout: Layout) -> Result<NonNull<u8>, AllocError>
Source§fn try_allocate_sized<T>(&self) -> Result<NonNull<T>, AllocError>
fn try_allocate_sized<T>(&self) -> Result<NonNull<T>, AllocError>
Source§fn allocate_slice<T>(&self, len: usize) -> NonNull<T>
fn allocate_slice<T>(&self, len: usize) -> NonNull<T>
Source§fn try_allocate_slice<T>(&self, len: usize) -> Result<NonNull<T>, AllocError>
fn try_allocate_slice<T>(&self, len: usize) -> Result<NonNull<T>, AllocError>
Source§unsafe fn shrink_slice<T>(
&self,
ptr: NonNull<T>,
old_len: usize,
new_len: usize,
) -> Option<NonNull<T>>
unsafe fn shrink_slice<T>( &self, ptr: NonNull<T>, old_len: usize, new_len: usize, ) -> Option<NonNull<T>>
Source§fn prepare_slice_allocation<T>(&self, len: usize) -> NonNull<[T]>
fn prepare_slice_allocation<T>(&self, len: usize) -> NonNull<[T]>
prepare_allocation. Read moreSource§fn try_prepare_slice_allocation<T>(
&self,
len: usize,
) -> Result<NonNull<[T]>, AllocError>
fn try_prepare_slice_allocation<T>( &self, len: usize, ) -> Result<NonNull<[T]>, AllocError>
prepare_allocation. Read moreSource§unsafe fn allocate_prepared_slice<T>(
&self,
ptr: NonNull<T>,
len: usize,
cap: usize,
) -> NonNull<[T]>
unsafe fn allocate_prepared_slice<T>( &self, ptr: NonNull<T>, len: usize, cap: usize, ) -> NonNull<[T]>
allocate_prepared. Read moreSource§unsafe fn allocate_prepared_slice_rev<T>(
&self,
ptr: NonNull<T>,
len: usize,
cap: usize,
) -> NonNull<[T]>
unsafe fn allocate_prepared_slice_rev<T>( &self, ptr: NonNull<T>, len: usize, cap: usize, ) -> NonNull<[T]>
allocate_prepared_rev. Read more