| 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_allocator |
| 13 | #include <boost/interprocess/managed_shared_memory.hpp> |
| 14 | #include <boost/interprocess/allocators/allocator.hpp> |
| 15 | #include <cassert> |
| 16 | //<- |
| 17 | #include "../test/get_process_id_name.hpp" |
| 18 | //-> |
| 19 | |
| 20 | using namespace boost::interprocess; |
| 21 | |
| 22 | int main () |
| 23 | { |
| 24 | //Remove shared memory on construction and destruction |
| 25 | struct shm_remove |
| 26 | { |
| 27 | //<- |
| 28 | #if 1 |
| 29 | shm_remove() { shared_memory_object::remove(filename: test::get_process_id_name()); } |
| 30 | ~shm_remove(){ shared_memory_object::remove(filename: test::get_process_id_name()); } |
| 31 | #else |
| 32 | //-> |
| 33 | shm_remove() { shared_memory_object::remove("MySharedMemory"); } |
| 34 | ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); } |
| 35 | //<- |
| 36 | #endif |
| 37 | //-> |
| 38 | } remover; |
| 39 | //<- |
| 40 | (void)remover; |
| 41 | //-> |
| 42 | |
| 43 | //Create shared memory |
| 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 | //Create an allocator that allocates ints from the managed segment |
| 57 | allocator<int, managed_shared_memory::segment_manager> |
| 58 | allocator_instance(segment.get_segment_manager()); |
| 59 | |
| 60 | //Copy constructed allocator is equal |
| 61 | allocator<int, managed_shared_memory::segment_manager> |
| 62 | allocator_instance2(allocator_instance); |
| 63 | assert(allocator_instance2 == allocator_instance); |
| 64 | |
| 65 | //Allocate and deallocate memory for 100 ints |
| 66 | allocator_instance2.deallocate(ptr: allocator_instance.allocate(count: 100), 100); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | //] |
| 71 | |
| 72 |
