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_SHARED_MEMORY_HPP
|
Chris@16
|
12 #define BOOST_INTERPROCESS_MANAGED_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
|
Chris@16
|
25 #include <boost/interprocess/detail/managed_memory_impl.hpp>
|
Chris@16
|
26 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
|
Chris@16
|
27 #include <boost/interprocess/shared_memory_object.hpp>
|
Chris@16
|
28 #include <boost/interprocess/creation_tags.hpp>
|
Chris@16
|
29 #include <boost/interprocess/permissions.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
|
Chris@16
|
35 namespace boost {
|
Chris@16
|
36 namespace interprocess {
|
Chris@16
|
37
|
Chris@16
|
38 namespace ipcdetail {
|
Chris@16
|
39
|
Chris@16
|
40 template<class AllocationAlgorithm>
|
Chris@16
|
41 struct shmem_open_or_create
|
Chris@16
|
42 {
|
Chris@16
|
43 typedef ipcdetail::managed_open_or_create_impl
|
Chris@16
|
44 < shared_memory_object, AllocationAlgorithm::Alignment, true, false> type;
|
Chris@16
|
45 };
|
Chris@16
|
46
|
Chris@16
|
47 } //namespace ipcdetail {
|
Chris@16
|
48
|
Chris@16
|
49 //!A basic shared memory named object creation class. Initializes the
|
Chris@16
|
50 //!shared memory segment. Inherits all basic functionality from
|
Chris@16
|
51 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>*/
|
Chris@16
|
52 template
|
Chris@16
|
53 <
|
Chris@16
|
54 class CharType,
|
Chris@16
|
55 class AllocationAlgorithm,
|
Chris@16
|
56 template<class IndexConfig> class IndexType
|
Chris@16
|
57 >
|
Chris@16
|
58 class basic_managed_shared_memory
|
Chris@16
|
59 : public ipcdetail::basic_managed_memory_impl
|
Chris@16
|
60 <CharType, AllocationAlgorithm, IndexType
|
Chris@16
|
61 ,ipcdetail::shmem_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset>
|
Chris@16
|
62 , private ipcdetail::shmem_open_or_create<AllocationAlgorithm>::type
|
Chris@16
|
63 {
|
Chris@101
|
64 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
65 typedef ipcdetail::basic_managed_memory_impl
|
Chris@16
|
66 <CharType, AllocationAlgorithm, IndexType,
|
Chris@16
|
67 ipcdetail::shmem_open_or_create<AllocationAlgorithm>::type::ManagedOpenOrCreateUserOffset> base_t;
|
Chris@16
|
68 typedef typename ipcdetail::shmem_open_or_create<AllocationAlgorithm>::type base2_t;
|
Chris@16
|
69
|
Chris@16
|
70 typedef ipcdetail::create_open_func<base_t> create_open_func_t;
|
Chris@16
|
71
|
Chris@16
|
72 basic_managed_shared_memory *get_this_pointer()
|
Chris@16
|
73 { return this; }
|
Chris@16
|
74
|
Chris@16
|
75 public:
|
Chris@16
|
76 typedef shared_memory_object device_type;
|
Chris@16
|
77 typedef typename base_t::size_type size_type;
|
Chris@16
|
78
|
Chris@16
|
79 private:
|
Chris@16
|
80 typedef typename base_t::char_ptr_holder_t char_ptr_holder_t;
|
Chris@16
|
81 BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_shared_memory)
|
Chris@101
|
82 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
83
|
Chris@16
|
84 public: //functions
|
Chris@16
|
85
|
Chris@16
|
86 //!Destroys *this and indicates that the calling process is finished using
|
Chris@16
|
87 //!the resource. The destructor function will deallocate
|
Chris@16
|
88 //!any system resources allocated by the system for use by this process for
|
Chris@16
|
89 //!this resource. The resource can still be opened again calling
|
Chris@16
|
90 //!the open constructor overload. To erase the resource from the system
|
Chris@16
|
91 //!use remove().
|
Chris@16
|
92 ~basic_managed_shared_memory()
|
Chris@16
|
93 {}
|
Chris@16
|
94
|
Chris@16
|
95 //!Default constructor. Does nothing.
|
Chris@16
|
96 //!Useful in combination with move semantics
|
Chris@16
|
97 basic_managed_shared_memory()
|
Chris@16
|
98 {}
|
Chris@16
|
99
|
Chris@16
|
100 //!Creates shared memory and creates and places the segment manager.
|
Chris@16
|
101 //!This can throw.
|
Chris@16
|
102 basic_managed_shared_memory(create_only_t, const char *name,
|
Chris@16
|
103 size_type size, const void *addr = 0, const permissions& perm = permissions())
|
Chris@16
|
104 : base_t()
|
Chris@16
|
105 , base2_t(create_only, name, size, read_write, addr,
|
Chris@16
|
106 create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm)
|
Chris@16
|
107 {}
|
Chris@16
|
108
|
Chris@16
|
109 //!Creates shared memory and creates and places the segment manager if
|
Chris@16
|
110 //!segment was not created. If segment was created it connects to the
|
Chris@16
|
111 //!segment.
|
Chris@16
|
112 //!This can throw.
|
Chris@16
|
113 basic_managed_shared_memory (open_or_create_t,
|
Chris@16
|
114 const char *name, size_type size,
|
Chris@16
|
115 const void *addr = 0, const permissions& perm = permissions())
|
Chris@16
|
116 : base_t()
|
Chris@16
|
117 , base2_t(open_or_create, name, size, read_write, addr,
|
Chris@16
|
118 create_open_func_t(get_this_pointer(),
|
Chris@16
|
119 ipcdetail::DoOpenOrCreate), perm)
|
Chris@16
|
120 {}
|
Chris@16
|
121
|
Chris@16
|
122 //!Connects to a created shared memory and its segment manager.
|
Chris@16
|
123 //!in copy_on_write mode.
|
Chris@16
|
124 //!This can throw.
|
Chris@16
|
125 basic_managed_shared_memory (open_copy_on_write_t, const char* name,
|
Chris@16
|
126 const void *addr = 0)
|
Chris@16
|
127 : base_t()
|
Chris@16
|
128 , base2_t(open_only, name, copy_on_write, addr,
|
Chris@16
|
129 create_open_func_t(get_this_pointer(),
|
Chris@16
|
130 ipcdetail::DoOpen))
|
Chris@16
|
131 {}
|
Chris@16
|
132
|
Chris@16
|
133 //!Connects to a created shared memory and its segment manager.
|
Chris@16
|
134 //!in read-only mode.
|
Chris@16
|
135 //!This can throw.
|
Chris@16
|
136 basic_managed_shared_memory (open_read_only_t, const char* name,
|
Chris@16
|
137 const void *addr = 0)
|
Chris@16
|
138 : base_t()
|
Chris@16
|
139 , base2_t(open_only, name, read_only, addr,
|
Chris@16
|
140 create_open_func_t(get_this_pointer(),
|
Chris@16
|
141 ipcdetail::DoOpen))
|
Chris@16
|
142 {}
|
Chris@16
|
143
|
Chris@16
|
144 //!Connects to a created shared memory and its segment manager.
|
Chris@16
|
145 //!This can throw.
|
Chris@16
|
146 basic_managed_shared_memory (open_only_t, const char* name,
|
Chris@16
|
147 const void *addr = 0)
|
Chris@16
|
148 : base_t()
|
Chris@16
|
149 , base2_t(open_only, name, read_write, addr,
|
Chris@16
|
150 create_open_func_t(get_this_pointer(),
|
Chris@16
|
151 ipcdetail::DoOpen))
|
Chris@16
|
152 {}
|
Chris@16
|
153
|
Chris@16
|
154 //!Moves the ownership of "moved"'s managed memory to *this.
|
Chris@16
|
155 //!Does not throw
|
Chris@16
|
156 basic_managed_shared_memory(BOOST_RV_REF(basic_managed_shared_memory) moved)
|
Chris@16
|
157 {
|
Chris@16
|
158 basic_managed_shared_memory tmp;
|
Chris@16
|
159 this->swap(moved);
|
Chris@16
|
160 tmp.swap(moved);
|
Chris@16
|
161 }
|
Chris@16
|
162
|
Chris@16
|
163 //!Moves the ownership of "moved"'s managed memory to *this.
|
Chris@16
|
164 //!Does not throw
|
Chris@16
|
165 basic_managed_shared_memory &operator=(BOOST_RV_REF(basic_managed_shared_memory) moved)
|
Chris@16
|
166 {
|
Chris@16
|
167 basic_managed_shared_memory tmp(boost::move(moved));
|
Chris@16
|
168 this->swap(tmp);
|
Chris@16
|
169 return *this;
|
Chris@16
|
170 }
|
Chris@16
|
171
|
Chris@16
|
172 //!Swaps the ownership of the managed shared memories managed by *this and other.
|
Chris@16
|
173 //!Never throws.
|
Chris@16
|
174 void swap(basic_managed_shared_memory &other)
|
Chris@16
|
175 {
|
Chris@16
|
176 base_t::swap(other);
|
Chris@16
|
177 base2_t::swap(other);
|
Chris@16
|
178 }
|
Chris@16
|
179
|
Chris@16
|
180 //!Tries to resize the managed shared memory object so that we have
|
Chris@16
|
181 //!room for more objects.
|
Chris@16
|
182 //!
|
Chris@16
|
183 //!This function is not synchronized so no other thread or process should
|
Chris@16
|
184 //!be reading or writing the file
|
Chris@16
|
185 static bool grow(const char *shmname, size_type extra_bytes)
|
Chris@16
|
186 {
|
Chris@16
|
187 return base_t::template grow
|
Chris@16
|
188 <basic_managed_shared_memory>(shmname, extra_bytes);
|
Chris@16
|
189 }
|
Chris@16
|
190
|
Chris@16
|
191 //!Tries to resize the managed shared memory to minimized the size of the file.
|
Chris@16
|
192 //!
|
Chris@16
|
193 //!This function is not synchronized so no other thread or process should
|
Chris@16
|
194 //!be reading or writing the file
|
Chris@16
|
195 static bool shrink_to_fit(const char *shmname)
|
Chris@16
|
196 {
|
Chris@16
|
197 return base_t::template shrink_to_fit
|
Chris@16
|
198 <basic_managed_shared_memory>(shmname);
|
Chris@16
|
199 }
|
Chris@101
|
200 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
201
|
Chris@16
|
202 //!Tries to find a previous named allocation address. Returns a memory
|
Chris@16
|
203 //!buffer and the object count. If not found returned pointer is 0.
|
Chris@16
|
204 //!Never throws.
|
Chris@16
|
205 template <class T>
|
Chris@16
|
206 std::pair<T*, size_type> find (char_ptr_holder_t name)
|
Chris@16
|
207 {
|
Chris@16
|
208 if(base2_t::get_mapped_region().get_mode() == read_only){
|
Chris@16
|
209 return base_t::template find_no_lock<T>(name);
|
Chris@16
|
210 }
|
Chris@16
|
211 else{
|
Chris@16
|
212 return base_t::template find<T>(name);
|
Chris@16
|
213 }
|
Chris@16
|
214 }
|
Chris@16
|
215
|
Chris@101
|
216 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
217 };
|
Chris@16
|
218
|
Chris@101
|
219 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@101
|
220
|
Chris@101
|
221 //!Typedef for a default basic_managed_shared_memory
|
Chris@101
|
222 //!of narrow characters
|
Chris@101
|
223 typedef basic_managed_shared_memory
|
Chris@101
|
224 <char
|
Chris@101
|
225 ,rbtree_best_fit<mutex_family>
|
Chris@101
|
226 ,iset_index>
|
Chris@101
|
227 managed_shared_memory;
|
Chris@101
|
228
|
Chris@101
|
229 //!Typedef for a default basic_managed_shared_memory
|
Chris@101
|
230 //!of wide characters
|
Chris@101
|
231 typedef basic_managed_shared_memory
|
Chris@101
|
232 <wchar_t
|
Chris@101
|
233 ,rbtree_best_fit<mutex_family>
|
Chris@101
|
234 ,iset_index>
|
Chris@101
|
235 wmanaged_shared_memory;
|
Chris@101
|
236
|
Chris@101
|
237 //!Typedef for a default basic_managed_shared_memory
|
Chris@101
|
238 //!of narrow characters to be placed in a fixed address
|
Chris@101
|
239 typedef basic_managed_shared_memory
|
Chris@101
|
240 <char
|
Chris@101
|
241 ,rbtree_best_fit<mutex_family, void*>
|
Chris@101
|
242 ,iset_index>
|
Chris@101
|
243 fixed_managed_shared_memory;
|
Chris@101
|
244
|
Chris@101
|
245 //!Typedef for a default basic_managed_shared_memory
|
Chris@101
|
246 //!of narrow characters to be placed in a fixed address
|
Chris@101
|
247 typedef basic_managed_shared_memory
|
Chris@101
|
248 <wchar_t
|
Chris@101
|
249 ,rbtree_best_fit<mutex_family, void*>
|
Chris@101
|
250 ,iset_index>
|
Chris@101
|
251 wfixed_managed_shared_memory;
|
Chris@101
|
252
|
Chris@101
|
253
|
Chris@101
|
254 #endif //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@101
|
255
|
Chris@16
|
256 } //namespace interprocess {
|
Chris@16
|
257 } //namespace boost {
|
Chris@16
|
258
|
Chris@16
|
259 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
260
|
Chris@16
|
261 #endif //BOOST_INTERPROCESS_MANAGED_SHARED_MEMORY_HPP
|
Chris@16
|
262
|