• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_grow
12 #include <boost/interprocess/managed_shared_memory.hpp>
13 #include <boost/interprocess/managed_mapped_file.hpp>
14 #include <cassert>
15 //<-
16 #include "../test/get_process_id_name.hpp"
17 //->
18 
19 class MyClass
20 {
21    //...
22 };
23 
main()24 int main()
25 {
26    using namespace boost::interprocess;
27    //Remove shared memory on construction and destruction
28    struct shm_remove
29    {
30    //<-
31    #if 1
32       shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
33       ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
34    #else
35    //->
36       shm_remove() { shared_memory_object::remove("MySharedMemory"); }
37       ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
38    //<-
39    #endif
40    //->
41    } remover;
42    //<-
43    (void)remover;
44    //->
45 
46    {
47       //Create a managed shared memory
48       //<-
49       #if 1
50       managed_shared_memory shm(create_only, test::get_process_id_name(), 1000);
51       #else
52       //->
53       managed_shared_memory shm(create_only, "MySharedMemory", 1000);
54       //<-
55       #endif
56       //->
57 
58       //Check size
59       assert(shm.get_size() == 1000);
60       //Construct a named object
61       MyClass *myclass = shm.construct<MyClass>("MyClass")();
62       //The managed segment is unmapped here
63       //<-
64       (void)myclass;
65       //->
66    }
67    {
68       //Now that the segment is not mapped grow it adding extra 500 bytes
69       //<-
70       #if 1
71       managed_shared_memory::grow(test::get_process_id_name(), 500);
72       #else
73       //->
74       managed_shared_memory::grow("MySharedMemory", 500);
75       //<-
76       #endif
77       //->
78 
79       //Map it again
80       //<-
81       #if 1
82       managed_shared_memory shm(open_only, test::get_process_id_name());
83       #else
84       //->
85       managed_shared_memory shm(open_only, "MySharedMemory");
86       //<-
87       #endif
88       //->
89       //Check size
90       assert(shm.get_size() == 1500);
91       //Check "MyClass" is still there
92       MyClass *myclass = shm.find<MyClass>("MyClass").first;
93       assert(myclass != 0);
94       //<-
95       (void)myclass;
96       //->
97       //The managed segment is unmapped here
98    }
99    {
100       //Now minimize the size of the segment
101       //<-
102       #if 1
103       managed_shared_memory::shrink_to_fit(test::get_process_id_name());
104       #else
105       //->
106       managed_shared_memory::shrink_to_fit("MySharedMemory");
107       //<-
108       #endif
109       //->
110 
111       //Map it again
112       //<-
113       #if 1
114       managed_shared_memory shm(open_only, test::get_process_id_name());
115       #else
116       //->
117       managed_shared_memory shm(open_only, "MySharedMemory");
118       //<-
119       #endif
120       //->
121       //Check size
122       assert(shm.get_size() < 1000);
123       //Check "MyClass" is still there
124       MyClass *myclass = shm.find<MyClass>("MyClass").first;
125       assert(myclass != 0);
126       //The managed segment is unmapped here
127       //<-
128       (void)myclass;
129       //->
130    }
131    return 0;
132 }
133 //]
134 #include <boost/interprocess/detail/config_end.hpp>
135