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_MAPPED_FILE_HPP
12 #define BOOST_INTERPROCESS_MANAGED_MAPPED_FILE_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>
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>
38 namespace interprocess {
41 template<class AllocationAlgorithm>
42 struct mfile_open_or_create
44 typedef ipcdetail::managed_open_or_create_impl
45 < file_wrapper, AllocationAlgorithm::Alignment, true, false> type;
48 } //namespace ipcdetail {
50 //!A basic mapped file named object creation class. Initializes the
51 //!mapped file. Inherits all basic functionality from
52 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>
56 class AllocationAlgorithm,
57 template<class IndexConfig> class IndexType
59 class basic_managed_mapped_file
60 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
61 : public ipcdetail::basic_managed_memory_impl
62 <CharType, AllocationAlgorithm, IndexType
63 ,ipcdetail::mfile_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset>
64 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
66 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
68 typedef ipcdetail::basic_managed_memory_impl
69 <CharType, AllocationAlgorithm, IndexType,
70 ipcdetail::mfile_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset> base_t;
71 typedef ipcdetail::file_wrapper device_type;
75 typedef ipcdetail::create_open_func<base_t> create_open_func_t;
77 basic_managed_mapped_file *get_this_pointer()
81 typedef typename base_t::char_ptr_holder_t char_ptr_holder_t;
82 BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_mapped_file)
83 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
87 //!Unsigned integral type enough to represent
88 //!the size of a basic_managed_mapped_file.
89 typedef typename BOOST_INTERPROCESS_IMPDEF(base_t::size_type) size_type;
91 //!Creates mapped file and creates and places the segment manager.
93 basic_managed_mapped_file()
96 //!Creates mapped file and creates and places the segment manager.
98 basic_managed_mapped_file(create_only_t, const char *name,
99 size_type size, const void *addr = 0, const permissions &perm = permissions())
100 : m_mfile(create_only, name, size, read_write, addr,
101 create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
104 //!Creates mapped file and creates and places the segment manager if
105 //!segment was not created. If segment was created it connects to the
108 basic_managed_mapped_file (open_or_create_t,
109 const char *name, size_type size,
110 const void *addr = 0, const permissions &perm = permissions())
111 : m_mfile(open_or_create, name, size, read_write, addr,
112 create_open_func_t(get_this_pointer(),
113 ipcdetail::DoOpenOrCreate), perm)
116 //!Connects to a created mapped file and its segment manager.
118 basic_managed_mapped_file (open_only_t, const char* name,
119 const void *addr = 0)
120 : m_mfile(open_only, name, read_write, addr,
121 create_open_func_t(get_this_pointer(),
125 //!Connects to a created mapped file and its segment manager
126 //!in copy_on_write mode.
128 basic_managed_mapped_file (open_copy_on_write_t, const char* name,
129 const void *addr = 0)
130 : m_mfile(open_only, name, copy_on_write, addr,
131 create_open_func_t(get_this_pointer(),
135 //!Connects to a created mapped file and its segment manager
136 //!in read-only mode.
138 basic_managed_mapped_file (open_read_only_t, const char* name,
139 const void *addr = 0)
140 : m_mfile(open_only, name, read_only, addr,
141 create_open_func_t(get_this_pointer(),
145 //!Moves the ownership of "moved"'s managed memory to *this.
147 basic_managed_mapped_file(BOOST_RV_REF(basic_managed_mapped_file) moved)
152 //!Moves the ownership of "moved"'s managed memory to *this.
154 basic_managed_mapped_file &operator=(BOOST_RV_REF(basic_managed_mapped_file) moved)
156 basic_managed_mapped_file tmp(boost::move(moved));
161 //!Destroys *this and indicates that the calling process is finished using
162 //!the resource. The destructor function will deallocate
163 //!any system resources allocated by the system for use by this process for
164 //!this resource. The resource can still be opened again calling
165 //!the open constructor overload. To erase the resource from the system
167 ~basic_managed_mapped_file()
170 //!Swaps the ownership of the managed mapped memories managed by *this and other.
172 void swap(basic_managed_mapped_file &other)
175 m_mfile.swap(other.m_mfile);
178 //!Flushes cached data to file.
181 { return m_mfile.flush(); }
183 //!Tries to resize mapped file so that we have room for
186 //!This function is not synchronized so no other thread or process should
187 //!be reading or writing the file
188 static bool grow(const char *filename, size_type extra_bytes)
190 return base_t::template grow
191 <basic_managed_mapped_file>(filename, extra_bytes);
194 //!Tries to resize mapped file to minimized the size of the file.
196 //!This function is not synchronized so no other thread or process should
197 //!be reading or writing the file
198 static bool shrink_to_fit(const char *filename)
200 return base_t::template shrink_to_fit
201 <basic_managed_mapped_file>(filename);
204 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
206 //!Tries to find a previous named allocation address. Returns a memory
207 //!buffer and the object count. If not found returned pointer is 0.
210 std::pair<T*, size_type> find (char_ptr_holder_t name)
212 if(m_mfile.get_mapped_region().get_mode() == read_only){
213 return base_t::template find_no_lock<T>(name);
216 return base_t::template find<T>(name);
221 typename ipcdetail::mfile_open_or_create<AllocationAlgorithm>::type m_mfile;
222 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
225 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
227 //!Typedef for a default basic_managed_mapped_file
228 //!of narrow characters
229 typedef basic_managed_mapped_file
231 ,rbtree_best_fit<mutex_family>
235 //!Typedef for a default basic_managed_mapped_file
236 //!of wide characters
237 typedef basic_managed_mapped_file
239 ,rbtree_best_fit<mutex_family>
241 wmanaged_mapped_file;
243 #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
245 } //namespace interprocess {
246 } //namespace boost {
248 #include <boost/interprocess/detail/config_end.hpp>
250 #endif //BOOST_INTERPROCESS_MANAGED_MAPPED_FILE_HPP