Skip to main content

BasicMemory

Struct BasicMemory 

Source
pub struct BasicMemory<const BASE_ADDR: u64, const SIZE: usize> { /* private fields */ }
Expand description

Basic memory implementation.

Flat structure, no rwx protections, no alignment requirements. It uses stack, so for larger allocation it’ll need to be boxed (zero-initialized is fine) or a custom implementation to be used.

BASE_ADDR + SIZE must fit into u64, meaning the memory region can’t wrap around or end at the very end of the address space, which is checked at compile time. This is what allows an address below BASE_ADDR to be recognized by the bounds check that follows the subtraction of the base address rather than by a check of its own.

This implementation is intentionally basic and correct, but not the most performant. It is possible to have a more efficient implementation that skips certain checks by placing additional constraints on the program.

This works for simpler cases, while a more sophisticated implementation might prevent certain memory from being writable, supporting actual virtual memory with dynamically allocated memory pages, etc.

Implementations§

Source§

impl<const BASE_ADDR: u64, const SIZE: usize> BasicMemory<BASE_ADDR, SIZE>

Source

pub fn new_boxed() -> Box<Self>

Source

pub const fn get_mut_bytes( &mut self, address: u64, size: usize, ) -> Result<&mut [u8], VirtualMemoryError>

Get a mutable slice of memory.

This is primarily useful for setting up the program and should not be used beyond that.

Trait Implementations§

Source§

impl<const BASE_ADDR: u64, const SIZE: usize> Clone for BasicMemory<BASE_ADDR, SIZE>

Source§

fn clone(&self) -> BasicMemory<BASE_ADDR, SIZE>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const BASE_ADDR: u64, const SIZE: usize> Copy for BasicMemory<BASE_ADDR, SIZE>

Source§

impl<const BASE_ADDR: u64, const SIZE: usize> Debug for BasicMemory<BASE_ADDR, SIZE>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const BASE_ADDR: u64, const SIZE: usize> Default for BasicMemory<BASE_ADDR, SIZE>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const BASE_ADDR: u64, const SIZE: usize> VirtualMemory for BasicMemory<BASE_ADDR, SIZE>

Source§

fn read<T>(&self, address: u64) -> Result<T, VirtualMemoryError>
where T: BasicInt,

Read a value from memory at the specified address
Source§

unsafe fn read_unchecked<T>(&self, address: u64) -> T
where T: BasicInt,

Unchecked read a value from memory at the specified address. Read more
Source§

fn read_slice( &self, address: u64, len: u32, ) -> Result<&[u8], VirtualMemoryError>

Read a contiguous byte slice from memory
Source§

fn read_slice_up_to(&self, address: u64, len: u32) -> &[u8]

Read as many contiguous bytes as possible starting at address, up to len bytes total. Read more
Source§

fn write<T>(&mut self, address: u64, value: T) -> Result<(), VirtualMemoryError>
where T: BasicInt,

Write a value to memory at the specified address
Source§

fn write_slice( &mut self, address: u64, data: &[u8], ) -> Result<(), VirtualMemoryError>

Write a contiguous byte slice to memory

Auto Trait Implementations§

§

impl<const BASE_ADDR: u64, const SIZE: usize> Freeze for BasicMemory<BASE_ADDR, SIZE>
where [u8; SIZE]: Freeze,

§

impl<const BASE_ADDR: u64, const SIZE: usize> RefUnwindSafe for BasicMemory<BASE_ADDR, SIZE>
where [u8; SIZE]: RefUnwindSafe,

§

impl<const BASE_ADDR: u64, const SIZE: usize> Send for BasicMemory<BASE_ADDR, SIZE>
where [u8; SIZE]: Send,

§

impl<const BASE_ADDR: u64, const SIZE: usize> Sync for BasicMemory<BASE_ADDR, SIZE>
where [u8; SIZE]: Sync,

§

impl<const BASE_ADDR: u64, const SIZE: usize> Unpin for BasicMemory<BASE_ADDR, SIZE>
where [u8; SIZE]: Unpin,

§

impl<const BASE_ADDR: u64, const SIZE: usize> UnsafeUnpin for BasicMemory<BASE_ADDR, SIZE>
where [u8; SIZE]: UnsafeUnpin,

§

impl<const BASE_ADDR: u64, const SIZE: usize> UnwindSafe for BasicMemory<BASE_ADDR, SIZE>
where [u8; SIZE]: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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.