|
| 1 | +////////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// (C) Copyright Ion Gaztanaga 2025-2025. 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/allocators/allocator.hpp> |
| 12 | +#include <boost/container/vector.hpp> |
| 13 | +#include <boost/interprocess/managed_heap_memory.hpp> |
| 14 | +#include <cstdio> |
| 15 | +#include <string> |
| 16 | +#include "get_process_id_name.hpp" |
| 17 | + |
| 18 | +using namespace boost::interprocess; |
| 19 | + |
| 20 | +template <class CharT> |
| 21 | +struct filename_traits; |
| 22 | + |
| 23 | +template <> |
| 24 | +struct filename_traits<char> |
| 25 | +{ |
| 26 | + |
| 27 | + static const char* get() |
| 28 | + { return filename.c_str(); } |
| 29 | + |
| 30 | + static std::string filename; |
| 31 | +}; |
| 32 | + |
| 33 | +std::string filename_traits<char>::filename = get_filename(); |
| 34 | + |
| 35 | + |
| 36 | +#ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES |
| 37 | + |
| 38 | +template <> |
| 39 | +struct filename_traits<wchar_t> |
| 40 | +{ |
| 41 | + |
| 42 | + static const wchar_t* get() |
| 43 | + { return filename.c_str(); } |
| 44 | + |
| 45 | + static std::wstring filename; |
| 46 | +}; |
| 47 | + |
| 48 | +std::wstring filename_traits<wchar_t>::filename = get_wfilename(); |
| 49 | + |
| 50 | +#endif //#ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES |
| 51 | + |
| 52 | +template<class CharT> |
| 53 | +int test_managed_heap_memory() |
| 54 | +{ |
| 55 | + const int MemSize = 65536*10; |
| 56 | + |
| 57 | + //STL compatible allocator object for heap |
| 58 | + typedef allocator<int, managed_heap_memory::segment_manager> |
| 59 | + allocator_int_t; |
| 60 | + //A vector that uses that allocator |
| 61 | + typedef boost::container::vector<int, allocator_int_t> MyVect; |
| 62 | + |
| 63 | + { |
| 64 | + const int max = 100; |
| 65 | + void *array[std::size_t(max)]; |
| 66 | + //Named allocate capable shared memory allocator |
| 67 | + managed_heap_memory mheap(MemSize); |
| 68 | + |
| 69 | + std::size_t i; |
| 70 | + //Let's allocate some memory |
| 71 | + for(i = 0; i < max; ++i){ |
| 72 | + array[std::ptrdiff_t(i)] = mheap.allocate(i+1u); |
| 73 | + } |
| 74 | + |
| 75 | + //Deallocate allocated memory |
| 76 | + for(i = 0; i < max; ++i){ |
| 77 | + mheap.deallocate(array[std::ptrdiff_t(i)]); |
| 78 | + } |
| 79 | + |
| 80 | + //Construct the STL-like allocator with the segment manager |
| 81 | + const allocator_int_t myallocator (mheap.get_segment_manager()); |
| 82 | + |
| 83 | + //Construct vector |
| 84 | + MyVect *mheap_vect = mheap.construct<MyVect> ("MyVector") (myallocator); |
| 85 | + |
| 86 | + //Test that vector can be found via name |
| 87 | + if(mheap_vect != mheap.find<MyVect>("MyVector").first) |
| 88 | + return -1; |
| 89 | + |
| 90 | + //Destroy and check it is not present |
| 91 | + mheap.destroy<MyVect> ("MyVector"); |
| 92 | + mheap_vect = 0; |
| 93 | + if(0 != mheap.find<MyVect>("MyVector").first) |
| 94 | + return -1; |
| 95 | + |
| 96 | + //Construct a vector in the heap |
| 97 | + mheap_vect = mheap.construct<MyVect> ("MyVector") (myallocator); |
| 98 | + |
| 99 | + managed_heap_memory::size_type old_free_memory = mheap.get_free_memory(); |
| 100 | + |
| 101 | + //Now grow the file |
| 102 | + mheap.grow(MemSize); |
| 103 | + |
| 104 | + //Check vector is still there |
| 105 | + mheap_vect = mheap.find<MyVect>("MyVector").first; |
| 106 | + if(!mheap_vect) |
| 107 | + return -1; |
| 108 | + |
| 109 | + if(mheap.get_size() != (MemSize*2)) |
| 110 | + return -1; |
| 111 | + if(mheap.get_free_memory() <= old_free_memory) |
| 112 | + return -1; |
| 113 | + |
| 114 | + //Destroy and check it is not present |
| 115 | + mheap.destroy_ptr(mheap_vect); |
| 116 | + mheap_vect = 0; |
| 117 | + if(0 != mheap.find<MyVect>("MyVector").first) |
| 118 | + return -1; |
| 119 | + |
| 120 | + { |
| 121 | + //Now test move semantics |
| 122 | + managed_heap_memory original(1024u); |
| 123 | + managed_heap_memory move_ctor(boost::move(original)); |
| 124 | + managed_heap_memory move_assign; |
| 125 | + move_assign = boost::move(move_ctor); |
| 126 | + move_assign.swap(original); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return 0; |
| 131 | +} |
| 132 | + |
| 133 | +int main () |
| 134 | +{ |
| 135 | + int r; |
| 136 | + r = test_managed_heap_memory<char>(); |
| 137 | + if(r) return r; |
| 138 | + #ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES |
| 139 | + r = test_managed_heap_memory<wchar_t>(); |
| 140 | + if(r) return r; |
| 141 | + #endif |
| 142 | + return 0; |
| 143 | +} |
| 144 | + |
| 145 | +/* |
| 146 | +////////////////////////////////////////////////////////////////////////////// |
| 147 | +// |
| 148 | +// (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost |
| 149 | +// Software License, Version 1.0. (See accompanying file |
| 150 | +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 151 | +// |
| 152 | +// See http://www.boost.org/libs/interprocess for documentation. |
| 153 | +// |
| 154 | +////////////////////////////////////////////////////////////////////////////// |
| 155 | +
|
| 156 | +#include <boost/interprocess/detail/workaround.hpp> |
| 157 | +//[doc_managed_heap_memory |
| 158 | +#include <boost/container/list.hpp> |
| 159 | +#include <boost/interprocess/managed_heap_memory.hpp> |
| 160 | +#include <boost/interprocess/allocators/allocator.hpp> |
| 161 | +#include <cstddef> |
| 162 | +
|
| 163 | +using namespace boost::interprocess; |
| 164 | +typedef boost::container::list<int, allocator<int, managed_heap_memory::segment_manager> > |
| 165 | + MyList; |
| 166 | +
|
| 167 | +int main () |
| 168 | +{ |
| 169 | + //We will create a buffer of 1000 bytes to store a list |
| 170 | + managed_heap_memory heap_memory(1000); |
| 171 | +
|
| 172 | + MyList * mylist = heap_memory.construct<MyList>("MyList") |
| 173 | + (heap_memory.get_segment_manager()); |
| 174 | +
|
| 175 | + //Obtain handle, that identifies the list in the buffer |
| 176 | + managed_heap_memory::handle_t list_handle = heap_memory.get_handle_from_address(mylist); |
| 177 | +
|
| 178 | + //Fill list until there is no more memory in the buffer |
| 179 | + BOOST_INTERPROCESS_TRY{ |
| 180 | + while(1) { |
| 181 | + mylist->insert(mylist->begin(), 0); |
| 182 | + } |
| 183 | + } |
| 184 | + BOOST_INTERPROCESS_CATCH(const bad_alloc &){ |
| 185 | + //memory is full |
| 186 | + } BOOST_INTERPROCESS_CATCH_END |
| 187 | + //Let's obtain the size of the list |
| 188 | + MyList::size_type old_size = mylist->size(); |
| 189 | + //<- |
| 190 | + (void)old_size; |
| 191 | + //-> |
| 192 | +
|
| 193 | + //To make the list bigger, let's increase the heap buffer |
| 194 | + //in 1000 bytes more. |
| 195 | + heap_memory.grow(1000); |
| 196 | +
|
| 197 | + //If memory has been reallocated, the old pointer is invalid, so |
| 198 | + //use previously obtained handle to find the new pointer. |
| 199 | + mylist = static_cast<MyList *> |
| 200 | + (heap_memory.get_address_from_handle(list_handle)); |
| 201 | +
|
| 202 | + //Fill list until there is no more memory in the buffer |
| 203 | + BOOST_INTERPROCESS_TRY{ |
| 204 | + while(1) { |
| 205 | + mylist->insert(mylist->begin(), 0); |
| 206 | + } |
| 207 | + } |
| 208 | + BOOST_INTERPROCESS_CATCH(const bad_alloc &){ |
| 209 | + //memory is full |
| 210 | + } BOOST_INTERPROCESS_CATCH_END |
| 211 | +
|
| 212 | + //Let's obtain the new size of the list |
| 213 | + MyList::size_type new_size = mylist->size(); |
| 214 | + //<- |
| 215 | + (void)new_size; |
| 216 | + //-> |
| 217 | +
|
| 218 | + assert(new_size > old_size); |
| 219 | +
|
| 220 | + //Destroy list |
| 221 | + heap_memory.destroy_ptr(mylist); |
| 222 | +
|
| 223 | + return 0; |
| 224 | +} |
| 225 | +//] |
| 226 | +*/ |
| 227 | + |
0 commit comments