1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2013. 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/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_CONTAINER_ADAPTIVE_POOL_HPP
12 #define BOOST_CONTAINER_ADAPTIVE_POOL_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/container/detail/config_begin.hpp>
23 #include <boost/container/detail/workaround.hpp>
24 #include <boost/container/container_fwd.hpp>
25 #include <boost/container/detail/version_type.hpp>
26 #include <boost/container/throw_exception.hpp>
27 #include <boost/container/detail/adaptive_node_pool.hpp>
28 #include <boost/container/detail/multiallocation_chain.hpp>
29 #include <boost/container/detail/mpl.hpp>
30 #include <boost/container/detail/dlmalloc.hpp>
31 #include <boost/container/detail/singleton.hpp>
32 #include <boost/container/detail/placement_new.hpp>
34 #include <boost/assert.hpp>
35 #include <boost/static_assert.hpp>
36 #include <boost/move/utility_core.hpp>
43 //!An STL node allocator that uses a modified DLMalloc as memory
46 //!This node allocator shares a segregated storage between all instances
47 //!of adaptive_pool with equal sizeof(T).
49 //!NodesPerBlock is the number of nodes allocated at once when the allocator
50 //!needs runs out of nodes. MaxFreeBlocks is the maximum number of totally free blocks
51 //!that the adaptive node pool will hold. The rest of the totally free blocks will be
52 //!deallocated to the memory manager.
54 //!OverheadPercent is the (approximated) maximum size overhead (1-20%) of the allocator:
55 //!(memory usable for nodes / total memory allocated from the memory allocator)
57 , std::size_t NodesPerBlock BOOST_CONTAINER_DOCONLY(= ADP_nodes_per_block)
58 , std::size_t MaxFreeBlocks BOOST_CONTAINER_DOCONLY(= ADP_max_free_blocks)
59 , std::size_t OverheadPercent BOOST_CONTAINER_DOCONLY(= ADP_overhead_percent)
60 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I unsigned Version)
64 //!If Version is 1, the allocator is a STL conforming allocator. If Version is 2,
65 //!the allocator offers advanced expand in place and burst allocation capabilities.
67 typedef unsigned int allocation_type;
69 <T, NodesPerBlock, MaxFreeBlocks, OverheadPercent
70 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version)
73 static const std::size_t nodes_per_block = NodesPerBlock;
74 static const std::size_t max_free_blocks = MaxFreeBlocks;
75 static const std::size_t overhead_percent = OverheadPercent;
76 static const std::size_t real_nodes_per_block = NodesPerBlock;
78 BOOST_CONTAINER_DOCIGN(BOOST_STATIC_ASSERT((Version <=2)));
84 typedef const T * const_pointer;
85 typedef typename ::boost::container::
86 dtl::unvoid_ref<T>::type reference;
87 typedef typename ::boost::container::
88 dtl::unvoid_ref<const T>::type const_reference;
89 typedef std::size_t size_type;
90 typedef std::ptrdiff_t difference_type;
92 typedef boost::container::dtl::
93 version_type<self_t, Version> version;
95 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
96 typedef boost::container::dtl::
97 basic_multiallocation_chain<void*> multiallocation_chain_void;
98 typedef boost::container::dtl::
99 transform_multiallocation_chain
100 <multiallocation_chain_void, T> multiallocation_chain;
101 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
103 //!Obtains adaptive_pool from
108 typedef adaptive_pool
113 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version)
117 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
119 //!Not assignable from related adaptive_pool
120 template<class T2, std::size_t N2, std::size_t F2, std::size_t O2, unsigned Version2>
121 adaptive_pool& operator=
122 (const adaptive_pool<T2, N2, F2, O2, Version2>&);
124 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
127 //!Default constructor
128 adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW
131 //!Copy constructor from other adaptive_pool.
132 adaptive_pool(const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
135 //!Copy constructor from related adaptive_pool.
138 (const adaptive_pool<T2, NodesPerBlock, MaxFreeBlocks, OverheadPercent
139 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version)> &) BOOST_NOEXCEPT_OR_NOTHROW
143 ~adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW
146 //!Returns the number of elements that could be allocated.
148 size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
149 { return size_type(-1)/(2u*sizeof(T)); }
151 //!Allocate memory for an array of count elements.
152 //!Throws std::bad_alloc if there is no enough memory
153 pointer allocate(size_type count, const void * = 0)
155 if(BOOST_UNLIKELY(count > size_type(-1)/(2u*sizeof(T))))
156 boost::container::throw_bad_alloc();
158 if(Version == 1 && count == 1){
159 typedef typename dtl::shared_adaptive_node_pool
160 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
161 typedef dtl::singleton_default<shared_pool_t> singleton_t;
162 return pointer(static_cast<T*>(singleton_t::instance().allocate_node()));
165 return static_cast<pointer>(dlmalloc_malloc(count*sizeof(T)));
169 //!Deallocate allocated memory.
171 void deallocate(const pointer &ptr, size_type count) BOOST_NOEXCEPT_OR_NOTHROW
174 if(Version == 1 && count == 1){
175 typedef dtl::shared_adaptive_node_pool
176 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
177 typedef dtl::singleton_default<shared_pool_t> singleton_t;
178 singleton_t::instance().deallocate_node(ptr);
185 pointer allocation_command(allocation_type command,
186 size_type limit_size,
187 size_type &prefer_in_recvd_out_size,
190 pointer ret = this->priv_allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse);
191 if(BOOST_UNLIKELY(!ret && !(command & BOOST_CONTAINER_NOTHROW_ALLOCATION)))
192 boost::container::throw_bad_alloc();
196 //!Returns maximum the number of objects the previously allocated memory
197 //!pointed by p can hold.
198 size_type size(pointer p) const BOOST_NOEXCEPT_OR_NOTHROW
199 { return dlmalloc_size(p); }
201 //!Allocates just one object. Memory allocated with this function
202 //!must be deallocated only with deallocate_one().
203 //!Throws bad_alloc if there is no enough memory
204 pointer allocate_one()
206 typedef dtl::shared_adaptive_node_pool
207 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
208 typedef dtl::singleton_default<shared_pool_t> singleton_t;
209 return (pointer)singleton_t::instance().allocate_node();
212 //!Allocates many elements of size == 1.
213 //!Elements must be individually deallocated with deallocate_one()
214 void allocate_individual(std::size_t num_elements, multiallocation_chain &chain)
216 typedef dtl::shared_adaptive_node_pool
217 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
218 typedef dtl::singleton_default<shared_pool_t> singleton_t;
219 singleton_t::instance().allocate_nodes(num_elements, static_cast<typename shared_pool_t::multiallocation_chain&>(chain));
220 //typename shared_pool_t::multiallocation_chain ch;
221 //singleton_t::instance().allocate_nodes(num_elements, ch);
222 //chain.incorporate_after
223 //(chain.before_begin(), (T*)&*ch.begin(), (T*)&*ch.last(), ch.size());
226 //!Deallocates memory previously allocated with allocate_one().
227 //!You should never use deallocate_one to deallocate memory allocated
228 //!with other functions different from allocate_one(). Never throws
229 void deallocate_one(pointer p) BOOST_NOEXCEPT_OR_NOTHROW
231 typedef dtl::shared_adaptive_node_pool
232 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
233 typedef dtl::singleton_default<shared_pool_t> singleton_t;
234 singleton_t::instance().deallocate_node(p);
237 void deallocate_individual(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
239 typedef dtl::shared_adaptive_node_pool
240 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
241 typedef dtl::singleton_default<shared_pool_t> singleton_t;
242 //typename shared_pool_t::multiallocation_chain ch(&*chain.begin(), &*chain.last(), chain.size());
243 //singleton_t::instance().deallocate_nodes(ch);
244 singleton_t::instance().deallocate_nodes(chain);
247 //!Allocates many elements of size elem_size.
248 //!Elements must be individually deallocated with deallocate()
249 void allocate_many(size_type elem_size, std::size_t n_elements, multiallocation_chain &chain)
251 BOOST_STATIC_ASSERT(( Version > 1 ));/*
252 dlmalloc_memchain ch;
253 BOOST_CONTAINER_MEMCHAIN_INIT(&ch);
254 if(BOOST_UNLIKELY(!dlmalloc_multialloc_nodes(n_elements, elem_size*sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch))){
255 boost::container::throw_bad_alloc();
257 chain.incorporate_after(chain.before_begin()
258 ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch)
259 ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch)
260 ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );*/
261 if(BOOST_UNLIKELY(!dlmalloc_multialloc_nodes
262 (n_elements, elem_size*sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<dlmalloc_memchain *>(&chain)))){
263 boost::container::throw_bad_alloc();
267 //!Allocates n_elements elements, each one of size elem_sizes[i]
268 //!Elements must be individually deallocated with deallocate()
269 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain)
271 BOOST_STATIC_ASSERT(( Version > 1 ));/*
272 dlmalloc_memchain ch;
273 BOOST_CONTAINER_MEMCHAIN_INIT(&ch);
274 if(BOOST_UNLIKELY(!dlmalloc_multialloc_arrays(n_elements, elem_sizes, sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch))){
275 boost::container::throw_bad_alloc();
277 chain.incorporate_after(chain.before_begin()
278 ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch)
279 ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch)
280 ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );*/
281 if(BOOST_UNLIKELY(!dlmalloc_multialloc_arrays
282 (n_elements, elem_sizes, sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<dlmalloc_memchain *>(&chain)))){
283 boost::container::throw_bad_alloc();
287 void deallocate_many(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
289 dlmalloc_memchain ch;
290 void *beg(&*chain.begin()), *last(&*chain.last());
291 size_t size(chain.size());
292 BOOST_CONTAINER_MEMCHAIN_INIT_FROM(&ch, beg, last, size);
293 dlmalloc_multidealloc(&ch);*/
294 dlmalloc_multidealloc(reinterpret_cast<dlmalloc_memchain *>(&chain));
297 //!Deallocates all free blocks of the pool
298 static void deallocate_free_blocks() BOOST_NOEXCEPT_OR_NOTHROW
300 typedef dtl::shared_adaptive_node_pool
301 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
302 typedef dtl::singleton_default<shared_pool_t> singleton_t;
303 singleton_t::instance().deallocate_free_blocks();
306 //!Swaps allocators. Does not throw. If each allocator is placed in a
307 //!different memory segment, the result is undefined.
308 friend void swap(adaptive_pool &, adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
311 //!An allocator always compares to true, as memory allocated with one
312 //!instance can be deallocated by another instance
313 friend bool operator==(const adaptive_pool &, const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
316 //!An allocator always compares to false, as memory allocated with one
317 //!instance can be deallocated by another instance
318 friend bool operator!=(const adaptive_pool &, const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
322 pointer priv_allocation_command
323 (allocation_type command, std::size_t limit_size
324 ,size_type &prefer_in_recvd_out_size, pointer &reuse_ptr)
326 std::size_t const preferred_size = prefer_in_recvd_out_size;
327 dlmalloc_command_ret_t ret = {0 , 0};
328 if(BOOST_UNLIKELY(limit_size > this->max_size() || preferred_size > this->max_size())){
331 std::size_t l_size = limit_size*sizeof(T);
332 std::size_t p_size = preferred_size*sizeof(T);
335 void* reuse_ptr_void = reuse_ptr;
336 ret = dlmalloc_allocation_command(command, sizeof(T), l_size, p_size, &r_size, reuse_ptr_void);
337 reuse_ptr = ret.second ? static_cast<T*>(reuse_ptr_void) : 0;
339 prefer_in_recvd_out_size = r_size/sizeof(T);
340 return (pointer)ret.first;
364 , std::size_t NodesPerBlock = ADP_nodes_per_block
365 , std::size_t MaxFreeBlocks = ADP_max_free_blocks
366 , std::size_t OverheadPercent = ADP_overhead_percent
367 , unsigned Version = 2
369 class private_adaptive_pool
371 //!If Version is 1, the allocator is a STL conforming allocator. If Version is 2,
372 //!the allocator offers advanced expand in place and burst allocation capabilities.
374 typedef unsigned int allocation_type;
375 typedef private_adaptive_pool
376 <T, NodesPerBlock, MaxFreeBlocks, OverheadPercent
377 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version)
380 static const std::size_t nodes_per_block = NodesPerBlock;
381 static const std::size_t max_free_blocks = MaxFreeBlocks;
382 static const std::size_t overhead_percent = OverheadPercent;
383 static const std::size_t real_nodes_per_block = NodesPerBlock;
385 BOOST_CONTAINER_DOCIGN(BOOST_STATIC_ASSERT((Version <=2)));
387 typedef dtl::private_adaptive_node_pool
388 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> pool_t;
393 typedef T value_type;
395 typedef const T * const_pointer;
396 typedef typename ::boost::container::
397 dtl::unvoid_ref<T>::type reference;
398 typedef typename ::boost::container::
399 dtl::unvoid_ref<const T>::type const_reference;
400 typedef std::size_t size_type;
401 typedef std::ptrdiff_t difference_type;
403 typedef boost::container::dtl::
404 version_type<self_t, Version> version;
406 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
407 typedef boost::container::dtl::
408 basic_multiallocation_chain<void*> multiallocation_chain_void;
409 typedef boost::container::dtl::
410 transform_multiallocation_chain
411 <multiallocation_chain_void, T> multiallocation_chain;
412 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
414 //!Obtains private_adaptive_pool from
415 //!private_adaptive_pool
419 typedef private_adaptive_pool
424 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version)
428 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
430 //!Not assignable from related private_adaptive_pool
431 template<class T2, std::size_t N2, std::size_t F2, std::size_t O2, unsigned Version2>
432 private_adaptive_pool& operator=
433 (const private_adaptive_pool<T2, N2, F2, O2, Version2>&);
434 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
437 //!Default constructor
438 private_adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW
441 //!Copy constructor from other private_adaptive_pool.
442 private_adaptive_pool(const private_adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
445 //!Copy constructor from related private_adaptive_pool.
447 private_adaptive_pool
448 (const private_adaptive_pool<T2, NodesPerBlock, MaxFreeBlocks, OverheadPercent
449 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version)> &) BOOST_NOEXCEPT_OR_NOTHROW
453 ~private_adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW
456 //!Returns the number of elements that could be allocated.
458 size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
459 { return size_type(-1)/(2u*sizeof(T)); }
461 //!Allocate memory for an array of count elements.
462 //!Throws std::bad_alloc if there is no enough memory
463 pointer allocate(size_type count, const void * = 0)
465 if(BOOST_UNLIKELY(count > size_type(-1)/(2u*sizeof(T))))
466 boost::container::throw_bad_alloc();
468 if(Version == 1 && count == 1){
469 return pointer(static_cast<T*>(m_pool.allocate_node()));
472 return static_cast<pointer>(dlmalloc_malloc(count*sizeof(T)));
476 //!Deallocate allocated memory.
478 void deallocate(const pointer &ptr, size_type count) BOOST_NOEXCEPT_OR_NOTHROW
481 if(Version == 1 && count == 1){
482 m_pool.deallocate_node(ptr);
489 pointer allocation_command(allocation_type command,
490 size_type limit_size,
491 size_type &prefer_in_recvd_out_size,
494 pointer ret = this->priv_allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse);
495 if(BOOST_UNLIKELY(!ret && !(command & BOOST_CONTAINER_NOTHROW_ALLOCATION)))
496 boost::container::throw_bad_alloc();
500 //!Returns maximum the number of objects the previously allocated memory
501 //!pointed by p can hold.
502 size_type size(pointer p) const BOOST_NOEXCEPT_OR_NOTHROW
503 { return dlmalloc_size(p); }
505 //!Allocates just one object. Memory allocated with this function
506 //!must be deallocated only with deallocate_one().
507 //!Throws bad_alloc if there is no enough memory
508 pointer allocate_one()
510 return (pointer)m_pool.allocate_node();
513 //!Allocates many elements of size == 1.
514 //!Elements must be individually deallocated with deallocate_one()
515 void allocate_individual(std::size_t num_elements, multiallocation_chain &chain)
517 m_pool.allocate_nodes(num_elements, static_cast<typename pool_t::multiallocation_chain&>(chain));
520 //!Deallocates memory previously allocated with allocate_one().
521 //!You should never use deallocate_one to deallocate memory allocated
522 //!with other functions different from allocate_one(). Never throws
523 void deallocate_one(pointer p) BOOST_NOEXCEPT_OR_NOTHROW
525 m_pool.deallocate_node(p);
528 void deallocate_individual(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
530 m_pool.deallocate_nodes(chain);
533 //!Allocates many elements of size elem_size.
534 //!Elements must be individually deallocated with deallocate()
535 void allocate_many(size_type elem_size, std::size_t n_elements, multiallocation_chain &chain)
537 BOOST_STATIC_ASSERT(( Version > 1 ));
538 if(BOOST_UNLIKELY(!dlmalloc_multialloc_nodes
539 (n_elements, elem_size*sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<dlmalloc_memchain *>(&chain)))){
540 boost::container::throw_bad_alloc();
544 //!Allocates n_elements elements, each one of size elem_sizes[i]
545 //!Elements must be individually deallocated with deallocate()
546 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain)
548 BOOST_STATIC_ASSERT(( Version > 1 ));
549 if(BOOST_UNLIKELY(!dlmalloc_multialloc_arrays
550 (n_elements, elem_sizes, sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<dlmalloc_memchain *>(&chain)))){
551 boost::container::throw_bad_alloc();
555 void deallocate_many(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
557 dlmalloc_multidealloc(reinterpret_cast<dlmalloc_memchain *>(&chain));
560 //!Deallocates all free blocks of the pool
561 void deallocate_free_blocks() BOOST_NOEXCEPT_OR_NOTHROW
563 m_pool.deallocate_free_blocks();
566 //!Swaps allocators. Does not throw. If each allocator is placed in a
567 //!different memory segment, the result is undefined.
568 friend void swap(private_adaptive_pool &, private_adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
571 //!An allocator always compares to true, as memory allocated with one
572 //!instance can be deallocated by another instance
573 friend bool operator==(const private_adaptive_pool &, const private_adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
576 //!An allocator always compares to false, as memory allocated with one
577 //!instance can be deallocated by another instance
578 friend bool operator!=(const private_adaptive_pool &, const private_adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
582 pointer priv_allocation_command
583 (allocation_type command, std::size_t limit_size
584 ,size_type &prefer_in_recvd_out_size, pointer &reuse_ptr)
586 std::size_t const preferred_size = prefer_in_recvd_out_size;
587 dlmalloc_command_ret_t ret = {0 , 0};
588 if(BOOST_UNLIKELY(limit_size > this->max_size() || preferred_size > this->max_size())){
591 std::size_t l_size = limit_size*sizeof(T);
592 std::size_t p_size = preferred_size*sizeof(T);
595 void* reuse_ptr_void = reuse_ptr;
596 ret = dlmalloc_allocation_command(command, sizeof(T), l_size, p_size, &r_size, reuse_ptr_void);
597 reuse_ptr = ret.second ? static_cast<T*>(reuse_ptr_void) : 0;
599 prefer_in_recvd_out_size = r_size/sizeof(T);
600 return (pointer)ret.first;
604 } //namespace container {
605 } //namespace boost {
607 #include <boost/container/detail/config_end.hpp>
609 #endif //#ifndef BOOST_CONTAINER_ADAPTIVE_POOL_HPP