Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
|
Chris@16
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 //
|
Chris@16
|
7 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@16
|
8 //
|
Chris@16
|
9 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_INTERPROCESS_MANAGED_MAPPED_FILE_HPP
|
Chris@16
|
12 #define BOOST_INTERPROCESS_MANAGED_MAPPED_FILE_HPP
|
Chris@16
|
13
|
Chris@101
|
14 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
15 # include <boost/config.hpp>
|
Chris@101
|
16 #endif
|
Chris@101
|
17 #
|
Chris@101
|
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@16
|
19 # pragma once
|
Chris@16
|
20 #endif
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
23 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
24 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
|
Chris@16
|
25 #include <boost/interprocess/detail/managed_memory_impl.hpp>
|
Chris@16
|
26 #include <boost/interprocess/creation_tags.hpp>
|
Chris@16
|
27 #include <boost/interprocess/detail/file_wrapper.hpp>
|
Chris@101
|
28 #include <boost/move/utility_core.hpp>
|
Chris@16
|
29 #include <boost/interprocess/file_mapping.hpp>
|
Chris@16
|
30 #include <boost/interprocess/permissions.hpp>
|
Chris@16
|
31 //These includes needed to fulfill default template parameters of
|
Chris@16
|
32 //predeclarations in interprocess_fwd.hpp
|
Chris@16
|
33 #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
|
Chris@16
|
34 #include <boost/interprocess/sync/mutex_family.hpp>
|
Chris@16
|
35 #include <boost/interprocess/indexes/iset_index.hpp>
|
Chris@16
|
36
|
Chris@16
|
37 namespace boost {
|
Chris@16
|
38 namespace interprocess {
|
Chris@16
|
39 namespace ipcdetail {
|
Chris@16
|
40
|
Chris@16
|
41 template<class AllocationAlgorithm>
|
Chris@16
|
42 struct mfile_open_or_create
|
Chris@16
|
43 {
|
Chris@16
|
44 typedef ipcdetail::managed_open_or_create_impl
|
Chris@16
|
45 < file_wrapper, AllocationAlgorithm::Alignment, true, false> type;
|
Chris@16
|
46 };
|
Chris@16
|
47
|
Chris@16
|
48 } //namespace ipcdetail {
|
Chris@16
|
49
|
Chris@16
|
50 //!A basic mapped file named object creation class. Initializes the
|
Chris@16
|
51 //!mapped file. Inherits all basic functionality from
|
Chris@16
|
52 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>
|
Chris@16
|
53 template
|
Chris@16
|
54 <
|
Chris@16
|
55 class CharType,
|
Chris@16
|
56 class AllocationAlgorithm,
|
Chris@16
|
57 template<class IndexConfig> class IndexType
|
Chris@16
|
58 >
|
Chris@16
|
59 class basic_managed_mapped_file
|
Chris@101
|
60 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
61 : public ipcdetail::basic_managed_memory_impl
|
Chris@16
|
62 <CharType, AllocationAlgorithm, IndexType
|
Chris@16
|
63 ,ipcdetail::mfile_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset>
|
Chris@101
|
64 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
65 {
|
Chris@101
|
66 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
67 public:
|
Chris@16
|
68 typedef ipcdetail::basic_managed_memory_impl
|
Chris@16
|
69 <CharType, AllocationAlgorithm, IndexType,
|
Chris@16
|
70 ipcdetail::mfile_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset> base_t;
|
Chris@16
|
71 typedef ipcdetail::file_wrapper device_type;
|
Chris@16
|
72
|
Chris@16
|
73 private:
|
Chris@16
|
74
|
Chris@16
|
75 typedef ipcdetail::create_open_func<base_t> create_open_func_t;
|
Chris@16
|
76
|
Chris@16
|
77 basic_managed_mapped_file *get_this_pointer()
|
Chris@16
|
78 { return this; }
|
Chris@16
|
79
|
Chris@16
|
80 private:
|
Chris@16
|
81 typedef typename base_t::char_ptr_holder_t char_ptr_holder_t;
|
Chris@16
|
82 BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_mapped_file)
|
Chris@101
|
83 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
84
|
Chris@16
|
85 public: //functions
|
Chris@16
|
86
|
Chris@101
|
87 //!Unsigned integral type enough to represent
|
Chris@101
|
88 //!the size of a basic_managed_mapped_file.
|
Chris@101
|
89 typedef typename BOOST_INTERPROCESS_IMPDEF(base_t::size_type) size_type;
|
Chris@101
|
90
|
Chris@16
|
91 //!Creates mapped file and creates and places the segment manager.
|
Chris@16
|
92 //!This can throw.
|
Chris@16
|
93 basic_managed_mapped_file()
|
Chris@16
|
94 {}
|
Chris@16
|
95
|
Chris@16
|
96 //!Creates mapped file and creates and places the segment manager.
|
Chris@16
|
97 //!This can throw.
|
Chris@16
|
98 basic_managed_mapped_file(create_only_t, const char *name,
|
Chris@16
|
99 size_type size, const void *addr = 0, const permissions &perm = permissions())
|
Chris@16
|
100 : m_mfile(create_only, name, size, read_write, addr,
|
Chris@16
|
101 create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
|
Chris@16
|
102 {}
|
Chris@16
|
103
|
Chris@16
|
104 //!Creates mapped file and creates and places the segment manager if
|
Chris@16
|
105 //!segment was not created. If segment was created it connects to the
|
Chris@16
|
106 //!segment.
|
Chris@16
|
107 //!This can throw.
|
Chris@16
|
108 basic_managed_mapped_file (open_or_create_t,
|
Chris@16
|
109 const char *name, size_type size,
|
Chris@16
|
110 const void *addr = 0, const permissions &perm = permissions())
|
Chris@16
|
111 : m_mfile(open_or_create, name, size, read_write, addr,
|
Chris@16
|
112 create_open_func_t(get_this_pointer(),
|
Chris@16
|
113 ipcdetail::DoOpenOrCreate), perm)
|
Chris@16
|
114 {}
|
Chris@16
|
115
|
Chris@16
|
116 //!Connects to a created mapped file and its segment manager.
|
Chris@16
|
117 //!This can throw.
|
Chris@16
|
118 basic_managed_mapped_file (open_only_t, const char* name,
|
Chris@16
|
119 const void *addr = 0)
|
Chris@16
|
120 : m_mfile(open_only, name, read_write, addr,
|
Chris@16
|
121 create_open_func_t(get_this_pointer(),
|
Chris@16
|
122 ipcdetail::DoOpen))
|
Chris@16
|
123 {}
|
Chris@16
|
124
|
Chris@16
|
125 //!Connects to a created mapped file and its segment manager
|
Chris@16
|
126 //!in copy_on_write mode.
|
Chris@16
|
127 //!This can throw.
|
Chris@16
|
128 basic_managed_mapped_file (open_copy_on_write_t, const char* name,
|
Chris@16
|
129 const void *addr = 0)
|
Chris@16
|
130 : m_mfile(open_only, name, copy_on_write, addr,
|
Chris@16
|
131 create_open_func_t(get_this_pointer(),
|
Chris@16
|
132 ipcdetail::DoOpen))
|
Chris@16
|
133 {}
|
Chris@16
|
134
|
Chris@16
|
135 //!Connects to a created mapped file and its segment manager
|
Chris@16
|
136 //!in read-only mode.
|
Chris@16
|
137 //!This can throw.
|
Chris@16
|
138 basic_managed_mapped_file (open_read_only_t, const char* name,
|
Chris@16
|
139 const void *addr = 0)
|
Chris@16
|
140 : m_mfile(open_only, name, read_only, addr,
|
Chris@16
|
141 create_open_func_t(get_this_pointer(),
|
Chris@16
|
142 ipcdetail::DoOpen))
|
Chris@16
|
143 {}
|
Chris@16
|
144
|
Chris@16
|
145 //!Moves the ownership of "moved"'s managed memory to *this.
|
Chris@16
|
146 //!Does not throw
|
Chris@16
|
147 basic_managed_mapped_file(BOOST_RV_REF(basic_managed_mapped_file) moved)
|
Chris@16
|
148 {
|
Chris@16
|
149 this->swap(moved);
|
Chris@16
|
150 }
|
Chris@16
|
151
|
Chris@16
|
152 //!Moves the ownership of "moved"'s managed memory to *this.
|
Chris@16
|
153 //!Does not throw
|
Chris@16
|
154 basic_managed_mapped_file &operator=(BOOST_RV_REF(basic_managed_mapped_file) moved)
|
Chris@16
|
155 {
|
Chris@16
|
156 basic_managed_mapped_file tmp(boost::move(moved));
|
Chris@16
|
157 this->swap(tmp);
|
Chris@16
|
158 return *this;
|
Chris@16
|
159 }
|
Chris@16
|
160
|
Chris@16
|
161 //!Destroys *this and indicates that the calling process is finished using
|
Chris@16
|
162 //!the resource. The destructor function will deallocate
|
Chris@16
|
163 //!any system resources allocated by the system for use by this process for
|
Chris@16
|
164 //!this resource. The resource can still be opened again calling
|
Chris@16
|
165 //!the open constructor overload. To erase the resource from the system
|
Chris@16
|
166 //!use remove().
|
Chris@16
|
167 ~basic_managed_mapped_file()
|
Chris@16
|
168 {}
|
Chris@16
|
169
|
Chris@16
|
170 //!Swaps the ownership of the managed mapped memories managed by *this and other.
|
Chris@16
|
171 //!Never throws.
|
Chris@16
|
172 void swap(basic_managed_mapped_file &other)
|
Chris@16
|
173 {
|
Chris@16
|
174 base_t::swap(other);
|
Chris@16
|
175 m_mfile.swap(other.m_mfile);
|
Chris@16
|
176 }
|
Chris@16
|
177
|
Chris@16
|
178 //!Flushes cached data to file.
|
Chris@16
|
179 //!Never throws
|
Chris@16
|
180 bool flush()
|
Chris@16
|
181 { return m_mfile.flush(); }
|
Chris@16
|
182
|
Chris@16
|
183 //!Tries to resize mapped file so that we have room for
|
Chris@16
|
184 //!more objects.
|
Chris@16
|
185 //!
|
Chris@16
|
186 //!This function is not synchronized so no other thread or process should
|
Chris@16
|
187 //!be reading or writing the file
|
Chris@16
|
188 static bool grow(const char *filename, size_type extra_bytes)
|
Chris@16
|
189 {
|
Chris@16
|
190 return base_t::template grow
|
Chris@16
|
191 <basic_managed_mapped_file>(filename, extra_bytes);
|
Chris@16
|
192 }
|
Chris@16
|
193
|
Chris@16
|
194 //!Tries to resize mapped file to minimized the size of the file.
|
Chris@16
|
195 //!
|
Chris@16
|
196 //!This function is not synchronized so no other thread or process should
|
Chris@16
|
197 //!be reading or writing the file
|
Chris@16
|
198 static bool shrink_to_fit(const char *filename)
|
Chris@16
|
199 {
|
Chris@16
|
200 return base_t::template shrink_to_fit
|
Chris@16
|
201 <basic_managed_mapped_file>(filename);
|
Chris@16
|
202 }
|
Chris@16
|
203
|
Chris@101
|
204 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
205
|
Chris@16
|
206 //!Tries to find a previous named allocation address. Returns a memory
|
Chris@16
|
207 //!buffer and the object count. If not found returned pointer is 0.
|
Chris@16
|
208 //!Never throws.
|
Chris@16
|
209 template <class T>
|
Chris@16
|
210 std::pair<T*, size_type> find (char_ptr_holder_t name)
|
Chris@16
|
211 {
|
Chris@16
|
212 if(m_mfile.get_mapped_region().get_mode() == read_only){
|
Chris@16
|
213 return base_t::template find_no_lock<T>(name);
|
Chris@16
|
214 }
|
Chris@16
|
215 else{
|
Chris@16
|
216 return base_t::template find<T>(name);
|
Chris@16
|
217 }
|
Chris@16
|
218 }
|
Chris@16
|
219
|
Chris@16
|
220 private:
|
Chris@16
|
221 typename ipcdetail::mfile_open_or_create<AllocationAlgorithm>::type m_mfile;
|
Chris@101
|
222 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
223 };
|
Chris@16
|
224
|
Chris@101
|
225 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@101
|
226
|
Chris@101
|
227 //!Typedef for a default basic_managed_mapped_file
|
Chris@101
|
228 //!of narrow characters
|
Chris@101
|
229 typedef basic_managed_mapped_file
|
Chris@101
|
230 <char
|
Chris@101
|
231 ,rbtree_best_fit<mutex_family>
|
Chris@101
|
232 ,iset_index>
|
Chris@101
|
233 managed_mapped_file;
|
Chris@101
|
234
|
Chris@101
|
235 //!Typedef for a default basic_managed_mapped_file
|
Chris@101
|
236 //!of wide characters
|
Chris@101
|
237 typedef basic_managed_mapped_file
|
Chris@101
|
238 <wchar_t
|
Chris@101
|
239 ,rbtree_best_fit<mutex_family>
|
Chris@101
|
240 ,iset_index>
|
Chris@101
|
241 wmanaged_mapped_file;
|
Chris@101
|
242
|
Chris@101
|
243 #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@101
|
244
|
Chris@16
|
245 } //namespace interprocess {
|
Chris@16
|
246 } //namespace boost {
|
Chris@16
|
247
|
Chris@16
|
248 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
249
|
Chris@16
|
250 #endif //BOOST_INTERPROCESS_MANAGED_MAPPED_FILE_HPP
|