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