Skip to main content

ArenaState

Struct ArenaState 

Source
#[doc(hidden)]
pub struct ArenaState<M> { pub map: MaybeUninit<M>, pub backing: SEXP, pub capacity: usize, pub len: usize, pub free_list: Vec<usize>, pub next_slot: usize, }
Expand description

Core arena state without interior mutability.

This is used internally by both Arena (with RefCell) and thread-local arenas (with UnsafeCell).

Fields§

§map: MaybeUninit<M>

Map from SEXP pointer to entry

§backing: SEXP

Backing VECSXP (preserved via R_PreserveObject)

§capacity: usize

Current capacity

§len: usize

Number of active entries

§free_list: Vec<usize>

Free list for slot reuse

§next_slot: usize

Monotonic write cursor: next index to hand out on the fresh-slot path. Distinct from len (live count) — after release cycles len can be lower than the highest index ever handed out, so using len as the cursor can return an index that is already on the free-list.

Implementations§

Source§

impl<M: MapStorage> ArenaState<M>

Source

pub const INITIAL_CAPACITY: usize = 16

Initial capacity for the backing VECSXP.

This is suitable for light usage (a handful of protected values). For ppsize-scale workloads (hundreds or thousands of protected values), use Arena::with_capacity or init_with_capacity to avoid repeated backing VECSXP growth and map rehashing.

Source

const MAX_CAPACITY: usize

Maximum capacity: the backing VECSXP is indexed by R_xlen_t (isize), so the capacity must fit in a non-negative R_xlen_t.

Source

fn capacity_as_r_xlen(cap: usize) -> R_xlen_t

Convert a usize capacity to R_xlen_t, panicking on overflow.

Source

pub const fn uninit() -> Self

Create uninitialized state (for thread_local).

Source

pub unsafe fn init(&mut self, capacity: usize)

Initialize the state.

§Safety

Must be called exactly once before using the state.

Source

unsafe fn new(capacity: usize) -> Self

Create initialized state.

Source

unsafe fn init_backing(&mut self, capacity: usize)

Initialize just the backing (map already initialized).

Source

fn map(&self) -> &M

Get a reference to the map.

Source

fn map_mut(&mut self) -> &mut M

Get a mutable reference to the map.

Source

pub unsafe fn protect(&mut self, x: SEXP) -> SEXP

Protect a SEXP from garbage collection.

§Safety

Must be called from the R main thread. The SEXP must be valid.

Source

pub unsafe fn unprotect(&mut self, x: SEXP)

Unprotect a SEXP, allowing garbage collection when refcount reaches zero.

§Safety

Must be called from the R main thread. The SEXP must have been previously protected by this arena.

Source

pub unsafe fn try_unprotect(&mut self, x: SEXP) -> bool

Try to unprotect a SEXP. Returns false if not protected by this arena.

§Safety

Must be called from the R main thread.

Source

pub fn is_protected(&self, x: SEXP) -> bool

Returns true if this arena currently protects x.

Source

pub fn ref_count(&self, x: SEXP) -> usize

Returns the current reference count for x in this arena.

Returns 0 if x is not protected or is SEXP::nil().

Source

fn allocate_slot(&mut self) -> usize

Source

unsafe fn grow(&mut self)

Source

pub unsafe fn clear(&mut self)

Clear all protected values from the arena.

§Safety

Must be called from the R main thread.

Source

unsafe fn release_backing(&mut self)

Layout§

Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.