]>
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_WINDOWS_SHARED_MEMORY_HPP | |
12 | #define BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_MEMORY_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/windows_shared_memory.hpp> | |
28 | #include <boost/interprocess/permissions.hpp> | |
29 | #include <boost/move/utility_core.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> | |
34 | #include <boost/interprocess/indexes/iset_index.hpp> | |
35 | ||
36 | namespace boost { | |
37 | namespace interprocess { | |
38 | ||
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 wshmem_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 | < windows_shared_memory |
61 | , final_segment_manager_alignment | |
62 | , false | |
63 | , false> type; | |
7c673cae FG |
64 | }; |
65 | ||
1e59de90 | 66 | |
7c673cae FG |
67 | } //namespace ipcdetail { |
68 | ||
69 | //!A basic managed windows shared memory creation class. Initializes the | |
70 | //!shared memory segment. Inherits all basic functionality from | |
71 | //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType> | |
72 | //!Unlike basic_managed_shared_memory, it has | |
73 | //!no kernel persistence and the shared memory is destroyed | |
74 | //!when all processes destroy all their windows_shared_memory | |
75 | //!objects and mapped regions for the same shared memory | |
76 | //!or the processes end/crash. | |
77 | //! | |
78 | //!Warning: basic_managed_windows_shared_memory and | |
79 | //!basic_managed_shared_memory can't communicate between them. | |
80 | template | |
81 | < | |
82 | class CharType, | |
83 | class AllocationAlgorithm, | |
84 | template<class IndexConfig> class IndexType | |
85 | > | |
86 | class basic_managed_windows_shared_memory | |
87 | : public ipcdetail::basic_managed_memory_impl | |
88 | < CharType, AllocationAlgorithm, IndexType | |
1e59de90 TL |
89 | , ipcdetail::wshmem_open_or_create |
90 | <CharType, AllocationAlgorithm, IndexType>::type::ManagedOpenOrCreateUserOffset> | |
7c673cae FG |
91 | { |
92 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
93 | private: | |
94 | typedef ipcdetail::basic_managed_memory_impl | |
1e59de90 TL |
95 | < CharType, AllocationAlgorithm, IndexType |
96 | , ipcdetail::wshmem_open_or_create | |
97 | <CharType, AllocationAlgorithm, IndexType>::type::ManagedOpenOrCreateUserOffset> base_t; | |
7c673cae FG |
98 | typedef ipcdetail::create_open_func<base_t> create_open_func_t; |
99 | ||
100 | basic_managed_windows_shared_memory *get_this_pointer() | |
101 | { return this; } | |
102 | ||
103 | private: | |
104 | typedef typename base_t::char_ptr_holder_t char_ptr_holder_t; | |
105 | BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_windows_shared_memory) | |
106 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
107 | ||
108 | public: //functions | |
109 | typedef typename base_t::size_type size_type; | |
110 | ||
111 | //!Default constructor. Does nothing. | |
112 | //!Useful in combination with move semantics | |
1e59de90 | 113 | basic_managed_windows_shared_memory() BOOST_NOEXCEPT |
7c673cae FG |
114 | {} |
115 | ||
116 | //!Creates shared memory and creates and places the segment manager. | |
117 | //!This can throw. | |
118 | basic_managed_windows_shared_memory | |
119 | (create_only_t, const char *name, | |
120 | size_type size, const void *addr = 0, const permissions &perm = permissions()) | |
121 | : m_wshm(create_only, name, size, read_write, addr, | |
122 | create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm) | |
123 | {} | |
124 | ||
125 | //!Creates shared memory and creates and places the segment manager if | |
126 | //!segment was not created. If segment was created it connects to the | |
127 | //!segment. | |
128 | //!This can throw. | |
129 | basic_managed_windows_shared_memory | |
130 | (open_or_create_t, | |
131 | const char *name, size_type size, | |
132 | const void *addr = 0, | |
133 | const permissions &perm = permissions()) | |
134 | : m_wshm(open_or_create, name, size, read_write, addr, | |
135 | create_open_func_t(get_this_pointer(), | |
136 | ipcdetail::DoOpenOrCreate), perm) | |
137 | {} | |
138 | ||
139 | //!Connects to a created shared memory and its segment manager. | |
140 | //!This can throw. | |
141 | basic_managed_windows_shared_memory | |
142 | (open_only_t, const char* name, const void *addr = 0) | |
143 | : m_wshm(open_only, name, read_write, addr, | |
144 | create_open_func_t(get_this_pointer(), | |
145 | ipcdetail::DoOpen)) | |
146 | {} | |
147 | ||
148 | //!Connects to a created shared memory and its segment manager | |
149 | //!in copy_on_write mode. | |
150 | //!This can throw. | |
151 | basic_managed_windows_shared_memory | |
152 | (open_copy_on_write_t, const char* name, const void *addr = 0) | |
153 | : m_wshm(open_only, name, copy_on_write, addr, | |
154 | create_open_func_t(get_this_pointer(), ipcdetail::DoOpen)) | |
155 | {} | |
156 | ||
157 | //!Connects to a created shared memory and its segment manager | |
158 | //!in read-only mode. | |
159 | //!This can throw. | |
160 | basic_managed_windows_shared_memory | |
161 | (open_read_only_t, const char* name, const void *addr = 0) | |
162 | : base_t() | |
163 | , m_wshm(open_only, name, read_only, addr, | |
164 | create_open_func_t(get_this_pointer(), ipcdetail::DoOpen)) | |
165 | {} | |
166 | ||
1e59de90 TL |
167 | //!Creates shared memory and creates and places the segment manager if |
168 | //!segment was not created. If segment was created it connects to the | |
169 | //!segment. | |
170 | //!This can throw. | |
171 | basic_managed_windows_shared_memory | |
172 | (open_or_create_t, | |
173 | const wchar_t *name, size_type size, | |
174 | const void *addr = 0, | |
175 | const permissions &perm = permissions()) | |
176 | : m_wshm(open_or_create, name, size, read_write, addr, | |
177 | create_open_func_t(get_this_pointer(), | |
178 | ipcdetail::DoOpenOrCreate), perm) | |
179 | {} | |
180 | ||
181 | //!Connects to a created shared memory and its segment manager. | |
182 | //!This can throw. | |
183 | basic_managed_windows_shared_memory | |
184 | (open_only_t, const wchar_t* name, const void *addr = 0) | |
185 | : m_wshm(open_only, name, read_write, addr, | |
186 | create_open_func_t(get_this_pointer(), | |
187 | ipcdetail::DoOpen)) | |
188 | {} | |
189 | ||
190 | //!Connects to a created shared memory and its segment manager | |
191 | //!in copy_on_write mode. | |
192 | //!This can throw. | |
193 | basic_managed_windows_shared_memory | |
194 | (open_copy_on_write_t, const wchar_t* name, const void *addr = 0) | |
195 | : m_wshm(open_only, name, copy_on_write, addr, | |
196 | create_open_func_t(get_this_pointer(), ipcdetail::DoOpen)) | |
197 | {} | |
198 | ||
199 | //!Connects to a created shared memory and its segment manager | |
200 | //!in read-only mode. | |
201 | //!This can throw. | |
202 | basic_managed_windows_shared_memory | |
203 | (open_read_only_t, const wchar_t* name, const void *addr = 0) | |
204 | : base_t() | |
205 | , m_wshm(open_only, name, read_only, addr, | |
206 | create_open_func_t(get_this_pointer(), ipcdetail::DoOpen)) | |
207 | {} | |
208 | ||
7c673cae FG |
209 | //!Moves the ownership of "moved"'s managed memory to *this. |
210 | //!Does not throw | |
211 | basic_managed_windows_shared_memory | |
1e59de90 | 212 | (BOOST_RV_REF(basic_managed_windows_shared_memory) moved) BOOST_NOEXCEPT |
7c673cae FG |
213 | { this->swap(moved); } |
214 | ||
215 | //!Moves the ownership of "moved"'s managed memory to *this. | |
216 | //!Does not throw | |
1e59de90 | 217 | basic_managed_windows_shared_memory &operator=(BOOST_RV_REF(basic_managed_windows_shared_memory) moved) BOOST_NOEXCEPT |
7c673cae FG |
218 | { |
219 | basic_managed_windows_shared_memory tmp(boost::move(moved)); | |
220 | this->swap(tmp); | |
221 | return *this; | |
222 | } | |
223 | ||
224 | //!Destroys *this and indicates that the calling process is finished using | |
225 | //!the resource. All mapped regions are still valid after | |
226 | //!destruction. When all mapped regions and basic_managed_windows_shared_memory | |
227 | //!objects referring the shared memory are destroyed, the | |
228 | //!operating system will destroy the shared memory. | |
229 | ~basic_managed_windows_shared_memory() | |
230 | {} | |
231 | ||
232 | //!Swaps the ownership of the managed mapped memories managed by *this and other. | |
233 | //!Never throws. | |
1e59de90 | 234 | void swap(basic_managed_windows_shared_memory &other) BOOST_NOEXCEPT |
7c673cae FG |
235 | { |
236 | base_t::swap(other); | |
237 | m_wshm.swap(other.m_wshm); | |
238 | } | |
239 | ||
240 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
241 | ||
242 | //!Tries to find a previous named allocation address. Returns a memory | |
243 | //!buffer and the object count. If not found returned pointer is 0. | |
244 | //!Never throws. | |
245 | template <class T> | |
246 | std::pair<T*, size_type> find (char_ptr_holder_t name) | |
247 | { | |
248 | if(m_wshm.get_mapped_region().get_mode() == read_only){ | |
249 | return base_t::template find_no_lock<T>(name); | |
250 | } | |
251 | else{ | |
252 | return base_t::template find<T>(name); | |
253 | } | |
254 | } | |
255 | ||
256 | private: | |
1e59de90 TL |
257 | typename ipcdetail::wshmem_open_or_create |
258 | <CharType, AllocationAlgorithm, IndexType>::type m_wshm; | |
7c673cae FG |
259 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
260 | }; | |
261 | ||
262 | #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
263 | ||
264 | //!Typedef for a default basic_managed_windows_shared_memory | |
265 | //!of narrow characters | |
266 | typedef basic_managed_windows_shared_memory | |
267 | <char | |
268 | ,rbtree_best_fit<mutex_family> | |
269 | ,iset_index> | |
270 | managed_windows_shared_memory; | |
271 | ||
272 | //!Typedef for a default basic_managed_windows_shared_memory | |
273 | //!of wide characters | |
274 | typedef basic_managed_windows_shared_memory | |
275 | <wchar_t | |
276 | ,rbtree_best_fit<mutex_family> | |
277 | ,iset_index> | |
278 | wmanaged_windows_shared_memory; | |
279 | ||
280 | #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
281 | ||
282 | ||
283 | } //namespace interprocess { | |
284 | } //namespace boost { | |
285 | ||
286 | #include <boost/interprocess/detail/config_end.hpp> | |
287 | ||
288 | #endif //BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_MEMORY_HPP |