| 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_offset_ptr |
| 13 | #include <boost/interprocess/managed_shared_memory.hpp> |
| 14 | #include <boost/interprocess/offset_ptr.hpp> |
| 15 | //<- |
| 16 | #include "../test/get_process_id_name.hpp" |
| 17 | //-> |
| 18 | |
| 19 | using namespace boost::interprocess; |
| 20 | |
| 21 | //Shared memory linked list node |
| 22 | struct list_node |
| 23 | { |
| 24 | offset_ptr<list_node> next; |
| 25 | int value; |
| 26 | }; |
| 27 | |
| 28 | int main () |
| 29 | { |
| 30 | //Remove shared memory on construction and destruction |
| 31 | struct shm_remove |
| 32 | { |
| 33 | //<- |
| 34 | #if 1 |
| 35 | shm_remove() { shared_memory_object::remove(filename: test::get_process_id_name()); } |
| 36 | ~shm_remove(){ shared_memory_object::remove(filename: test::get_process_id_name()); } |
| 37 | #else |
| 38 | //-> |
| 39 | shm_remove() { shared_memory_object::remove("MySharedMemory"); } |
| 40 | ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); } |
| 41 | //<- |
| 42 | #endif |
| 43 | //-> |
| 44 | } remover; |
| 45 | //<- |
| 46 | (void)remover; |
| 47 | //-> |
| 48 | |
| 49 | //Create shared memory |
| 50 | //<- |
| 51 | #if 1 |
| 52 | managed_shared_memory segment(create_only, |
| 53 | test::get_process_id_name(), //segment name |
| 54 | 65536); |
| 55 | #else |
| 56 | //-> |
| 57 | managed_shared_memory segment(create_only, |
| 58 | "MySharedMemory", //segment name |
| 59 | 65536); |
| 60 | //<- |
| 61 | #endif |
| 62 | //-> |
| 63 | |
| 64 | //Create linked list with 10 nodes in shared memory |
| 65 | offset_ptr<list_node> prev = 0, current, first; |
| 66 | |
| 67 | int i; |
| 68 | for(i = 0; i < 10; ++i, prev = current){ |
| 69 | current = static_cast<list_node*>(segment.allocate(nbytes: sizeof(list_node))); |
| 70 | current->value = i; |
| 71 | current->next = 0; |
| 72 | |
| 73 | if(!prev) |
| 74 | first = current; |
| 75 | else |
| 76 | prev->next = current; |
| 77 | } |
| 78 | |
| 79 | //Communicate list to other processes |
| 80 | //. . . |
| 81 | //When done, destroy list |
| 82 | for(current = first; current; /**/){ |
| 83 | prev = current; |
| 84 | current = current->next; |
| 85 | segment.deallocate(addr: prev.get()); |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | //] |
| 90 | |
| 91 |
