| 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_cont |
| 13 | #include <boost/interprocess/containers/vector.hpp> |
| 14 | #include <boost/interprocess/allocators/allocator.hpp> |
| 15 | #include <boost/interprocess/managed_shared_memory.hpp> |
| 16 | //<- |
| 17 | #include "../test/get_process_id_name.hpp" |
| 18 | //-> |
| 19 | |
| 20 | int main () |
| 21 | { |
| 22 | using namespace boost::interprocess; |
| 23 | //Remove shared memory on construction and destruction |
| 24 | struct shm_remove |
| 25 | { |
| 26 | //<- |
| 27 | #if 1 |
| 28 | shm_remove() { shared_memory_object::remove(filename: test::get_process_id_name()); } |
| 29 | ~shm_remove(){ shared_memory_object::remove(filename: test::get_process_id_name()); } |
| 30 | #else |
| 31 | //-> |
| 32 | shm_remove() { shared_memory_object::remove("MySharedMemory" ); } |
| 33 | ~shm_remove(){ shared_memory_object::remove("MySharedMemory" ); } |
| 34 | //<- |
| 35 | #endif |
| 36 | //-> |
| 37 | } remover; |
| 38 | //<- |
| 39 | (void)remover; |
| 40 | //-> |
| 41 | |
| 42 | //A managed shared memory where we can construct objects |
| 43 | //associated with a c-string |
| 44 | //<- |
| 45 | #if 1 |
| 46 | managed_shared_memory segment(create_only,test::get_process_id_name(), 65536); |
| 47 | #else |
| 48 | //-> |
| 49 | managed_shared_memory segment(create_only, |
| 50 | "MySharedMemory" , //segment name |
| 51 | 65536); |
| 52 | //<- |
| 53 | #endif |
| 54 | //-> |
| 55 | |
| 56 | //Alias an STL-like allocator of ints that allocates ints from the segment |
| 57 | typedef allocator<int, managed_shared_memory::segment_manager> |
| 58 | ShmemAllocator; |
| 59 | |
| 60 | //Alias a vector that uses the previous STL-like allocator |
| 61 | typedef vector<int, ShmemAllocator> MyVector; |
| 62 | |
| 63 | int initVal[] = {0, 1, 2, 3, 4, 5, 6 }; |
| 64 | const int *begVal = initVal; |
| 65 | const int *endVal = initVal + sizeof(initVal)/sizeof(initVal[0]); |
| 66 | |
| 67 | //Initialize the STL-like allocator |
| 68 | const ShmemAllocator alloc_inst (segment.get_segment_manager()); |
| 69 | |
| 70 | //Construct the vector in the shared memory segment with the STL-like allocator |
| 71 | //from a range of iterators |
| 72 | MyVector *myvector = |
| 73 | segment.construct<MyVector> |
| 74 | (name: "MyVector" )/*object name*/ |
| 75 | (begVal /*first ctor parameter*/, |
| 76 | endVal /*second ctor parameter*/, |
| 77 | alloc_inst /*third ctor parameter*/); |
| 78 | |
| 79 | //Use vector as your want |
| 80 | std::sort(first: myvector->rbegin(), last: myvector->rend()); |
| 81 | // . . . |
| 82 | //When done, destroy and delete vector from the segment |
| 83 | segment.destroy<MyVector>(name: "MyVector" ); |
| 84 | return 0; |
| 85 | } |
| 86 | //] |
| 87 | |
| 88 | |