pub struct BumpAllocator { /* private fields */ }Expand description
A fast bump allocator for temporary allocations.
Allocates memory by simply incrementing a pointer. All allocated memory is freed at once when the allocator is reset or dropped.
This allocator is not thread-safe and should only be used from a single thread.
Implementations§
Source§impl BumpAllocator
impl BumpAllocator
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new bump allocator with the given initial capacity.
Sourcepub fn alloc<T>(&self, value: T) -> &mut T
pub fn alloc<T>(&self, value: T) -> &mut T
Allocates memory for a value of type T and initializes it.
Sourcepub fn alloc_slice_copy<T: Copy>(&self, values: &[T]) -> &mut [T]
pub fn alloc_slice_copy<T: Copy>(&self, values: &[T]) -> &mut [T]
Allocates memory for a slice and copies the values.
Sourcepub fn alloc_slice_clone<T: Clone>(&self, values: &[T]) -> &mut [T]
pub fn alloc_slice_clone<T: Clone>(&self, values: &[T]) -> &mut [T]
Allocates memory for a slice and clones the values.
Sourcepub fn alloc_str(&self, s: &str) -> &mut str
pub fn alloc_str(&self, s: &str) -> &mut str
Allocates memory for a string and returns a mutable reference.
Sourcepub fn alloc_layout(&self, layout: Layout) -> NonNull<u8>
pub fn alloc_layout(&self, layout: Layout) -> NonNull<u8>
Allocates raw bytes with the given layout.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the allocator, freeing all allocated memory.
This is very fast - it just resets the internal pointer.
Sourcepub fn allocated_bytes(&self) -> usize
pub fn allocated_bytes(&self) -> usize
Returns the number of bytes currently allocated.
Sourcepub fn allocation_count(&self) -> usize
pub fn allocation_count(&self) -> usize
Returns the number of allocations made.
Sourcepub fn chunk_count(&mut self) -> usize
pub fn chunk_count(&mut self) -> usize
Returns the number of chunks in use.