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
impl Arena
Sourcepub fn alloc_count(&self) -> u32
pub fn alloc_count(&self) -> u32
Number of allocations performed so far.
Sourcepub fn alloc_count_exceeded(&self) -> bool
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.
Sourcepub fn alloc<T>(&self, count: usize) -> Result<&mut [T]>where
T: Copy,
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.
Sourcepub fn reset(&mut self)
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.