1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2004-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/allocators/allocator.hpp>
12 #include <boost/interprocess/containers/vector.hpp>
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <cstdio>
15 #include <string>
16 #include "get_process_id_name.hpp"
17
18 using namespace boost::interprocess;
19
main()20 int main ()
21 {
22 const int ShmemSize = 65536;
23 const char *const ShmemName = test::get_process_id_name();
24
25 //STL compatible allocator object for memory-mapped shmem
26 typedef allocator<int, managed_shared_memory::segment_manager>
27 allocator_int_t;
28 //A vector that uses that allocator
29 typedef boost::interprocess::vector<int, allocator_int_t> MyVect;
30
31 {
32 //Remove the shmem it is already created
33 shared_memory_object::remove(ShmemName);
34
35 const int max = 100;
36 void *array[max];
37 //Named allocate capable shared memory allocator
38 managed_shared_memory shmem(create_only, ShmemName, ShmemSize);
39
40 int i;
41 //Let's allocate some memory
42 for(i = 0; i < max; ++i){
43 array[i] = shmem.allocate(i+1);
44 }
45
46 //Deallocate allocated memory
47 for(i = 0; i < max; ++i){
48 shmem.deallocate(array[i]);
49 }
50 }
51
52 {
53 //Remove the shmem it is already created
54 shared_memory_object::remove(ShmemName);
55
56 //Named allocate capable memory mapped shmem managed memory class
57 managed_shared_memory shmem(create_only, ShmemName, ShmemSize);
58
59 //Construct the STL-like allocator with the segment manager
60 const allocator_int_t myallocator (shmem.get_segment_manager());
61
62 //Construct vector
63 MyVect *shmem_vect = shmem.construct<MyVect> ("MyVector") (myallocator);
64
65 //Test that vector can be found via name
66 if(shmem_vect != shmem.find<MyVect>("MyVector").first)
67 return -1;
68
69 //Destroy and check it is not present
70 shmem.destroy<MyVect> ("MyVector");
71 if(0 != shmem.find<MyVect>("MyVector").first)
72 return -1;
73
74 //Construct a vector in the memory-mapped shmem
75 shmem_vect = shmem.construct<MyVect> ("MyVector") (myallocator);
76 }
77 {
78 //Map preexisting shmem again in memory
79 managed_shared_memory shmem(open_only, ShmemName);
80
81 //Check vector is still there
82 MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
83 if(!shmem_vect)
84 return -1;
85 }
86 {
87 {
88 //Map preexisting shmem again in copy-on-write
89 managed_shared_memory shmem(open_copy_on_write, ShmemName);
90
91 //Check vector is still there
92 MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
93 if(!shmem_vect)
94 return -1;
95
96 //Erase vector
97 shmem.destroy_ptr(shmem_vect);
98
99 //Make sure vector is erased
100 shmem_vect = shmem.find<MyVect>("MyVector").first;
101 if(shmem_vect)
102 return -1;
103 }
104 //Now check vector is still in the shmem
105 {
106 //Map preexisting shmem again in copy-on-write
107 managed_shared_memory shmem(open_copy_on_write, ShmemName);
108
109 //Check vector is still there
110 MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
111 if(!shmem_vect)
112 return -1;
113 }
114 }
115 {
116 //Map preexisting shmem again in copy-on-write
117 managed_shared_memory shmem(open_read_only, ShmemName);
118
119 //Check vector is still there
120 MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
121 if(!shmem_vect)
122 return -1;
123 }
124 #ifndef BOOST_INTERPROCESS_POSIX_SHARED_MEMORY_OBJECTS_NO_GROW
125 {
126 managed_shared_memory::size_type old_free_memory;
127 {
128 //Map preexisting shmem again in memory
129 managed_shared_memory shmem(open_only, ShmemName);
130 old_free_memory = shmem.get_free_memory();
131 }
132
133 //Now grow the shmem
134 managed_shared_memory::grow(ShmemName, ShmemSize);
135
136 //Map preexisting shmem again in memory
137 managed_shared_memory shmem(open_only, ShmemName);
138
139 //Check vector is still there
140 MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
141 if(!shmem_vect)
142 return -1;
143
144 if(shmem.get_size() != (ShmemSize*2))
145 return -1;
146 if(shmem.get_free_memory() <= old_free_memory)
147 return -1;
148 }
149 {
150 managed_shared_memory::size_type old_free_memory, next_free_memory,
151 old_shmem_size, next_shmem_size, final_shmem_size;
152 {
153 //Map preexisting shmem again in memory
154 managed_shared_memory shmem(open_only, ShmemName);
155 old_free_memory = shmem.get_free_memory();
156 old_shmem_size = shmem.get_size();
157 }
158
159 //Now shrink the shmem
160 managed_shared_memory::shrink_to_fit(ShmemName);
161
162 {
163 //Map preexisting shmem again in memory
164 managed_shared_memory shmem(open_only, ShmemName);
165 next_shmem_size = shmem.get_size();
166
167 //Check vector is still there
168 MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
169 if(!shmem_vect)
170 return -1;
171
172 next_free_memory = shmem.get_free_memory();
173 if(next_free_memory >= old_free_memory)
174 return -1;
175 if(old_shmem_size <= next_shmem_size)
176 return -1;
177 }
178
179 //Now destroy the vector
180 {
181 //Map preexisting shmem again in memory
182 managed_shared_memory shmem(open_only, ShmemName);
183
184 //Destroy and check it is not present
185 shmem.destroy<MyVect>("MyVector");
186 if(0 != shmem.find<MyVect>("MyVector").first)
187 return -1;
188 }
189
190 //Now shrink the shmem
191 managed_shared_memory::shrink_to_fit(ShmemName);
192 {
193 //Map preexisting shmem again in memory
194 managed_shared_memory shmem(open_only, ShmemName);
195 final_shmem_size = shmem.get_size();
196 if(next_shmem_size <= final_shmem_size)
197 return -1;
198 }
199 }
200 #endif //ifndef BOOST_INTERPROCESS_POSIX_SHARED_MEMORY_OBJECTS_NO_GROW
201
202 {
203 //Now test move semantics
204 managed_shared_memory original(open_only, ShmemName);
205 managed_shared_memory move_ctor(boost::move(original));
206 managed_shared_memory move_assign;
207 move_assign = boost::move(move_ctor);
208 move_assign.swap(original);
209 }
210
211 shared_memory_object::remove(ShmemName);
212 return 0;
213 }
214