1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use super::*;
pub struct SharedMemRaw {
os_data: os_impl::MapData,
}
impl SharedMemRaw {
pub fn create(unique_id: String, size: usize) -> Result<SharedMemRaw> {
let os_map: os_impl::MapData = os_impl::create_mapping(&unique_id, size)?;
Ok(SharedMemRaw {
os_data: os_map,
})
}
pub fn open(unique_id: String) -> Result<SharedMemRaw> {
let os_map = os_impl::open_mapping(&unique_id)?;
Ok(SharedMemRaw {
os_data: os_map,
})
}
#[inline]
pub fn get_size(&self) -> &usize {
&self.os_data.map_size
}
#[inline]
pub fn get_path(&self) -> &String {
&self.os_data.unique_id
}
#[inline]
pub fn get_ptr(&self) -> *mut c_void {
self.os_data.map_ptr
}
}
impl ReadRaw for SharedMemRaw {
unsafe fn get_raw<D: SharedMemCast>(&self) -> &D {
return &(*(self.os_data.map_ptr as *const D))
}
unsafe fn get_raw_slice<D: SharedMemCast>(&self) -> &[D] {
let item_size = std::mem::size_of::<D>();
if item_size > self.os_data.map_size {
panic!("Tried to map type of {} bytes to a lock holding only {} bytes", item_size, self.os_data.map_size);
}
let num_items: usize = self.os_data.map_size / item_size;
return slice::from_raw_parts(self.os_data.map_ptr as *const D, num_items);
}
}
impl WriteRaw for SharedMemRaw {
unsafe fn get_raw_mut<D: SharedMemCast>(&mut self) -> &mut D {
return &mut (*(self.os_data.map_ptr as *mut D))
}
unsafe fn get_raw_slice_mut<D: SharedMemCast>(&mut self) -> &mut[D] {
let item_size = std::mem::size_of::<D>();
if item_size > self.os_data.map_size {
panic!("Tried to map type of {} bytes to a lock holding only {} bytes", item_size, self.os_data.map_size);
}
let num_items: usize = self.os_data.map_size / item_size;
return slice::from_raw_parts_mut(self.os_data.map_ptr as *mut D, num_items);
}
}