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/workaround.hpp>
12
13 #if defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS)
14
15 #include <fstream>
16 #include <iostream>
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>
21 #include <string>
22 #include <iostream>
23 #include "get_process_id_name.hpp"
24
25 using namespace boost::interprocess;
26
remove_shared_memory(const xsi_key & key)27 void remove_shared_memory(const xsi_key &key)
28 {
29 try{
30 xsi_shared_memory xsi(open_only, key);
31 xsi_shared_memory::remove(xsi.get_shmid());
32 }
33 catch(interprocess_exception &e){
34 if(e.get_error_code() != not_found_error)
35 throw;
36 }
37 }
38
39 class xsi_shared_memory_remover
40 {
41 public:
xsi_shared_memory_remover(xsi_shared_memory & xsi_shm)42 xsi_shared_memory_remover(xsi_shared_memory &xsi_shm)
43 : xsi_shm_(xsi_shm)
44 {}
45
~xsi_shared_memory_remover()46 ~xsi_shared_memory_remover()
47 { xsi_shared_memory::remove(xsi_shm_.get_shmid()); }
48 private:
49 xsi_shared_memory & xsi_shm_;
50 };
51
get_filename()52 inline std::string get_filename()
53 {
54 std::string ret (ipcdetail::get_temporary_path());
55 ret += "/";
56 ret += test::get_process_id_name();
57 return ret;
58 }
59
main()60 int main ()
61 {
62 std::string filename(get_filename());
63 const char *names[2] = { filename.c_str(), 0 };
64
65 file_mapping::remove(names[0]);
66 { ipcdetail::file_wrapper(create_only, names[0], read_write); }
67 xsi_key key(names[0], 1);
68 file_mapping::remove(names[0]);
69 remove_shared_memory(key);
70
71 unsigned int i;
72 try{
73 for(i = 0; i < sizeof(names)/sizeof(names[0]); ++i)
74 {
75 const std::size_t FileSize = 99999*2;
76 //Create a file mapping
77 xsi_shared_memory mapping (create_only, names[i] ? key : xsi_key(), FileSize);
78 xsi_shared_memory_remover rem(mapping);
79 try{
80 {
81 //Partial mapping should fail fox XSI shared memory
82 bool thrown = false;
83 try{
84 mapped_region region2(mapping, read_write, FileSize/2, FileSize - FileSize/2, 0);
85 }
86 catch(...){
87 thrown = true;
88 }
89 if(thrown == false){
90 return 1;
91 }
92 //Create a mapped region
93 mapped_region region (mapping, read_write, 0, FileSize, 0);
94
95 //Fill two regions with a pattern
96 unsigned char *filler = static_cast<unsigned char*>(region.get_address());
97 for(std::size_t i = 0; i < FileSize; ++i){
98 *filler++ = static_cast<unsigned char>(i);
99 }
100 }
101
102 //Now check the pattern mapping a single read only mapped_region
103 {
104 //Create a single region, 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; i < FileSize; ++i, ++pattern){
110 if(*pattern != static_cast<unsigned char>(i)){
111 return 1;
112 }
113 }
114 }
115 }
116 catch(std::exception &exc){
117 std::cout << "Unhandled exception: " << exc.what() << std::endl;
118 return 1;
119 }
120 }
121 }
122 catch(std::exception &exc){
123 std::cout << "Unhandled exception: " << exc.what() << std::endl;
124 return 1;
125 }
126 return 0;
127 }
128
129 #else
130
main()131 int main()
132 {
133 return 0;
134 }
135
136 #endif //BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
137