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

source code of boost/libs/interprocess/test/windows_shared_memory_mapping_test.cpp