17a1b7cdfSAlex Crichton use crate::{
290ac295eSAlex Crichton WasmtimeStoreContext, WasmtimeStoreContextMut, handle_result, wasm_extern_t, wasm_memorytype_t,
390ac295eSAlex Crichton wasm_store_t, wasmtime_error_t,
47a1b7cdfSAlex Crichton };
5e68aa995SAlex Crichton use std::convert::TryFrom;
6cca558cdSAlex Crichton use wasmtime::{Extern, Memory};
76ef09359SAlex Crichton
86ef09359SAlex Crichton #[derive(Clone)]
96ef09359SAlex Crichton #[repr(transparent)]
106ef09359SAlex Crichton pub struct wasm_memory_t {
116ef09359SAlex Crichton ext: wasm_extern_t,
126ef09359SAlex Crichton }
136ef09359SAlex Crichton
146ef09359SAlex Crichton wasmtime_c_api_macros::declare_ref!(wasm_memory_t);
156ef09359SAlex Crichton
166ef09359SAlex Crichton pub type wasm_memory_pages_t = u32;
176ef09359SAlex Crichton
186ef09359SAlex Crichton impl wasm_memory_t {
try_from(e: &wasm_extern_t) -> Option<&wasm_memory_t>196ef09359SAlex Crichton pub(crate) fn try_from(e: &wasm_extern_t) -> Option<&wasm_memory_t> {
206ef09359SAlex Crichton match &e.which {
21cca558cdSAlex Crichton Extern::Memory(_) => Some(unsafe { &*(e as *const _ as *const _) }),
226ef09359SAlex Crichton _ => None,
236ef09359SAlex Crichton }
246ef09359SAlex Crichton }
256ef09359SAlex Crichton
memory(&self) -> Memory267a1b7cdfSAlex Crichton fn memory(&self) -> Memory {
277a1b7cdfSAlex Crichton match self.ext.which {
28cca558cdSAlex Crichton Extern::Memory(m) => m,
296ef09359SAlex Crichton _ => unsafe { std::hint::unreachable_unchecked() },
306ef09359SAlex Crichton }
316ef09359SAlex Crichton }
326ef09359SAlex Crichton }
336ef09359SAlex Crichton
34ae84e6edSAlex Crichton #[unsafe(no_mangle)]
wasm_memory_new( store: &mut wasm_store_t, mt: &wasm_memorytype_t, ) -> Option<Box<wasm_memory_t>>357a1b7cdfSAlex Crichton pub unsafe extern "C" fn wasm_memory_new(
367a1b7cdfSAlex Crichton store: &mut wasm_store_t,
376ef09359SAlex Crichton mt: &wasm_memorytype_t,
38f12b4c46SPeter Huene ) -> Option<Box<wasm_memory_t>> {
397a1b7cdfSAlex Crichton let memory = Memory::new(store.store.context_mut(), mt.ty().ty.clone()).ok()?;
40f12b4c46SPeter Huene Some(Box::new(wasm_memory_t {
416ef09359SAlex Crichton ext: wasm_extern_t {
427a1b7cdfSAlex Crichton store: store.store.clone(),
43cca558cdSAlex Crichton which: memory.into(),
446ef09359SAlex Crichton },
45f12b4c46SPeter Huene }))
466ef09359SAlex Crichton }
476ef09359SAlex Crichton
48ae84e6edSAlex Crichton #[unsafe(no_mangle)]
wasm_memory_as_extern(m: &mut wasm_memory_t) -> &mut wasm_extern_t497a1b7cdfSAlex Crichton pub extern "C" fn wasm_memory_as_extern(m: &mut wasm_memory_t) -> &mut wasm_extern_t {
507a1b7cdfSAlex Crichton &mut m.ext
517a1b7cdfSAlex Crichton }
527a1b7cdfSAlex Crichton
53ae84e6edSAlex Crichton #[unsafe(no_mangle)]
wasm_memory_as_extern_const(m: &wasm_memory_t) -> &wasm_extern_t547a1b7cdfSAlex Crichton pub extern "C" fn wasm_memory_as_extern_const(m: &wasm_memory_t) -> &wasm_extern_t {
556ef09359SAlex Crichton &m.ext
566ef09359SAlex Crichton }
576ef09359SAlex Crichton
58ae84e6edSAlex Crichton #[unsafe(no_mangle)]
wasm_memory_type(m: &wasm_memory_t) -> Box<wasm_memorytype_t>597a1b7cdfSAlex Crichton pub unsafe extern "C" fn wasm_memory_type(m: &wasm_memory_t) -> Box<wasm_memorytype_t> {
607a1b7cdfSAlex Crichton let ty = m.memory().ty(m.ext.store.context());
616ef09359SAlex Crichton Box::new(wasm_memorytype_t::new(ty))
626ef09359SAlex Crichton }
636ef09359SAlex Crichton
64ae84e6edSAlex Crichton #[unsafe(no_mangle)]
wasm_memory_data(m: &wasm_memory_t) -> *mut u8657a1b7cdfSAlex Crichton pub unsafe extern "C" fn wasm_memory_data(m: &wasm_memory_t) -> *mut u8 {
667a1b7cdfSAlex Crichton m.memory().data_ptr(m.ext.store.context())
676ef09359SAlex Crichton }
686ef09359SAlex Crichton
69ae84e6edSAlex Crichton #[unsafe(no_mangle)]
wasm_memory_data_size(m: &wasm_memory_t) -> usize707a1b7cdfSAlex Crichton pub unsafe extern "C" fn wasm_memory_data_size(m: &wasm_memory_t) -> usize {
717a1b7cdfSAlex Crichton m.memory().data_size(m.ext.store.context())
726ef09359SAlex Crichton }
736ef09359SAlex Crichton
74ae84e6edSAlex Crichton #[unsafe(no_mangle)]
wasm_memory_size(m: &wasm_memory_t) -> wasm_memory_pages_t757a1b7cdfSAlex Crichton pub unsafe extern "C" fn wasm_memory_size(m: &wasm_memory_t) -> wasm_memory_pages_t {
76e68aa995SAlex Crichton u32::try_from(m.memory().size(m.ext.store.context())).unwrap()
776ef09359SAlex Crichton }
786ef09359SAlex Crichton
79ae84e6edSAlex Crichton #[unsafe(no_mangle)]
wasm_memory_grow( m: &mut wasm_memory_t, delta: wasm_memory_pages_t, ) -> bool807a1b7cdfSAlex Crichton pub unsafe extern "C" fn wasm_memory_grow(
817a1b7cdfSAlex Crichton m: &mut wasm_memory_t,
827a1b7cdfSAlex Crichton delta: wasm_memory_pages_t,
837a1b7cdfSAlex Crichton ) -> bool {
847a1b7cdfSAlex Crichton let memory = m.memory();
857a1b7cdfSAlex Crichton let mut store = m.ext.store.context_mut();
86e68aa995SAlex Crichton memory.grow(&mut store, u64::from(delta)).is_ok()
877a1b7cdfSAlex Crichton }
887a1b7cdfSAlex Crichton
89ae84e6edSAlex Crichton #[unsafe(no_mangle)]
wasmtime_memory_new( store: WasmtimeStoreContextMut<'_>, ty: &wasm_memorytype_t, ret: &mut Memory, ) -> Option<Box<wasmtime_error_t>>907a1b7cdfSAlex Crichton pub extern "C" fn wasmtime_memory_new(
91420fc3d1SNick Fitzgerald store: WasmtimeStoreContextMut<'_>,
927a1b7cdfSAlex Crichton ty: &wasm_memorytype_t,
937a1b7cdfSAlex Crichton ret: &mut Memory,
947a1b7cdfSAlex Crichton ) -> Option<Box<wasmtime_error_t>> {
957a1b7cdfSAlex Crichton handle_result(Memory::new(store, ty.ty().ty.clone()), |mem| *ret = mem)
967a1b7cdfSAlex Crichton }
977a1b7cdfSAlex Crichton
98ae84e6edSAlex Crichton #[unsafe(no_mangle)]
wasmtime_memory_type( store: WasmtimeStoreContext<'_>, mem: &Memory, ) -> Box<wasm_memorytype_t>997a1b7cdfSAlex Crichton pub extern "C" fn wasmtime_memory_type(
100420fc3d1SNick Fitzgerald store: WasmtimeStoreContext<'_>,
1017a1b7cdfSAlex Crichton mem: &Memory,
1027a1b7cdfSAlex Crichton ) -> Box<wasm_memorytype_t> {
1037a1b7cdfSAlex Crichton Box::new(wasm_memorytype_t::new(mem.ty(store)))
1047a1b7cdfSAlex Crichton }
1057a1b7cdfSAlex Crichton
106ae84e6edSAlex Crichton #[unsafe(no_mangle)]
wasmtime_memory_data(store: WasmtimeStoreContext<'_>, mem: &Memory) -> *const u8107420fc3d1SNick Fitzgerald pub extern "C" fn wasmtime_memory_data(store: WasmtimeStoreContext<'_>, mem: &Memory) -> *const u8 {
1087a1b7cdfSAlex Crichton mem.data(store).as_ptr()
1097a1b7cdfSAlex Crichton }
1107a1b7cdfSAlex Crichton
111ae84e6edSAlex Crichton #[unsafe(no_mangle)]
wasmtime_memory_data_size( store: WasmtimeStoreContext<'_>, mem: &Memory, ) -> usize112420fc3d1SNick Fitzgerald pub extern "C" fn wasmtime_memory_data_size(
113420fc3d1SNick Fitzgerald store: WasmtimeStoreContext<'_>,
114420fc3d1SNick Fitzgerald mem: &Memory,
115420fc3d1SNick Fitzgerald ) -> usize {
1167a1b7cdfSAlex Crichton mem.data(store).len()
1177a1b7cdfSAlex Crichton }
1187a1b7cdfSAlex Crichton
119ae84e6edSAlex Crichton #[unsafe(no_mangle)]
wasmtime_memory_size(store: WasmtimeStoreContext<'_>, mem: &Memory) -> u64120420fc3d1SNick Fitzgerald pub extern "C" fn wasmtime_memory_size(store: WasmtimeStoreContext<'_>, mem: &Memory) -> u64 {
1217a1b7cdfSAlex Crichton mem.size(store)
1227a1b7cdfSAlex Crichton }
1237a1b7cdfSAlex Crichton
124ae84e6edSAlex Crichton #[unsafe(no_mangle)]
wasmtime_memory_grow( store: WasmtimeStoreContextMut<'_>, mem: &Memory, delta: u64, prev_size: &mut u64, ) -> Option<Box<wasmtime_error_t>>1257a1b7cdfSAlex Crichton pub extern "C" fn wasmtime_memory_grow(
126420fc3d1SNick Fitzgerald store: WasmtimeStoreContextMut<'_>,
1277a1b7cdfSAlex Crichton mem: &Memory,
128e68aa995SAlex Crichton delta: u64,
129e68aa995SAlex Crichton prev_size: &mut u64,
1307a1b7cdfSAlex Crichton ) -> Option<Box<wasmtime_error_t>> {
1317a1b7cdfSAlex Crichton handle_result(mem.grow(store, delta), |prev| *prev_size = prev)
1326ef09359SAlex Crichton }
133*7948e0ffSNick Fitzgerald
134*7948e0ffSNick Fitzgerald #[unsafe(no_mangle)]
wasmtime_memory_page_size(store: WasmtimeStoreContext<'_>, mem: &Memory) -> u64135*7948e0ffSNick Fitzgerald pub extern "C" fn wasmtime_memory_page_size(store: WasmtimeStoreContext<'_>, mem: &Memory) -> u64 {
136*7948e0ffSNick Fitzgerald mem.page_size(store)
137*7948e0ffSNick Fitzgerald }
138*7948e0ffSNick Fitzgerald
139*7948e0ffSNick Fitzgerald #[unsafe(no_mangle)]
wasmtime_memory_page_size_log2( store: WasmtimeStoreContext<'_>, mem: &Memory, ) -> u8140*7948e0ffSNick Fitzgerald pub extern "C" fn wasmtime_memory_page_size_log2(
141*7948e0ffSNick Fitzgerald store: WasmtimeStoreContext<'_>,
142*7948e0ffSNick Fitzgerald mem: &Memory,
143*7948e0ffSNick Fitzgerald ) -> u8 {
144*7948e0ffSNick Fitzgerald mem.page_size_log2(store)
145*7948e0ffSNick Fitzgerald }
146