• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <fstream>
12 #include <iostream>
13 #include <boost/interprocess/shared_memory_object.hpp>
14 #include <boost/interprocess/mapped_region.hpp>
15 #include <boost/interprocess/anonymous_shared_memory.hpp>
16 #include <string>
17 #include "get_process_id_name.hpp"
18 
19 using namespace boost::interprocess;
20 
get_shared_memory_mapping()21 shared_memory_object get_shared_memory_mapping()
22 {
23    shared_memory_object sh;
24    return shared_memory_object(boost::move(sh));
25 }
26 
main()27 int main ()
28 {
29    std::string process_id = test::get_process_id_name();
30    std::string process_id2(process_id);
31    process_id2 += "_2";
32    try{
33       const std::size_t FileSize = 99999*4;
34       {
35          //Remove shared memory
36          shared_memory_object::remove(process_id.c_str());
37          shared_memory_object::remove(process_id2.c_str());
38 
39          //Create shared memory and file mapping
40          shared_memory_object mapping(create_only, process_id.c_str(), read_write);
41          mapping.truncate(FileSize);
42       }
43 
44       {
45          //Create a file mapping
46          shared_memory_object mapping(open_only, process_id.c_str(), read_write);
47 
48          //Create two mapped regions, one half of the file each
49          mapped_region region (mapping
50                               ,read_write
51                               ,0
52                               ,FileSize/2
53                               ,0);
54 
55          mapped_region region2(mapping
56                               ,read_write
57                               ,FileSize/2
58                               ,FileSize - FileSize/2
59                               ,0);
60 
61          //Fill two regions with a pattern
62          unsigned char *filler = static_cast<unsigned char*>(region.get_address());
63          for(std::size_t i = 0
64             ;i < FileSize/2
65             ;++i){
66             *filler++ = static_cast<unsigned char>(i);
67          }
68 
69          filler = static_cast<unsigned char*>(region2.get_address());
70          for(std::size_t i = FileSize/2
71             ;i < FileSize
72             ;++i){
73             *filler++ = static_cast<unsigned char>(i);
74          }
75          if(!region.flush(0, 0, false)){
76             return 1;
77          }
78 
79          if(!region2.flush(0, 0, true)){
80             return 1;
81          }
82       }
83 
84       //See if the pattern is correct in the file using two mapped regions
85       {
86          //Create a file mapping
87          shared_memory_object mapping(open_only, process_id.c_str(), read_write);
88          mapped_region region(mapping, read_write, 0, FileSize/2, 0);
89          mapped_region region2(mapping, read_write, FileSize/2, FileSize - FileSize/2, 0);
90 
91          unsigned char *checker = static_cast<unsigned char*>(region.get_address());
92          //Check pattern
93          for(std::size_t i = 0
94             ;i < FileSize/2
95             ;++i){
96             if(*checker++ != static_cast<unsigned char>(i)){
97                return 1;
98             }
99          }
100 
101          //Check second half
102          checker = static_cast<unsigned char *>(region2.get_address());
103 
104          //Check pattern
105          for(std::size_t i = FileSize/2
106             ;i < FileSize
107             ;++i){
108             if(*checker++ != static_cast<unsigned char>(i)){
109                return 1;
110             }
111          }
112       }
113 
114       //Now check the pattern mapping a single read only mapped_region
115       {
116          //Create a file mapping
117          shared_memory_object mapping(open_only, process_id.c_str(), read_only);
118 
119          //Create a single regions, mapping all the file
120          mapped_region region (mapping, read_only);
121 
122          //Check pattern
123          unsigned char *pattern = static_cast<unsigned char*>(region.get_address());
124          for(std::size_t i = 0
125             ;i < FileSize
126             ;++i, ++pattern){
127             if(*pattern != static_cast<unsigned char>(i)){
128                return 1;
129             }
130          }
131          //Now shrink
132          const std::size_t original_region_size = region.get_size();
133          if(!region.shrink_by(region.get_size()/2, false) || region.get_size() != original_region_size/2){
134             return 1;
135          }
136          const std::size_t shrunk_region_size = region.get_size();
137          if(!region.shrink_by(region.get_size()/2, true) || region.get_size() != shrunk_region_size/2){
138             return 1;
139          }
140 
141          //Now advise
142          #if defined(POSIX_MADV_NORMAL) || defined(MADV_NORMAL)
143          std::cout << "Advice normal" << std::endl;
144          if(!region.advise(mapped_region::advice_normal)){
145             return 1;
146          }
147          #endif
148 
149          #if defined(POSIX_MADV_SEQUENTIAL) || defined(MADV_SEQUENTIAL)
150          std::cout << "Advice sequential" << std::endl;
151          if(!region.advise(mapped_region::advice_sequential)){
152             return 1;
153          }
154          #endif
155 
156          #if defined(POSIX_MADV_RANDOM) || defined(MADV_RANDOM)
157          std::cout << "Advice random" << std::endl;
158          if(!region.advise(mapped_region::advice_random)){
159             return 1;
160          }
161          #endif
162 
163          #if defined(POSIX_MADV_WILLNEED) || defined(MADV_WILLNEED)
164          std::cout << "Advice 'will need'" << std::endl;
165          if(!region.advise(mapped_region::advice_willneed)){
166             return 1;
167          }
168          #endif
169 
170          #if defined(POSIX_MADV_DONTNEED) || (defined(MADV_DONTNEED) && defined(BOOST_INTERPROCESS_MADV_DONTNEED_HAS_NONDESTRUCTIVE_SEMANTICS))
171          std::cout << "Advice 'dont't need'" << std::endl;
172          if(!region.advise(mapped_region::advice_dontneed)){
173             return 1;
174          }
175          #endif
176 
177       }
178       {
179          //Check for busy address space
180          shared_memory_object mapping(open_only, process_id.c_str(), read_only);
181          mapped_region region (mapping, read_only);
182          shared_memory_object mapping2(create_only, process_id2.c_str(), read_write);
183          mapping2.truncate(FileSize);
184          try{
185             mapped_region region2 (mapping2, read_only, 0, FileSize, region.get_address());
186          }
187          catch(interprocess_exception &e){
188             shared_memory_object::remove(process_id2.c_str());
189             if(e.get_error_code() != busy_error){
190                throw e;
191             }
192          }
193          catch(std::exception &){
194             shared_memory_object::remove(process_id2.c_str());
195             throw;
196          }
197          shared_memory_object::remove(process_id2.c_str());
198       }
199       {
200          //Now check anonymous mapping
201          mapped_region region(anonymous_shared_memory(FileSize));
202 
203          //Write pattern
204          unsigned char *pattern = static_cast<unsigned char*>(region.get_address());
205          for(std::size_t i = 0
206             ;i < FileSize
207             ;++i, ++pattern){
208             *pattern = static_cast<unsigned char>(i);
209          }
210 
211          //Check pattern
212          pattern = static_cast<unsigned char*>(region.get_address());
213          for(std::size_t i = 0
214             ;i < FileSize
215             ;++i, ++pattern){
216             if(*pattern != static_cast<unsigned char>(i)){
217                return 1;
218             }
219          }
220       }
221       {
222          //Now test move semantics
223          shared_memory_object mapping(open_only, process_id.c_str(), read_write);
224          shared_memory_object move_ctor(boost::move(mapping));
225          shared_memory_object move_assign;
226          move_assign = boost::move(move_ctor);
227          shared_memory_object ret(get_shared_memory_mapping());
228       }
229    }
230    catch(std::exception &exc){
231       shared_memory_object::remove(process_id.c_str());
232       shared_memory_object::remove(process_id2.c_str());
233       std::cout << "Unhandled exception: " << exc.what() << std::endl;
234       return 1;
235    }
236    shared_memory_object::remove(process_id.c_str());
237    return 0;
238 }
239