Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/generic/include/boost/container/adaptive_pool.hpp @ 102:f46d142149f5
Whoops, finish that update
author | Chris Cannam |
---|---|
date | Mon, 07 Sep 2015 11:13:41 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
101:c530137014c0 | 102:f46d142149f5 |
---|---|
1 ////////////////////////////////////////////////////////////////////////////// | |
2 // | |
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) | |
6 // | |
7 // See http://www.boost.org/libs/container for documentation. | |
8 // | |
9 ////////////////////////////////////////////////////////////////////////////// | |
10 | |
11 #ifndef BOOST_CONTAINER_ADAPTIVE_POOL_HPP | |
12 #define BOOST_CONTAINER_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/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/alloc_lib_auto_link.hpp> | |
31 #include <boost/container/detail/singleton.hpp> | |
32 #include <boost/container/detail/placement_new.hpp> | |
33 | |
34 #include <boost/assert.hpp> | |
35 #include <boost/static_assert.hpp> | |
36 #include <boost/move/utility_core.hpp> | |
37 #include <cstddef> | |
38 | |
39 | |
40 namespace boost { | |
41 namespace container { | |
42 | |
43 //!An STL node allocator that uses a modified DLMalloc as memory | |
44 //!source. | |
45 //! | |
46 //!This node allocator shares a segregated storage between all instances | |
47 //!of adaptive_pool with equal sizeof(T). | |
48 //! | |
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. | |
53 //! | |
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) | |
56 template < class T | |
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) | |
61 > | |
62 class adaptive_pool | |
63 { | |
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. | |
66 public: | |
67 typedef unsigned int allocation_type; | |
68 typedef adaptive_pool | |
69 <T, NodesPerBlock, MaxFreeBlocks, OverheadPercent | |
70 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version) | |
71 > self_t; | |
72 | |
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; | |
77 | |
78 BOOST_CONTAINER_DOCIGN(BOOST_STATIC_ASSERT((Version <=2))); | |
79 | |
80 public: | |
81 //------- | |
82 typedef T value_type; | |
83 typedef T * pointer; | |
84 typedef const T * const_pointer; | |
85 typedef typename ::boost::container:: | |
86 container_detail::unvoid<T>::type & reference; | |
87 typedef const typename ::boost::container:: | |
88 container_detail::unvoid<T>::type & const_reference; | |
89 typedef std::size_t size_type; | |
90 typedef std::ptrdiff_t difference_type; | |
91 | |
92 typedef boost::container::container_detail:: | |
93 version_type<self_t, Version> version; | |
94 | |
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 | |
102 | |
103 //!Obtains adaptive_pool from | |
104 //!adaptive_pool | |
105 template<class T2> | |
106 struct rebind | |
107 { | |
108 typedef adaptive_pool | |
109 < T2 | |
110 , NodesPerBlock | |
111 , MaxFreeBlocks | |
112 , OverheadPercent | |
113 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version) | |
114 > other; | |
115 }; | |
116 | |
117 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED | |
118 private: | |
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>&); | |
123 | |
124 //!Not assignable from other adaptive_pool | |
125 adaptive_pool& operator=(const adaptive_pool&); | |
126 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED | |
127 | |
128 public: | |
129 //!Default constructor | |
130 adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW | |
131 {} | |
132 | |
133 //!Copy constructor from other adaptive_pool. | |
134 adaptive_pool(const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW | |
135 {} | |
136 | |
137 //!Copy constructor from related adaptive_pool. | |
138 template<class T2> | |
139 adaptive_pool | |
140 (const adaptive_pool<T2, NodesPerBlock, MaxFreeBlocks, OverheadPercent | |
141 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version)> &) BOOST_NOEXCEPT_OR_NOTHROW | |
142 {} | |
143 | |
144 //!Destructor | |
145 ~adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW | |
146 {} | |
147 | |
148 //!Returns the number of elements that could be allocated. | |
149 //!Never throws | |
150 size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW | |
151 { return size_type(-1)/sizeof(T); } | |
152 | |
153 //!Allocate memory for an array of count elements. | |
154 //!Throws std::bad_alloc if there is no enough memory | |
155 pointer allocate(size_type count, const void * = 0) | |
156 { | |
157 if(BOOST_UNLIKELY(count > this->max_size())) | |
158 boost::container::throw_bad_alloc(); | |
159 | |
160 if(Version == 1 && count == 1){ | |
161 typedef typename container_detail::shared_adaptive_node_pool | |
162 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t; | |
163 typedef container_detail::singleton_default<shared_pool_t> singleton_t; | |
164 return pointer(static_cast<T*>(singleton_t::instance().allocate_node())); | |
165 } | |
166 else{ | |
167 return static_cast<pointer>(boost_cont_malloc(count*sizeof(T))); | |
168 } | |
169 } | |
170 | |
171 //!Deallocate allocated memory. | |
172 //!Never throws | |
173 void deallocate(const pointer &ptr, size_type count) BOOST_NOEXCEPT_OR_NOTHROW | |
174 { | |
175 (void)count; | |
176 if(Version == 1 && count == 1){ | |
177 typedef container_detail::shared_adaptive_node_pool | |
178 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t; | |
179 typedef container_detail::singleton_default<shared_pool_t> singleton_t; | |
180 singleton_t::instance().deallocate_node(ptr); | |
181 } | |
182 else{ | |
183 boost_cont_free(ptr); | |
184 } | |
185 } | |
186 | |
187 pointer allocation_command(allocation_type command, | |
188 size_type limit_size, | |
189 size_type &prefer_in_recvd_out_size, | |
190 pointer &reuse) | |
191 { | |
192 pointer ret = this->priv_allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse); | |
193 if(BOOST_UNLIKELY(!ret && !(command & BOOST_CONTAINER_NOTHROW_ALLOCATION))) | |
194 boost::container::throw_bad_alloc(); | |
195 return ret; | |
196 } | |
197 | |
198 //!Returns maximum the number of objects the previously allocated memory | |
199 //!pointed by p can hold. | |
200 size_type size(pointer p) const BOOST_NOEXCEPT_OR_NOTHROW | |
201 { return boost_cont_size(p); } | |
202 | |
203 //!Allocates just one object. Memory allocated with this function | |
204 //!must be deallocated only with deallocate_one(). | |
205 //!Throws bad_alloc if there is no enough memory | |
206 pointer allocate_one() | |
207 { | |
208 typedef container_detail::shared_adaptive_node_pool | |
209 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t; | |
210 typedef container_detail::singleton_default<shared_pool_t> singleton_t; | |
211 return (pointer)singleton_t::instance().allocate_node(); | |
212 } | |
213 | |
214 //!Allocates many elements of size == 1. | |
215 //!Elements must be individually deallocated with deallocate_one() | |
216 void allocate_individual(std::size_t num_elements, multiallocation_chain &chain) | |
217 { | |
218 typedef container_detail::shared_adaptive_node_pool | |
219 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t; | |
220 typedef container_detail::singleton_default<shared_pool_t> singleton_t; | |
221 singleton_t::instance().allocate_nodes(num_elements, static_cast<typename shared_pool_t::multiallocation_chain&>(chain)); | |
222 //typename shared_pool_t::multiallocation_chain ch; | |
223 //singleton_t::instance().allocate_nodes(num_elements, ch); | |
224 //chain.incorporate_after | |
225 //(chain.before_begin(), (T*)&*ch.begin(), (T*)&*ch.last(), ch.size()); | |
226 } | |
227 | |
228 //!Deallocates memory previously allocated with allocate_one(). | |
229 //!You should never use deallocate_one to deallocate memory allocated | |
230 //!with other functions different from allocate_one(). Never throws | |
231 void deallocate_one(pointer p) BOOST_NOEXCEPT_OR_NOTHROW | |
232 { | |
233 typedef container_detail::shared_adaptive_node_pool | |
234 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t; | |
235 typedef container_detail::singleton_default<shared_pool_t> singleton_t; | |
236 singleton_t::instance().deallocate_node(p); | |
237 } | |
238 | |
239 void deallocate_individual(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW | |
240 { | |
241 typedef container_detail::shared_adaptive_node_pool | |
242 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t; | |
243 typedef container_detail::singleton_default<shared_pool_t> singleton_t; | |
244 //typename shared_pool_t::multiallocation_chain ch(&*chain.begin(), &*chain.last(), chain.size()); | |
245 //singleton_t::instance().deallocate_nodes(ch); | |
246 singleton_t::instance().deallocate_nodes(chain); | |
247 } | |
248 | |
249 //!Allocates many elements of size elem_size. | |
250 //!Elements must be individually deallocated with deallocate() | |
251 void allocate_many(size_type elem_size, std::size_t n_elements, multiallocation_chain &chain) | |
252 { | |
253 BOOST_STATIC_ASSERT(( Version > 1 ));/* | |
254 boost_cont_memchain ch; | |
255 BOOST_CONTAINER_MEMCHAIN_INIT(&ch); | |
256 if(BOOST_UNLIKELY(!boost_cont_multialloc_nodes(n_elements, elem_size*sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch))){ | |
257 boost::container::throw_bad_alloc(); | |
258 } | |
259 chain.incorporate_after(chain.before_begin() | |
260 ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch) | |
261 ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch) | |
262 ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );*/ | |
263 if(BOOST_UNLIKELY(!boost_cont_multialloc_nodes | |
264 (n_elements, elem_size*sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<boost_cont_memchain *>(&chain)))){ | |
265 boost::container::throw_bad_alloc(); | |
266 } | |
267 } | |
268 | |
269 //!Allocates n_elements elements, each one of size elem_sizes[i] | |
270 //!Elements must be individually deallocated with deallocate() | |
271 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain) | |
272 { | |
273 BOOST_STATIC_ASSERT(( Version > 1 ));/* | |
274 boost_cont_memchain ch; | |
275 BOOST_CONTAINER_MEMCHAIN_INIT(&ch); | |
276 if(BOOST_UNLIKELY(!boost_cont_multialloc_arrays(n_elements, elem_sizes, sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch))){ | |
277 boost::container::throw_bad_alloc(); | |
278 } | |
279 chain.incorporate_after(chain.before_begin() | |
280 ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch) | |
281 ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch) | |
282 ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );*/ | |
283 if(BOOST_UNLIKELY(!boost_cont_multialloc_arrays | |
284 (n_elements, elem_sizes, sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<boost_cont_memchain *>(&chain)))){ | |
285 boost::container::throw_bad_alloc(); | |
286 } | |
287 } | |
288 | |
289 void deallocate_many(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW | |
290 {/* | |
291 boost_cont_memchain ch; | |
292 void *beg(&*chain.begin()), *last(&*chain.last()); | |
293 size_t size(chain.size()); | |
294 BOOST_CONTAINER_MEMCHAIN_INIT_FROM(&ch, beg, last, size); | |
295 boost_cont_multidealloc(&ch);*/ | |
296 boost_cont_multidealloc(reinterpret_cast<boost_cont_memchain *>(&chain)); | |
297 } | |
298 | |
299 //!Deallocates all free blocks of the pool | |
300 static void deallocate_free_blocks() BOOST_NOEXCEPT_OR_NOTHROW | |
301 { | |
302 typedef container_detail::shared_adaptive_node_pool | |
303 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t; | |
304 typedef container_detail::singleton_default<shared_pool_t> singleton_t; | |
305 singleton_t::instance().deallocate_free_blocks(); | |
306 } | |
307 | |
308 //!Swaps allocators. Does not throw. If each allocator is placed in a | |
309 //!different memory segment, the result is undefined. | |
310 friend void swap(adaptive_pool &, adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW | |
311 {} | |
312 | |
313 //!An allocator always compares to true, as memory allocated with one | |
314 //!instance can be deallocated by another instance | |
315 friend bool operator==(const adaptive_pool &, const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW | |
316 { return true; } | |
317 | |
318 //!An allocator always compares to false, as memory allocated with one | |
319 //!instance can be deallocated by another instance | |
320 friend bool operator!=(const adaptive_pool &, const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW | |
321 { return false; } | |
322 | |
323 private: | |
324 pointer priv_allocation_command | |
325 (allocation_type command, std::size_t limit_size | |
326 ,size_type &prefer_in_recvd_out_size, pointer &reuse_ptr) | |
327 { | |
328 std::size_t const preferred_size = prefer_in_recvd_out_size; | |
329 boost_cont_command_ret_t ret = {0 , 0}; | |
330 if(BOOST_UNLIKELY(limit_size > this->max_size() || preferred_size > this->max_size())){ | |
331 return pointer(); | |
332 } | |
333 std::size_t l_size = limit_size*sizeof(T); | |
334 std::size_t p_size = preferred_size*sizeof(T); | |
335 std::size_t r_size; | |
336 { | |
337 void* reuse_ptr_void = reuse_ptr; | |
338 ret = boost_cont_allocation_command(command, sizeof(T), l_size, p_size, &r_size, reuse_ptr_void); | |
339 reuse_ptr = ret.second ? static_cast<T*>(reuse_ptr_void) : 0; | |
340 } | |
341 prefer_in_recvd_out_size = r_size/sizeof(T); | |
342 return (pointer)ret.first; | |
343 } | |
344 }; | |
345 | |
346 } //namespace container { | |
347 } //namespace boost { | |
348 | |
349 #include <boost/container/detail/config_end.hpp> | |
350 | |
351 #endif //#ifndef BOOST_CONTAINER_ADAPTIVE_POOL_HPP |