pub struct SSlice { /* private fields */ }Expand description
An allocated block of stable memory.
Represented by a pointer to the first byte of the memory block and a u64 size of this block in bytes. It implements Copy, but using it after deallocation is undefined behavior.
In stable memory each memory block has the following layout:
- bytes
0..8-size+allocated bit flag(the flag uses the first bit of little endian encoded size) - bytes
8..(size + 8)- the data - bytes
(size + 8)..(size + 16)- anothersize+allocated bit flagSo, a memory block is simplysizebytes of data wrapped with some metadata from both sides. FreeBlock is stored exactly in a same way.
Implementations§
Source§impl SSlice
impl SSlice
Sourcepub unsafe fn from_ptr(ptr: StablePtr) -> Option<Self>
pub unsafe fn from_ptr(ptr: StablePtr) -> Option<Self>
Recreate an SSlice from a pointer to the front of the memory block.
See also SSlice::from_rear_ptr.
This call will check whether a pointer is valid (points to an allocated memory block) and if it’s not, it will return None.
§Safety
By calling this function, you’re basically create a copy of an SSlice, be careful
Sourcepub unsafe fn from_rear_ptr(ptr: StablePtr) -> Option<Self>
pub unsafe fn from_rear_ptr(ptr: StablePtr) -> Option<Self>
Recreate an SSlice from a pointer to the back of the memory block.
See also SSlice::from_ptr.
§Safety
By calling this function, you’re basically create a copy of an SSlice, be careful
Sourcepub fn as_ptr(&self) -> StablePtr
pub fn as_ptr(&self) -> StablePtr
Returns a pointer to the memory block.
Don’t use this function to point to the data inside this memory block! Use SSlice::offset instead.
Sourcepub fn get_size_bytes(&self) -> u64
pub fn get_size_bytes(&self) -> u64
Returns the size of the data in this memory block in bytes.
Sourcepub fn get_total_size_bytes(&self) -> u64
pub fn get_total_size_bytes(&self) -> u64
Returns the size of the whole memory block in bytes (including metadata).
Sourcepub fn _offset(self_ptr: u64, offset: u64) -> StablePtr
pub fn _offset(self_ptr: u64, offset: u64) -> StablePtr
Static analog of SSlice::offset.
Does not perform boundary check.
Sourcepub fn offset(&self, offset: u64) -> StablePtr
pub fn offset(&self, offset: u64) -> StablePtr
Returns a pointer to the data inside SSlice.
One should use this function to write data in a memory block by using [mem::write_fixed] or [mem::write_bytes].
§Panics
Panics if boundary check fails (if the offset is outside the memory block).
§Example
let slice = unsafe { allocate(100).expect("Out of memory") };
let ptr = slice.offset(20);
// will write `10` as little endian bytes into the memory block
// starting from 20th byte
unsafe { mem::write_fixed(ptr, &mut 10u64); }