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_WINDOWS_SHARED_MEMORY_HPP
|
Chris@16
|
12 #define BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_MEMORY_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/windows_shared_memory.hpp>
|
Chris@16
|
28 #include <boost/interprocess/permissions.hpp>
|
Chris@101
|
29 #include <boost/move/utility_core.hpp>
|
Chris@16
|
30 //These includes needed to fulfill default template parameters of
|
Chris@16
|
31 //predeclarations in interprocess_fwd.hpp
|
Chris@16
|
32 #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
|
Chris@16
|
33 #include <boost/interprocess/sync/mutex_family.hpp>
|
Chris@16
|
34 #include <boost/interprocess/indexes/iset_index.hpp>
|
Chris@16
|
35
|
Chris@16
|
36 namespace boost {
|
Chris@16
|
37 namespace interprocess {
|
Chris@16
|
38
|
Chris@16
|
39 namespace ipcdetail {
|
Chris@16
|
40
|
Chris@16
|
41 template<class AllocationAlgorithm>
|
Chris@16
|
42 struct wshmem_open_or_create
|
Chris@16
|
43 {
|
Chris@16
|
44 typedef ipcdetail::managed_open_or_create_impl
|
Chris@16
|
45 < windows_shared_memory, AllocationAlgorithm::Alignment, false, false> type;
|
Chris@16
|
46 };
|
Chris@16
|
47
|
Chris@16
|
48 } //namespace ipcdetail {
|
Chris@16
|
49
|
Chris@16
|
50 //!A basic managed windows shared memory creation class. Initializes the
|
Chris@16
|
51 //!shared memory segment. Inherits all basic functionality from
|
Chris@16
|
52 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>
|
Chris@16
|
53 //!Unlike basic_managed_shared_memory, it has
|
Chris@16
|
54 //!no kernel persistence and the shared memory is destroyed
|
Chris@16
|
55 //!when all processes destroy all their windows_shared_memory
|
Chris@16
|
56 //!objects and mapped regions for the same shared memory
|
Chris@16
|
57 //!or the processes end/crash.
|
Chris@16
|
58 //!
|
Chris@16
|
59 //!Warning: basic_managed_windows_shared_memory and
|
Chris@16
|
60 //!basic_managed_shared_memory can't communicate between them.
|
Chris@16
|
61 template
|
Chris@16
|
62 <
|
Chris@16
|
63 class CharType,
|
Chris@16
|
64 class AllocationAlgorithm,
|
Chris@16
|
65 template<class IndexConfig> class IndexType
|
Chris@16
|
66 >
|
Chris@16
|
67 class basic_managed_windows_shared_memory
|
Chris@16
|
68 : public ipcdetail::basic_managed_memory_impl
|
Chris@16
|
69 < CharType, AllocationAlgorithm, IndexType
|
Chris@16
|
70 , ipcdetail::wshmem_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset>
|
Chris@16
|
71 {
|
Chris@101
|
72 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
73 private:
|
Chris@16
|
74 typedef ipcdetail::basic_managed_memory_impl
|
Chris@16
|
75 <CharType, AllocationAlgorithm, IndexType,
|
Chris@16
|
76 ipcdetail::wshmem_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset> base_t;
|
Chris@16
|
77 typedef ipcdetail::create_open_func<base_t> create_open_func_t;
|
Chris@16
|
78
|
Chris@16
|
79 basic_managed_windows_shared_memory *get_this_pointer()
|
Chris@16
|
80 { return this; }
|
Chris@16
|
81
|
Chris@16
|
82 private:
|
Chris@16
|
83 typedef typename base_t::char_ptr_holder_t char_ptr_holder_t;
|
Chris@16
|
84 BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_windows_shared_memory)
|
Chris@101
|
85 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
86
|
Chris@16
|
87 public: //functions
|
Chris@16
|
88 typedef typename base_t::size_type size_type;
|
Chris@16
|
89
|
Chris@16
|
90 //!Default constructor. Does nothing.
|
Chris@16
|
91 //!Useful in combination with move semantics
|
Chris@16
|
92 basic_managed_windows_shared_memory()
|
Chris@16
|
93 {}
|
Chris@16
|
94
|
Chris@16
|
95 //!Creates shared memory and creates and places the segment manager.
|
Chris@16
|
96 //!This can throw.
|
Chris@16
|
97 basic_managed_windows_shared_memory
|
Chris@16
|
98 (create_only_t, const char *name,
|
Chris@16
|
99 size_type size, const void *addr = 0, const permissions &perm = permissions())
|
Chris@16
|
100 : m_wshm(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 shared memory 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_windows_shared_memory
|
Chris@16
|
109 (open_or_create_t,
|
Chris@16
|
110 const char *name, size_type size,
|
Chris@16
|
111 const void *addr = 0,
|
Chris@16
|
112 const permissions &perm = permissions())
|
Chris@16
|
113 : m_wshm(open_or_create, name, size, read_write, addr,
|
Chris@16
|
114 create_open_func_t(get_this_pointer(),
|
Chris@16
|
115 ipcdetail::DoOpenOrCreate), perm)
|
Chris@16
|
116 {}
|
Chris@16
|
117
|
Chris@16
|
118 //!Connects to a created shared memory and its segment manager.
|
Chris@16
|
119 //!This can throw.
|
Chris@16
|
120 basic_managed_windows_shared_memory
|
Chris@16
|
121 (open_only_t, const char* name, const void *addr = 0)
|
Chris@16
|
122 : m_wshm(open_only, name, read_write, addr,
|
Chris@16
|
123 create_open_func_t(get_this_pointer(),
|
Chris@16
|
124 ipcdetail::DoOpen))
|
Chris@16
|
125 {}
|
Chris@16
|
126
|
Chris@16
|
127 //!Connects to a created shared memory and its segment manager
|
Chris@16
|
128 //!in copy_on_write mode.
|
Chris@16
|
129 //!This can throw.
|
Chris@16
|
130 basic_managed_windows_shared_memory
|
Chris@16
|
131 (open_copy_on_write_t, const char* name, const void *addr = 0)
|
Chris@16
|
132 : m_wshm(open_only, name, copy_on_write, addr,
|
Chris@16
|
133 create_open_func_t(get_this_pointer(), ipcdetail::DoOpen))
|
Chris@16
|
134 {}
|
Chris@16
|
135
|
Chris@16
|
136 //!Connects to a created shared memory and its segment manager
|
Chris@16
|
137 //!in read-only mode.
|
Chris@16
|
138 //!This can throw.
|
Chris@16
|
139 basic_managed_windows_shared_memory
|
Chris@16
|
140 (open_read_only_t, const char* name, const void *addr = 0)
|
Chris@16
|
141 : base_t()
|
Chris@16
|
142 , m_wshm(open_only, name, read_only, addr,
|
Chris@16
|
143 create_open_func_t(get_this_pointer(), ipcdetail::DoOpen))
|
Chris@16
|
144 {}
|
Chris@16
|
145
|
Chris@16
|
146 //!Moves the ownership of "moved"'s managed memory to *this.
|
Chris@16
|
147 //!Does not throw
|
Chris@16
|
148 basic_managed_windows_shared_memory
|
Chris@16
|
149 (BOOST_RV_REF(basic_managed_windows_shared_memory) moved)
|
Chris@16
|
150 { this->swap(moved); }
|
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_windows_shared_memory &operator=(BOOST_RV_REF(basic_managed_windows_shared_memory) moved)
|
Chris@16
|
155 {
|
Chris@16
|
156 basic_managed_windows_shared_memory 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. All mapped regions are still valid after
|
Chris@16
|
163 //!destruction. When all mapped regions and basic_managed_windows_shared_memory
|
Chris@16
|
164 //!objects referring the shared memory are destroyed, the
|
Chris@16
|
165 //!operating system will destroy the shared memory.
|
Chris@16
|
166 ~basic_managed_windows_shared_memory()
|
Chris@16
|
167 {}
|
Chris@16
|
168
|
Chris@16
|
169 //!Swaps the ownership of the managed mapped memories managed by *this and other.
|
Chris@16
|
170 //!Never throws.
|
Chris@16
|
171 void swap(basic_managed_windows_shared_memory &other)
|
Chris@16
|
172 {
|
Chris@16
|
173 base_t::swap(other);
|
Chris@16
|
174 m_wshm.swap(other.m_wshm);
|
Chris@16
|
175 }
|
Chris@16
|
176
|
Chris@101
|
177 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
178
|
Chris@16
|
179 //!Tries to find a previous named allocation address. Returns a memory
|
Chris@16
|
180 //!buffer and the object count. If not found returned pointer is 0.
|
Chris@16
|
181 //!Never throws.
|
Chris@16
|
182 template <class T>
|
Chris@16
|
183 std::pair<T*, size_type> find (char_ptr_holder_t name)
|
Chris@16
|
184 {
|
Chris@16
|
185 if(m_wshm.get_mapped_region().get_mode() == read_only){
|
Chris@16
|
186 return base_t::template find_no_lock<T>(name);
|
Chris@16
|
187 }
|
Chris@16
|
188 else{
|
Chris@16
|
189 return base_t::template find<T>(name);
|
Chris@16
|
190 }
|
Chris@16
|
191 }
|
Chris@16
|
192
|
Chris@16
|
193 private:
|
Chris@16
|
194 typename ipcdetail::wshmem_open_or_create<AllocationAlgorithm>::type m_wshm;
|
Chris@101
|
195 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
196 };
|
Chris@16
|
197
|
Chris@101
|
198 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@101
|
199
|
Chris@101
|
200 //!Typedef for a default basic_managed_windows_shared_memory
|
Chris@101
|
201 //!of narrow characters
|
Chris@101
|
202 typedef basic_managed_windows_shared_memory
|
Chris@101
|
203 <char
|
Chris@101
|
204 ,rbtree_best_fit<mutex_family>
|
Chris@101
|
205 ,iset_index>
|
Chris@101
|
206 managed_windows_shared_memory;
|
Chris@101
|
207
|
Chris@101
|
208 //!Typedef for a default basic_managed_windows_shared_memory
|
Chris@101
|
209 //!of wide characters
|
Chris@101
|
210 typedef basic_managed_windows_shared_memory
|
Chris@101
|
211 <wchar_t
|
Chris@101
|
212 ,rbtree_best_fit<mutex_family>
|
Chris@101
|
213 ,iset_index>
|
Chris@101
|
214 wmanaged_windows_shared_memory;
|
Chris@101
|
215
|
Chris@101
|
216 #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@101
|
217
|
Chris@101
|
218
|
Chris@16
|
219 } //namespace interprocess {
|
Chris@16
|
220 } //namespace boost {
|
Chris@16
|
221
|
Chris@16
|
222 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
223
|
Chris@16
|
224 #endif //BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_MEMORY_HPP
|