wasmtime_c_api/
sharedmemory.rs

1use crate::{handle_result, wasm_memorytype_t, wasmtime_error_t};
2use std::cell::UnsafeCell;
3use wasmtime::SharedMemory;
4
5type wasmtime_sharedmemory_t = SharedMemory;
6
7wasmtime_c_api_macros::declare_own!(wasmtime_sharedmemory_t);
8
9#[unsafe(no_mangle)]
10#[cfg(feature = "threads")]
11pub extern "C" fn wasmtime_sharedmemory_new(
12    engine: &crate::wasm_engine_t,
13    ty: &wasm_memorytype_t,
14    ret: &mut *mut wasmtime_sharedmemory_t,
15) -> Option<Box<wasmtime_error_t>> {
16    handle_result(
17        SharedMemory::new(&engine.engine, ty.ty().ty.clone()),
18        |mem| *ret = Box::<wasmtime_sharedmemory_t>::into_raw(Box::new(mem)),
19    )
20}
21
22#[unsafe(no_mangle)]
23pub extern "C" fn wasmtime_sharedmemory_clone(
24    mem: &wasmtime_sharedmemory_t,
25) -> Box<wasmtime_sharedmemory_t> {
26    Box::new(mem.clone())
27}
28
29#[unsafe(no_mangle)]
30pub extern "C" fn wasmtime_sharedmemory_type(
31    mem: &wasmtime_sharedmemory_t,
32) -> Box<wasm_memorytype_t> {
33    Box::new(wasm_memorytype_t::new(mem.ty()))
34}
35
36#[unsafe(no_mangle)]
37pub extern "C" fn wasmtime_sharedmemory_data(
38    mem: &wasmtime_sharedmemory_t,
39) -> *const UnsafeCell<u8> {
40    mem.data().as_ptr()
41}
42
43#[unsafe(no_mangle)]
44pub extern "C" fn wasmtime_sharedmemory_data_size(mem: &wasmtime_sharedmemory_t) -> usize {
45    mem.data().len()
46}
47
48#[unsafe(no_mangle)]
49pub extern "C" fn wasmtime_sharedmemory_size(mem: &wasmtime_sharedmemory_t) -> u64 {
50    mem.size()
51}
52
53#[unsafe(no_mangle)]
54pub extern "C" fn wasmtime_sharedmemory_grow(
55    mem: &wasmtime_sharedmemory_t,
56    delta: u64,
57    prev_size: &mut u64,
58) -> Option<Box<wasmtime_error_t>> {
59    handle_result(mem.grow(delta), |prev| *prev_size = prev)
60}