]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/example/doc_managed_heap_memory.cpp
1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
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 BOOST_CATCH(const bad_alloc
&){
42 //Let's obtain the size of the list
43 MyList::size_type old_size
= mylist
->size();
48 //To make the list bigger, let's increase the heap buffer
50 heap_memory
.grow(1000);
52 //If memory has been reallocated, the old pointer is invalid, so
53 //use previously obtained handle to find the new pointer.
54 mylist
= static_cast<MyList
*>
55 (heap_memory
.get_address_from_handle(list_handle
));
57 //Fill list until there is no more memory in the buffer
60 mylist
->insert(mylist
->begin(), 0);
63 BOOST_CATCH(const bad_alloc
&){
67 //Let's obtain the new size of the list
68 MyList::size_type new_size
= mylist
->size();
73 assert(new_size
> old_size
);
76 heap_memory
.destroy_ptr(mylist
);