pub struct BumpAllocator { /* private fields */ }
Expand description

A suballocator which can allocate dynamically, but can only free all allocations at once.

With bump allocation, the used up space increases linearly as allocations are made and allocations can never be freed individually, which is why this algorithm is also called linear allocation. It is also known as arena allocation.

BumpAllocators are best suited for very short-lived (say a few frames at best) resources that need to be allocated often (say each frame), to really take advantage of the performance gains. For creating long-lived allocations, FreeListAllocator is best suited. The way you would typically use this allocator is to have one for each frame in flight. At the start of a frame, you reset it and allocate your resources with it. You write to the resources, render with them, and drop them at the end of the frame.

See also the Suballocator implementation.

Algorithm

What happens is that every time you make an allocation, you receive one with an offset corresponding to the free start within the region, and then the free start is bumped, so that following allocations wouldn’t alias it. As you can imagine, this is extremely fast, because it doesn’t need to keep a free-list. It only needs to do a few additions and comparisons. But beware, fast is about all this is. It is horribly memory inefficient when used wrong, and is very susceptible to memory leaks.

Once you know that you are done with the allocations, meaning you know they have all been dropped, you can safely reset the allocator using the reset method as long as the allocator is not shared between threads. This is one of the reasons you are generally advised to use one BumpAllocator per thread if you can.

Efficiency

Allocation is O(1), and so is resetting the allocator (freeing all allocations).

Implementations§

source§

impl BumpAllocator

source

pub fn reset(&mut self)

Resets the free-start back to the beginning of the region.

Trait Implementations§

source§

impl Debug for BumpAllocator

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Suballocator for BumpAllocator

source§

fn new(region: Region) -> Self

Creates a new BumpAllocator for the given region.

source§

fn allocate( &self, layout: DeviceLayout, allocation_type: AllocationType, buffer_image_granularity: DeviceAlignment ) -> Result<Suballocation, SuballocatorError>

Creates a new suballocation within the region. Read more
source§

unsafe fn deallocate(&self, _suballocation: Suballocation)

Deallocates the given suballocation. Read more
source§

fn free_size(&self) -> DeviceSize

Returns the total amount of free space that is left in the region.
source§

fn cleanup(&mut self)

Tries to free some space, if applicable. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.