1 //////////////////////////////////////////////////////////////////////////////
2 //
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)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10 
11 #include <boost/interprocess/detail/config_begin.hpp>
12 #include <boost/interprocess/detail/workaround.hpp>
13 
14 #ifdef BOOST_INTERPROCESS_WINDOWS
15 
16 #include <fstream>
17 #include <iostream>
18 #include <boost/interprocess/windows_shared_memory.hpp>
19 #include <boost/interprocess/mapped_region.hpp>
20 #include <string>
21 #include "get_process_id_name.hpp"
22 
23 using namespace boost::interprocess;
24 
main()25 int main ()
26 {
27    try{
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)
30       {
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)
36             return 1;
37          {
38 
39             //Create two mapped regions, one half of the file each
40             mapped_region region (mapping
41                                  ,read_write
42                                  ,0
43                                  ,FileSize/2
44                                  ,0);
45 
46             mapped_region region2(mapping
47                                  ,read_write
48                                  ,FileSize/2
49                                  ,FileSize - FileSize/2
50                                  ,0);
51 
52             //Fill two regions with a pattern
53             unsigned char *filler = static_cast<unsigned char*>(region.get_address());
54             for(std::size_t i = 0
55                ;i < FileSize/2
56                ;++i){
57                *filler++ = static_cast<unsigned char>(i);
58             }
59 
60             filler = static_cast<unsigned char*>(region2.get_address());
61             for(std::size_t i = FileSize/2
62                ;i < FileSize
63                ;++i){
64                *filler++ = static_cast<unsigned char>(i);
65             }
66             if(!region.flush(0, 0, false)){
67                return 1;
68             }
69 
70             if(!region2.flush(0, 0, true)){
71                return 1;
72             }
73          }
74 
75          //See if the pattern is correct in the file using two mapped regions
76          {
77             mapped_region region (mapping, read_only, 0, FileSize/2, 0);
78             mapped_region region2(mapping, read_only, FileSize/2, FileSize - FileSize/2, 0);
79 
80             unsigned char *checker = static_cast<unsigned char*>(region.get_address());
81             //Check pattern
82             for(std::size_t i = 0
83                ;i < FileSize/2
84                ;++i){
85                if(*checker++ != static_cast<unsigned char>(i)){
86                   return 1;
87                }
88             }
89 
90             //Check second half
91             checker = static_cast<unsigned char *>(region2.get_address());
92 
93             //Check pattern
94             for(std::size_t i = FileSize/2
95                ;i < FileSize
96                ;++i){
97                if(*checker++ != static_cast<unsigned char>(i)){
98                   return 1;
99                }
100             }
101          }
102 
103          //Now check the pattern mapping a single read only mapped_region
104          {
105             //Create a single regions, mapping all the file
106             mapped_region region (mapping, read_only);
107 
108             //Check pattern
109             unsigned char *pattern = static_cast<unsigned char*>(region.get_address());
110             for(std::size_t i = 0
111                ;i < FileSize
112                ;++i, ++pattern){
113                if(*pattern != static_cast<unsigned char>(i)){
114                   return 1;
115                }
116             }
117          }
118       }
119    }
120    catch(std::exception &exc){
121       //shared_memory_object::remove(test::get_process_id_name());
122       std::cout << "Unhandled exception: " << exc.what() << std::endl;
123       return 1;
124    }
125 
126    return 0;
127 }
128 
129 #else
130 
main()131 int main()
132 {
133    return 0;
134 }
135 
136 #endif
137 
138 #include <boost/interprocess/detail/config_end.hpp>
139