]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/container/adaptive_pool.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / container / adaptive_pool.hpp
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/dlmalloc.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_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;
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 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
125
126 public:
127 //!Default constructor
128 adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW
129 {}
130
131 //!Copy constructor from other adaptive_pool.
132 adaptive_pool(const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
133 {}
134
135 //!Copy constructor from related adaptive_pool.
136 template<class T2>
137 adaptive_pool
138 (const adaptive_pool<T2, NodesPerBlock, MaxFreeBlocks, OverheadPercent
139 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version)> &) BOOST_NOEXCEPT_OR_NOTHROW
140 {}
141
142 //!Destructor
143 ~adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW
144 {}
145
146 //!Returns the number of elements that could be allocated.
147 //!Never throws
148 size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
149 { return size_type(-1)/sizeof(T); }
150
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)
154 {
155 if(BOOST_UNLIKELY(count > this->max_size()))
156 boost::container::throw_bad_alloc();
157
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()));
163 }
164 else{
165 return static_cast<pointer>(dlmalloc_malloc(count*sizeof(T)));
166 }
167 }
168
169 //!Deallocate allocated memory.
170 //!Never throws
171 void deallocate(const pointer &ptr, size_type count) BOOST_NOEXCEPT_OR_NOTHROW
172 {
173 (void)count;
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);
179 }
180 else{
181 dlmalloc_free(ptr);
182 }
183 }
184
185 pointer allocation_command(allocation_type command,
186 size_type limit_size,
187 size_type &prefer_in_recvd_out_size,
188 pointer &reuse)
189 {
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();
193 return ret;
194 }
195
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); }
200
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()
205 {
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();
210 }
211
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)
215 {
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());
224 }
225
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
230 {
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);
235 }
236
237 void deallocate_individual(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
238 {
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);
245 }
246
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)
250 {
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();
256 }
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();
264 }
265 }
266
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)
270 {
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();
276 }
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();
284 }
285 }
286
287 void deallocate_many(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
288 {/*
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));
295 }
296
297 //!Deallocates all free blocks of the pool
298 static void deallocate_free_blocks() BOOST_NOEXCEPT_OR_NOTHROW
299 {
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();
304 }
305
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
309 {}
310
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
314 { return true; }
315
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
319 { return false; }
320
321 private:
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)
325 {
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())){
329 return pointer();
330 }
331 std::size_t l_size = limit_size*sizeof(T);
332 std::size_t p_size = preferred_size*sizeof(T);
333 std::size_t r_size;
334 {
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;
338 }
339 prefer_in_recvd_out_size = r_size/sizeof(T);
340 return (pointer)ret.first;
341 }
342 };
343
344 } //namespace container {
345 } //namespace boost {
346
347 #include <boost/container/detail/config_end.hpp>
348
349 #endif //#ifndef BOOST_CONTAINER_ADAPTIVE_POOL_HPP