@@ -3,11 +3,24 @@ use std::io::ErrorKind;
33use std:: os:: windows:: { fs:: OpenOptionsExt , io:: AsRawHandle } ;
44use std:: path:: PathBuf ;
55
6- use crate :: log:: * ;
6+ use crate :: { log:: * , ShmemConf } ;
77use win_sys:: * ;
88
99use crate :: ShmemError ;
1010
11+ #[ derive( Clone , Default ) ]
12+ pub struct ShmemConfExt {
13+ allow_raw : bool ,
14+ }
15+
16+ impl ShmemConf {
17+ /// If set to true, enables openning raw shared memory that is not managed by this crate
18+ pub fn allow_raw ( mut self , allow : bool ) -> Self {
19+ self . ext . allow_raw = allow;
20+ self
21+ }
22+ }
23+
1124pub struct MapData {
1225 owner : bool ,
1326
@@ -118,7 +131,12 @@ fn get_tmp_dir() -> Result<PathBuf, ShmemError> {
118131 }
119132}
120133
121- fn new_map ( unique_id : & str , map_size : usize , create : bool ) -> Result < MapData , ShmemError > {
134+ fn new_map (
135+ unique_id : & str ,
136+ mut map_size : usize ,
137+ create : bool ,
138+ allow_raw : bool ,
139+ ) -> Result < MapData , ShmemError > {
122140 // Create file to back the shared memory
123141 let mut file_path = get_tmp_dir ( ) ?;
124142 file_path. push ( unique_id. trim_start_matches ( '/' ) ) ;
@@ -188,6 +206,8 @@ fn new_map(unique_id: &str, map_size: usize, create: bool) -> Result<MapData, Sh
188206 Err ( e) => {
189207 if create {
190208 return Err ( ShmemError :: MapCreateFailed ( e. raw_os_error ( ) . unwrap ( ) as _ ) ) ;
209+ } else if !allow_raw {
210+ return Err ( ShmemError :: MapOpenFailed ( ERROR_FILE_NOT_FOUND . 0 ) ) ;
191211 }
192212
193213 // This may be a mapping that isnt managed by this crate
@@ -216,7 +236,7 @@ fn new_map(unique_id: &str, map_size: usize, create: bool) -> Result<MapData, Sh
216236 ( FILE_MAP_READ | FILE_MAP_WRITE ) . 0 ,
217237 ) ;
218238 let map_ptr = match MapViewOfFile ( map_h. as_handle ( ) , FILE_MAP_READ | FILE_MAP_WRITE , 0 , 0 , 0 ) {
219- Ok ( v) => v as _ ,
239+ Ok ( v) => v,
220240 Err ( e) => {
221241 return Err ( if create {
222242 ShmemError :: MapCreateFailed ( e. win32_error ( ) . unwrap ( ) . 0 )
@@ -227,33 +247,35 @@ fn new_map(unique_id: &str, map_size: usize, create: bool) -> Result<MapData, Sh
227247 } ;
228248 trace ! ( "\t {:p}" , map_ptr) ;
229249
230- let mut new_map = MapData {
231- owner : create,
232- file_map : map_h,
233- persistent_file,
234- unique_id : unique_id. to_string ( ) ,
235- map_size : 0 ,
236- view : map_ptr,
237- } ;
238-
239250 if !create {
240251 //Get the real size of the openned mapping
241252 let mut info = MEMORY_BASIC_INFORMATION :: default ( ) ;
242- if let Err ( e) = VirtualQuery ( new_map . view . as_mut_ptr ( ) as _ , & mut info) {
253+ if let Err ( e) = VirtualQuery ( map_ptr . as_mut_ptr ( ) , & mut info) {
243254 return Err ( ShmemError :: UnknownOsError ( e. win32_error ( ) . unwrap ( ) . 0 ) ) ;
244255 }
245- new_map . map_size = info. RegionSize ;
256+ map_size = info. RegionSize ;
246257 }
247258
248- Ok ( new_map)
259+ Ok ( MapData {
260+ owner : create,
261+ file_map : map_h,
262+ persistent_file,
263+ unique_id : unique_id. to_string ( ) ,
264+ map_size,
265+ view : map_ptr,
266+ } )
249267}
250268
251269//Creates a mapping specified by the uid and size
252270pub fn create_mapping ( unique_id : & str , map_size : usize ) -> Result < MapData , ShmemError > {
253- new_map ( unique_id, map_size, true )
271+ new_map ( unique_id, map_size, true , false )
254272}
255273
256274//Opens an existing mapping specified by its uid
257- pub fn open_mapping ( unique_id : & str , map_size : usize ) -> Result < MapData , ShmemError > {
258- new_map ( unique_id, map_size, false )
275+ pub fn open_mapping (
276+ unique_id : & str ,
277+ map_size : usize ,
278+ ext : & ShmemConfExt ,
279+ ) -> Result < MapData , ShmemError > {
280+ new_map ( unique_id, map_size, false , ext. allow_raw )
259281}
0 commit comments