1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_ADAPTIVE_POOL_HPP
12 #define BOOST_INTERPROCESS_ADAPTIVE_POOL_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
25 #include <boost/intrusive/pointer_traits.hpp>
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>
42 //!Describes adaptive_pool pooled shared memory STL compatible allocator
45 namespace interprocess {
47 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
51 template < unsigned int Version
53 , class SegmentManager
54 , std::size_t NodesPerBlock
55 , std::size_t MaxFreeBlocks
56 , unsigned char OverheadPercent
58 class adaptive_pool_base
59 : public node_pool_allocation_impl
61 < Version, T, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent>
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;
73 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
78 typedef ipcdetail::shared_adaptive_node_pool
79 < SegmentManager, sizeof_value<T>::value, NodesPerBlock, MaxFreeBlocks, OverheadPercent> type;
81 static type *get(void *p)
82 { return static_cast<type*>(p); }
84 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
86 BOOST_STATIC_ASSERT((Version <=2));
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;
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;
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;
108 //!Obtains adaptive_pool_base from
109 //!adaptive_pool_base
113 typedef adaptive_pool_base<Version, T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
116 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
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>&);
123 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
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)) { }
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())
137 node_pool<0>::get(ipcdetail::to_raw_pointer(mp_node_pool))->inc_ref_count();
140 //!Assignment from other adaptive_pool_base
141 adaptive_pool_base& operator=(const adaptive_pool_base &other)
143 adaptive_pool_base c(other);
144 boost::adl_move_swap(*this, c);
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
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())) { }
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))); }
161 //!Returns a pointer to the node pool.
163 void* get_node_pool() const
164 { return ipcdetail::to_raw_pointer(mp_node_pool); }
166 //!Returns the segment manager.
168 segment_manager* get_segment_manager()const
169 { return node_pool<0>::get(ipcdetail::to_raw_pointer(mp_node_pool))->get_segment_manager(); }
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); }
176 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
178 void_pointer mp_node_pool;
179 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
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(); }
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(); }
197 , class SegmentManager
198 , std::size_t NodesPerBlock = 64
199 , std::size_t MaxFreeBlocks = 2
200 , unsigned char OverheadPercent = 5
202 class adaptive_pool_v1
203 : public adaptive_pool_base
213 typedef ipcdetail::adaptive_pool_base
214 < 1, T, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> base_t;
219 typedef adaptive_pool_v1<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
222 adaptive_pool_v1(SegmentManager *segment_mngr)
223 : base_t(segment_mngr)
228 (const adaptive_pool_v1<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other)
233 } //namespace ipcdetail{
235 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
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...
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.
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)
252 , class SegmentManager
253 , std::size_t NodesPerBlock
254 , std::size_t MaxFreeBlocks
255 , unsigned char OverheadPercent
258 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
259 : public ipcdetail::adaptive_pool_base
267 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
270 #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
271 typedef ipcdetail::adaptive_pool_base
272 < 2, T, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> base_t;
274 typedef boost::interprocess::version_type<adaptive_pool, 2> version;
279 typedef adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
282 adaptive_pool(SegmentManager *segment_mngr)
283 : base_t(segment_mngr)
288 (const adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other)
292 #else //BOOST_INTERPROCESS_DOXYGEN_INVOKED
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;
306 //!Obtains adaptive_pool from
311 typedef adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
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>&);
321 //!Not assignable from
322 //!other adaptive_pool
323 //adaptive_pool& operator=(const adaptive_pool&);
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);
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);
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
340 (const adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other);
342 //!Destructor, removes node_pool_t from memory
343 //!if its reference count reaches to zero. Never throws
346 //!Returns a pointer to the node pool.
348 void* get_node_pool() const;
350 //!Returns the segment manager.
352 segment_manager* get_segment_manager()const;
354 //!Returns the number of elements that could be allocated.
356 size_type max_size() const;
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);
362 //!Deallocate allocated memory.
364 void deallocate(const pointer &ptr, size_type count);
366 //!Deallocates all free blocks
368 void deallocate_free_blocks();
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);
374 //!Returns address of mutable object.
376 pointer address(reference value) const;
378 //!Returns address of non mutable object.
380 const_pointer address(const_reference value) const;
382 //!Copy construct an object.
383 //!Throws if T's copy constructor throws
384 void construct(const pointer &ptr, const_reference v);
386 //!Destroys object. Throws if object's
388 void destroy(const pointer &ptr);
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;
395 pointer allocation_command(boost::interprocess::allocation_type command,
396 size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse);
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);
406 //!Allocates n_elements elements, each one of size elem_sizes[i]in a
408 //!of memory. The elements must be deallocated
409 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain);
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);
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();
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);
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);
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);
447 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
449 //!Equality test for same type
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);
455 //!Inequality test for same type
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);
463 } //namespace interprocess {
464 } //namespace boost {
466 #include <boost/interprocess/detail/config_end.hpp>
468 #endif //#ifndef BOOST_INTERPROCESS_ADAPTIVE_POOL_HPP