]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/test/managed_mapped_file_test.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / interprocess / test / managed_mapped_file_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 #if defined(BOOST_INTERPROCESS_MAPPED_FILES)
12
13 #include <boost/interprocess/detail/config_begin.hpp>
14 #include <boost/interprocess/allocators/allocator.hpp>
15 #include <boost/interprocess/containers/vector.hpp>
16 #include <boost/interprocess/managed_mapped_file.hpp>
17 #include <cstdio>
18 #include <string>
19 #include "get_process_id_name.hpp"
20
21 using namespace boost::interprocess;
22
23 inline std::string get_filename()
24 {
25 std::string ret (ipcdetail::get_temporary_path());
26 ret += "/";
27 ret += test::get_process_id_name();
28 return ret;
29 }
30
31 int main ()
32 {
33 const int FileSize = 65536*10;
34 std::string filename(get_filename());
35 const char *FileName = filename.c_str();
36
37 //STL compatible allocator object for memory-mapped file
38 typedef allocator<int, managed_mapped_file::segment_manager>
39 allocator_int_t;
40 //A vector that uses that allocator
41 typedef boost::interprocess::vector<int, allocator_int_t> MyVect;
42
43 {
44 //Remove the file it is already created
45 file_mapping::remove(FileName);
46
47 const int max = 100;
48 void *array[max];
49 //Named allocate capable shared memory allocator
50 managed_mapped_file mfile(create_only, FileName, FileSize);
51
52 int i;
53 //Let's allocate some memory
54 for(i = 0; i < max; ++i){
55 array[i] = mfile.allocate(i+1);
56 }
57
58 //Deallocate allocated memory
59 for(i = 0; i < max; ++i){
60 mfile.deallocate(array[i]);
61 }
62 }
63
64 {
65 //Remove the file it is already created
66 file_mapping::remove(FileName);
67
68 //Named allocate capable memory mapped file managed memory class
69 managed_mapped_file mfile(create_only, FileName, FileSize);
70
71 //Construct the STL-like allocator with the segment manager
72 const allocator_int_t myallocator (mfile.get_segment_manager());
73
74 //Construct vector
75 MyVect *mfile_vect = mfile.construct<MyVect> ("MyVector") (myallocator);
76
77 //Test that vector can be found via name
78 if(mfile_vect != mfile.find<MyVect>("MyVector").first)
79 return -1;
80
81 //Destroy and check it is not present
82 mfile.destroy<MyVect> ("MyVector");
83 if(0 != mfile.find<MyVect>("MyVector").first)
84 return -1;
85
86 //Construct a vector in the memory-mapped file
87 mfile_vect = mfile.construct<MyVect> ("MyVector") (myallocator);
88
89 //Flush cached data from memory-mapped file to disk
90 mfile.flush();
91 }
92 {
93 //Map preexisting file again in memory
94 managed_mapped_file mfile(open_only, FileName);
95
96 //Check vector is still there
97 MyVect *mfile_vect = mfile.find<MyVect>("MyVector").first;
98 if(!mfile_vect)
99 return -1;
100 }
101
102 {
103 {
104 //Map preexisting file again in copy-on-write
105 managed_mapped_file mfile(open_copy_on_write, FileName);
106
107 //Check vector is still there
108 MyVect *mfile_vect = mfile.find<MyVect>("MyVector").first;
109 if(!mfile_vect)
110 return -1;
111
112 //Erase vector
113 mfile.destroy_ptr(mfile_vect);
114
115 //Make sure vector is erased
116 mfile_vect = mfile.find<MyVect>("MyVector").first;
117 if(mfile_vect)
118 return -1;
119 }
120 //Now check vector is still in the file
121 {
122 //Map preexisting file again in copy-on-write
123 managed_mapped_file mfile(open_copy_on_write, FileName);
124
125 //Check vector is still there
126 MyVect *mfile_vect = mfile.find<MyVect>("MyVector").first;
127 if(!mfile_vect)
128 return -1;
129 }
130 }
131 {
132 //Map preexisting file again in copy-on-write
133 managed_mapped_file mfile(open_read_only, FileName);
134
135 //Check vector is still there
136 MyVect *mfile_vect = mfile.find<MyVect>("MyVector").first;
137 if(!mfile_vect)
138 return -1;
139 }
140 {
141 managed_mapped_file::size_type old_free_memory;
142 {
143 //Map preexisting file again in memory
144 managed_mapped_file mfile(open_only, FileName);
145 old_free_memory = mfile.get_free_memory();
146 }
147
148 //Now grow the file
149 managed_mapped_file::grow(FileName, FileSize);
150
151 //Map preexisting file again in memory
152 managed_mapped_file mfile(open_only, FileName);
153
154 //Check vector is still there
155 MyVect *mfile_vect = mfile.find<MyVect>("MyVector").first;
156 if(!mfile_vect)
157 return -1;
158
159 if(mfile.get_size() != (FileSize*2))
160 return -1;
161 if(mfile.get_free_memory() <= old_free_memory)
162 return -1;
163 }
164 {
165 managed_mapped_file::size_type old_free_memory, next_free_memory,
166 old_file_size, next_file_size, final_file_size;
167 {
168 //Map preexisting file again in memory
169 managed_mapped_file mfile(open_only, FileName);
170 old_free_memory = mfile.get_free_memory();
171 old_file_size = mfile.get_size();
172 }
173
174 //Now shrink the file
175 managed_mapped_file::shrink_to_fit(FileName);
176
177 {
178 //Map preexisting file again in memory
179 managed_mapped_file mfile(open_only, FileName);
180 next_file_size = mfile.get_size();
181
182 //Check vector is still there
183 MyVect *mfile_vect = mfile.find<MyVect>("MyVector").first;
184 if(!mfile_vect)
185 return -1;
186
187 next_free_memory = mfile.get_free_memory();
188 if(next_free_memory >= old_free_memory)
189 return -1;
190 if(old_file_size <= next_file_size)
191 return -1;
192 }
193
194 //Now destroy the vector
195 {
196 //Map preexisting file again in memory
197 managed_mapped_file mfile(open_only, FileName);
198
199 //Destroy and check it is not present
200 mfile.destroy<MyVect>("MyVector");
201 if(0 != mfile.find<MyVect>("MyVector").first)
202 return -1;
203 }
204
205 //Now shrink the file
206 managed_mapped_file::shrink_to_fit(FileName);
207 {
208 //Map preexisting file again in memory
209 managed_mapped_file mfile(open_only, FileName);
210 final_file_size = mfile.get_size();
211 if(next_file_size <= final_file_size)
212 return -1;
213 }
214 {
215 //Now test move semantics
216 managed_mapped_file original(open_only, FileName);
217 managed_mapped_file move_ctor(boost::move(original));
218 managed_mapped_file move_assign;
219 move_assign = boost::move(move_ctor);
220 move_assign.swap(original);
221 }
222 }
223
224 file_mapping::remove(FileName);
225 return 0;
226 }
227
228 #include <boost/interprocess/detail/config_end.hpp>
229
230 #else //#if defined(BOOST_INTERPROCESS_MAPPED_FILES)
231
232 int main()
233 {
234 return 0;
235 }
236
237 #endif//#if defined(BOOST_INTERPROCESS_MAPPED_FILES)