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