]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/include/boost/interprocess/allocators/adaptive_pool.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / interprocess / include / boost / interprocess / allocators / adaptive_pool.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_ADAPTIVE_POOL_HPP
12 #define BOOST_INTERPROCESS_ADAPTIVE_POOL_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/assert.hpp>
29 #include <boost/utility/addressof.hpp>
30 #include <boost/interprocess/detail/utilities.hpp>
31 #include <boost/interprocess/detail/type_traits.hpp>
32 #include <boost/interprocess/allocators/detail/adaptive_node_pool.hpp>
33 #include <boost/interprocess/containers/version_type.hpp>
34 #include <boost/interprocess/exceptions.hpp>
35 #include <boost/interprocess/allocators/detail/allocator_common.hpp>
36 #include <boost/container/detail/multiallocation_chain.hpp>
37 #include <boost/interprocess/detail/mpl.hpp>
38 #include <boost/move/adl_move_swap.hpp>
39 #include <cstddef>
40
41 //!\file
42 //!Describes adaptive_pool pooled shared memory STL compatible allocator
43
44 namespace boost {
45 namespace interprocess {
46
47 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
48
49 namespace ipcdetail{
50
51 template < unsigned int Version
52 , class T
53 , class SegmentManager
54 , std::size_t NodesPerBlock
55 , std::size_t MaxFreeBlocks
56 , unsigned char OverheadPercent
57 >
58 class adaptive_pool_base
59 : public node_pool_allocation_impl
60 < adaptive_pool_base
61 < Version, T, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent>
62 , Version
63 , T
64 , SegmentManager
65 >
66 {
67 public:
68 typedef typename SegmentManager::void_pointer void_pointer;
69 typedef SegmentManager segment_manager;
70 typedef adaptive_pool_base
71 <Version, T, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> self_t;
72
73 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
74
75 template <int dummy>
76 struct node_pool
77 {
78 typedef ipcdetail::shared_adaptive_node_pool
79 < SegmentManager, sizeof_value<T>::value, NodesPerBlock, MaxFreeBlocks, OverheadPercent> type;
80
81 static type *get(void *p)
82 { return static_cast<type*>(p); }
83 };
84 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
85
86 BOOST_STATIC_ASSERT((Version <=2));
87
88 public:
89 //-------
90 typedef typename boost::intrusive::
91 pointer_traits<void_pointer>::template
92 rebind_pointer<T>::type pointer;
93 typedef typename boost::intrusive::
94 pointer_traits<void_pointer>::template
95 rebind_pointer<const T>::type const_pointer;
96 typedef T value_type;
97 typedef typename ipcdetail::add_reference
98 <value_type>::type reference;
99 typedef typename ipcdetail::add_reference
100 <const value_type>::type const_reference;
101 typedef typename segment_manager::size_type size_type;
102 typedef typename segment_manager::difference_type difference_type;
103
104 typedef boost::interprocess::version_type<adaptive_pool_base, Version> version;
105 typedef boost::container::container_detail::transform_multiallocation_chain
106 <typename SegmentManager::multiallocation_chain, T>multiallocation_chain;
107
108 //!Obtains adaptive_pool_base from
109 //!adaptive_pool_base
110 template<class T2>
111 struct rebind
112 {
113 typedef adaptive_pool_base<Version, T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
114 };
115
116 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
117 private:
118 //!Not assignable from related adaptive_pool_base
119 template<unsigned int Version2, class T2, class SegmentManager2, std::size_t N2, std::size_t F2, unsigned char O2>
120 adaptive_pool_base& operator=
121 (const adaptive_pool_base<Version2, T2, SegmentManager2, N2, F2, O2>&);
122
123 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
124
125 public:
126 //!Constructor from a segment manager. If not present, constructs a node
127 //!pool. Increments the reference count of the associated node pool.
128 //!Can throw boost::interprocess::bad_alloc
129 adaptive_pool_base(segment_manager *segment_mngr)
130 : mp_node_pool(ipcdetail::get_or_create_node_pool<typename node_pool<0>::type>(segment_mngr)) { }
131
132 //!Copy constructor from other adaptive_pool_base. Increments the reference
133 //!count of the associated node pool. Never throws
134 adaptive_pool_base(const adaptive_pool_base &other)
135 : mp_node_pool(other.get_node_pool())
136 {
137 node_pool<0>::get(ipcdetail::to_raw_pointer(mp_node_pool))->inc_ref_count();
138 }
139
140 //!Assignment from other adaptive_pool_base
141 adaptive_pool_base& operator=(const adaptive_pool_base &other)
142 {
143 adaptive_pool_base c(other);
144 boost::adl_move_swap(*this, c);
145 return *this;
146 }
147
148 //!Copy constructor from related adaptive_pool_base. If not present, constructs
149 //!a node pool. Increments the reference count of the associated node pool.
150 //!Can throw boost::interprocess::bad_alloc
151 template<class T2>
152 adaptive_pool_base
153 (const adaptive_pool_base<Version, T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other)
154 : mp_node_pool(ipcdetail::get_or_create_node_pool<typename node_pool<0>::type>(other.get_segment_manager())) { }
155
156 //!Destructor, removes node_pool_t from memory
157 //!if its reference count reaches to zero. Never throws
158 ~adaptive_pool_base()
159 { ipcdetail::destroy_node_pool_if_last_link(node_pool<0>::get(ipcdetail::to_raw_pointer(mp_node_pool))); }
160
161 //!Returns a pointer to the node pool.
162 //!Never throws
163 void* get_node_pool() const
164 { return ipcdetail::to_raw_pointer(mp_node_pool); }
165
166 //!Returns the segment manager.
167 //!Never throws
168 segment_manager* get_segment_manager()const
169 { return node_pool<0>::get(ipcdetail::to_raw_pointer(mp_node_pool))->get_segment_manager(); }
170
171 //!Swaps allocators. Does not throw. If each allocator is placed in a
172 //!different memory segment, the result is undefined.
173 friend void swap(self_t &alloc1, self_t &alloc2)
174 { boost::adl_move_swap(alloc1.mp_node_pool, alloc2.mp_node_pool); }
175
176 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
177 private:
178 void_pointer mp_node_pool;
179 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
180 };
181
182 //!Equality test for same type
183 //!of adaptive_pool_base
184 template<unsigned int V, class T, class S, std::size_t NPC, std::size_t F, unsigned char OP> inline
185 bool operator==(const adaptive_pool_base<V, T, S, NPC, F, OP> &alloc1,
186 const adaptive_pool_base<V, T, S, NPC, F, OP> &alloc2)
187 { return alloc1.get_node_pool() == alloc2.get_node_pool(); }
188
189 //!Inequality test for same type
190 //!of adaptive_pool_base
191 template<unsigned int V, class T, class S, std::size_t NPC, std::size_t F, unsigned char OP> inline
192 bool operator!=(const adaptive_pool_base<V, T, S, NPC, F, OP> &alloc1,
193 const adaptive_pool_base<V, T, S, NPC, F, OP> &alloc2)
194 { return alloc1.get_node_pool() != alloc2.get_node_pool(); }
195
196 template < class T
197 , class SegmentManager
198 , std::size_t NodesPerBlock = 64
199 , std::size_t MaxFreeBlocks = 2
200 , unsigned char OverheadPercent = 5
201 >
202 class adaptive_pool_v1
203 : public adaptive_pool_base
204 < 1
205 , T
206 , SegmentManager
207 , NodesPerBlock
208 , MaxFreeBlocks
209 , OverheadPercent
210 >
211 {
212 public:
213 typedef ipcdetail::adaptive_pool_base
214 < 1, T, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> base_t;
215
216 template<class T2>
217 struct rebind
218 {
219 typedef adaptive_pool_v1<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
220 };
221
222 adaptive_pool_v1(SegmentManager *segment_mngr)
223 : base_t(segment_mngr)
224 {}
225
226 template<class T2>
227 adaptive_pool_v1
228 (const adaptive_pool_v1<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other)
229 : base_t(other)
230 {}
231 };
232
233 } //namespace ipcdetail{
234
235 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
236
237 //!An STL node allocator that uses a segment manager as memory
238 //!source. The internal pointer type will of the same type (raw, smart) as
239 //!"typename SegmentManager::void_pointer" type. This allows
240 //!placing the allocator in shared memory, memory mapped-files, etc...
241 //!
242 //!This node allocator shares a segregated storage between all instances
243 //!of adaptive_pool with equal sizeof(T) placed in the same segment
244 //!group. NodesPerBlock is the number of nodes allocated at once when the allocator
245 //!needs runs out of nodes. MaxFreeBlocks is the maximum number of totally free blocks
246 //!that the adaptive node pool will hold. The rest of the totally free blocks will be
247 //!deallocated with the segment manager.
248 //!
249 //!OverheadPercent is the (approximated) maximum size overhead (1-20%) of the allocator:
250 //!(memory usable for nodes / total memory allocated from the segment manager)
251 template < class T
252 , class SegmentManager
253 , std::size_t NodesPerBlock
254 , std::size_t MaxFreeBlocks
255 , unsigned char OverheadPercent
256 >
257 class adaptive_pool
258 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
259 : public ipcdetail::adaptive_pool_base
260 < 2
261 , T
262 , SegmentManager
263 , NodesPerBlock
264 , MaxFreeBlocks
265 , OverheadPercent
266 >
267 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
268 {
269
270 #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
271 typedef ipcdetail::adaptive_pool_base
272 < 2, T, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> base_t;
273 public:
274 typedef boost::interprocess::version_type<adaptive_pool, 2> version;
275
276 template<class T2>
277 struct rebind
278 {
279 typedef adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
280 };
281
282 adaptive_pool(SegmentManager *segment_mngr)
283 : base_t(segment_mngr)
284 {}
285
286 template<class T2>
287 adaptive_pool
288 (const adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other)
289 : base_t(other)
290 {}
291
292 #else //BOOST_INTERPROCESS_DOXYGEN_INVOKED
293 public:
294 typedef implementation_defined::segment_manager segment_manager;
295 typedef segment_manager::void_pointer void_pointer;
296 typedef implementation_defined::pointer pointer;
297 typedef implementation_defined::const_pointer const_pointer;
298 typedef T value_type;
299 typedef typename ipcdetail::add_reference
300 <value_type>::type reference;
301 typedef typename ipcdetail::add_reference
302 <const value_type>::type const_reference;
303 typedef typename segment_manager::size_type size_type;
304 typedef typename segment_manager::difference_type difference_type;
305
306 //!Obtains adaptive_pool from
307 //!adaptive_pool
308 template<class T2>
309 struct rebind
310 {
311 typedef adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
312 };
313
314 private:
315 //!Not assignable from
316 //!related adaptive_pool
317 template<class T2, class SegmentManager2, std::size_t N2, std::size_t F2, unsigned char OP2>
318 adaptive_pool& operator=
319 (const adaptive_pool<T2, SegmentManager2, N2, F2, OP2>&);
320
321 //!Not assignable from
322 //!other adaptive_pool
323 //adaptive_pool& operator=(const adaptive_pool&);
324
325 public:
326 //!Constructor from a segment manager. If not present, constructs a node
327 //!pool. Increments the reference count of the associated node pool.
328 //!Can throw boost::interprocess::bad_alloc
329 adaptive_pool(segment_manager *segment_mngr);
330
331 //!Copy constructor from other adaptive_pool. Increments the reference
332 //!count of the associated node pool. Never throws
333 adaptive_pool(const adaptive_pool &other);
334
335 //!Copy constructor from related adaptive_pool. If not present, constructs
336 //!a node pool. Increments the reference count of the associated node pool.
337 //!Can throw boost::interprocess::bad_alloc
338 template<class T2>
339 adaptive_pool
340 (const adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other);
341
342 //!Destructor, removes node_pool_t from memory
343 //!if its reference count reaches to zero. Never throws
344 ~adaptive_pool();
345
346 //!Returns a pointer to the node pool.
347 //!Never throws
348 void* get_node_pool() const;
349
350 //!Returns the segment manager.
351 //!Never throws
352 segment_manager* get_segment_manager()const;
353
354 //!Returns the number of elements that could be allocated.
355 //!Never throws
356 size_type max_size() const;
357
358 //!Allocate memory for an array of count elements.
359 //!Throws boost::interprocess::bad_alloc if there is no enough memory
360 pointer allocate(size_type count, cvoid_pointer hint = 0);
361
362 //!Deallocate allocated memory.
363 //!Never throws
364 void deallocate(const pointer &ptr, size_type count);
365
366 //!Deallocates all free blocks
367 //!of the pool
368 void deallocate_free_blocks();
369
370 //!Swaps allocators. Does not throw. If each allocator is placed in a
371 //!different memory segment, the result is undefined.
372 friend void swap(self_t &alloc1, self_t &alloc2);
373
374 //!Returns address of mutable object.
375 //!Never throws
376 pointer address(reference value) const;
377
378 //!Returns address of non mutable object.
379 //!Never throws
380 const_pointer address(const_reference value) const;
381 /*
382 //!Copy construct an object.
383 //!Throws if T's copy constructor throws
384 void construct(const pointer &ptr, const_reference v);
385
386 //!Destroys object. Throws if object's
387 //!destructor throws
388 void destroy(const pointer &ptr);
389 */
390 //!Returns maximum the number of objects the previously allocated memory
391 //!pointed by p can hold. This size only works for memory allocated with
392 //!allocate, allocation_command and allocate_many.
393 size_type size(const pointer &p) const;
394
395 pointer allocation_command(boost::interprocess::allocation_type command,
396 size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse);
397
398 //!Allocates many elements of size elem_size in a contiguous block
399 //!of memory. The minimum number to be allocated is min_elements,
400 //!the preferred and maximum number is
401 //!preferred_elements. The number of actually allocated elements is
402 //!will be assigned to received_size. The elements must be deallocated
403 //!with deallocate(...)
404 void allocate_many(size_type elem_size, size_type num_elements, multiallocation_chain &chain);
405
406 //!Allocates n_elements elements, each one of size elem_sizes[i]in a
407 //!contiguous block
408 //!of memory. The elements must be deallocated
409 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain);
410
411 //!Allocates many elements of size elem_size in a contiguous block
412 //!of memory. The minimum number to be allocated is min_elements,
413 //!the preferred and maximum number is
414 //!preferred_elements. The number of actually allocated elements is
415 //!will be assigned to received_size. The elements must be deallocated
416 //!with deallocate(...)
417 void deallocate_many(multiallocation_chain &chain);
418
419 //!Allocates just one object. Memory allocated with this function
420 //!must be deallocated only with deallocate_one().
421 //!Throws boost::interprocess::bad_alloc if there is no enough memory
422 pointer allocate_one();
423
424 //!Allocates many elements of size == 1 in a contiguous block
425 //!of memory. The minimum number to be allocated is min_elements,
426 //!the preferred and maximum number is
427 //!preferred_elements. The number of actually allocated elements is
428 //!will be assigned to received_size. Memory allocated with this function
429 //!must be deallocated only with deallocate_one().
430 void allocate_individual(size_type num_elements, multiallocation_chain &chain);
431
432 //!Deallocates memory previously allocated with allocate_one().
433 //!You should never use deallocate_one to deallocate memory allocated
434 //!with other functions different from allocate_one(). Never throws
435 void deallocate_one(const pointer &p);
436
437 //!Allocates many elements of size == 1 in a contiguous block
438 //!of memory. The minimum number to be allocated is min_elements,
439 //!the preferred and maximum number is
440 //!preferred_elements. The number of actually allocated elements is
441 //!will be assigned to received_size. Memory allocated with this function
442 //!must be deallocated only with deallocate_one().
443 void deallocate_individual(multiallocation_chain &chain);
444 #endif
445 };
446
447 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
448
449 //!Equality test for same type
450 //!of adaptive_pool
451 template<class T, class S, std::size_t NodesPerBlock, std::size_t F, unsigned char OP> inline
452 bool operator==(const adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc1,
453 const adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc2);
454
455 //!Inequality test for same type
456 //!of adaptive_pool
457 template<class T, class S, std::size_t NodesPerBlock, std::size_t F, unsigned char OP> inline
458 bool operator!=(const adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc1,
459 const adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc2);
460
461 #endif
462
463 } //namespace interprocess {
464 } //namespace boost {
465
466 #include <boost/interprocess/detail/config_end.hpp>
467
468 #endif //#ifndef BOOST_INTERPROCESS_ADAPTIVE_POOL_HPP
469