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