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.try_alloc_uninit(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(&self, layout: Layout) -> &mut [MaybeUninit<u8>]
pub fn alloc_uninit(&self, layout: Layout) -> &mut [MaybeUninit<u8>]
Allocates a mutable slice of MaybeUninit<u8> that satisfies
layout, panicking if the allocation fails.
See BumpArena::try_alloc_uninit for fallible allocation semantics.
§Panics
Panics if the arena does not have enough free space after accounting for the requested size and alignment.
Sourcepub fn try_alloc_uninit(&self, layout: Layout) -> Option<&mut [MaybeUninit<u8>]>
pub fn try_alloc_uninit(&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 core::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 BumpArena::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 fn alloc_uninit_slice(
&self,
layout: Layout,
) -> Option<&mut [MaybeUninit<u8>]>
👎Deprecated since 1.2.0: Use BumpArena::try_alloc_uninit instead.
pub fn alloc_uninit_slice( &self, layout: Layout, ) -> Option<&mut [MaybeUninit<u8>]>
Use BumpArena::try_alloc_uninit instead.
Allocates a mutable slice of MaybeUninit<u8> that satisfies
layout.
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.
Trait Implementations§
Source§impl Allocator for BumpArena
Available on crate feature nightly only.
impl Allocator for BumpArena
nightly only.Source§fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
allocator_api)Source§unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout)
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout)
allocator_api)ptr. Read moreSource§unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
allocator_api)Source§unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow_zeroed( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
allocator_api)grow, but also ensures that the new contents are set to zero before being
returned. Read moreSource§unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn shrink( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
allocator_api)Source§fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
allocator_api)allocate, but also ensures that the returned memory is zero-initialized. Read more