1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-2009. 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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
10 #include <boost/interprocess/detail/config_begin.hpp>
11 #include <boost/interprocess/detail/workaround.hpp>
12 //[doc_managed_heap_memory
13 #include <boost/interprocess/containers/list.hpp>
14 #include <boost/interprocess/managed_heap_memory.hpp>
15 #include <boost/interprocess/allocators/allocator.hpp>
18 using namespace boost::interprocess;
19 typedef list<int, allocator<int, managed_heap_memory::segment_manager> >
24 //We will create a buffer of 1000 bytes to store a list
25 managed_heap_memory heap_memory(1000);
27 MyList * mylist = heap_memory.construct<MyList>("MyList")
28 (heap_memory.get_segment_manager());
30 //Obtain handle, that identifies the list in the buffer
31 managed_heap_memory::handle_t list_handle = heap_memory.get_handle_from_address(mylist);
33 //Fill list until there is no more memory in the buffer
36 mylist->insert(mylist->begin(), 0);
39 catch(const bad_alloc &){
42 //Let's obtain the size of the list
43 std::size_t old_size = mylist->size();
45 //To make the list bigger, let's increase the heap buffer
47 heap_memory.grow(1000);
49 //If memory has been reallocated, the old pointer is invalid, so
50 //use previously obtained handle to find the new pointer.
51 mylist = static_cast<MyList *>
52 (heap_memory.get_address_from_handle(list_handle));
54 //Fill list until there is no more memory in the buffer
57 mylist->insert(mylist->begin(), 0);
60 catch(const bad_alloc &){
64 //Let's obtain the new size of the list
65 std::size_t new_size = mylist->size();
67 assert(new_size > old_size);
70 heap_memory.destroy_ptr(mylist);
75 #include <boost/interprocess/detail/config_end.hpp>