1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2004-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #include <boost/interprocess/detail/workaround.hpp>
13 #if defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS)
17 #include <boost/interprocess/xsi_shared_memory.hpp>
18 #include <boost/interprocess/mapped_region.hpp>
19 #include <boost/interprocess/file_mapping.hpp>
20 #include <boost/interprocess/detail/file_wrapper.hpp>
23 #include "get_process_id_name.hpp"
25 using namespace boost::interprocess
;
27 void remove_shared_memory(const xsi_key
&key
)
30 xsi_shared_memory
xsi(open_only
, key
);
31 xsi_shared_memory::remove(xsi
.get_shmid());
33 BOOST_CATCH(interprocess_exception
&e
){
34 if(e
.get_error_code() != not_found_error
)
39 class xsi_shared_memory_remover
42 xsi_shared_memory_remover(xsi_shared_memory
&xsi_shm
)
46 ~xsi_shared_memory_remover()
47 { xsi_shared_memory::remove(xsi_shm_
.get_shmid()); }
49 xsi_shared_memory
& xsi_shm_
;
54 std::string
filename(get_filename());
55 const char *names
[2] = { filename
.c_str(), 0 };
57 file_mapping::remove(names
[0]);
58 { ipcdetail::file_wrapper(create_only
, names
[0], read_write
); }
59 xsi_key
key(names
[0], 1);
60 file_mapping::remove(names
[0]);
61 remove_shared_memory(key
);
65 for(i
= 0; i
< sizeof(names
)/sizeof(names
[0]); ++i
)
67 const std::size_t FileSize
= 99999*2;
68 //Create a file mapping
69 xsi_shared_memory
mapping (create_only
, names
[i
] ? key
: xsi_key(), FileSize
);
70 xsi_shared_memory_remover
rem(mapping
);
73 //Partial mapping should fail fox XSI shared memory
76 mapped_region
region2(mapping
, read_write
, FileSize
/2, FileSize
- FileSize
/2, 0);
84 //Create a mapped region
85 mapped_region
region (mapping
, read_write
, 0, FileSize
, 0);
87 //Fill two regions with a pattern
88 unsigned char *filler
= static_cast<unsigned char*>(region
.get_address());
89 for(std::size_t i
= 0; i
< FileSize
; ++i
){
90 *filler
++ = static_cast<unsigned char>(i
);
94 //Now check the pattern mapping a single read only mapped_region
96 //Create a single region, mapping all the file
97 mapped_region
region (mapping
, read_only
);
100 unsigned char *pattern
= static_cast<unsigned char*>(region
.get_address());
101 for(std::size_t i
= 0; i
< FileSize
; ++i
, ++pattern
){
102 if(*pattern
!= static_cast<unsigned char>(i
)){
108 BOOST_CATCH(std::exception
&exc
){
109 std::cout
<< "Unhandled exception: " << exc
.what() << std::endl
;
114 BOOST_CATCH(std::exception
&exc
){
115 std::cout
<< "Unhandled exception: " << exc
.what() << std::endl
;
128 #endif //BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS