wasmer_types/
memory_view.rs

1use crate::lib::std::cell::Cell;
2use crate::lib::std::marker::PhantomData;
3use crate::lib::std::ops::Deref;
4// use crate::lib::std::ops::{Bound, RangeBounds};
5use crate::lib::std::slice;
6use crate::lib::std::sync::atomic::{
7    AtomicI16, AtomicI32, AtomicI64, AtomicI8, AtomicU16, AtomicU32, AtomicU64, AtomicU8,
8};
9use crate::native::ValueType;
10
11pub trait Atomic {
12    type Output;
13}
14
15macro_rules! atomic {
16    ( $($for:ty => $output:ty),+ ) => {
17        $(
18            impl Atomic for $for {
19                type Output = $output;
20            }
21        )+
22    }
23}
24
25atomic!(
26    i8 => AtomicI8,
27    i16 => AtomicI16,
28    i32 => AtomicI32,
29    i64 => AtomicI64,
30    u8 => AtomicU8,
31    u16 => AtomicU16,
32    u32 => AtomicU32,
33    u64 => AtomicU64,
34    f32 => AtomicU32,
35    f64 => AtomicU64
36);
37
38/// A trait that represants an atomic type.
39pub trait Atomicity {}
40
41/// Atomically.
42pub struct Atomically;
43impl Atomicity for Atomically {}
44
45/// Non-atomically.
46pub struct NonAtomically;
47impl Atomicity for NonAtomically {}
48
49/// A view into a memory.
50pub struct MemoryView<'a, T: 'a, A = NonAtomically> {
51    ptr: *mut T,
52    // Note: the length is in the terms of `size::<T>()`.
53    // The total length in memory is `size::<T>() * length`.
54    length: usize,
55    _phantom: PhantomData<(&'a [Cell<T>], A)>,
56}
57
58impl<'a, T> MemoryView<'a, T, NonAtomically>
59where
60    T: ValueType,
61{
62    /// Creates a new MemoryView given a `pointer` and `length`.
63    pub unsafe fn new(ptr: *mut T, length: u32) -> Self {
64        Self {
65            ptr,
66            length: length as usize,
67            _phantom: PhantomData,
68        }
69    }
70
71    /// Creates a subarray view from this `MemoryView`.
72    pub fn subarray(&self, start: u32, end: u32) -> Self {
73        assert!(
74            (start as usize) < self.length,
75            "The range start is bigger than current length"
76        );
77        assert!(
78            (end as usize) < self.length,
79            "The range end is bigger than current length"
80        );
81
82        Self {
83            ptr: unsafe { self.ptr.add(start as usize) },
84            length: (end - start) as usize,
85            _phantom: PhantomData,
86        }
87    }
88
89    /// Copy the contents of the source slice into this `MemoryView`.
90    ///
91    /// This function will efficiently copy the memory from within the wasm
92    /// module’s own linear memory to this typed array.
93    ///
94    /// # Safety
95    ///
96    /// This method is unsafe because the caller will need to make sure
97    /// there are no data races when copying memory into the view.
98    pub unsafe fn copy_from(&self, src: &[T]) {
99        // We cap at a max length
100        let sliced_src = &src[..self.length];
101        for (i, byte) in sliced_src.iter().enumerate() {
102            *self.ptr.offset(i as isize) = *byte;
103        }
104    }
105}
106
107impl<'a, T: Atomic> MemoryView<'a, T> {
108    /// Get atomic access to a memory view.
109    pub fn atomically(&self) -> MemoryView<'a, T::Output, Atomically> {
110        MemoryView {
111            ptr: self.ptr as *mut T::Output,
112            length: self.length,
113            _phantom: PhantomData,
114        }
115    }
116}
117
118impl<'a, T> Deref for MemoryView<'a, T, NonAtomically> {
119    type Target = [Cell<T>];
120    fn deref(&self) -> &[Cell<T>] {
121        let mut_slice: &mut [T] = unsafe { slice::from_raw_parts_mut(self.ptr, self.length) };
122        let cell_slice: &Cell<[T]> = Cell::from_mut(mut_slice);
123        cell_slice.as_slice_of_cells()
124    }
125}
126
127impl<'a, T> Deref for MemoryView<'a, T, Atomically> {
128    type Target = [T];
129    fn deref(&self) -> &[T] {
130        unsafe { slice::from_raw_parts(self.ptr as *const T, self.length) }
131    }
132}