]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/test/xsi_shared_memory_mapping_test.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / interprocess / test / xsi_shared_memory_mapping_test.cpp
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
27 void remove_shared_memory(const xsi_key &key)
28 {
29 BOOST_TRY{
30 xsi_shared_memory xsi(open_only, key);
31 xsi_shared_memory::remove(xsi.get_shmid());
32 }
33 BOOST_CATCH(interprocess_exception &e){
34 if(e.get_error_code() != not_found_error)
35 BOOST_RETHROW
36 } BOOST_CATCH_END
37 }
38
39 class xsi_shared_memory_remover
40 {
41 public:
42 xsi_shared_memory_remover(xsi_shared_memory &xsi_shm)
43 : xsi_shm_(xsi_shm)
44 {}
45
46 ~xsi_shared_memory_remover()
47 { xsi_shared_memory::remove(xsi_shm_.get_shmid()); }
48 private:
49 xsi_shared_memory & xsi_shm_;
50 };
51
52 int main ()
53 {
54 std::string filename(get_filename());
55 const char *names[2] = { filename.c_str(), 0 };
56
57 file_mapping::remove(names[0]);
58 { ipcdetail::file_wrapper(create_only, names[0], read_write); }
59 xsi_key key(names[0], 1);
60 file_mapping::remove(names[0]);
61 remove_shared_memory(key);
62
63 unsigned int i;
64 BOOST_TRY{
65 for(i = 0; i < sizeof(names)/sizeof(names[0]); ++i)
66 {
67 const std::size_t FileSize = 99999*2;
68 //Create a file mapping
69 xsi_shared_memory mapping (create_only, names[i] ? key : xsi_key(), FileSize);
70 xsi_shared_memory_remover rem(mapping);
71 BOOST_TRY{
72 {
73 //Partial mapping should fail fox XSI shared memory
74 bool thrown = false;
75 BOOST_TRY{
76 mapped_region region2(mapping, read_write, FileSize/2, FileSize - FileSize/2, 0);
77 }
78 BOOST_CATCH(...){
79 thrown = true;
80 } BOOST_CATCH_END
81 if(thrown == false){
82 return 1;
83 }
84 //Create a mapped region
85 mapped_region region (mapping, read_write, 0, FileSize, 0);
86
87 //Fill two regions with a pattern
88 unsigned char *filler = static_cast<unsigned char*>(region.get_address());
89 for(std::size_t i = 0; i < FileSize; ++i){
90 *filler++ = static_cast<unsigned char>(i);
91 }
92 }
93
94 //Now check the pattern mapping a single read only mapped_region
95 {
96 //Create a single region, mapping all the file
97 mapped_region region (mapping, read_only);
98
99 //Check pattern
100 unsigned char *pattern = static_cast<unsigned char*>(region.get_address());
101 for(std::size_t i = 0; i < FileSize; ++i, ++pattern){
102 if(*pattern != static_cast<unsigned char>(i)){
103 return 1;
104 }
105 }
106 }
107 }
108 BOOST_CATCH(std::exception &exc){
109 std::cout << "Unhandled exception: " << exc.what() << std::endl;
110 return 1;
111 } BOOST_CATCH_END
112 }
113 }
114 BOOST_CATCH(std::exception &exc){
115 std::cout << "Unhandled exception: " << exc.what() << std::endl;
116 return 1;
117 } BOOST_CATCH_END
118 return 0;
119 }
120
121 #else
122
123 int main()
124 {
125 return 0;
126 }
127
128 #endif //BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS