Skip to main content

Arena

Struct Arena 

Source
pub struct Arena { /* private fields */ }
Expand description

A simple bump allocator for temporary allocations.

The arena pre-allocates a contiguous block of memory and hands out slices from it via bump pointer allocation. When the frame is complete, the arena is reset (O(1)) and the memory is reused.

Implementations§

Source§

impl Arena

Source

pub fn new(capacity: usize) -> Self

Create a new arena with the specified capacity.

Source

pub fn with_default_size() -> Self

Create a new arena with the default size (64KB).

Source

pub fn reset(&self)

Reset the arena for reuse.

This is an O(1) operation - it just resets the bump pointer. The memory contents are not cleared.

Source

pub fn alloc_slice(&self, len: usize) -> Option<&mut [u8]>

Allocate a mutable slice of bytes.

Returns None if there’s not enough space in the arena. SAFETY: This returns a mutable reference from a shared reference, which is safe because we use interior mutability (Cell) to track the allocation position, ensuring each region is only handed out once.

Source

pub fn alloc_slice_zeroed(&self, len: usize) -> Option<&mut [u8]>

Allocate a mutable slice of bytes, zeroed.

Returns None if there’s not enough space in the arena.

Source

pub fn alloc_vec(&self, initial_capacity: usize) -> Option<ArenaVec<'_>>

Allocate a Vec-like buffer backed by the arena.

Returns an ArenaVec that can grow up to the remaining capacity.

Source

pub fn usage(&self) -> usize

Get the current usage of the arena.

Source

pub fn capacity(&self) -> usize

Get the total capacity of the arena.

Source

pub fn remaining(&self) -> usize

Get the remaining capacity.

Source

pub fn peak_usage(&self) -> usize

Get the peak usage (highest watermark since creation).

Trait Implementations§

Source§

impl Debug for Arena

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Arena

§

impl !RefUnwindSafe for Arena

§

impl Send for Arena

§

impl !Sync for Arena

§

impl Unpin for Arena

§

impl UnwindSafe for Arena

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.