| 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 | #if defined(BOOST_INTERPROCESS_MAPPED_FILES) |
| 11 | |
| 12 | |
| 13 | //[doc_managed_multiple_allocation |
| 14 | #include <boost/interprocess/managed_shared_memory.hpp> |
| 15 | #include <boost/move/utility_core.hpp> //boost::move |
| 16 | #include <cassert>//assert |
| 17 | #include <cstring>//std::memset |
| 18 | #include <new> //std::nothrow |
| 19 | #include <vector> //std::vector |
| 20 | //<- |
| 21 | #include "../test/get_process_id_name.hpp" |
| 22 | //-> |
| 23 | |
| 24 | int main() |
| 25 | { |
| 26 | using namespace boost::interprocess; |
| 27 | typedef managed_shared_memory::multiallocation_chain multiallocation_chain; |
| 28 | |
| 29 | //Remove shared memory on construction and destruction |
| 30 | struct shm_remove |
| 31 | { |
| 32 | //<- |
| 33 | #if 1 |
| 34 | shm_remove() { shared_memory_object::remove(test::get_process_id_name()); } |
| 35 | ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); } |
| 36 | #else |
| 37 | //-> |
| 38 | shm_remove() { shared_memory_object::remove("MySharedMemory" ); } |
| 39 | ~shm_remove(){ shared_memory_object::remove("MySharedMemory" ); } |
| 40 | //<- |
| 41 | #endif |
| 42 | //-> |
| 43 | } remover; |
| 44 | //<- |
| 45 | (void)remover; |
| 46 | //-> |
| 47 | |
| 48 | //<- |
| 49 | #if 1 |
| 50 | managed_shared_memory managed_shm(create_only,test::get_process_id_name(), 65536); |
| 51 | #else |
| 52 | //-> |
| 53 | managed_shared_memory managed_shm(create_only,"MySharedMemory" , 65536); |
| 54 | //<- |
| 55 | #endif |
| 56 | //-> |
| 57 | |
| 58 | //Allocate 16 elements of 100 bytes in a single call. Non-throwing version. |
| 59 | multiallocation_chain chain; |
| 60 | managed_shm.allocate_many(std::nothrow, 100, 16, chain); |
| 61 | |
| 62 | //Check if the memory allocation was successful |
| 63 | if(chain.empty()) return 1; |
| 64 | |
| 65 | //Allocated buffers |
| 66 | std::vector<void*> allocated_buffers; |
| 67 | |
| 68 | //Initialize our data |
| 69 | while(!chain.empty()){ |
| 70 | void *buf = chain.pop_front(); |
| 71 | allocated_buffers.push_back(buf); |
| 72 | //The iterator must be incremented before overwriting memory |
| 73 | //because otherwise, the iterator is invalidated. |
| 74 | std::memset(buf, 0, 100); |
| 75 | } |
| 76 | |
| 77 | //Now deallocate |
| 78 | while(!allocated_buffers.empty()){ |
| 79 | managed_shm.deallocate(allocated_buffers.back()); |
| 80 | allocated_buffers.pop_back(); |
| 81 | } |
| 82 | |
| 83 | //Allocate 10 buffers of different sizes in a single call. Throwing version |
| 84 | managed_shared_memory::size_type sizes[10]; |
| 85 | for(std::size_t i = 0; i < 10; ++i) |
| 86 | sizes[i] = i*3; |
| 87 | |
| 88 | managed_shm.allocate_many(sizes, 10, 1, chain); |
| 89 | managed_shm.deallocate_many(chain); |
| 90 | return 0; |
| 91 | } |
| 92 | //] |
| 93 | |
| 94 | |
| 95 | #else //#if defined(BOOST_INTERPROCESS_MAPPED_FILES) |
| 96 | int main() |
| 97 | { |
| 98 | return 0; |
| 99 | } |
| 100 | #endif//#if defined(BOOST_INTERPROCESS_MAPPED_FILES) |
| 101 | |