pub struct Memory {
pub buffer: Rc<RefCell<Vec<u8>>>,
pub len: usize,
/* private fields */
}Expand description
A cheaply clonable callframe-shared memory buffer.
When a new callframe is created a RC clone of this memory is made, with the current base offset at the length of the buffer at that time.
Fields§
§buffer: Rc<RefCell<Vec<u8>>>§len: usizeImplementations§
Source§impl Memory
impl Memory
pub fn new() -> Self
Sourcepub fn reset_for_reuse(&mut self)
pub fn reset_for_reuse(&mut self)
Resets this memory so its buffer can be reused from a pool by the next transaction: drops all contents (length → 0, capacity retained) and rebases to 0.
Truncating the buffer to length 0 is REQUIRED for correctness, not just hygiene:
Memory::resize only zero-fills bytes grown past buffer.len(), so handing a
non-empty buffer to the next tx would expose stale data from the previous one (a
consensus bug). Capacity is kept so the grown allocation is reused.
Sourcepub fn next_memory(&self) -> Memory
pub fn next_memory(&self) -> Memory
Gets the Memory for the next children callframe.
Sourcepub fn clean_from_base(&self)
pub fn clean_from_base(&self)
Cleans the memory from base onwards, this must be used in callframes when handling returns.
On the callframe that is about to be dropped.
pub fn is_empty(&self) -> bool
Sourcepub fn live_bytes(&self) -> Vec<u8> ⓘ
pub fn live_bytes(&self) -> Vec<u8> ⓘ
Returns a copy of the live byte slice for this frame (from current_base to
current_base + len). Used by the struct-log tracer for memory capture.
Sourcepub fn resize(&mut self, new_memory_size: usize) -> Result<(), VMError>
pub fn resize(&mut self, new_memory_size: usize) -> Result<(), VMError>
Resizes the from the current base to fit the memory specified at new_memory_size.
Note: new_memory_size is increased to the next 32 byte multiple.
Sourcepub fn load_range(
&mut self,
offset: usize,
size: usize,
) -> Result<Bytes, VMError>
pub fn load_range( &mut self, offset: usize, size: usize, ) -> Result<Bytes, VMError>
Load size bytes from the given offset. Returning a Bytes.
Sourcepub fn with_range<R>(
&mut self,
offset: usize,
size: usize,
f: impl FnOnce(&[u8]) -> R,
) -> Result<R, VMError>
pub fn with_range<R>( &mut self, offset: usize, size: usize, f: impl FnOnce(&[u8]) -> R, ) -> Result<R, VMError>
Borrow size bytes from the given offset and pass them to f, without
allocating a Bytes copy of the range.
load_range reads through self.buffer.borrow(), whose Ref guard cannot
outlive this call — so a -> &[u8] accessor can’t be written. Callers that
only need to read the range (e.g. hashing) take the borrow via this closure
instead. Semantics match load_range exactly, including zero-padding reads
past the current length (handled by resize).
Sourcepub fn load_range_const<const N: usize>(
&mut self,
offset: usize,
) -> Result<[u8; N], VMError>
pub fn load_range_const<const N: usize>( &mut self, offset: usize, ) -> Result<[u8; N], VMError>
Load N bytes from the given offset.
Sourcepub fn load_word(&mut self, offset: usize) -> Result<U256, VMError>
pub fn load_word(&mut self, offset: usize) -> Result<U256, VMError>
Load a word from at the given offset.
Sourcepub fn store_data(&mut self, offset: usize, data: &[u8]) -> Result<(), VMError>
pub fn store_data(&mut self, offset: usize, data: &[u8]) -> Result<(), VMError>
Stores the given data at the given offset.
Sourcepub fn store_data_zero_padded(
&mut self,
offset: usize,
data: &[u8],
total_size: usize,
) -> Result<(), VMError>
pub fn store_data_zero_padded( &mut self, offset: usize, data: &[u8], total_size: usize, ) -> Result<(), VMError>
Stores data and zero-pads up to total_size at the given offset.
Sourcepub fn store_word(&mut self, offset: usize, word: U256) -> Result<(), VMError>
pub fn store_word(&mut self, offset: usize, word: U256) -> Result<(), VMError>
Stores a word at the given offset, resizing memory if needed.
Sourcepub fn copy_within(
&mut self,
from_offset: usize,
to_offset: usize,
size: usize,
) -> Result<(), VMError>
pub fn copy_within( &mut self, from_offset: usize, to_offset: usize, size: usize, ) -> Result<(), VMError>
Copies memory within 2 offsets. Like a memmove.
Resizes if needed, because one can copy from “expanded memory”, which is initialized with zeroes.
pub fn store_zeros(&mut self, offset: usize, size: usize) -> Result<(), VMError>
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Memory
impl !Send for Memory
impl !Sync for Memory
impl !UnwindSafe for Memory
impl Freeze for Memory
impl Unpin for Memory
impl UnsafeUnpin for Memory
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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 moreSource§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.