Skip to main content

ArenaState

Struct ArenaState 

Source
struct ArenaState {
    map: MaybeUninit<BTreeMap<usize, Entry>>,
    backing: SEXP,
    capacity: usize,
    len: usize,
    free_list: Vec<usize>,
    next_slot: usize,
}
Expand description

Core arena state without interior mutability.

This is used internally by both RefCountedArena (with RefCell) and ThreadLocalArena (with UnsafeCell).

Fields§

§map: MaybeUninit<BTreeMap<usize, Entry>>

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 ArenaState

Source

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 RefCountedArena::with_capacity or ThreadLocalArena::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

const fn uninit() -> Self

Create uninitialized state (for thread_local).

Source

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) -> &BTreeMap<usize, Entry>

Get a reference to the map.

Source

fn map_mut(&mut self) -> &mut BTreeMap<usize, Entry>

Get a mutable reference to the map.

Source

fn decrement_and_maybe_remove(&mut self, key: &usize) -> Option<(bool, usize)>

Decrement the count for a key and remove if zero.

Returns Some((true, index)) if the entry was found and removed, Some((false, index)) if the entry was found but count > 0 after decrement, None if the entry was not found.

Source

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

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

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

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

Returns true if this arena currently protects x.

Source

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

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)

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> SizeHint for T
where T: ?Sized,

Source§

default fn lower_bound(&self) -> usize

🔬This is a nightly-only experimental API. (core_io_internals)
Returns a lower bound on the number of elements this container-like item contains. For example, an array [u8; 12] could return any value between 0 and 12 inclusively as a correct implementation. Read more
Source§

default fn upper_bound(&self) -> Option<usize>

🔬This is a nightly-only experimental API. (core_io_internals)
Returns an upper bound on the number of elements this container-like item contains if it can be determined, otherwise None. Read more
Source§

final fn size_hint(&self) -> (usize, Option<usize>)

🔬This is a nightly-only experimental API. (core_io_internals)
Returns an estimate for the number of elements this container like type contains. Read more
Source§

impl<T> SizedTypeProperties for T

Source§

#[doc(hidden)]
const SIZE: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGNMENT: Alignment = _

🔬This is a nightly-only experimental API. (ptr_alignment_type)
Source§

#[doc(hidden)]
const IS_ZST: bool = _

🔬This is a nightly-only experimental API. (sized_type_properties)
true if this type requires no storage. false if its size is greater than zero. Read more
Source§

#[doc(hidden)]
const LAYOUT: Layout = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const MAX_SLICE_LEN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
The largest safe length for a [Self]. Read more
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.

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 80 bytes