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 container_detail::unvoid_ref<T>::type reference;
87 typedef typename ::boost::container::
88 container_detail::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::container_detail::
93 version_type<self_t, Version> version;
95 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
96 typedef boost::container::container_detail::
97 basic_multiallocation_chain<void*> multiallocation_chain_void;
98 typedef boost::container::container_detail::
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, unsigned Version2, std::size_t N2, std::size_t F2>
121 adaptive_pool& operator=
122 (const adaptive_pool<T2, Version2, N2, F2>&);
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)/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 > this->max_size()))
156 boost::container::throw_bad_alloc();
158 if(Version == 1 && count == 1){
159 typedef typename container_detail::shared_adaptive_node_pool
160 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
161 typedef container_detail::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 container_detail::shared_adaptive_node_pool
176 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
177 typedef container_detail::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 container_detail::shared_adaptive_node_pool
207 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
208 typedef container_detail::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 container_detail::shared_adaptive_node_pool
217 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
218 typedef container_detail::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 container_detail::shared_adaptive_node_pool
232 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
233 typedef container_detail::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 container_detail::shared_adaptive_node_pool
240 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
241 typedef container_detail::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 container_detail::shared_adaptive_node_pool
301 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
302 typedef container_detail::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;
344 } //namespace container {
345 } //namespace boost {
347 #include <boost/container/detail/config_end.hpp>
349 #endif //#ifndef BOOST_CONTAINER_ADAPTIVE_POOL_HPP