pub use shared::SharedMemory;
use wasmer_types::{MemoryError, MemoryType, Pages};
use crate::{
AsStoreMut, AsStoreRef, ExportError, Exportable, Extern, StoreMut, StoreRef,
vm::{VMExtern, VMExternMemory, VMMemory},
};
pub(crate) mod buffer;
pub(crate) mod inner;
pub(crate) mod location;
pub(crate) mod shared;
pub(crate) mod view;
pub(crate) use inner::*;
pub use view::*;
#[derive(Debug, Clone, PartialEq, Eq, derive_more::From)]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct Memory(pub(crate) BackendMemory);
impl Memory {
pub fn new(store: &mut impl AsStoreMut, ty: MemoryType) -> Result<Self, MemoryError> {
BackendMemory::new(store, ty).map(Self)
}
pub fn new_from_existing<IntoVMMemory>(
new_store: &mut impl AsStoreMut,
memory: IntoVMMemory,
) -> Self
where
IntoVMMemory: Into<VMMemory>,
{
Self(BackendMemory::new_from_existing(new_store, memory.into()))
}
pub fn ty(&self, store: &impl AsStoreRef) -> MemoryType {
self.0.ty(store)
}
pub fn view<'a>(&self, store: &'a (impl AsStoreRef + ?Sized)) -> MemoryView<'a> {
MemoryView::new(self, store)
}
pub fn size(&self, store: &impl AsStoreRef) -> Pages {
self.0.size(store)
}
pub fn grow<IntoPages>(
&self,
store: &mut impl AsStoreMut,
delta: IntoPages,
) -> Result<Pages, MemoryError>
where
IntoPages: Into<Pages>,
{
self.0.grow(store, delta)
}
pub fn grow_at_least(
&self,
store: &mut impl AsStoreMut,
min_size: u64,
) -> Result<(), MemoryError> {
self.0.grow_at_least(store, min_size)
}
pub fn reset(&self, store: &mut impl AsStoreMut) -> Result<(), MemoryError> {
self.0.reset(store)
}
pub fn copy_to_store(
&self,
store: &impl AsStoreRef,
new_store: &mut impl AsStoreMut,
) -> Result<Self, MemoryError> {
self.0.copy_to_store(store, new_store).map(Self)
}
pub(crate) fn from_vm_extern(store: &mut impl AsStoreMut, vm_extern: VMExternMemory) -> Self {
Self(BackendMemory::from_vm_extern(store, vm_extern))
}
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
self.0.is_from_store(store)
}
pub fn try_clone(&self, store: &impl AsStoreRef) -> Result<VMMemory, MemoryError> {
self.0.try_clone(store)
}
pub fn share_in_store(
&self,
store: &impl AsStoreRef,
new_store: &mut impl AsStoreMut,
) -> Result<Self, MemoryError> {
self.0.share_in_store(store, new_store).map(Self)
}
pub fn as_shared(&self, store: &impl AsStoreRef) -> Option<SharedMemory> {
self.0.as_shared(store)
}
pub(crate) fn to_vm_extern(&self) -> VMExtern {
self.0.to_vm_extern()
}
}
impl<'a> Exportable<'a> for Memory {
fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
match _extern {
Extern::Memory(memory) => Ok(memory),
_ => Err(ExportError::IncompatibleType),
}
}
}