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 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; 91 92 typedef boost::container::dtl:: 93 version_type<self_t, Version> version; 94 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 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, 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>&); 123 124 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED 125 126 public: 127 //!Default constructor adaptive_pool()128 adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW 129 {} 130 131 //!Copy constructor from other adaptive_pool. adaptive_pool(const 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> adaptive_pool(const adaptive_pool<T2,NodesPerBlock,MaxFreeBlocks,OverheadPercent BOOST_CONTAINER_DOCIGN (BOOST_MOVE_I Version)> &)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 ~adaptive_pool()143 ~adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW 144 {} 145 146 //!Returns the number of elements that could be allocated. 147 //!Never throws max_size() const148 size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW 149 { return size_type(-1)/(2u*sizeof(T)); } 150 151 //!Allocate memory for an array of count elements. 152 //!Throws std::bad_alloc if there is no enough memory allocate(size_type count,const void * =0)153 pointer allocate(size_type count, const void * = 0) 154 { 155 if(BOOST_UNLIKELY(count > size_type(-1)/(2u*sizeof(T)))) 156 boost::container::throw_bad_alloc(); 157 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())); 163 } 164 else{ 165 return static_cast<pointer>(dlmalloc_malloc(count*sizeof(T))); 166 } 167 } 168 169 //!Deallocate allocated memory. 170 //!Never throws deallocate(const pointer & ptr,size_type count)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 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); 179 } 180 else{ 181 dlmalloc_free(ptr); 182 } 183 } 184 allocation_command(allocation_type command,size_type limit_size,size_type & prefer_in_recvd_out_size,pointer & reuse)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. size(pointer p) const198 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 allocate_one()204 pointer allocate_one() 205 { 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(); 210 } 211 212 //!Allocates many elements of size == 1. 213 //!Elements must be individually deallocated with deallocate_one() allocate_individual(std::size_t num_elements,multiallocation_chain & chain)214 void allocate_individual(std::size_t num_elements, multiallocation_chain &chain) 215 { 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()); 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 deallocate_one(pointer p)229 void deallocate_one(pointer p) BOOST_NOEXCEPT_OR_NOTHROW 230 { 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); 235 } 236 deallocate_individual(multiallocation_chain & chain)237 void deallocate_individual(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW 238 { 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); 245 } 246 247 //!Allocates many elements of size elem_size. 248 //!Elements must be individually deallocated with deallocate() allocate_many(size_type elem_size,std::size_t n_elements,multiallocation_chain & chain)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), BOOST_CONTAINER_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), BOOST_CONTAINER_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() allocate_many(const size_type * elem_sizes,size_type n_elements,multiallocation_chain & chain)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), BOOST_CONTAINER_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), BOOST_CONTAINER_DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<dlmalloc_memchain *>(&chain)))){ 283 boost::container::throw_bad_alloc(); 284 } 285 } 286 deallocate_many(multiallocation_chain & chain)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 deallocate_free_blocks()298 static void deallocate_free_blocks() BOOST_NOEXCEPT_OR_NOTHROW 299 { 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(); 304 } 305 306 //!Swaps allocators. Does not throw. If each allocator is placed in a 307 //!different memory segment, the result is undefined. swap(adaptive_pool &,adaptive_pool &)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 operator ==(const adaptive_pool &,const adaptive_pool &)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 operator !=(const adaptive_pool &,const adaptive_pool &)318 friend bool operator!=(const adaptive_pool &, const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW 319 { return false; } 320 321 private: priv_allocation_command(allocation_type command,std::size_t limit_size,size_type & prefer_in_recvd_out_size,pointer & reuse_ptr)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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 template < class T 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 368 > 369 class private_adaptive_pool 370 { 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. 373 public: 374 typedef unsigned int allocation_type; 375 typedef private_adaptive_pool 376 <T, NodesPerBlock, MaxFreeBlocks, OverheadPercent 377 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version) 378 > self_t; 379 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; 384 385 BOOST_CONTAINER_DOCIGN(BOOST_STATIC_ASSERT((Version <=2))); 386 387 typedef dtl::private_adaptive_node_pool 388 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> pool_t; 389 pool_t m_pool; 390 391 public: 392 //------- 393 typedef T value_type; 394 typedef T * pointer; 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; 402 403 typedef boost::container::dtl:: 404 version_type<self_t, Version> version; 405 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 413 414 //!Obtains private_adaptive_pool from 415 //!private_adaptive_pool 416 template<class T2> 417 struct rebind 418 { 419 typedef private_adaptive_pool 420 < T2 421 , NodesPerBlock 422 , MaxFreeBlocks 423 , OverheadPercent 424 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version) 425 > other; 426 }; 427 428 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED 429 private: 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 435 436 public: 437 //!Default constructor private_adaptive_pool()438 private_adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW 439 {} 440 441 //!Copy constructor from other private_adaptive_pool. private_adaptive_pool(const private_adaptive_pool &)442 private_adaptive_pool(const private_adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW 443 {} 444 445 //!Copy constructor from related private_adaptive_pool. 446 template<class T2> private_adaptive_pool(const private_adaptive_pool<T2,NodesPerBlock,MaxFreeBlocks,OverheadPercent BOOST_CONTAINER_DOCIGN (BOOST_MOVE_I Version)> &)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 450 {} 451 452 //!Destructor ~private_adaptive_pool()453 ~private_adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW 454 {} 455 456 //!Returns the number of elements that could be allocated. 457 //!Never throws max_size() const458 size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW 459 { return size_type(-1)/(2u*sizeof(T)); } 460 461 //!Allocate memory for an array of count elements. 462 //!Throws std::bad_alloc if there is no enough memory allocate(size_type count,const void * =0)463 pointer allocate(size_type count, const void * = 0) 464 { 465 if(BOOST_UNLIKELY(count > size_type(-1)/(2u*sizeof(T)))) 466 boost::container::throw_bad_alloc(); 467 468 if(Version == 1 && count == 1){ 469 return pointer(static_cast<T*>(m_pool.allocate_node())); 470 } 471 else{ 472 return static_cast<pointer>(dlmalloc_malloc(count*sizeof(T))); 473 } 474 } 475 476 //!Deallocate allocated memory. 477 //!Never throws deallocate(const pointer & ptr,size_type count)478 void deallocate(const pointer &ptr, size_type count) BOOST_NOEXCEPT_OR_NOTHROW 479 { 480 (void)count; 481 if(Version == 1 && count == 1){ 482 m_pool.deallocate_node(ptr); 483 } 484 else{ 485 dlmalloc_free(ptr); 486 } 487 } 488 allocation_command(allocation_type command,size_type limit_size,size_type & prefer_in_recvd_out_size,pointer & reuse)489 pointer allocation_command(allocation_type command, 490 size_type limit_size, 491 size_type &prefer_in_recvd_out_size, 492 pointer &reuse) 493 { 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(); 497 return ret; 498 } 499 500 //!Returns maximum the number of objects the previously allocated memory 501 //!pointed by p can hold. size(pointer p) const502 size_type size(pointer p) const BOOST_NOEXCEPT_OR_NOTHROW 503 { return dlmalloc_size(p); } 504 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 allocate_one()508 pointer allocate_one() 509 { 510 return (pointer)m_pool.allocate_node(); 511 } 512 513 //!Allocates many elements of size == 1. 514 //!Elements must be individually deallocated with deallocate_one() allocate_individual(std::size_t num_elements,multiallocation_chain & chain)515 void allocate_individual(std::size_t num_elements, multiallocation_chain &chain) 516 { 517 m_pool.allocate_nodes(num_elements, static_cast<typename pool_t::multiallocation_chain&>(chain)); 518 } 519 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 deallocate_one(pointer p)523 void deallocate_one(pointer p) BOOST_NOEXCEPT_OR_NOTHROW 524 { 525 m_pool.deallocate_node(p); 526 } 527 deallocate_individual(multiallocation_chain & chain)528 void deallocate_individual(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW 529 { 530 m_pool.deallocate_nodes(chain); 531 } 532 533 //!Allocates many elements of size elem_size. 534 //!Elements must be individually deallocated with deallocate() allocate_many(size_type elem_size,std::size_t n_elements,multiallocation_chain & chain)535 void allocate_many(size_type elem_size, std::size_t n_elements, multiallocation_chain &chain) 536 { 537 BOOST_STATIC_ASSERT(( Version > 1 )); 538 if(BOOST_UNLIKELY(!dlmalloc_multialloc_nodes 539 (n_elements, elem_size*sizeof(T), BOOST_CONTAINER_DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<dlmalloc_memchain *>(&chain)))){ 540 boost::container::throw_bad_alloc(); 541 } 542 } 543 544 //!Allocates n_elements elements, each one of size elem_sizes[i] 545 //!Elements must be individually deallocated with deallocate() allocate_many(const size_type * elem_sizes,size_type n_elements,multiallocation_chain & chain)546 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain) 547 { 548 BOOST_STATIC_ASSERT(( Version > 1 )); 549 if(BOOST_UNLIKELY(!dlmalloc_multialloc_arrays 550 (n_elements, elem_sizes, sizeof(T), BOOST_CONTAINER_DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<dlmalloc_memchain *>(&chain)))){ 551 boost::container::throw_bad_alloc(); 552 } 553 } 554 deallocate_many(multiallocation_chain & chain)555 void deallocate_many(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW 556 { 557 dlmalloc_multidealloc(reinterpret_cast<dlmalloc_memchain *>(&chain)); 558 } 559 560 //!Deallocates all free blocks of the pool deallocate_free_blocks()561 void deallocate_free_blocks() BOOST_NOEXCEPT_OR_NOTHROW 562 { 563 m_pool.deallocate_free_blocks(); 564 } 565 566 //!Swaps allocators. Does not throw. If each allocator is placed in a 567 //!different memory segment, the result is undefined. swap(private_adaptive_pool &,private_adaptive_pool &)568 friend void swap(private_adaptive_pool &, private_adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW 569 {} 570 571 //!An allocator always compares to true, as memory allocated with one 572 //!instance can be deallocated by another instance operator ==(const private_adaptive_pool &,const private_adaptive_pool &)573 friend bool operator==(const private_adaptive_pool &, const private_adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW 574 { return true; } 575 576 //!An allocator always compares to false, as memory allocated with one 577 //!instance can be deallocated by another instance operator !=(const private_adaptive_pool &,const private_adaptive_pool &)578 friend bool operator!=(const private_adaptive_pool &, const private_adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW 579 { return false; } 580 581 private: priv_allocation_command(allocation_type command,std::size_t limit_size,size_type & prefer_in_recvd_out_size,pointer & reuse_ptr)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) 585 { 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())){ 589 return pointer(); 590 } 591 std::size_t l_size = limit_size*sizeof(T); 592 std::size_t p_size = preferred_size*sizeof(T); 593 std::size_t r_size; 594 { 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; 598 } 599 prefer_in_recvd_out_size = r_size/sizeof(T); 600 return (pointer)ret.first; 601 } 602 }; 603 604 } //namespace container { 605 } //namespace boost { 606 607 #include <boost/container/detail/config_end.hpp> 608 609 #endif //#ifndef BOOST_CONTAINER_ADAPTIVE_POOL_HPP 610