Skip to main content

BumpArena

Struct BumpArena 

Source
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

Source

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).

Source

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.

Source

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.

Source

pub fn capacity(&self) -> usize

Returns the total capacity of the backing memory, in bytes.

Source

pub fn used(&self) -> usize

Returns the number of bytes that have been allocated so far.

Trait Implementations§

Source§

impl Drop for BumpArena

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.