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/config_begin.hpp>
12 #include <boost/interprocess/detail/workaround.hpp>
14 #ifdef BOOST_INTERPROCESS_WINDOWS
18 #include <boost/interprocess/windows_shared_memory.hpp>
19 #include <boost/interprocess/mapped_region.hpp>
21 #include "get_process_id_name.hpp"
23 using namespace boost::interprocess
;
28 const char *names
[2] = { test::get_process_id_name(), 0 };
29 for(unsigned int i_name
= 0; i_name
< sizeof(names
)/sizeof(names
[0]); ++i_name
)
31 const std::size_t FileSize
= 99999*2;
32 //Create a file mapping
33 windows_shared_memory mapping
34 (create_only
, names
[i_name
], read_write
, FileSize
);
35 if(static_cast<std::size_t>(mapping
.get_size()) < FileSize
)
39 //Create two mapped regions, one half of the file each
40 mapped_region
region (mapping
46 mapped_region
region2(mapping
49 ,FileSize
- FileSize
/2
52 //Fill two regions with a pattern
53 unsigned char *filler
= static_cast<unsigned char*>(region
.get_address());
57 *filler
++ = static_cast<unsigned char>(i
);
60 filler
= static_cast<unsigned char*>(region2
.get_address());
61 for(std::size_t i
= FileSize
/2
64 *filler
++ = static_cast<unsigned char>(i
);
66 if(!region
.flush(0, 0, false)){
70 if(!region2
.flush(0, 0, true)){
75 //See if the pattern is correct in the file using two mapped regions
77 mapped_region
region (mapping
, read_only
, 0, FileSize
/2, 0);
78 mapped_region
region2(mapping
, read_only
, FileSize
/2, FileSize
- FileSize
/2, 0);
80 unsigned char *checker
= static_cast<unsigned char*>(region
.get_address());
85 if(*checker
++ != static_cast<unsigned char>(i
)){
91 checker
= static_cast<unsigned char *>(region2
.get_address());
94 for(std::size_t i
= FileSize
/2
97 if(*checker
++ != static_cast<unsigned char>(i
)){
103 //Now check the pattern mapping a single read only mapped_region
105 //Create a single regions, mapping all the file
106 mapped_region
region (mapping
, read_only
);
109 unsigned char *pattern
= static_cast<unsigned char*>(region
.get_address());
110 for(std::size_t i
= 0
113 if(*pattern
!= static_cast<unsigned char>(i
)){
120 catch(std::exception
&exc
){
121 //shared_memory_object::remove(test::get_process_id_name());
122 std::cout
<< "Unhandled exception: " << exc
.what() << std::endl
;
138 #include <boost/interprocess/detail/config_end.hpp>