Skip to main content

Arena

Struct Arena 

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

A bump-allocated region of memory.

Values allocated in an Arena are tied to its lifetime and must not outlive it. The public API enforces this with borrow checker lifetimes.

§Type safety note

Arena does not run destructors. Only store Copy or otherwise non-owning values until drop support is added.

§Example

use ocas_core::arena::Arena;

let arena = Arena::new();
let value = arena.allocate_with(|| 42);
assert_eq!(*value, 42);

Implementations§

Source§

impl Arena

Source

pub fn new() -> Self

Create a new arena with the default block size.

§Example
use ocas_core::arena::Arena;

let arena = Arena::new();
let n = arena.allocate_with(|| 7);
assert_eq!(*n, 7);
Source

pub fn with_capacity(block_size: usize) -> Self

Create a new arena with a custom initial block size.

Source

pub fn allocate_with<T>(&self, init: impl FnOnce() -> T) -> &mut T

Allocate a value in the arena, constructing it inside init, and return a mutable reference tied to self.

The closure form avoids any ambiguity about when mutation of the arena occurs. The returned &mut T is unique because alloc_raw advances the arena offset for each allocation via interior mutability.

§Panics

Panics if the requested layout has size zero.

§Example
use ocas_core::arena::Arena;

let arena = Arena::new();
let value = arena.allocate_with(|| "hello");
assert_eq!(*value, "hello");
Source

pub fn allocate_slice<T: Copy>(&self, values: &[T]) -> &[T]

Allocate a contiguous slice of T values in the arena.

The returned slice is tied to the arena lifetime. Because the arena does not run destructors, T must be Copy so that dropping the arena does not leak resources owned by the slice elements.

§Panics

Panics if T has zero size or if the total allocation size overflows.

§Example
use ocas_core::arena::Arena;

let arena = Arena::new();
let slice = arena.allocate_slice(&[1, 2, 3, 4, 5]);
assert_eq!(slice, &[1, 2, 3, 4, 5]);
Source

pub fn reset(&self)

Reset the arena, invalidating all previously allocated values.

The first chunk is kept and reused; additional chunks are released. This makes repeated build–reset cycles allocation-free in the steady state, which is the basis of the workspace pool in ocas-atom.

§Safety contract (enforced by convention)

Any reference returned by allocate_with or allocate_slice before the reset must not be used afterwards — the memory may be handed out again for different values. Callers must treat reset as the end of a generation.

§Example
use ocas_core::arena::Arena;

let arena = Arena::new();
let _ = arena.allocate_with(|| 1);
arena.reset();
let value = arena.allocate_with(|| 2);
assert_eq!(*value, 2);
Source

pub fn chunk_count(&self) -> usize

Return the number of chunks currently held by the arena.

Trait Implementations§

Source§

impl Default for Arena

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for Arena

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§

§

impl !Freeze for Arena

§

impl !RefUnwindSafe for Arena

§

impl !Send for Arena

§

impl !Sync for Arena

§

impl Unpin for Arena

§

impl UnsafeUnpin 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.