pub struct BumpArena { /* private fields */ }Expand description
A fixed‑capacity, single‑threaded bump allocator.
The arena hands out mutable slices of MaybeUninit<u8> that
are logically uninitialised. The caller must initialise the
memory before reading from it. The backing store is a boxed
slice whose capacity is set once at construction and never
changes, so addresses remain stable. For zero capacity, the
boxed slice may be a dangling, non‑allocated value.
§Thread safety
BumpArena is !Send and !Sync – it contains a raw
pointer marker, which is !Send and !Sync.
§Examples
use core::alloc::Layout;
use linalloc::BumpArena;
let bump = BumpArena::new(1024);
// Allocate space for a `u64`.
let layout = Layout::new::<u64>();
let slice = bump.alloc_uninit_slice(layout).unwrap();
let ptr = slice.as_mut_ptr().cast::<u64>();
unsafe { ptr.write(42) };
let val = unsafe { &*ptr };
assert_eq!(*val, 42);
// Memory is freed when `bump` goes out of scope.Implementations§
Source§impl BumpArena
impl BumpArena
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Creates a bump allocator with exactly capacity bytes of memory.
The memory is allocated from the global allocator and is uninitialised. No zeroing or default‑initialisation is performed.
§Panics
If allocation fails, the global allocator error handler is invoked (typically aborting the process).
Sourcepub fn alloc_uninit_slice(
&self,
layout: Layout,
) -> Option<&mut [MaybeUninit<u8>]>
pub fn alloc_uninit_slice( &self, layout: Layout, ) -> Option<&mut [MaybeUninit<u8>]>
Allocates a mutable slice of MaybeUninit<u8> that satisfies
layout.
The returned memory is logically uninitialised – it must be
initialised (e.g. with ptr::write) before any reads are
performed.
The slice borrows the arena immutably (&self), so the arena
cannot be dropped or moved while the slice is alive. The
backing store is never resized, so non‑zero allocations remain
valid until the arena is dropped or reset is called. A
zero‑size allocation returns a well‑aligned dangling slice and
does not advance the bump pointer.
§Returns
None if the arena does not have enough free space after
accounting for the requested size and alignment.
Sourcepub unsafe fn reset(&self)
pub unsafe fn reset(&self)
Resets the bump pointer to the beginning, making the entire capacity available for new allocations.
§Safety
All previously returned slices must no longer be in use.
This method does not run any destructors – the caller is
responsible for dropping all values placed in the arena before
calling reset.