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_ALLOCATOR_HPP
|
Chris@16
|
12 #define BOOST_INTERPROCESS_ALLOCATOR_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/intrusive/pointer_traits.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 #include <boost/interprocess/interprocess_fwd.hpp>
|
Chris@16
|
28 #include <boost/interprocess/containers/allocation_type.hpp>
|
Chris@16
|
29 #include <boost/container/detail/multiallocation_chain.hpp>
|
Chris@16
|
30 #include <boost/interprocess/allocators/detail/allocator_common.hpp>
|
Chris@16
|
31 #include <boost/interprocess/detail/utilities.hpp>
|
Chris@16
|
32 #include <boost/interprocess/containers/version_type.hpp>
|
Chris@16
|
33 #include <boost/interprocess/exceptions.hpp>
|
Chris@16
|
34 #include <boost/assert.hpp>
|
Chris@16
|
35 #include <boost/utility/addressof.hpp>
|
Chris@16
|
36 #include <boost/interprocess/detail/type_traits.hpp>
|
Chris@101
|
37 #include <boost/container/detail/placement_new.hpp>
|
Chris@16
|
38
|
Chris@16
|
39 #include <cstddef>
|
Chris@16
|
40 #include <stdexcept>
|
Chris@16
|
41
|
Chris@16
|
42 //!\file
|
Chris@16
|
43 //!Describes an allocator that allocates portions of fixed size
|
Chris@16
|
44 //!memory buffer (shared memory, mapped file...)
|
Chris@16
|
45
|
Chris@16
|
46 namespace boost {
|
Chris@16
|
47 namespace interprocess {
|
Chris@16
|
48
|
Chris@16
|
49
|
Chris@16
|
50 //!An STL compatible allocator that uses a segment manager as
|
Chris@16
|
51 //!memory source. The internal pointer type will of the same type (raw, smart) as
|
Chris@16
|
52 //!"typename SegmentManager::void_pointer" type. This allows
|
Chris@16
|
53 //!placing the allocator in shared memory, memory mapped-files, etc...
|
Chris@16
|
54 template<class T, class SegmentManager>
|
Chris@16
|
55 class allocator
|
Chris@16
|
56 {
|
Chris@16
|
57 public:
|
Chris@16
|
58 //Segment manager
|
Chris@16
|
59 typedef SegmentManager segment_manager;
|
Chris@16
|
60 typedef typename SegmentManager::void_pointer void_pointer;
|
Chris@16
|
61
|
Chris@101
|
62 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
63 private:
|
Chris@16
|
64
|
Chris@16
|
65 //Self type
|
Chris@16
|
66 typedef allocator<T, SegmentManager> self_t;
|
Chris@16
|
67
|
Chris@16
|
68 //Pointer to void
|
Chris@16
|
69 typedef typename segment_manager::void_pointer aux_pointer_t;
|
Chris@16
|
70
|
Chris@16
|
71 //Typedef to const void pointer
|
Chris@16
|
72 typedef typename boost::intrusive::
|
Chris@16
|
73 pointer_traits<aux_pointer_t>::template
|
Chris@16
|
74 rebind_pointer<const void>::type cvoid_ptr;
|
Chris@16
|
75
|
Chris@16
|
76 //Pointer to the allocator
|
Chris@16
|
77 typedef typename boost::intrusive::
|
Chris@16
|
78 pointer_traits<cvoid_ptr>::template
|
Chris@16
|
79 rebind_pointer<segment_manager>::type alloc_ptr_t;
|
Chris@16
|
80
|
Chris@16
|
81 //Not assignable from related allocator
|
Chris@16
|
82 template<class T2, class SegmentManager2>
|
Chris@16
|
83 allocator& operator=(const allocator<T2, SegmentManager2>&);
|
Chris@16
|
84
|
Chris@16
|
85 //Not assignable from other allocator
|
Chris@16
|
86 allocator& operator=(const allocator&);
|
Chris@16
|
87
|
Chris@16
|
88 //Pointer to the allocator
|
Chris@16
|
89 alloc_ptr_t mp_mngr;
|
Chris@101
|
90 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
91
|
Chris@16
|
92 public:
|
Chris@16
|
93 typedef T value_type;
|
Chris@16
|
94 typedef typename boost::intrusive::
|
Chris@16
|
95 pointer_traits<cvoid_ptr>::template
|
Chris@16
|
96 rebind_pointer<T>::type pointer;
|
Chris@16
|
97 typedef typename boost::intrusive::
|
Chris@16
|
98 pointer_traits<pointer>::template
|
Chris@16
|
99 rebind_pointer<const T>::type const_pointer;
|
Chris@16
|
100 typedef typename ipcdetail::add_reference
|
Chris@16
|
101 <value_type>::type reference;
|
Chris@16
|
102 typedef typename ipcdetail::add_reference
|
Chris@16
|
103 <const value_type>::type const_reference;
|
Chris@16
|
104 typedef typename segment_manager::size_type size_type;
|
Chris@16
|
105 typedef typename segment_manager::difference_type difference_type;
|
Chris@16
|
106
|
Chris@16
|
107 typedef boost::interprocess::version_type<allocator, 2> version;
|
Chris@16
|
108
|
Chris@101
|
109 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
110
|
Chris@16
|
111 //Experimental. Don't use.
|
Chris@16
|
112 typedef boost::container::container_detail::transform_multiallocation_chain
|
Chris@16
|
113 <typename SegmentManager::multiallocation_chain, T>multiallocation_chain;
|
Chris@101
|
114 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
115
|
Chris@16
|
116 //!Obtains an allocator that allocates
|
Chris@16
|
117 //!objects of type T2
|
Chris@16
|
118 template<class T2>
|
Chris@16
|
119 struct rebind
|
Chris@16
|
120 {
|
Chris@16
|
121 typedef allocator<T2, SegmentManager> other;
|
Chris@16
|
122 };
|
Chris@16
|
123
|
Chris@16
|
124 //!Returns the segment manager.
|
Chris@16
|
125 //!Never throws
|
Chris@16
|
126 segment_manager* get_segment_manager()const
|
Chris@16
|
127 { return ipcdetail::to_raw_pointer(mp_mngr); }
|
Chris@16
|
128
|
Chris@16
|
129 //!Constructor from the segment manager.
|
Chris@16
|
130 //!Never throws
|
Chris@16
|
131 allocator(segment_manager *segment_mngr)
|
Chris@16
|
132 : mp_mngr(segment_mngr) { }
|
Chris@16
|
133
|
Chris@16
|
134 //!Constructor from other allocator.
|
Chris@16
|
135 //!Never throws
|
Chris@16
|
136 allocator(const allocator &other)
|
Chris@16
|
137 : mp_mngr(other.get_segment_manager()){ }
|
Chris@16
|
138
|
Chris@16
|
139 //!Constructor from related allocator.
|
Chris@16
|
140 //!Never throws
|
Chris@16
|
141 template<class T2>
|
Chris@16
|
142 allocator(const allocator<T2, SegmentManager> &other)
|
Chris@16
|
143 : mp_mngr(other.get_segment_manager()){}
|
Chris@16
|
144
|
Chris@16
|
145 //!Allocates memory for an array of count elements.
|
Chris@16
|
146 //!Throws boost::interprocess::bad_alloc if there is no enough memory
|
Chris@16
|
147 pointer allocate(size_type count, cvoid_ptr hint = 0)
|
Chris@16
|
148 {
|
Chris@16
|
149 (void)hint;
|
Chris@16
|
150 if(size_overflows<sizeof(T)>(count)){
|
Chris@16
|
151 throw bad_alloc();
|
Chris@16
|
152 }
|
Chris@16
|
153 return pointer(static_cast<value_type*>(mp_mngr->allocate(count*sizeof(T))));
|
Chris@16
|
154 }
|
Chris@16
|
155
|
Chris@16
|
156 //!Deallocates memory previously allocated.
|
Chris@16
|
157 //!Never throws
|
Chris@16
|
158 void deallocate(const pointer &ptr, size_type)
|
Chris@16
|
159 { mp_mngr->deallocate((void*)ipcdetail::to_raw_pointer(ptr)); }
|
Chris@16
|
160
|
Chris@16
|
161 //!Returns the number of elements that could be allocated.
|
Chris@16
|
162 //!Never throws
|
Chris@16
|
163 size_type max_size() const
|
Chris@16
|
164 { return mp_mngr->get_size()/sizeof(T); }
|
Chris@16
|
165
|
Chris@16
|
166 //!Swap segment manager. Does not throw. If each allocator is placed in
|
Chris@16
|
167 //!different memory segments, the result is undefined.
|
Chris@16
|
168 friend void swap(self_t &alloc1, self_t &alloc2)
|
Chris@101
|
169 { boost::adl_move_swap(alloc1.mp_mngr, alloc2.mp_mngr); }
|
Chris@16
|
170
|
Chris@16
|
171 //!Returns maximum the number of objects the previously allocated memory
|
Chris@16
|
172 //!pointed by p can hold. This size only works for memory allocated with
|
Chris@16
|
173 //!allocate, allocation_command and allocate_many.
|
Chris@16
|
174 size_type size(const pointer &p) const
|
Chris@16
|
175 {
|
Chris@16
|
176 return (size_type)mp_mngr->size(ipcdetail::to_raw_pointer(p))/sizeof(T);
|
Chris@16
|
177 }
|
Chris@16
|
178
|
Chris@101
|
179 pointer allocation_command(boost::interprocess::allocation_type command,
|
Chris@101
|
180 size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse)
|
Chris@16
|
181 {
|
Chris@101
|
182 value_type *reuse_raw = ipcdetail::to_raw_pointer(reuse);
|
Chris@101
|
183 pointer const p = mp_mngr->allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse_raw);
|
Chris@101
|
184 reuse = reuse_raw;
|
Chris@101
|
185 return p;
|
Chris@16
|
186 }
|
Chris@16
|
187
|
Chris@16
|
188 //!Allocates many elements of size elem_size in a contiguous block
|
Chris@16
|
189 //!of memory. The minimum number to be allocated is min_elements,
|
Chris@16
|
190 //!the preferred and maximum number is
|
Chris@16
|
191 //!preferred_elements. The number of actually allocated elements is
|
Chris@16
|
192 //!will be assigned to received_size. The elements must be deallocated
|
Chris@16
|
193 //!with deallocate(...)
|
Chris@16
|
194 void allocate_many(size_type elem_size, size_type num_elements, multiallocation_chain &chain)
|
Chris@16
|
195 {
|
Chris@16
|
196 if(size_overflows<sizeof(T)>(elem_size)){
|
Chris@16
|
197 throw bad_alloc();
|
Chris@16
|
198 }
|
Chris@16
|
199 mp_mngr->allocate_many(elem_size*sizeof(T), num_elements, chain);
|
Chris@16
|
200 }
|
Chris@16
|
201
|
Chris@16
|
202 //!Allocates n_elements elements, each one of size elem_sizes[i]in a
|
Chris@16
|
203 //!contiguous block
|
Chris@16
|
204 //!of memory. The elements must be deallocated
|
Chris@16
|
205 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain)
|
Chris@16
|
206 {
|
Chris@16
|
207 mp_mngr->allocate_many(elem_sizes, n_elements, sizeof(T), chain);
|
Chris@16
|
208 }
|
Chris@16
|
209
|
Chris@16
|
210 //!Allocates many elements of size elem_size in a contiguous block
|
Chris@16
|
211 //!of memory. The minimum number to be allocated is min_elements,
|
Chris@16
|
212 //!the preferred and maximum number is
|
Chris@16
|
213 //!preferred_elements. The number of actually allocated elements is
|
Chris@16
|
214 //!will be assigned to received_size. The elements must be deallocated
|
Chris@16
|
215 //!with deallocate(...)
|
Chris@16
|
216 void deallocate_many(multiallocation_chain &chain)
|
Chris@16
|
217 { mp_mngr->deallocate_many(chain); }
|
Chris@16
|
218
|
Chris@16
|
219 //!Allocates just one object. Memory allocated with this function
|
Chris@16
|
220 //!must be deallocated only with deallocate_one().
|
Chris@16
|
221 //!Throws boost::interprocess::bad_alloc if there is no enough memory
|
Chris@16
|
222 pointer allocate_one()
|
Chris@16
|
223 { return this->allocate(1); }
|
Chris@16
|
224
|
Chris@16
|
225 //!Allocates many elements of size == 1 in a contiguous block
|
Chris@16
|
226 //!of memory. The minimum number to be allocated is min_elements,
|
Chris@16
|
227 //!the preferred and maximum number is
|
Chris@16
|
228 //!preferred_elements. The number of actually allocated elements is
|
Chris@16
|
229 //!will be assigned to received_size. Memory allocated with this function
|
Chris@16
|
230 //!must be deallocated only with deallocate_one().
|
Chris@16
|
231 void allocate_individual(size_type num_elements, multiallocation_chain &chain)
|
Chris@16
|
232 { this->allocate_many(1, num_elements, chain); }
|
Chris@16
|
233
|
Chris@16
|
234 //!Deallocates memory previously allocated with allocate_one().
|
Chris@16
|
235 //!You should never use deallocate_one to deallocate memory allocated
|
Chris@16
|
236 //!with other functions different from allocate_one(). Never throws
|
Chris@16
|
237 void deallocate_one(const pointer &p)
|
Chris@16
|
238 { return this->deallocate(p, 1); }
|
Chris@16
|
239
|
Chris@16
|
240 //!Allocates many elements of size == 1 in a contiguous block
|
Chris@16
|
241 //!of memory. The minimum number to be allocated is min_elements,
|
Chris@16
|
242 //!the preferred and maximum number is
|
Chris@16
|
243 //!preferred_elements. The number of actually allocated elements is
|
Chris@16
|
244 //!will be assigned to received_size. Memory allocated with this function
|
Chris@16
|
245 //!must be deallocated only with deallocate_one().
|
Chris@16
|
246 void deallocate_individual(multiallocation_chain &chain)
|
Chris@16
|
247 { this->deallocate_many(chain); }
|
Chris@16
|
248
|
Chris@16
|
249 //!Returns address of mutable object.
|
Chris@16
|
250 //!Never throws
|
Chris@16
|
251 pointer address(reference value) const
|
Chris@16
|
252 { return pointer(boost::addressof(value)); }
|
Chris@16
|
253
|
Chris@16
|
254 //!Returns address of non mutable object.
|
Chris@16
|
255 //!Never throws
|
Chris@16
|
256 const_pointer address(const_reference value) const
|
Chris@16
|
257 { return const_pointer(boost::addressof(value)); }
|
Chris@16
|
258
|
Chris@16
|
259 //!Constructs an object
|
Chris@16
|
260 //!Throws if T's constructor throws
|
Chris@16
|
261 //!For backwards compatibility with libraries using C++03 allocators
|
Chris@16
|
262 template<class P>
|
Chris@16
|
263 void construct(const pointer &ptr, BOOST_FWD_REF(P) p)
|
Chris@101
|
264 { ::new((void*)ipcdetail::to_raw_pointer(ptr), boost_container_new_t()) value_type(::boost::forward<P>(p)); }
|
Chris@16
|
265
|
Chris@16
|
266 //!Destroys object. Throws if object's
|
Chris@16
|
267 //!destructor throws
|
Chris@16
|
268 void destroy(const pointer &ptr)
|
Chris@16
|
269 { BOOST_ASSERT(ptr != 0); (*ptr).~value_type(); }
|
Chris@16
|
270
|
Chris@16
|
271 };
|
Chris@16
|
272
|
Chris@16
|
273 //!Equality test for same type
|
Chris@16
|
274 //!of allocator
|
Chris@16
|
275 template<class T, class SegmentManager> inline
|
Chris@16
|
276 bool operator==(const allocator<T , SegmentManager> &alloc1,
|
Chris@16
|
277 const allocator<T, SegmentManager> &alloc2)
|
Chris@16
|
278 { return alloc1.get_segment_manager() == alloc2.get_segment_manager(); }
|
Chris@16
|
279
|
Chris@16
|
280 //!Inequality test for same type
|
Chris@16
|
281 //!of allocator
|
Chris@16
|
282 template<class T, class SegmentManager> inline
|
Chris@16
|
283 bool operator!=(const allocator<T, SegmentManager> &alloc1,
|
Chris@16
|
284 const allocator<T, SegmentManager> &alloc2)
|
Chris@16
|
285 { return alloc1.get_segment_manager() != alloc2.get_segment_manager(); }
|
Chris@16
|
286
|
Chris@16
|
287 } //namespace interprocess {
|
Chris@16
|
288
|
Chris@101
|
289 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
290
|
Chris@16
|
291 template<class T>
|
Chris@16
|
292 struct has_trivial_destructor;
|
Chris@16
|
293
|
Chris@16
|
294 template<class T, class SegmentManager>
|
Chris@16
|
295 struct has_trivial_destructor
|
Chris@16
|
296 <boost::interprocess::allocator <T, SegmentManager> >
|
Chris@16
|
297 {
|
Chris@16
|
298 static const bool value = true;
|
Chris@16
|
299 };
|
Chris@101
|
300 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
301
|
Chris@16
|
302 } //namespace boost {
|
Chris@16
|
303
|
Chris@16
|
304 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
305
|
Chris@16
|
306 #endif //BOOST_INTERPROCESS_ALLOCATOR_HPP
|
Chris@16
|
307
|