]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/interprocess/example/doc_managed_allocation_command.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / interprocess / example / doc_managed_allocation_command.cpp
CommitLineData
7c673cae
FG
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#include <boost/interprocess/detail/config_begin.hpp>
11//[doc_managed_allocation_command
12#include <boost/interprocess/managed_shared_memory.hpp>
13#include <cassert>
14//<-
15#include "../test/get_process_id_name.hpp"
16//->
17
18int main()
19{
20 using namespace boost::interprocess;
21
22 //Remove shared memory on construction and destruction
23 struct shm_remove
24 {
25 //<-
26 #if 1
27 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
28 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
29 #else
30 //->
31 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
32 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
33 //<-
34 #endif
35 //->
36 } remover;
37 //<-
38 (void)remover;
39 //->
40
41 //Managed memory segment that allocates portions of a shared memory
42 //segment with the default management algorithm
43 //<-
44 #if 1
45 managed_shared_memory managed_shm(create_only, test::get_process_id_name(), 10000*sizeof(std::size_t));
46 #else
47 //->
48 managed_shared_memory managed_shm(create_only, "MySharedMemory", 10000*sizeof(std::size_t));
49 //<-
50 #endif
51 //->
52
53 //Allocate at least 100 bytes, 1000 bytes if possible
54 managed_shared_memory::size_type min_size = 100;
55 managed_shared_memory::size_type first_received_size = 1000;
56 std::size_t *hint = 0;
57 std::size_t *ptr = managed_shm.allocation_command<std::size_t>
58 (boost::interprocess::allocate_new, min_size, first_received_size, hint);
59
60 //Received size must be bigger than min_size
61 assert(first_received_size >= min_size);
62
63 //Get free memory
64 managed_shared_memory::size_type free_memory_after_allocation = managed_shm.get_free_memory();
65 //<-
66 (void)free_memory_after_allocation;
67 //->
68
69 //Now write the data
70 for(std::size_t i = 0; i < first_received_size; ++i) ptr[i] = i;
71
72 //Now try to triplicate the buffer. We won't admit an expansion
73 //lower to the double of the original buffer.
74 //This "should" be successful since no other class is allocating
75 //memory from the segment
76 min_size = first_received_size*2;
77 managed_shared_memory::size_type expanded_size = first_received_size*3;
78 std::size_t * ret = managed_shm.allocation_command
79 (boost::interprocess::expand_fwd, min_size, expanded_size, ptr);
80 //<-
81 (void)ret;
82 //->
83 //Check invariants
84 assert(ptr != 0);
85 assert(ret == ptr);
86 assert(expanded_size >= first_received_size*2);
87
88 //Get free memory and compare
89 managed_shared_memory::size_type free_memory_after_expansion = managed_shm.get_free_memory();
90 assert(free_memory_after_expansion < free_memory_after_allocation);
92f5a8d4
TL
91 //<-
92 (void)free_memory_after_expansion;
93 //->
7c673cae
FG
94
95 //Write new values
96 for(std::size_t i = first_received_size; i < expanded_size; ++i) ptr[i] = i;
97
98 //Try to shrink approximately to min_size, but the new size
99 //should be smaller than min_size*2.
100 //This "should" be successful since no other class is allocating
101 //memory from the segment
102 managed_shared_memory::size_type shrunk_size = min_size;
103 ret = managed_shm.allocation_command
104 (boost::interprocess::shrink_in_place, min_size*2, shrunk_size, ptr);
105
106 //Check invariants
107 assert(ptr != 0);
108 assert(ret == ptr);
109 assert(shrunk_size <= min_size*2);
110 assert(shrunk_size >= min_size);
111
112 //Get free memory and compare
113 managed_shared_memory::size_type free_memory_after_shrinking = managed_shm.get_free_memory();
114 assert(free_memory_after_shrinking > free_memory_after_expansion);
92f5a8d4
TL
115 //<-
116 (void)free_memory_after_shrinking;
117 //->
7c673cae
FG
118
119 //Deallocate the buffer
120 managed_shm.deallocate(ptr);
121 return 0;
122}
123//]
124#include <boost/interprocess/detail/config_end.hpp>