| 1 | ////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // (C) Copyright Ion Gaztanaga 2006-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 | //[doc_named_alloc |
| 13 | #include <boost/interprocess/managed_shared_memory.hpp> |
| 14 | #include <cstdlib> //std::system |
| 15 | #include <cstddef> |
| 16 | #include <cassert> |
| 17 | #include <utility> |
| 18 | //<- |
| 19 | #include "../test/get_process_id_name.hpp" |
| 20 | //-> |
| 21 | |
| 22 | int main(int argc, char *argv[]) |
| 23 | { |
| 24 | using namespace boost::interprocess; |
| 25 | typedef std::pair<double, int> MyType; |
| 26 | |
| 27 | if(argc == 1){ //Parent process |
| 28 | //Remove shared memory on construction and destruction |
| 29 | struct shm_remove |
| 30 | { |
| 31 | //<- |
| 32 | #if 1 |
| 33 | shm_remove() { shared_memory_object::remove(filename: test::get_process_id_name()); } |
| 34 | ~shm_remove(){ shared_memory_object::remove(filename: test::get_process_id_name()); } |
| 35 | #else |
| 36 | //-> |
| 37 | shm_remove() { shared_memory_object::remove("MySharedMemory" ); } |
| 38 | ~shm_remove(){ shared_memory_object::remove("MySharedMemory" ); } |
| 39 | //<- |
| 40 | #endif |
| 41 | //-> |
| 42 | } remover; |
| 43 | //<- |
| 44 | (void)remover; |
| 45 | //-> |
| 46 | |
| 47 | //Construct managed shared memory |
| 48 | //<- |
| 49 | #if 1 |
| 50 | managed_shared_memory segment(create_only, test::get_process_id_name(), 65536); |
| 51 | #else |
| 52 | //-> |
| 53 | managed_shared_memory segment(create_only, "MySharedMemory" , 65536); |
| 54 | //<- |
| 55 | #endif |
| 56 | //-> |
| 57 | |
| 58 | //Create an object of MyType initialized to {0.0, 0} |
| 59 | MyType *instance = segment.construct<MyType> |
| 60 | (name: "MyType instance" ) //name of the object |
| 61 | (0.0, 0); //ctor first argument |
| 62 | |
| 63 | //Create an array of 10 elements of MyType initialized to {0.0, 0} |
| 64 | MyType *array = segment.construct<MyType> |
| 65 | (name: "MyType array" ) //name of the object |
| 66 | [10] //number of elements |
| 67 | (0.0, 0); //Same two ctor arguments for all objects |
| 68 | |
| 69 | //Create an array of 3 elements of MyType initializing each one |
| 70 | //to a different value {0.0, 0}, {1.0, 1}, {2.0, 2}... |
| 71 | float float_initializer[3] = { 0.0, 1.0, 2.0 }; |
| 72 | int int_initializer[3] = { 0, 1, 2 }; |
| 73 | |
| 74 | MyType *array_it = segment.construct_it<MyType> |
| 75 | (name: "MyType array from it" ) //name of the object |
| 76 | [3] //number of elements |
| 77 | ( &float_initializer[0] //Iterator for the 1st ctor argument |
| 78 | , &int_initializer[0]); //Iterator for the 2nd ctor argument |
| 79 | |
| 80 | //Launch child process |
| 81 | std::string s(argv[0]); s += " child " ; |
| 82 | //<- |
| 83 | s += test::get_process_id_name(); |
| 84 | //-> |
| 85 | if(0 != std::system(command: s.c_str())) |
| 86 | return 1; |
| 87 | |
| 88 | //<- |
| 89 | (void)instance; |
| 90 | (void)array; |
| 91 | (void)array_it; |
| 92 | //-> |
| 93 | |
| 94 | //Check child has destroyed all objects |
| 95 | if(segment.find<MyType>(name: "MyType array" ).first || |
| 96 | segment.find<MyType>(name: "MyType instance" ).first || |
| 97 | segment.find<MyType>(name: "MyType array from it" ).first) |
| 98 | return 1; |
| 99 | } |
| 100 | else{ |
| 101 | //Open managed shared memory |
| 102 | //<- |
| 103 | #if 1 |
| 104 | managed_shared_memory segment(open_only, argv[2]); |
| 105 | #else |
| 106 | //-> |
| 107 | managed_shared_memory segment(open_only, "MySharedMemory" ); |
| 108 | //<- |
| 109 | #endif |
| 110 | //-> |
| 111 | |
| 112 | std::pair<MyType*, managed_shared_memory::size_type> res; |
| 113 | |
| 114 | //Find the array |
| 115 | res = segment.find<MyType> (name: "MyType array" ); |
| 116 | //Length should be 10 |
| 117 | if(res.second != 10) return 1; |
| 118 | |
| 119 | //Find the object |
| 120 | res = segment.find<MyType> (name: "MyType instance" ); |
| 121 | //Length should be 1 |
| 122 | if(res.second != 1) return 1; |
| 123 | |
| 124 | //Find the array constructed from iterators |
| 125 | res = segment.find<MyType> (name: "MyType array from it" ); |
| 126 | //Length should be 3 |
| 127 | if(res.second != 3) return 1; |
| 128 | |
| 129 | //We're done, delete all the objects |
| 130 | segment.destroy<MyType>(name: "MyType array" ); |
| 131 | segment.destroy<MyType>(name: "MyType instance" ); |
| 132 | segment.destroy<MyType>(name: "MyType array from it" ); |
| 133 | } |
| 134 | return 0; |
| 135 | } |
| 136 | //] |
| 137 | |
| 138 | |