Skip to main content

ArenaManager

Struct ArenaManager 

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

Manages multiple specialized memory arenas.

Each arena is optimized for a specific purpose:

  • Witness Arena: For private ZK inputs, with secure wiping.
  • Polynomial Arena: For FFT/NTT coefficient vectors.
  • Scratch Arena: For temporary computation buffers.

§Drop Safety

The ArenaManager tracks the number of outstanding arena handles. On drop, it verifies that all handles have been released before deallocating memory. If handles are still in use, the memory is intentionally leaked to prevent use-after-free (with a warning).

Implementations§

Source§

impl ArenaManager

Source

pub fn new() -> Result<Self, AllocFailed>

Create a new ArenaManager with default sizes.

This will allocate a total of ~1.4 GB of virtual memory. Note: On modern OSes, virtual memory is cheap; physical pages are only allocated when touched.

Source

pub fn with_sizes( witness_size: usize, poly_size: usize, scratch_size: usize, ) -> Result<Self, AllocFailed>

Create a new ArenaManager with custom sizes.

Use this for fine-tuned configurations based on your circuit size.

Source

pub fn with_guard_pages( witness_size: usize, poly_size: usize, scratch_size: usize, ) -> Result<Self, AllocFailed>

Create arenas with guard pages for buffer overflow protection.

Source

pub fn lock_witness(&self) -> Result<(), AllocFailed>

Lock witness memory to prevent swapping (important for sensitive data).

Source

pub fn unlock_witness(&self) -> Result<(), AllocFailed>

Unlock previously locked witness memory.

Source

pub fn witness(&self) -> Arc<BumpAlloc>

Get a handle to the witness arena.

Source

pub fn polynomial(&self) -> Arc<BumpAlloc>

Get a handle to the polynomial arena.

Source

pub fn scratch(&self) -> Arc<BumpAlloc>

Get a handle to the scratch arena.

Source

pub unsafe fn reset_all(&self)

Reset all arenas.

The witness arena is securely wiped (zeroed) before reset.

§Safety

This will invalidate all memory previously allocated from these arenas. The caller must ensure:

  • No other thread is concurrently allocating from these arenas
  • No references to arena memory exist
  • No concurrent access to arena-allocated memory occurs during or after reset
Source

pub fn stats(&self) -> ArenaStats

Get statistics about arena usage.

Source

pub fn is_sole_owner(&self) -> bool

Check if all arena handles have been released.

Returns true if this ArenaManager is the sole owner of all arenas.

Source

pub fn ref_counts(&self) -> (usize, usize, usize)

Get the reference counts for each arena (for debugging).

Source

pub fn contains_address(&self, addr: usize) -> bool

Check if an address falls within any of the arena memory ranges.

Used by Issue #1 fix to distinguish arena allocations from fallback allocations. Returns true if the address is within witness, polynomial, or scratch arena.

Trait Implementations§

Source§

impl Drop for ArenaManager

Source§

fn drop(&mut self)

§Safety Note: ref-count check is not atomic with deallocation

The ref-count read and the subsequent deallocation are two separate operations with no lock between them. In theory, another thread could clone an Arc<BumpAlloc> handle between the check and the dealloc, causing a use-after-free.

In practice this cannot occur because ArenaManager::drop is only reachable when NAlloc is dropped (&mut self ⇒ exclusive access). At that point no thread can obtain new WitnessArena / PolynomialArena handles from this NAlloc, so the ref counts are stable.

The check therefore serves as a debug-mode invariant assertion, not as a concurrent-safety mechanism.

Source§

impl Send for ArenaManager

Source§

impl Sync for ArenaManager

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.