]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | ////////////////////////////////////////////////////////////////////////////// |
2 | // | |
3 | // (C) Copyright Ion Gaztanaga 2005-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 | #ifndef BOOST_INTERPROCESS_MANAGED_MAPPED_FILE_HPP | |
12 | #define BOOST_INTERPROCESS_MANAGED_MAPPED_FILE_HPP | |
13 | ||
14 | #ifndef BOOST_CONFIG_HPP | |
15 | # include <boost/config.hpp> | |
16 | #endif | |
17 | # | |
18 | #if defined(BOOST_HAS_PRAGMA_ONCE) | |
19 | # pragma once | |
20 | #endif | |
21 | ||
22 | #include <boost/interprocess/detail/config_begin.hpp> | |
23 | #include <boost/interprocess/detail/workaround.hpp> | |
24 | #include <boost/interprocess/detail/managed_open_or_create_impl.hpp> | |
25 | #include <boost/interprocess/detail/managed_memory_impl.hpp> | |
26 | #include <boost/interprocess/creation_tags.hpp> | |
27 | #include <boost/interprocess/detail/file_wrapper.hpp> | |
28 | #include <boost/move/utility_core.hpp> | |
29 | #include <boost/interprocess/file_mapping.hpp> | |
30 | #include <boost/interprocess/permissions.hpp> | |
31 | //These includes needed to fulfill default template parameters of | |
32 | //predeclarations in interprocess_fwd.hpp | |
33 | #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp> | |
34 | #include <boost/interprocess/sync/mutex_family.hpp> | |
35 | #include <boost/interprocess/indexes/iset_index.hpp> | |
36 | ||
37 | namespace boost { | |
38 | namespace interprocess { | |
39 | namespace ipcdetail { | |
40 | ||
1e59de90 TL |
41 | template |
42 | < | |
43 | class CharType, | |
44 | class AllocationAlgorithm, | |
45 | template<class IndexConfig> class IndexType | |
46 | > | |
7c673cae FG |
47 | struct mfile_open_or_create |
48 | { | |
1e59de90 TL |
49 | static const std::size_t segment_manager_alignment = boost::move_detail::alignment_of |
50 | < segment_manager | |
51 | < CharType | |
52 | , AllocationAlgorithm | |
53 | , IndexType> | |
54 | >::value; | |
55 | static const std::size_t final_segment_manager_alignment | |
56 | = segment_manager_alignment > AllocationAlgorithm::Alignment | |
57 | ? segment_manager_alignment : AllocationAlgorithm::Alignment; | |
58 | ||
7c673cae | 59 | typedef ipcdetail::managed_open_or_create_impl |
1e59de90 TL |
60 | < file_wrapper |
61 | , final_segment_manager_alignment | |
62 | , true | |
63 | , false> type; | |
7c673cae FG |
64 | }; |
65 | ||
66 | } //namespace ipcdetail { | |
67 | ||
68 | //!A basic mapped file named object creation class. Initializes the | |
69 | //!mapped file. Inherits all basic functionality from | |
70 | //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType> | |
71 | template | |
72 | < | |
73 | class CharType, | |
74 | class AllocationAlgorithm, | |
75 | template<class IndexConfig> class IndexType | |
76 | > | |
77 | class basic_managed_mapped_file | |
78 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
79 | : public ipcdetail::basic_managed_memory_impl | |
1e59de90 TL |
80 | < CharType, AllocationAlgorithm, IndexType |
81 | , ipcdetail::mfile_open_or_create | |
82 | <CharType, AllocationAlgorithm, IndexType>::type::ManagedOpenOrCreateUserOffset> | |
7c673cae FG |
83 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
84 | { | |
85 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
86 | public: | |
87 | typedef ipcdetail::basic_managed_memory_impl | |
88 | <CharType, AllocationAlgorithm, IndexType, | |
1e59de90 TL |
89 | ipcdetail::mfile_open_or_create<CharType, AllocationAlgorithm, IndexType> |
90 | ::type::ManagedOpenOrCreateUserOffset> base_t; | |
7c673cae FG |
91 | typedef ipcdetail::file_wrapper device_type; |
92 | ||
93 | private: | |
94 | ||
95 | typedef ipcdetail::create_open_func<base_t> create_open_func_t; | |
96 | ||
97 | basic_managed_mapped_file *get_this_pointer() | |
98 | { return this; } | |
99 | ||
100 | private: | |
101 | typedef typename base_t::char_ptr_holder_t char_ptr_holder_t; | |
102 | BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_mapped_file) | |
103 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
104 | ||
105 | public: //functions | |
106 | ||
107 | //!Unsigned integral type enough to represent | |
108 | //!the size of a basic_managed_mapped_file. | |
109 | typedef typename BOOST_INTERPROCESS_IMPDEF(base_t::size_type) size_type; | |
110 | ||
111 | //!Creates mapped file and creates and places the segment manager. | |
112 | //!This can throw. | |
1e59de90 | 113 | basic_managed_mapped_file() BOOST_NOEXCEPT |
7c673cae FG |
114 | {} |
115 | ||
116 | //!Creates mapped file and creates and places the segment manager. | |
117 | //!This can throw. | |
118 | basic_managed_mapped_file(create_only_t, const char *name, | |
119 | size_type size, const void *addr = 0, const permissions &perm = permissions()) | |
120 | : m_mfile(create_only, name, size, read_write, addr, | |
121 | create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm) | |
122 | {} | |
123 | ||
124 | //!Creates mapped file and creates and places the segment manager if | |
125 | //!segment was not created. If segment was created it connects to the | |
126 | //!segment. | |
127 | //!This can throw. | |
128 | basic_managed_mapped_file (open_or_create_t, | |
129 | const char *name, size_type size, | |
130 | const void *addr = 0, const permissions &perm = permissions()) | |
131 | : m_mfile(open_or_create, name, size, read_write, addr, | |
132 | create_open_func_t(get_this_pointer(), | |
133 | ipcdetail::DoOpenOrCreate), perm) | |
134 | {} | |
135 | ||
136 | //!Connects to a created mapped file and its segment manager. | |
137 | //!This can throw. | |
138 | basic_managed_mapped_file (open_only_t, const char* name, | |
139 | const void *addr = 0) | |
140 | : m_mfile(open_only, name, read_write, addr, | |
141 | create_open_func_t(get_this_pointer(), | |
142 | ipcdetail::DoOpen)) | |
143 | {} | |
144 | ||
145 | //!Connects to a created mapped file and its segment manager | |
146 | //!in copy_on_write mode. | |
147 | //!This can throw. | |
148 | basic_managed_mapped_file (open_copy_on_write_t, const char* name, | |
149 | const void *addr = 0) | |
150 | : m_mfile(open_only, name, copy_on_write, addr, | |
151 | create_open_func_t(get_this_pointer(), | |
152 | ipcdetail::DoOpen)) | |
153 | {} | |
154 | ||
155 | //!Connects to a created mapped file and its segment manager | |
156 | //!in read-only mode. | |
157 | //!This can throw. | |
158 | basic_managed_mapped_file (open_read_only_t, const char* name, | |
159 | const void *addr = 0) | |
160 | : m_mfile(open_only, name, read_only, addr, | |
161 | create_open_func_t(get_this_pointer(), | |
162 | ipcdetail::DoOpen)) | |
163 | {} | |
164 | ||
1e59de90 TL |
165 | #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
166 | ||
167 | //!Creates mapped file and creates and places the segment manager. | |
168 | //!This can throw. | |
169 | //! | |
170 | //!Note: This function is only available on operating systems with | |
171 | //! native wchar_t APIs (e.g. Windows). | |
172 | basic_managed_mapped_file(create_only_t, const wchar_t *name, | |
173 | size_type size, const void *addr = 0, const permissions &perm = permissions()) | |
174 | : m_mfile(create_only, name, size, read_write, addr, | |
175 | create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm) | |
176 | {} | |
177 | ||
178 | //!Creates mapped file and creates and places the segment manager if | |
179 | //!segment was not created. If segment was created it connects to the | |
180 | //!segment. | |
181 | //!This can throw. | |
182 | //! | |
183 | //!Note: This function is only available on operating systems with | |
184 | //! native wchar_t APIs (e.g. Windows). | |
185 | basic_managed_mapped_file (open_or_create_t, | |
186 | const wchar_t *name, size_type size, | |
187 | const void *addr = 0, const permissions &perm = permissions()) | |
188 | : m_mfile(open_or_create, name, size, read_write, addr, | |
189 | create_open_func_t(get_this_pointer(), | |
190 | ipcdetail::DoOpenOrCreate), perm) | |
191 | {} | |
192 | ||
193 | //!Connects to a created mapped file and its segment manager. | |
194 | //!This can throw. | |
195 | //! | |
196 | //!Note: This function is only available on operating systems with | |
197 | //! native wchar_t APIs (e.g. Windows). | |
198 | basic_managed_mapped_file (open_only_t, const wchar_t* name, | |
199 | const void *addr = 0) | |
200 | : m_mfile(open_only, name, read_write, addr, | |
201 | create_open_func_t(get_this_pointer(), | |
202 | ipcdetail::DoOpen)) | |
203 | {} | |
204 | ||
205 | //!Connects to a created mapped file and its segment manager | |
206 | //!in copy_on_write mode. | |
207 | //!This can throw. | |
208 | //! | |
209 | //!Note: This function is only available on operating systems with | |
210 | //! native wchar_t APIs (e.g. Windows). | |
211 | basic_managed_mapped_file (open_copy_on_write_t, const wchar_t* name, | |
212 | const void *addr = 0) | |
213 | : m_mfile(open_only, name, copy_on_write, addr, | |
214 | create_open_func_t(get_this_pointer(), | |
215 | ipcdetail::DoOpen)) | |
216 | {} | |
217 | ||
218 | //!Connects to a created mapped file and its segment manager | |
219 | //!in read-only mode. | |
220 | //!This can throw. | |
221 | //! | |
222 | //!Note: This function is only available on operating systems with | |
223 | //! native wchar_t APIs (e.g. Windows). | |
224 | basic_managed_mapped_file (open_read_only_t, const wchar_t* name, | |
225 | const void *addr = 0) | |
226 | : m_mfile(open_only, name, read_only, addr, | |
227 | create_open_func_t(get_this_pointer(), | |
228 | ipcdetail::DoOpen)) | |
229 | {} | |
230 | ||
231 | #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
232 | ||
7c673cae FG |
233 | //!Moves the ownership of "moved"'s managed memory to *this. |
234 | //!Does not throw | |
1e59de90 | 235 | basic_managed_mapped_file(BOOST_RV_REF(basic_managed_mapped_file) moved) BOOST_NOEXCEPT |
7c673cae FG |
236 | { |
237 | this->swap(moved); | |
238 | } | |
239 | ||
240 | //!Moves the ownership of "moved"'s managed memory to *this. | |
241 | //!Does not throw | |
1e59de90 | 242 | basic_managed_mapped_file &operator=(BOOST_RV_REF(basic_managed_mapped_file) moved) BOOST_NOEXCEPT |
7c673cae FG |
243 | { |
244 | basic_managed_mapped_file tmp(boost::move(moved)); | |
245 | this->swap(tmp); | |
246 | return *this; | |
247 | } | |
248 | ||
249 | //!Destroys *this and indicates that the calling process is finished using | |
250 | //!the resource. The destructor function will deallocate | |
251 | //!any system resources allocated by the system for use by this process for | |
252 | //!this resource. The resource can still be opened again calling | |
253 | //!the open constructor overload. To erase the resource from the system | |
254 | //!use remove(). | |
255 | ~basic_managed_mapped_file() | |
256 | {} | |
257 | ||
258 | //!Swaps the ownership of the managed mapped memories managed by *this and other. | |
259 | //!Never throws. | |
1e59de90 | 260 | void swap(basic_managed_mapped_file &other) BOOST_NOEXCEPT |
7c673cae FG |
261 | { |
262 | base_t::swap(other); | |
263 | m_mfile.swap(other.m_mfile); | |
264 | } | |
265 | ||
266 | //!Flushes cached data to file. | |
267 | //!Never throws | |
268 | bool flush() | |
269 | { return m_mfile.flush(); } | |
270 | ||
271 | //!Tries to resize mapped file so that we have room for | |
272 | //!more objects. | |
273 | //! | |
274 | //!This function is not synchronized so no other thread or process should | |
275 | //!be reading or writing the file | |
276 | static bool grow(const char *filename, size_type extra_bytes) | |
277 | { | |
278 | return base_t::template grow | |
279 | <basic_managed_mapped_file>(filename, extra_bytes); | |
280 | } | |
281 | ||
282 | //!Tries to resize mapped file to minimized the size of the file. | |
283 | //! | |
284 | //!This function is not synchronized so no other thread or process should | |
285 | //!be reading or writing the file | |
286 | static bool shrink_to_fit(const char *filename) | |
287 | { | |
288 | return base_t::template shrink_to_fit | |
289 | <basic_managed_mapped_file>(filename); | |
290 | } | |
291 | ||
1e59de90 TL |
292 | #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
293 | ||
294 | //!Tries to resize mapped file so that we have room for | |
295 | //!more objects. | |
296 | //! | |
297 | //!This function is not synchronized so no other thread or process should | |
298 | //!be reading or writing the file | |
299 | //! | |
300 | //!Note: This function is only available on operating systems with | |
301 | //! native wchar_t APIs (e.g. Windows). | |
302 | static bool grow(const wchar_t *filename, size_type extra_bytes) | |
303 | { | |
304 | return base_t::template grow | |
305 | <basic_managed_mapped_file>(filename, extra_bytes); | |
306 | } | |
307 | ||
308 | //!Tries to resize mapped file to minimized the size of the file. | |
309 | //! | |
310 | //!This function is not synchronized so no other thread or process should | |
311 | //!be reading or writing the file | |
312 | //! | |
313 | //!Note: This function is only available on operating systems with | |
314 | //! native wchar_t APIs (e.g. Windows). | |
315 | static bool shrink_to_fit(const wchar_t *filename) | |
316 | { | |
317 | return base_t::template shrink_to_fit | |
318 | <basic_managed_mapped_file>(filename); | |
319 | } | |
320 | ||
321 | #endif | |
322 | ||
7c673cae FG |
323 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
324 | ||
325 | //!Tries to find a previous named allocation address. Returns a memory | |
326 | //!buffer and the object count. If not found returned pointer is 0. | |
327 | //!Never throws. | |
328 | template <class T> | |
329 | std::pair<T*, size_type> find (char_ptr_holder_t name) | |
330 | { | |
331 | if(m_mfile.get_mapped_region().get_mode() == read_only){ | |
332 | return base_t::template find_no_lock<T>(name); | |
333 | } | |
334 | else{ | |
335 | return base_t::template find<T>(name); | |
336 | } | |
337 | } | |
338 | ||
339 | private: | |
1e59de90 TL |
340 | typename ipcdetail::mfile_open_or_create |
341 | <CharType, AllocationAlgorithm, IndexType>::type m_mfile; | |
7c673cae FG |
342 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
343 | }; | |
344 | ||
345 | #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
346 | ||
347 | //!Typedef for a default basic_managed_mapped_file | |
348 | //!of narrow characters | |
349 | typedef basic_managed_mapped_file | |
350 | <char | |
351 | ,rbtree_best_fit<mutex_family> | |
352 | ,iset_index> | |
353 | managed_mapped_file; | |
354 | ||
355 | //!Typedef for a default basic_managed_mapped_file | |
356 | //!of wide characters | |
357 | typedef basic_managed_mapped_file | |
358 | <wchar_t | |
359 | ,rbtree_best_fit<mutex_family> | |
360 | ,iset_index> | |
361 | wmanaged_mapped_file; | |
362 | ||
363 | #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
364 | ||
365 | } //namespace interprocess { | |
366 | } //namespace boost { | |
367 | ||
368 | #include <boost/interprocess/detail/config_end.hpp> | |
369 | ||
370 | #endif //BOOST_INTERPROCESS_MANAGED_MAPPED_FILE_HPP |