1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #include <boost/interprocess/detail/config_begin.hpp>
12 #include <boost/interprocess/allocators/allocator.hpp>
13 #include <boost/interprocess/containers/vector.hpp>
14 #include <boost/interprocess/managed_shared_memory.hpp>
17 #include "get_process_id_name.hpp"
19 using namespace boost::interprocess;
23 const int ShmemSize = 65536;
24 const char *const ShmemName = test::get_process_id_name();
26 //STL compatible allocator object for memory-mapped shmem
27 typedef allocator<int, managed_shared_memory::segment_manager>
29 //A vector that uses that allocator
30 typedef boost::interprocess::vector<int, allocator_int_t> MyVect;
33 //Remove the shmem it is already created
34 shared_memory_object::remove(ShmemName);
38 //Named allocate capable shared memory allocator
39 managed_shared_memory shmem(create_only, ShmemName, ShmemSize);
42 //Let's allocate some memory
43 for(i = 0; i < max; ++i){
44 array[i] = shmem.allocate(i+1);
47 //Deallocate allocated memory
48 for(i = 0; i < max; ++i){
49 shmem.deallocate(array[i]);
54 //Remove the shmem it is already created
55 shared_memory_object::remove(ShmemName);
57 //Named allocate capable memory mapped shmem managed memory class
58 managed_shared_memory shmem(create_only, ShmemName, ShmemSize);
60 //Construct the STL-like allocator with the segment manager
61 const allocator_int_t myallocator (shmem.get_segment_manager());
64 MyVect *shmem_vect = shmem.construct<MyVect> ("MyVector") (myallocator);
66 //Test that vector can be found via name
67 if(shmem_vect != shmem.find<MyVect>("MyVector").first)
70 //Destroy and check it is not present
71 shmem.destroy<MyVect> ("MyVector");
72 if(0 != shmem.find<MyVect>("MyVector").first)
75 //Construct a vector in the memory-mapped shmem
76 shmem_vect = shmem.construct<MyVect> ("MyVector") (myallocator);
79 //Map preexisting shmem again in memory
80 managed_shared_memory shmem(open_only, ShmemName);
82 //Check vector is still there
83 MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
89 //Map preexisting shmem again in copy-on-write
90 managed_shared_memory shmem(open_copy_on_write, ShmemName);
92 //Check vector is still there
93 MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
98 shmem.destroy_ptr(shmem_vect);
100 //Make sure vector is erased
101 shmem_vect = shmem.find<MyVect>("MyVector").first;
105 //Now check vector is still in the shmem
107 //Map preexisting shmem again in copy-on-write
108 managed_shared_memory shmem(open_copy_on_write, ShmemName);
110 //Check vector is still there
111 MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
117 //Map preexisting shmem again in copy-on-write
118 managed_shared_memory shmem(open_read_only, ShmemName);
120 //Check vector is still there
121 MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
125 #ifndef BOOST_INTERPROCESS_POSIX_SHARED_MEMORY_OBJECTS_NO_GROW
127 managed_shared_memory::size_type old_free_memory;
129 //Map preexisting shmem again in memory
130 managed_shared_memory shmem(open_only, ShmemName);
131 old_free_memory = shmem.get_free_memory();
135 managed_shared_memory::grow(ShmemName, ShmemSize);
137 //Map preexisting shmem again in memory
138 managed_shared_memory shmem(open_only, ShmemName);
140 //Check vector is still there
141 MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
145 if(shmem.get_size() != (ShmemSize*2))
147 if(shmem.get_free_memory() <= old_free_memory)
151 managed_shared_memory::size_type old_free_memory, next_free_memory,
152 old_shmem_size, next_shmem_size, final_shmem_size;
154 //Map preexisting shmem again in memory
155 managed_shared_memory shmem(open_only, ShmemName);
156 old_free_memory = shmem.get_free_memory();
157 old_shmem_size = shmem.get_size();
160 //Now shrink the shmem
161 managed_shared_memory::shrink_to_fit(ShmemName);
164 //Map preexisting shmem again in memory
165 managed_shared_memory shmem(open_only, ShmemName);
166 next_shmem_size = shmem.get_size();
168 //Check vector is still there
169 MyVect *shmem_vect = shmem.find<MyVect>("MyVector").first;
173 next_free_memory = shmem.get_free_memory();
174 if(next_free_memory >= old_free_memory)
176 if(old_shmem_size <= next_shmem_size)
180 //Now destroy the vector
182 //Map preexisting shmem again in memory
183 managed_shared_memory shmem(open_only, ShmemName);
185 //Destroy and check it is not present
186 shmem.destroy<MyVect>("MyVector");
187 if(0 != shmem.find<MyVect>("MyVector").first)
191 //Now shrink the shmem
192 managed_shared_memory::shrink_to_fit(ShmemName);
194 //Map preexisting shmem again in memory
195 managed_shared_memory shmem(open_only, ShmemName);
196 final_shmem_size = shmem.get_size();
197 if(next_shmem_size <= final_shmem_size)
201 #endif //ifndef BOOST_INTERPROCESS_POSIX_SHARED_MEMORY_OBJECTS_NO_GROW
204 //Now test move semantics
205 managed_shared_memory original(open_only, ShmemName);
206 managed_shared_memory move_ctor(boost::move(original));
207 managed_shared_memory move_assign;
208 move_assign = boost::move(move_ctor);
209 move_assign.swap(original);
212 shared_memory_object::remove(ShmemName);
216 #include <boost/interprocess/detail/config_end.hpp>