pub unsafe trait BumpAllocatorCore: Allocator + Sealed {
// Required methods
fn any_stats(&self) -> AnyStats<'_>;
fn checkpoint(&self) -> Checkpoint;
unsafe fn reset_to(&self, checkpoint: Checkpoint);
fn is_claimed(&self) -> bool;
fn prepare_allocation(
&self,
layout: Layout,
) -> Result<Range<NonNull<u8>>, AllocError>;
unsafe fn allocate_prepared(
&self,
layout: Layout,
range: Range<NonNull<u8>>,
) -> NonNull<u8>;
fn prepare_allocation_rev(
&self,
layout: Layout,
) -> Result<Range<NonNull<u8>>, AllocError>;
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 BumpAllocatorCore has laxer safety conditions when using Allocator methods:
- You can call
grow*,shrinkanddeallocatewith pointers that came from a differentBumpAllocatorCore. 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.
shrinknever errors unless the new alignment is greaterdeallocatemay always be called when the pointer address is less than 16 and the size is 0
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. - The non-erroring behavior of
shrinkis necessary forBumpAllocatorTyped::shrink_slice deallocatewith a dangling pointer is used in the drop implementation ofBumpString
§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 - the bump allocator must be unclaimed at the time the checkpoint is created and when this function is called
§Examples
let bump: Bump = Bump::new();
let checkpoint = bump.checkpoint();
{
let hello = bump.alloc_str("hello");
assert_eq!(bump.stats().allocated(), 5);
}
unsafe { bump.reset_to(checkpoint); }
assert_eq!(bump.stats().allocated(), 0);Sourcefn is_claimed(&self) -> bool
fn is_claimed(&self) -> bool
Returns true if the bump allocator is currently claimed.
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().
The start 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- the bump allocator must be unclaimed at the time
prepare_allocationwas called and when calling this function
Sourcefn prepare_allocation_rev(
&self,
layout: Layout,
) -> Result<Range<NonNull<u8>>, AllocError>
fn prepare_allocation_rev( &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().
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_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_rev call starting at the end.
§Safety
rangemust have been returned from a call toprepare_allocation_rev- 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_rev, both in size and alignment- the bump allocator must be unclaimed at the time
prepare_allocation_revwas called and when calling this function
Trait Implementations§
Source§impl BumpAllocatorTyped for dyn BumpAllocatorCore + '_
impl BumpAllocatorTyped for dyn BumpAllocatorCore + '_
Source§type TypedStats<'b> = AnyStats<'b>
where
Self: 'b
type TypedStats<'b> = AnyStats<'b> where Self: 'b
Source§fn typed_stats(&self) -> AnyStats<'_>
fn typed_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§fn allocate_slice_for<T>(&self, slice: &[T]) -> NonNull<T>
fn allocate_slice_for<T>(&self, slice: &[T]) -> NonNull<T>
Source§fn try_allocate_slice_for<T>(
&self,
slice: &[T],
) -> Result<NonNull<T>, AllocError>
fn try_allocate_slice_for<T>( &self, slice: &[T], ) -> 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§fn prepare_slice_allocation_rev<T>(&self, len: usize) -> (NonNull<T>, usize)
fn prepare_slice_allocation_rev<T>(&self, len: usize) -> (NonNull<T>, usize)
prepare_allocation_rev. Read moreSource§fn try_prepare_slice_allocation_rev<T>(
&self,
len: usize,
) -> Result<(NonNull<T>, usize), AllocError>
fn try_prepare_slice_allocation_rev<T>( &self, len: usize, ) -> Result<(NonNull<T>, usize), AllocError>
prepare_allocation_rev. 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 moreSource§fn reserve(&self, additional: usize)
fn reserve(&self, additional: usize)
additional more bytes to be bump allocated.
The bump allocator may reserve more space to avoid frequent reallocations.
After calling reserve, self.stats().remaining() will be greater than or equal to
additional. Does nothing if the capacity is already sufficient. Read moreSource§fn try_reserve(&self, additional: usize) -> Result<(), AllocError>
fn try_reserve(&self, additional: usize) -> Result<(), AllocError>
additional more bytes to be bump allocated.
The bump allocator may reserve more space to avoid frequent reallocations.
After calling reserve, self.stats().remaining() will be greater than or equal to
additional. Does nothing if the capacity is already sufficient. Read more