use essential_types::Word;
use crate::{error::TemporaryError, OpResult};
#[cfg(test)]
mod tests;
#[derive(Default, Debug, PartialEq)]
pub struct Memory(Vec<Word>);
impl Memory {
pub const SIZE_LIMIT: usize = 1024 * 10;
pub fn new() -> Self {
Self::default()
}
pub fn alloc(&mut self, size: Word) -> OpResult<()> {
let size = usize::try_from(size).map_err(|_| TemporaryError::Overflow)?;
let new_size = self
.0
.len()
.checked_add(size)
.ok_or(TemporaryError::Overflow)?;
if new_size > Self::SIZE_LIMIT {
return Err(TemporaryError::Overflow.into());
}
self.0.resize(new_size, 0);
Ok(())
}
pub fn store(&mut self, address: Word, value: Word) -> OpResult<()> {
let index = usize::try_from(address).map_err(|_| TemporaryError::IndexOutOfBounds)?;
*self
.0
.get_mut(index)
.ok_or(TemporaryError::IndexOutOfBounds)? = value;
Ok(())
}
pub fn load(&mut self, address: Word) -> OpResult<Word> {
let index = usize::try_from(address).map_err(|_| TemporaryError::IndexOutOfBounds)?;
Ok(*self.0.get(index).ok_or(TemporaryError::IndexOutOfBounds)?)
}
pub fn store_range(&mut self, address: Word, values: &[Word]) -> OpResult<()> {
let address = usize::try_from(address).map_err(|_| TemporaryError::IndexOutOfBounds)?;
let end = address
.checked_add(values.len())
.ok_or(TemporaryError::Overflow)?;
if end > self.0.len() {
return Err(TemporaryError::IndexOutOfBounds.into());
}
self.0[address..end].copy_from_slice(values);
Ok(())
}
pub fn load_range(&mut self, address: Word, size: Word) -> OpResult<Vec<Word>> {
let address = usize::try_from(address).map_err(|_| TemporaryError::IndexOutOfBounds)?;
let size = usize::try_from(size).map_err(|_| TemporaryError::Overflow)?;
let end = address.checked_add(size).ok_or(TemporaryError::Overflow)?;
if end > self.0.len() {
return Err(TemporaryError::IndexOutOfBounds.into());
}
Ok(self.0[address..end].to_vec())
}
pub fn free(&mut self, address: Word) -> OpResult<()> {
let index = usize::try_from(address).map_err(|_| TemporaryError::IndexOutOfBounds)?;
if index >= self.0.len() {
return Err(TemporaryError::IndexOutOfBounds.into());
}
self.0.truncate(index);
self.0.shrink_to_fit();
Ok(())
}
pub fn len(&self) -> OpResult<Word> {
Ok(self
.0
.len()
.try_into()
.map_err(|_| TemporaryError::Overflow)?)
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}