1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_MANAGED_SHARED_MEMORY_HPP
12 #define BOOST_INTERPROCESS_MANAGED_SHARED_MEMORY_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
25 #include <boost/interprocess/detail/managed_memory_impl.hpp>
26 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
27 #include <boost/interprocess/shared_memory_object.hpp>
28 #include <boost/interprocess/creation_tags.hpp>
29 #include <boost/interprocess/permissions.hpp>
30 //These includes needed to fulfill default template parameters of
31 //predeclarations in interprocess_fwd.hpp
32 #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
33 #include <boost/interprocess/sync/mutex_family.hpp>
36 namespace interprocess {
43 class AllocationAlgorithm,
44 template<class IndexConfig> class IndexType
46 struct shmem_open_or_create
48 static const std::size_t segment_manager_alignment = boost::move_detail::alignment_of
54 static const std::size_t final_segment_manager_alignment
55 = segment_manager_alignment > AllocationAlgorithm::Alignment
56 ? segment_manager_alignment : AllocationAlgorithm::Alignment;
58 typedef ipcdetail::managed_open_or_create_impl
59 < shared_memory_object
60 , final_segment_manager_alignment
65 } //namespace ipcdetail {
67 //!A basic shared memory named object creation class. Initializes the
68 //!shared memory segment. Inherits all basic functionality from
69 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>*/
73 class AllocationAlgorithm,
74 template<class IndexConfig> class IndexType
76 class basic_managed_shared_memory
77 : public ipcdetail::basic_managed_memory_impl
78 < CharType, AllocationAlgorithm, IndexType
79 , ipcdetail::shmem_open_or_create<CharType, AllocationAlgorithm, IndexType>::type::ManagedOpenOrCreateUserOffset>
80 , private ipcdetail::shmem_open_or_create<CharType, AllocationAlgorithm, IndexType>::type
82 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
83 typedef typename ipcdetail::shmem_open_or_create
86 , IndexType>::type base2_t;
87 typedef ipcdetail::basic_managed_memory_impl
88 <CharType, AllocationAlgorithm, IndexType,
89 base2_t::ManagedOpenOrCreateUserOffset> base_t;
91 typedef ipcdetail::create_open_func<base_t> create_open_func_t;
93 basic_managed_shared_memory *get_this_pointer()
97 typedef shared_memory_object device_type;
98 typedef typename base_t::size_type size_type;
101 typedef typename base_t::char_ptr_holder_t char_ptr_holder_t;
102 BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_shared_memory)
103 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
107 //!Destroys *this and indicates that the calling process is finished using
108 //!the resource. The destructor function will deallocate
109 //!any system resources allocated by the system for use by this process for
110 //!this resource. The resource can still be opened again calling
111 //!the open constructor overload. To erase the resource from the system
113 ~basic_managed_shared_memory()
116 //!Default constructor. Does nothing.
117 //!Useful in combination with move semantics
118 basic_managed_shared_memory()
121 //!Creates shared memory and creates and places the segment manager.
123 basic_managed_shared_memory(create_only_t, const char *name,
124 size_type size, const void *addr = 0, const permissions& perm = permissions())
126 , base2_t(create_only, name, size, read_write, addr,
127 create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
130 //!Creates shared memory and creates and places the segment manager if
131 //!segment was not created. If segment was created it connects to the
134 basic_managed_shared_memory (open_or_create_t,
135 const char *name, size_type size,
136 const void *addr = 0, const permissions& perm = permissions())
138 , base2_t(open_or_create, name, size, read_write, addr,
139 create_open_func_t(get_this_pointer(),
140 ipcdetail::DoOpenOrCreate), perm)
143 //!Connects to a created shared memory and its segment manager.
144 //!in copy_on_write mode.
146 basic_managed_shared_memory (open_copy_on_write_t, const char* name,
147 const void *addr = 0)
149 , base2_t(open_only, name, copy_on_write, addr,
150 create_open_func_t(get_this_pointer(),
154 //!Connects to a created shared memory and its segment manager.
155 //!in read-only mode.
157 basic_managed_shared_memory (open_read_only_t, const char* name,
158 const void *addr = 0)
160 , base2_t(open_only, name, read_only, addr,
161 create_open_func_t(get_this_pointer(),
165 //!Connects to a created shared memory and its segment manager.
167 basic_managed_shared_memory (open_only_t, const char* name,
168 const void *addr = 0)
170 , base2_t(open_only, name, read_write, addr,
171 create_open_func_t(get_this_pointer(),
175 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
177 //!Creates shared memory and creates and places the segment manager.
180 //!Note: This function is only available on operating systems with
181 //! native wchar_t APIs (e.g. Windows).
182 basic_managed_shared_memory(create_only_t, const wchar_t *name,
183 size_type size, const void *addr = 0, const permissions& perm = permissions())
185 , base2_t(create_only, name, size, read_write, addr,
186 create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
189 //!Creates shared memory and creates and places the segment manager if
190 //!segment was not created. If segment was created it connects to the
194 //!Note: This function is only available on operating systems with
195 //! native wchar_t APIs (e.g. Windows).
196 basic_managed_shared_memory (open_or_create_t,
197 const wchar_t *name, size_type size,
198 const void *addr = 0, const permissions& perm = permissions())
200 , base2_t(open_or_create, name, size, read_write, addr,
201 create_open_func_t(get_this_pointer(),
202 ipcdetail::DoOpenOrCreate), perm)
205 //!Connects to a created shared memory and its segment manager.
206 //!in copy_on_write mode.
209 //!Note: This function is only available on operating systems with
210 //! native wchar_t APIs (e.g. Windows).
211 basic_managed_shared_memory (open_copy_on_write_t, const wchar_t* name,
212 const void *addr = 0)
214 , base2_t(open_only, name, copy_on_write, addr,
215 create_open_func_t(get_this_pointer(),
219 //!Connects to a created shared memory and its segment manager.
220 //!in read-only mode.
223 //!Note: This function is only available on operating systems with
224 //! native wchar_t APIs (e.g. Windows).
225 basic_managed_shared_memory (open_read_only_t, const wchar_t* name,
226 const void *addr = 0)
228 , base2_t(open_only, name, read_only, addr,
229 create_open_func_t(get_this_pointer(),
233 //!Connects to a created shared memory and its segment manager.
236 //!Note: This function is only available on operating systems with
237 //! native wchar_t APIs (e.g. Windows).
238 basic_managed_shared_memory (open_only_t, const wchar_t* name,
239 const void *addr = 0)
241 , base2_t(open_only, name, read_write, addr,
242 create_open_func_t(get_this_pointer(),
246 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
248 //!Moves the ownership of "moved"'s managed memory to *this.
250 basic_managed_shared_memory(BOOST_RV_REF(basic_managed_shared_memory) moved)
252 basic_managed_shared_memory tmp;
257 //!Moves the ownership of "moved"'s managed memory to *this.
259 basic_managed_shared_memory &operator=(BOOST_RV_REF(basic_managed_shared_memory) moved)
261 basic_managed_shared_memory tmp(boost::move(moved));
266 //!Swaps the ownership of the managed shared memories managed by *this and other.
268 void swap(basic_managed_shared_memory &other)
271 base2_t::swap(other);
274 //!Tries to resize the managed shared memory object so that we have
275 //!room for more objects.
277 //!This function is not synchronized so no other thread or process should
278 //!be reading or writing the file
279 static bool grow(const char *shmname, size_type extra_bytes)
281 return base_t::template grow
282 <basic_managed_shared_memory>(shmname, extra_bytes);
285 //!Tries to resize the managed shared memory to minimized the size of the file.
287 //!This function is not synchronized so no other thread or process should
288 //!be reading or writing the file
289 static bool shrink_to_fit(const char *shmname)
291 return base_t::template shrink_to_fit
292 <basic_managed_shared_memory>(shmname);
295 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
297 //!Tries to resize the managed shared memory object so that we have
298 //!room for more objects.
300 //!This function is not synchronized so no other thread or process should
301 //!be reading or writing the file
303 //!Note: This function is only available on operating systems with
304 //! native wchar_t APIs (e.g. Windows).
305 static bool grow(const wchar_t *shmname, size_type extra_bytes)
307 return base_t::template grow
308 <basic_managed_shared_memory>(shmname, extra_bytes);
311 //!Tries to resize the managed shared memory to minimized the size of the file.
313 //!This function is not synchronized so no other thread or process should
314 //!be reading or writing the file
316 //!Note: This function is only available on operating systems with
317 //! native wchar_t APIs (e.g. Windows).
318 static bool shrink_to_fit(const wchar_t *shmname)
320 return base_t::template shrink_to_fit
321 <basic_managed_shared_memory>(shmname);
324 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
326 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
328 //!Tries to find a previous named allocation address. Returns a memory
329 //!buffer and the object count. If not found returned pointer is 0.
332 std::pair<T*, size_type> find (char_ptr_holder_t name)
334 if(base2_t::get_mapped_region().get_mode() == read_only){
335 return base_t::template find_no_lock<T>(name);
338 return base_t::template find<T>(name);
342 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
345 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
347 //!Typedef for a default basic_managed_shared_memory
348 //!of narrow characters
349 typedef basic_managed_shared_memory
351 ,rbtree_best_fit<mutex_family>
353 managed_shared_memory;
355 //!Typedef for a default basic_managed_shared_memory
356 //!of wide characters
357 typedef basic_managed_shared_memory
359 ,rbtree_best_fit<mutex_family>
361 wmanaged_shared_memory;
363 //!Typedef for a default basic_managed_shared_memory
364 //!of narrow characters to be placed in a fixed address
365 typedef basic_managed_shared_memory
367 ,rbtree_best_fit<mutex_family, void*>
369 fixed_managed_shared_memory;
371 //!Typedef for a default basic_managed_shared_memory
372 //!of narrow characters to be placed in a fixed address
373 typedef basic_managed_shared_memory
375 ,rbtree_best_fit<mutex_family, void*>
377 wfixed_managed_shared_memory;
380 #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
382 } //namespace interprocess {
383 } //namespace boost {
385 #include <boost/interprocess/detail/config_end.hpp>
387 #endif //BOOST_INTERPROCESS_MANAGED_SHARED_MEMORY_HPP