pub struct Memory<F> {
inner: Vec<MemoryValue<F>>,
}Expand description
The VM’s memory.
Memory is internally represented as a vector of values. We grow the memory when values past the end are set, extending with 0s.
§Capacity Limits
The inner Vec is subject to Rust’s allocator limit of isize::MAX bytes.
This means:
- On 64-bit: Practical limit is available RAM (~200 GB for full u32 range)
- On 32-bit: Hard limit of ~44 million addressable slots
Exceeding these limits will cause a panic with “capacity overflow”.
Fields§
§inner: Vec<MemoryValue<F>>Implementations§
Source§impl<F: AcirField> Memory<F>
impl<F: AcirField> Memory<F>
Sourceconst MAX_MEMORY_SIZE: usize = 2_147_483_647usize
const MAX_MEMORY_SIZE: usize = 2_147_483_647usize
Maximum number of memory slots that can be allocated.
This limit is set to i32::MAX to ensure deterministic behavior across all architectures.
On 32-bit systems, Rust’s allocator limits allocations to isize::MAX bytes, which would
restrict us to fewer elements anyway. By using i32::MAX, we ensure the same behavior
on both 32-bit and 64-bit systems.
See: https://github.com/rust-lang/rust/pull/95295 and https://doc.rust-lang.org/1.81.0/src/core/alloc/layout.rs.html
Sourcefn get_stack_pointer(&self) -> u32
fn get_stack_pointer(&self) -> u32
Read the value from slot 0.
Panics if it’s not a U32.
Sourcefn resolve(&self, address: MemoryAddress) -> u32
fn resolve(&self, address: MemoryAddress) -> u32
Resolve an address to either:
- itself, if it’s a direct address, or
- the current stack pointer plus the offset, if it’s relative.
Returns a memory slot index.
Sourcepub fn read(&self, address: MemoryAddress) -> MemoryValue<F>
pub fn read(&self, address: MemoryAddress) -> MemoryValue<F>
Reads the numeric value at the address.
If the address is beyond the size of memory, a default value is returned.
Sourcepub fn read_ref(&self, ptr: MemoryAddress) -> MemoryAddress
pub fn read_ref(&self, ptr: MemoryAddress) -> MemoryAddress
Reads the value at the address and returns it as a direct memory address, without dereferencing the pointer itself to a numeric value.
Sourcepub fn write_ref(&mut self, ptr: MemoryAddress, address: MemoryAddress)
pub fn write_ref(&mut self, ptr: MemoryAddress, address: MemoryAddress)
Sets ptr to point at address.
Sourcepub fn read_slice(
&self,
address: MemoryAddress,
len: usize,
) -> &[MemoryValue<F>]
pub fn read_slice( &self, address: MemoryAddress, len: usize, ) -> &[MemoryValue<F>]
Read a contiguous vector of memory starting at address, up to len slots.
Panics if the end index is beyond the size of the memory.
Sourcepub fn write(&mut self, address: MemoryAddress, value: MemoryValue<F>)
pub fn write(&mut self, address: MemoryAddress, value: MemoryValue<F>)
Sets the value at address to value
Sourcefn resize_to_fit(&mut self, size: usize)
fn resize_to_fit(&mut self, size: usize)
Increase the size of memory fit size elements, or the current length, whichever is bigger.
§Panics
Panics if size exceeds Self::MAX_MEMORY_SIZE.
Sourcepub fn write_slice(&mut self, address: MemoryAddress, values: &[MemoryValue<F>])
pub fn write_slice(&mut self, address: MemoryAddress, values: &[MemoryValue<F>])
Sets the values after address to values
Sourcepub fn values(&self) -> &[MemoryValue<F>]
pub fn values(&self) -> &[MemoryValue<F>]
Returns the values of the memory
Trait Implementations§
impl<F: Eq> Eq for Memory<F>
impl<F> StructuralPartialEq for Memory<F>
Auto Trait Implementations§
impl<F> Freeze for Memory<F>
impl<F> RefUnwindSafe for Memory<F>where
F: RefUnwindSafe,
impl<F> Send for Memory<F>where
F: Send,
impl<F> Sync for Memory<F>where
F: Sync,
impl<F> Unpin for Memory<F>where
F: Unpin,
impl<F> UnwindSafe for Memory<F>where
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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