]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/example/doc_managed_heap_memory.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / interprocess / example / doc_managed_heap_memory.cpp
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_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>
16 #include <cstddef>
17
18 using namespace boost::interprocess;
19 typedef list<int, allocator<int, managed_heap_memory::segment_manager> >
20 MyList;
21
22 int main ()
23 {
24 //We will create a buffer of 1000 bytes to store a list
25 managed_heap_memory heap_memory(1000);
26
27 MyList * mylist = heap_memory.construct<MyList>("MyList")
28 (heap_memory.get_segment_manager());
29
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);
32
33 //Fill list until there is no more memory in the buffer
34 BOOST_TRY{
35 while(1) {
36 mylist->insert(mylist->begin(), 0);
37 }
38 }
39 BOOST_CATCH(const bad_alloc &){
40 //memory is full
41 } BOOST_CATCH_END
42 //Let's obtain the size of the list
43 MyList::size_type old_size = mylist->size();
44 //<-
45 (void)old_size;
46 //->
47
48 //To make the list bigger, let's increase the heap buffer
49 //in 1000 bytes more.
50 heap_memory.grow(1000);
51
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));
56
57 //Fill list until there is no more memory in the buffer
58 BOOST_TRY{
59 while(1) {
60 mylist->insert(mylist->begin(), 0);
61 }
62 }
63 BOOST_CATCH(const bad_alloc &){
64 //memory is full
65 } BOOST_CATCH_END
66
67 //Let's obtain the new size of the list
68 MyList::size_type new_size = mylist->size();
69 //<-
70 (void)new_size;
71 //->
72
73 assert(new_size > old_size);
74
75 //Destroy list
76 heap_memory.destroy_ptr(mylist);
77
78 return 0;
79 }
80 //]
81