1pub trait ShmFileHandle {
6 fn get_mut_ptr(&mut self) -> *mut std::ffi::c_void;
7 fn get_size(&self) -> usize;
8}
9
10cfg_if::cfg_if! {
11 if #[cfg(feature = "internal_shm_impl")] {
12 use std::fs::OpenOptions;
13
14 use memmap2::MmapOptions;
15
16 use crate::error;
17 pub struct ShmFile {
19 pub file_path: std::path::PathBuf,
20 pub file_size: u64,
21 mapped: memmap2::MmapRaw,
22 }
23
24 impl ShmFile {
25 pub fn open(file_path: std::path::PathBuf) -> error::LGMPResult<Self> {
27 let file = OpenOptions::new()
28 .read(true)
29 .write(true)
30 .create(false)
31 .open(&file_path)?;
32
33 let file_size = file.metadata()?.len();
34
35 let mapped = MmapOptions::new()
36 .len(file_size.try_into().expect("File length exceeed usize"))
37 .map_raw(&file)?;
38
39 Ok(ShmFile {
40 file_path,
41 file_size,
42 mapped,
43 })
44 }
45
46 pub fn get_ptr(&self) -> *mut () {
47 self.mapped.as_mut_ptr() as *mut ()
48 }
49 }
50
51 impl ShmFileHandle for ShmFile {
52 fn get_mut_ptr(&mut self) -> *mut std::ffi::c_void {
53 self.get_ptr() as *mut std::ffi::c_void
54 }
55
56 fn get_size(&self) -> usize {
57 usize::try_from(self.file_size).expect("File size exceeded maxint")
58 }
59 }
60 }
61
62}
63
64pub use shared_memory;
65
66impl ShmFileHandle for shared_memory::Shmem {
67 fn get_mut_ptr(&mut self) -> *mut std::ffi::c_void {
68 self.as_ptr() as *mut std::ffi::c_void
69 }
70
71 fn get_size(&self) -> usize {
72 self.len()
73 }
74}