annotate DEPENDENCIES/generic/include/boost/container/adaptive_pool.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents f46d142149f5
children
rev   line source
Chris@102 1 //////////////////////////////////////////////////////////////////////////////
Chris@102 2 //
Chris@102 3 // (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost
Chris@102 4 // Software License, Version 1.0. (See accompanying file
Chris@102 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@102 6 //
Chris@102 7 // See http://www.boost.org/libs/container for documentation.
Chris@102 8 //
Chris@102 9 //////////////////////////////////////////////////////////////////////////////
Chris@102 10
Chris@102 11 #ifndef BOOST_CONTAINER_ADAPTIVE_POOL_HPP
Chris@102 12 #define BOOST_CONTAINER_ADAPTIVE_POOL_HPP
Chris@102 13
Chris@102 14 #ifndef BOOST_CONFIG_HPP
Chris@102 15 # include <boost/config.hpp>
Chris@102 16 #endif
Chris@102 17
Chris@102 18 #if defined(BOOST_HAS_PRAGMA_ONCE)
Chris@102 19 # pragma once
Chris@102 20 #endif
Chris@102 21
Chris@102 22 #include <boost/container/detail/config_begin.hpp>
Chris@102 23 #include <boost/container/detail/workaround.hpp>
Chris@102 24 #include <boost/container/container_fwd.hpp>
Chris@102 25 #include <boost/container/detail/version_type.hpp>
Chris@102 26 #include <boost/container/throw_exception.hpp>
Chris@102 27 #include <boost/container/detail/adaptive_node_pool.hpp>
Chris@102 28 #include <boost/container/detail/multiallocation_chain.hpp>
Chris@102 29 #include <boost/container/detail/mpl.hpp>
Chris@102 30 #include <boost/container/detail/alloc_lib_auto_link.hpp>
Chris@102 31 #include <boost/container/detail/singleton.hpp>
Chris@102 32 #include <boost/container/detail/placement_new.hpp>
Chris@102 33
Chris@102 34 #include <boost/assert.hpp>
Chris@102 35 #include <boost/static_assert.hpp>
Chris@102 36 #include <boost/move/utility_core.hpp>
Chris@102 37 #include <cstddef>
Chris@102 38
Chris@102 39
Chris@102 40 namespace boost {
Chris@102 41 namespace container {
Chris@102 42
Chris@102 43 //!An STL node allocator that uses a modified DLMalloc as memory
Chris@102 44 //!source.
Chris@102 45 //!
Chris@102 46 //!This node allocator shares a segregated storage between all instances
Chris@102 47 //!of adaptive_pool with equal sizeof(T).
Chris@102 48 //!
Chris@102 49 //!NodesPerBlock is the number of nodes allocated at once when the allocator
Chris@102 50 //!needs runs out of nodes. MaxFreeBlocks is the maximum number of totally free blocks
Chris@102 51 //!that the adaptive node pool will hold. The rest of the totally free blocks will be
Chris@102 52 //!deallocated to the memory manager.
Chris@102 53 //!
Chris@102 54 //!OverheadPercent is the (approximated) maximum size overhead (1-20%) of the allocator:
Chris@102 55 //!(memory usable for nodes / total memory allocated from the memory allocator)
Chris@102 56 template < class T
Chris@102 57 , std::size_t NodesPerBlock BOOST_CONTAINER_DOCONLY(= ADP_nodes_per_block)
Chris@102 58 , std::size_t MaxFreeBlocks BOOST_CONTAINER_DOCONLY(= ADP_max_free_blocks)
Chris@102 59 , std::size_t OverheadPercent BOOST_CONTAINER_DOCONLY(= ADP_overhead_percent)
Chris@102 60 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I unsigned Version)
Chris@102 61 >
Chris@102 62 class adaptive_pool
Chris@102 63 {
Chris@102 64 //!If Version is 1, the allocator is a STL conforming allocator. If Version is 2,
Chris@102 65 //!the allocator offers advanced expand in place and burst allocation capabilities.
Chris@102 66 public:
Chris@102 67 typedef unsigned int allocation_type;
Chris@102 68 typedef adaptive_pool
Chris@102 69 <T, NodesPerBlock, MaxFreeBlocks, OverheadPercent
Chris@102 70 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version)
Chris@102 71 > self_t;
Chris@102 72
Chris@102 73 static const std::size_t nodes_per_block = NodesPerBlock;
Chris@102 74 static const std::size_t max_free_blocks = MaxFreeBlocks;
Chris@102 75 static const std::size_t overhead_percent = OverheadPercent;
Chris@102 76 static const std::size_t real_nodes_per_block = NodesPerBlock;
Chris@102 77
Chris@102 78 BOOST_CONTAINER_DOCIGN(BOOST_STATIC_ASSERT((Version <=2)));
Chris@102 79
Chris@102 80 public:
Chris@102 81 //-------
Chris@102 82 typedef T value_type;
Chris@102 83 typedef T * pointer;
Chris@102 84 typedef const T * const_pointer;
Chris@102 85 typedef typename ::boost::container::
Chris@102 86 container_detail::unvoid<T>::type & reference;
Chris@102 87 typedef const typename ::boost::container::
Chris@102 88 container_detail::unvoid<T>::type & const_reference;
Chris@102 89 typedef std::size_t size_type;
Chris@102 90 typedef std::ptrdiff_t difference_type;
Chris@102 91
Chris@102 92 typedef boost::container::container_detail::
Chris@102 93 version_type<self_t, Version> version;
Chris@102 94
Chris@102 95 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
Chris@102 96 typedef boost::container::container_detail::
Chris@102 97 basic_multiallocation_chain<void*> multiallocation_chain_void;
Chris@102 98 typedef boost::container::container_detail::
Chris@102 99 transform_multiallocation_chain
Chris@102 100 <multiallocation_chain_void, T> multiallocation_chain;
Chris@102 101 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
Chris@102 102
Chris@102 103 //!Obtains adaptive_pool from
Chris@102 104 //!adaptive_pool
Chris@102 105 template<class T2>
Chris@102 106 struct rebind
Chris@102 107 {
Chris@102 108 typedef adaptive_pool
Chris@102 109 < T2
Chris@102 110 , NodesPerBlock
Chris@102 111 , MaxFreeBlocks
Chris@102 112 , OverheadPercent
Chris@102 113 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version)
Chris@102 114 > other;
Chris@102 115 };
Chris@102 116
Chris@102 117 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
Chris@102 118 private:
Chris@102 119 //!Not assignable from related adaptive_pool
Chris@102 120 template<class T2, unsigned Version2, std::size_t N2, std::size_t F2>
Chris@102 121 adaptive_pool& operator=
Chris@102 122 (const adaptive_pool<T2, Version2, N2, F2>&);
Chris@102 123
Chris@102 124 //!Not assignable from other adaptive_pool
Chris@102 125 adaptive_pool& operator=(const adaptive_pool&);
Chris@102 126 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
Chris@102 127
Chris@102 128 public:
Chris@102 129 //!Default constructor
Chris@102 130 adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW
Chris@102 131 {}
Chris@102 132
Chris@102 133 //!Copy constructor from other adaptive_pool.
Chris@102 134 adaptive_pool(const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
Chris@102 135 {}
Chris@102 136
Chris@102 137 //!Copy constructor from related adaptive_pool.
Chris@102 138 template<class T2>
Chris@102 139 adaptive_pool
Chris@102 140 (const adaptive_pool<T2, NodesPerBlock, MaxFreeBlocks, OverheadPercent
Chris@102 141 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version)> &) BOOST_NOEXCEPT_OR_NOTHROW
Chris@102 142 {}
Chris@102 143
Chris@102 144 //!Destructor
Chris@102 145 ~adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW
Chris@102 146 {}
Chris@102 147
Chris@102 148 //!Returns the number of elements that could be allocated.
Chris@102 149 //!Never throws
Chris@102 150 size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
Chris@102 151 { return size_type(-1)/sizeof(T); }
Chris@102 152
Chris@102 153 //!Allocate memory for an array of count elements.
Chris@102 154 //!Throws std::bad_alloc if there is no enough memory
Chris@102 155 pointer allocate(size_type count, const void * = 0)
Chris@102 156 {
Chris@102 157 if(BOOST_UNLIKELY(count > this->max_size()))
Chris@102 158 boost::container::throw_bad_alloc();
Chris@102 159
Chris@102 160 if(Version == 1 && count == 1){
Chris@102 161 typedef typename container_detail::shared_adaptive_node_pool
Chris@102 162 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
Chris@102 163 typedef container_detail::singleton_default<shared_pool_t> singleton_t;
Chris@102 164 return pointer(static_cast<T*>(singleton_t::instance().allocate_node()));
Chris@102 165 }
Chris@102 166 else{
Chris@102 167 return static_cast<pointer>(boost_cont_malloc(count*sizeof(T)));
Chris@102 168 }
Chris@102 169 }
Chris@102 170
Chris@102 171 //!Deallocate allocated memory.
Chris@102 172 //!Never throws
Chris@102 173 void deallocate(const pointer &ptr, size_type count) BOOST_NOEXCEPT_OR_NOTHROW
Chris@102 174 {
Chris@102 175 (void)count;
Chris@102 176 if(Version == 1 && count == 1){
Chris@102 177 typedef container_detail::shared_adaptive_node_pool
Chris@102 178 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
Chris@102 179 typedef container_detail::singleton_default<shared_pool_t> singleton_t;
Chris@102 180 singleton_t::instance().deallocate_node(ptr);
Chris@102 181 }
Chris@102 182 else{
Chris@102 183 boost_cont_free(ptr);
Chris@102 184 }
Chris@102 185 }
Chris@102 186
Chris@102 187 pointer allocation_command(allocation_type command,
Chris@102 188 size_type limit_size,
Chris@102 189 size_type &prefer_in_recvd_out_size,
Chris@102 190 pointer &reuse)
Chris@102 191 {
Chris@102 192 pointer ret = this->priv_allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse);
Chris@102 193 if(BOOST_UNLIKELY(!ret && !(command & BOOST_CONTAINER_NOTHROW_ALLOCATION)))
Chris@102 194 boost::container::throw_bad_alloc();
Chris@102 195 return ret;
Chris@102 196 }
Chris@102 197
Chris@102 198 //!Returns maximum the number of objects the previously allocated memory
Chris@102 199 //!pointed by p can hold.
Chris@102 200 size_type size(pointer p) const BOOST_NOEXCEPT_OR_NOTHROW
Chris@102 201 { return boost_cont_size(p); }
Chris@102 202
Chris@102 203 //!Allocates just one object. Memory allocated with this function
Chris@102 204 //!must be deallocated only with deallocate_one().
Chris@102 205 //!Throws bad_alloc if there is no enough memory
Chris@102 206 pointer allocate_one()
Chris@102 207 {
Chris@102 208 typedef container_detail::shared_adaptive_node_pool
Chris@102 209 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
Chris@102 210 typedef container_detail::singleton_default<shared_pool_t> singleton_t;
Chris@102 211 return (pointer)singleton_t::instance().allocate_node();
Chris@102 212 }
Chris@102 213
Chris@102 214 //!Allocates many elements of size == 1.
Chris@102 215 //!Elements must be individually deallocated with deallocate_one()
Chris@102 216 void allocate_individual(std::size_t num_elements, multiallocation_chain &chain)
Chris@102 217 {
Chris@102 218 typedef container_detail::shared_adaptive_node_pool
Chris@102 219 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
Chris@102 220 typedef container_detail::singleton_default<shared_pool_t> singleton_t;
Chris@102 221 singleton_t::instance().allocate_nodes(num_elements, static_cast<typename shared_pool_t::multiallocation_chain&>(chain));
Chris@102 222 //typename shared_pool_t::multiallocation_chain ch;
Chris@102 223 //singleton_t::instance().allocate_nodes(num_elements, ch);
Chris@102 224 //chain.incorporate_after
Chris@102 225 //(chain.before_begin(), (T*)&*ch.begin(), (T*)&*ch.last(), ch.size());
Chris@102 226 }
Chris@102 227
Chris@102 228 //!Deallocates memory previously allocated with allocate_one().
Chris@102 229 //!You should never use deallocate_one to deallocate memory allocated
Chris@102 230 //!with other functions different from allocate_one(). Never throws
Chris@102 231 void deallocate_one(pointer p) BOOST_NOEXCEPT_OR_NOTHROW
Chris@102 232 {
Chris@102 233 typedef container_detail::shared_adaptive_node_pool
Chris@102 234 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
Chris@102 235 typedef container_detail::singleton_default<shared_pool_t> singleton_t;
Chris@102 236 singleton_t::instance().deallocate_node(p);
Chris@102 237 }
Chris@102 238
Chris@102 239 void deallocate_individual(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
Chris@102 240 {
Chris@102 241 typedef container_detail::shared_adaptive_node_pool
Chris@102 242 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
Chris@102 243 typedef container_detail::singleton_default<shared_pool_t> singleton_t;
Chris@102 244 //typename shared_pool_t::multiallocation_chain ch(&*chain.begin(), &*chain.last(), chain.size());
Chris@102 245 //singleton_t::instance().deallocate_nodes(ch);
Chris@102 246 singleton_t::instance().deallocate_nodes(chain);
Chris@102 247 }
Chris@102 248
Chris@102 249 //!Allocates many elements of size elem_size.
Chris@102 250 //!Elements must be individually deallocated with deallocate()
Chris@102 251 void allocate_many(size_type elem_size, std::size_t n_elements, multiallocation_chain &chain)
Chris@102 252 {
Chris@102 253 BOOST_STATIC_ASSERT(( Version > 1 ));/*
Chris@102 254 boost_cont_memchain ch;
Chris@102 255 BOOST_CONTAINER_MEMCHAIN_INIT(&ch);
Chris@102 256 if(BOOST_UNLIKELY(!boost_cont_multialloc_nodes(n_elements, elem_size*sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch))){
Chris@102 257 boost::container::throw_bad_alloc();
Chris@102 258 }
Chris@102 259 chain.incorporate_after(chain.before_begin()
Chris@102 260 ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch)
Chris@102 261 ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch)
Chris@102 262 ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );*/
Chris@102 263 if(BOOST_UNLIKELY(!boost_cont_multialloc_nodes
Chris@102 264 (n_elements, elem_size*sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<boost_cont_memchain *>(&chain)))){
Chris@102 265 boost::container::throw_bad_alloc();
Chris@102 266 }
Chris@102 267 }
Chris@102 268
Chris@102 269 //!Allocates n_elements elements, each one of size elem_sizes[i]
Chris@102 270 //!Elements must be individually deallocated with deallocate()
Chris@102 271 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain)
Chris@102 272 {
Chris@102 273 BOOST_STATIC_ASSERT(( Version > 1 ));/*
Chris@102 274 boost_cont_memchain ch;
Chris@102 275 BOOST_CONTAINER_MEMCHAIN_INIT(&ch);
Chris@102 276 if(BOOST_UNLIKELY(!boost_cont_multialloc_arrays(n_elements, elem_sizes, sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch))){
Chris@102 277 boost::container::throw_bad_alloc();
Chris@102 278 }
Chris@102 279 chain.incorporate_after(chain.before_begin()
Chris@102 280 ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch)
Chris@102 281 ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch)
Chris@102 282 ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );*/
Chris@102 283 if(BOOST_UNLIKELY(!boost_cont_multialloc_arrays
Chris@102 284 (n_elements, elem_sizes, sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<boost_cont_memchain *>(&chain)))){
Chris@102 285 boost::container::throw_bad_alloc();
Chris@102 286 }
Chris@102 287 }
Chris@102 288
Chris@102 289 void deallocate_many(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
Chris@102 290 {/*
Chris@102 291 boost_cont_memchain ch;
Chris@102 292 void *beg(&*chain.begin()), *last(&*chain.last());
Chris@102 293 size_t size(chain.size());
Chris@102 294 BOOST_CONTAINER_MEMCHAIN_INIT_FROM(&ch, beg, last, size);
Chris@102 295 boost_cont_multidealloc(&ch);*/
Chris@102 296 boost_cont_multidealloc(reinterpret_cast<boost_cont_memchain *>(&chain));
Chris@102 297 }
Chris@102 298
Chris@102 299 //!Deallocates all free blocks of the pool
Chris@102 300 static void deallocate_free_blocks() BOOST_NOEXCEPT_OR_NOTHROW
Chris@102 301 {
Chris@102 302 typedef container_detail::shared_adaptive_node_pool
Chris@102 303 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
Chris@102 304 typedef container_detail::singleton_default<shared_pool_t> singleton_t;
Chris@102 305 singleton_t::instance().deallocate_free_blocks();
Chris@102 306 }
Chris@102 307
Chris@102 308 //!Swaps allocators. Does not throw. If each allocator is placed in a
Chris@102 309 //!different memory segment, the result is undefined.
Chris@102 310 friend void swap(adaptive_pool &, adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
Chris@102 311 {}
Chris@102 312
Chris@102 313 //!An allocator always compares to true, as memory allocated with one
Chris@102 314 //!instance can be deallocated by another instance
Chris@102 315 friend bool operator==(const adaptive_pool &, const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
Chris@102 316 { return true; }
Chris@102 317
Chris@102 318 //!An allocator always compares to false, as memory allocated with one
Chris@102 319 //!instance can be deallocated by another instance
Chris@102 320 friend bool operator!=(const adaptive_pool &, const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
Chris@102 321 { return false; }
Chris@102 322
Chris@102 323 private:
Chris@102 324 pointer priv_allocation_command
Chris@102 325 (allocation_type command, std::size_t limit_size
Chris@102 326 ,size_type &prefer_in_recvd_out_size, pointer &reuse_ptr)
Chris@102 327 {
Chris@102 328 std::size_t const preferred_size = prefer_in_recvd_out_size;
Chris@102 329 boost_cont_command_ret_t ret = {0 , 0};
Chris@102 330 if(BOOST_UNLIKELY(limit_size > this->max_size() || preferred_size > this->max_size())){
Chris@102 331 return pointer();
Chris@102 332 }
Chris@102 333 std::size_t l_size = limit_size*sizeof(T);
Chris@102 334 std::size_t p_size = preferred_size*sizeof(T);
Chris@102 335 std::size_t r_size;
Chris@102 336 {
Chris@102 337 void* reuse_ptr_void = reuse_ptr;
Chris@102 338 ret = boost_cont_allocation_command(command, sizeof(T), l_size, p_size, &r_size, reuse_ptr_void);
Chris@102 339 reuse_ptr = ret.second ? static_cast<T*>(reuse_ptr_void) : 0;
Chris@102 340 }
Chris@102 341 prefer_in_recvd_out_size = r_size/sizeof(T);
Chris@102 342 return (pointer)ret.first;
Chris@102 343 }
Chris@102 344 };
Chris@102 345
Chris@102 346 } //namespace container {
Chris@102 347 } //namespace boost {
Chris@102 348
Chris@102 349 #include <boost/container/detail/config_end.hpp>
Chris@102 350
Chris@102 351 #endif //#ifndef BOOST_CONTAINER_ADAPTIVE_POOL_HPP