Skip to main content

Arena

Struct Arena 

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

One leased buffer from a ArenaPool. Send + Sync.

Allocations are bump-pointer on an atomic cursor: each call to Arena::alloc CAS-advances the cursor and returns a fresh aligned slice carved out of the buffer at the old position. There is no per-allocation header and no individual free — the entire arena is reset (returned to the pool) only when the Arena is dropped.

Concurrent calls to Arena::alloc on the same &Arena are supported and produce disjoint slices (the CAS loser retries against the new cursor). See the module docs for the full concurrency contract.

Implementations§

Source§

impl Arena

Source

pub fn capacity(&self) -> usize

Capacity of this arena in bytes.

Source

pub fn used(&self) -> usize

Bytes consumed by allocations so far.

Source

pub fn alloc_count(&self) -> u32

Number of allocations performed so far.

Source

pub fn alloc_count_exceeded(&self) -> bool

true once the per-arena allocation-count cap has been reached. Decoders that produce many small allocations should poll this and bail with Error::ResourceExhausted when it flips, instead of waiting for the next Arena::alloc call to fail.

Source

pub fn alloc<T>(&self, count: usize) -> Result<&mut [T]>
where T: Copy,

Allocate count Ts out of this arena. Returns a borrowed &mut [T] (lifetime bounded by the borrow of self). The bytes are not zero-initialised — the caller is responsible for fully writing the returned slice before reading it.

Returns Error::ResourceExhausted if either the per-arena byte cap or the per-arena allocation-count cap would be exceeded.

§Safety / contract

T must be a “plain old data” type with no Drop glue and no invariants that need a constructor — typically u8, i16, u32, f32, etc. The arena does not run destructors on allocated values. This is enforced via a T: Copy bound.

Concurrency: the bump cursor is advanced via a CAS loop, so concurrent alloc calls on the same &Arena produce disjoint slices. The CAS loser retries against the new cursor; in the uncontended case the cost is a single relaxed load plus one successful CAS.

Source

pub fn reset(&mut self)

Reset the arena to empty without releasing its buffer to the pool. Useful for a decoder that wants to reuse the same arena across several intermediate stages of the same frame. Callers must ensure no slice previously returned from Arena::alloc is still in use — Rust’s borrow checker enforces this, since reset takes &mut self.

Trait Implementations§

Source§

impl Drop for Arena

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Arena

Source§

impl Sync for Arena

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.