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 assignment from other adaptive_pool. operator =(const adaptive_pool &)136 adaptive_pool & operator=(const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW 137 { return *this; } 138 139 //!Copy constructor from related adaptive_pool. 140 template<class T2> adaptive_pool(const adaptive_pool<T2,NodesPerBlock,MaxFreeBlocks,OverheadPercent BOOST_CONTAINER_DOCIGN (BOOST_MOVE_I Version)> &)141 adaptive_pool 142 (const adaptive_pool<T2, NodesPerBlock, MaxFreeBlocks, OverheadPercent 143 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version)> &) BOOST_NOEXCEPT_OR_NOTHROW 144 {} 145 146 //!Destructor ~adaptive_pool()147 ~adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW 148 {} 149 150 //!Returns the number of elements that could be allocated. 151 //!Never throws max_size() const152 size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW 153 { return size_type(-1)/(2u*sizeof(T)); } 154 155 //!Allocate memory for an array of count elements. 156 //!Throws std::bad_alloc if there is no enough memory allocate(size_type count,const void * =0)157 pointer allocate(size_type count, const void * = 0) 158 { 159 if(BOOST_UNLIKELY(count > size_type(-1)/(2u*sizeof(T)))) 160 boost::container::throw_bad_alloc(); 161 162 if(Version == 1 && count == 1){ 163 typedef typename dtl::shared_adaptive_node_pool 164 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t; 165 typedef dtl::singleton_default<shared_pool_t> singleton_t; 166 return pointer(static_cast<T*>(singleton_t::instance().allocate_node())); 167 } 168 else{ 169 return static_cast<pointer>(dlmalloc_malloc(count*sizeof(T))); 170 } 171 } 172 173 //!Deallocate allocated memory. 174 //!Never throws deallocate(const pointer & ptr,size_type count)175 void deallocate(const pointer &ptr, size_type count) BOOST_NOEXCEPT_OR_NOTHROW 176 { 177 (void)count; 178 if(Version == 1 && count == 1){ 179 typedef dtl::shared_adaptive_node_pool 180 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t; 181 typedef dtl::singleton_default<shared_pool_t> singleton_t; 182 singleton_t::instance().deallocate_node(ptr); 183 } 184 else{ 185 dlmalloc_free(ptr); 186 } 187 } 188 allocation_command(allocation_type command,size_type limit_size,size_type & prefer_in_recvd_out_size,pointer & reuse)189 pointer allocation_command(allocation_type command, 190 size_type limit_size, 191 size_type &prefer_in_recvd_out_size, 192 pointer &reuse) 193 { 194 pointer ret = this->priv_allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse); 195 if(BOOST_UNLIKELY(!ret && !(command & BOOST_CONTAINER_NOTHROW_ALLOCATION))) 196 boost::container::throw_bad_alloc(); 197 return ret; 198 } 199 200 //!Returns maximum the number of objects the previously allocated memory 201 //!pointed by p can hold. size(pointer p) const202 size_type size(pointer p) const BOOST_NOEXCEPT_OR_NOTHROW 203 { return dlmalloc_size(p); } 204 205 //!Allocates just one object. Memory allocated with this function 206 //!must be deallocated only with deallocate_one(). 207 //!Throws bad_alloc if there is no enough memory allocate_one()208 pointer allocate_one() 209 { 210 typedef dtl::shared_adaptive_node_pool 211 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t; 212 typedef dtl::singleton_default<shared_pool_t> singleton_t; 213 return (pointer)singleton_t::instance().allocate_node(); 214 } 215 216 //!Allocates many elements of size == 1. 217 //!Elements must be individually deallocated with deallocate_one() allocate_individual(std::size_t num_elements,multiallocation_chain & chain)218 void allocate_individual(std::size_t num_elements, multiallocation_chain &chain) 219 { 220 typedef dtl::shared_adaptive_node_pool 221 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t; 222 typedef dtl::singleton_default<shared_pool_t> singleton_t; 223 singleton_t::instance().allocate_nodes(num_elements, static_cast<typename shared_pool_t::multiallocation_chain&>(chain)); 224 //typename shared_pool_t::multiallocation_chain ch; 225 //singleton_t::instance().allocate_nodes(num_elements, ch); 226 //chain.incorporate_after 227 //(chain.before_begin(), (T*)&*ch.begin(), (T*)&*ch.last(), ch.size()); 228 } 229 230 //!Deallocates memory previously allocated with allocate_one(). 231 //!You should never use deallocate_one to deallocate memory allocated 232 //!with other functions different from allocate_one(). Never throws deallocate_one(pointer p)233 void deallocate_one(pointer p) BOOST_NOEXCEPT_OR_NOTHROW 234 { 235 typedef dtl::shared_adaptive_node_pool 236 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t; 237 typedef dtl::singleton_default<shared_pool_t> singleton_t; 238 singleton_t::instance().deallocate_node(p); 239 } 240 deallocate_individual(multiallocation_chain & chain)241 void deallocate_individual(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW 242 { 243 typedef dtl::shared_adaptive_node_pool 244 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t; 245 typedef dtl::singleton_default<shared_pool_t> singleton_t; 246 //typename shared_pool_t::multiallocation_chain ch(&*chain.begin(), &*chain.last(), chain.size()); 247 //singleton_t::instance().deallocate_nodes(ch); 248 singleton_t::instance().deallocate_nodes(chain); 249 } 250 251 //!Allocates many elements of size elem_size. 252 //!Elements must be individually deallocated with deallocate() allocate_many(size_type elem_size,std::size_t n_elements,multiallocation_chain & chain)253 void allocate_many(size_type elem_size, std::size_t n_elements, multiallocation_chain &chain) 254 { 255 BOOST_STATIC_ASSERT(( Version > 1 ));/* 256 dlmalloc_memchain ch; 257 BOOST_CONTAINER_MEMCHAIN_INIT(&ch); 258 if(BOOST_UNLIKELY(!dlmalloc_multialloc_nodes(n_elements, elem_size*sizeof(T), BOOST_CONTAINER_DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch))){ 259 boost::container::throw_bad_alloc(); 260 } 261 chain.incorporate_after(chain.before_begin() 262 ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch) 263 ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch) 264 ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );*/ 265 if(BOOST_UNLIKELY(!dlmalloc_multialloc_nodes 266 (n_elements, elem_size*sizeof(T), BOOST_CONTAINER_DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<dlmalloc_memchain *>(&chain)))){ 267 boost::container::throw_bad_alloc(); 268 } 269 } 270 271 //!Allocates n_elements elements, each one of size elem_sizes[i] 272 //!Elements must be individually deallocated with deallocate() allocate_many(const size_type * elem_sizes,size_type n_elements,multiallocation_chain & chain)273 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain) 274 { 275 BOOST_STATIC_ASSERT(( Version > 1 ));/* 276 dlmalloc_memchain ch; 277 BOOST_CONTAINER_MEMCHAIN_INIT(&ch); 278 if(BOOST_UNLIKELY(!dlmalloc_multialloc_arrays(n_elements, elem_sizes, sizeof(T), BOOST_CONTAINER_DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch))){ 279 boost::container::throw_bad_alloc(); 280 } 281 chain.incorporate_after(chain.before_begin() 282 ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch) 283 ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch) 284 ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );*/ 285 if(BOOST_UNLIKELY(!dlmalloc_multialloc_arrays 286 (n_elements, elem_sizes, sizeof(T), BOOST_CONTAINER_DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<dlmalloc_memchain *>(&chain)))){ 287 boost::container::throw_bad_alloc(); 288 } 289 } 290 deallocate_many(multiallocation_chain & chain)291 void deallocate_many(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW 292 {/* 293 dlmalloc_memchain ch; 294 void *beg(&*chain.begin()), *last(&*chain.last()); 295 size_t size(chain.size()); 296 BOOST_CONTAINER_MEMCHAIN_INIT_FROM(&ch, beg, last, size); 297 dlmalloc_multidealloc(&ch);*/ 298 dlmalloc_multidealloc(reinterpret_cast<dlmalloc_memchain *>(&chain)); 299 } 300 301 //!Deallocates all free blocks of the pool deallocate_free_blocks()302 static void deallocate_free_blocks() BOOST_NOEXCEPT_OR_NOTHROW 303 { 304 typedef dtl::shared_adaptive_node_pool 305 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t; 306 typedef dtl::singleton_default<shared_pool_t> singleton_t; 307 singleton_t::instance().deallocate_free_blocks(); 308 } 309 310 //!Swaps allocators. Does not throw. If each allocator is placed in a 311 //!different memory segment, the result is undefined. swap(adaptive_pool &,adaptive_pool &)312 friend void swap(adaptive_pool &, adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW 313 {} 314 315 //!An allocator always compares to true, as memory allocated with one 316 //!instance can be deallocated by another instance operator ==(const adaptive_pool &,const adaptive_pool &)317 friend bool operator==(const adaptive_pool &, const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW 318 { return true; } 319 320 //!An allocator always compares to false, as memory allocated with one 321 //!instance can be deallocated by another instance operator !=(const adaptive_pool &,const adaptive_pool &)322 friend bool operator!=(const adaptive_pool &, const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW 323 { return false; } 324 325 private: priv_allocation_command(allocation_type command,std::size_t limit_size,size_type & prefer_in_recvd_out_size,pointer & reuse_ptr)326 pointer priv_allocation_command 327 (allocation_type command, std::size_t limit_size 328 ,size_type &prefer_in_recvd_out_size, pointer &reuse_ptr) 329 { 330 std::size_t const preferred_size = prefer_in_recvd_out_size; 331 dlmalloc_command_ret_t ret = {0 , 0}; 332 if(BOOST_UNLIKELY(limit_size > this->max_size() || preferred_size > this->max_size())){ 333 return pointer(); 334 } 335 std::size_t l_size = limit_size*sizeof(T); 336 std::size_t p_size = preferred_size*sizeof(T); 337 std::size_t r_size; 338 { 339 void* reuse_ptr_void = reuse_ptr; 340 ret = dlmalloc_allocation_command(command, sizeof(T), l_size, p_size, &r_size, reuse_ptr_void); 341 reuse_ptr = ret.second ? static_cast<T*>(reuse_ptr_void) : 0; 342 } 343 prefer_in_recvd_out_size = r_size/sizeof(T); 344 return (pointer)ret.first; 345 } 346 }; 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 template < class T 368 , std::size_t NodesPerBlock = ADP_nodes_per_block 369 , std::size_t MaxFreeBlocks = ADP_max_free_blocks 370 , std::size_t OverheadPercent = ADP_overhead_percent 371 , unsigned Version = 2 372 > 373 class private_adaptive_pool 374 { 375 //!If Version is 1, the allocator is a STL conforming allocator. If Version is 2, 376 //!the allocator offers advanced expand in place and burst allocation capabilities. 377 public: 378 typedef unsigned int allocation_type; 379 typedef private_adaptive_pool 380 <T, NodesPerBlock, MaxFreeBlocks, OverheadPercent 381 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version) 382 > self_t; 383 384 static const std::size_t nodes_per_block = NodesPerBlock; 385 static const std::size_t max_free_blocks = MaxFreeBlocks; 386 static const std::size_t overhead_percent = OverheadPercent; 387 static const std::size_t real_nodes_per_block = NodesPerBlock; 388 389 BOOST_CONTAINER_DOCIGN(BOOST_STATIC_ASSERT((Version <=2))); 390 391 typedef dtl::private_adaptive_node_pool 392 <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> pool_t; 393 pool_t m_pool; 394 395 public: 396 //------- 397 typedef T value_type; 398 typedef T * pointer; 399 typedef const T * const_pointer; 400 typedef typename ::boost::container:: 401 dtl::unvoid_ref<T>::type reference; 402 typedef typename ::boost::container:: 403 dtl::unvoid_ref<const T>::type const_reference; 404 typedef std::size_t size_type; 405 typedef std::ptrdiff_t difference_type; 406 407 typedef boost::container::dtl:: 408 version_type<self_t, Version> version; 409 410 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED 411 typedef boost::container::dtl:: 412 basic_multiallocation_chain<void*> multiallocation_chain_void; 413 typedef boost::container::dtl:: 414 transform_multiallocation_chain 415 <multiallocation_chain_void, T> multiallocation_chain; 416 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED 417 418 //!Obtains private_adaptive_pool from 419 //!private_adaptive_pool 420 template<class T2> 421 struct rebind 422 { 423 typedef private_adaptive_pool 424 < T2 425 , NodesPerBlock 426 , MaxFreeBlocks 427 , OverheadPercent 428 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version) 429 > other; 430 }; 431 432 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED 433 private: 434 //!Not assignable from related private_adaptive_pool 435 template<class T2, std::size_t N2, std::size_t F2, std::size_t O2, unsigned Version2> 436 private_adaptive_pool& operator= 437 (const private_adaptive_pool<T2, N2, F2, O2, Version2>&); 438 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED 439 440 public: 441 //!Default constructor private_adaptive_pool()442 private_adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW 443 {} 444 445 //!Copy constructor from other private_adaptive_pool. private_adaptive_pool(const private_adaptive_pool &)446 private_adaptive_pool(const private_adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW 447 {} 448 449 //!Copy assignment from other adaptive_pool. operator =(const private_adaptive_pool &)450 private_adaptive_pool & operator=(const private_adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW 451 { return *this; } 452 453 //!Copy constructor from related private_adaptive_pool. 454 template<class T2> private_adaptive_pool(const private_adaptive_pool<T2,NodesPerBlock,MaxFreeBlocks,OverheadPercent BOOST_CONTAINER_DOCIGN (BOOST_MOVE_I Version)> &)455 private_adaptive_pool 456 (const private_adaptive_pool<T2, NodesPerBlock, MaxFreeBlocks, OverheadPercent 457 BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version)> &) BOOST_NOEXCEPT_OR_NOTHROW 458 {} 459 460 //!Destructor ~private_adaptive_pool()461 ~private_adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW 462 {} 463 464 //!Returns the number of elements that could be allocated. 465 //!Never throws max_size() const466 size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW 467 { return size_type(-1)/(2u*sizeof(T)); } 468 469 //!Allocate memory for an array of count elements. 470 //!Throws std::bad_alloc if there is no enough memory allocate(size_type count,const void * =0)471 pointer allocate(size_type count, const void * = 0) 472 { 473 if(BOOST_UNLIKELY(count > size_type(-1)/(2u*sizeof(T)))) 474 boost::container::throw_bad_alloc(); 475 476 if(Version == 1 && count == 1){ 477 return pointer(static_cast<T*>(m_pool.allocate_node())); 478 } 479 else{ 480 return static_cast<pointer>(dlmalloc_malloc(count*sizeof(T))); 481 } 482 } 483 484 //!Deallocate allocated memory. 485 //!Never throws deallocate(const pointer & ptr,size_type count)486 void deallocate(const pointer &ptr, size_type count) BOOST_NOEXCEPT_OR_NOTHROW 487 { 488 (void)count; 489 if(Version == 1 && count == 1){ 490 m_pool.deallocate_node(ptr); 491 } 492 else{ 493 dlmalloc_free(ptr); 494 } 495 } 496 allocation_command(allocation_type command,size_type limit_size,size_type & prefer_in_recvd_out_size,pointer & reuse)497 pointer allocation_command(allocation_type command, 498 size_type limit_size, 499 size_type &prefer_in_recvd_out_size, 500 pointer &reuse) 501 { 502 pointer ret = this->priv_allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse); 503 if(BOOST_UNLIKELY(!ret && !(command & BOOST_CONTAINER_NOTHROW_ALLOCATION))) 504 boost::container::throw_bad_alloc(); 505 return ret; 506 } 507 508 //!Returns maximum the number of objects the previously allocated memory 509 //!pointed by p can hold. size(pointer p) const510 size_type size(pointer p) const BOOST_NOEXCEPT_OR_NOTHROW 511 { return dlmalloc_size(p); } 512 513 //!Allocates just one object. Memory allocated with this function 514 //!must be deallocated only with deallocate_one(). 515 //!Throws bad_alloc if there is no enough memory allocate_one()516 pointer allocate_one() 517 { 518 return (pointer)m_pool.allocate_node(); 519 } 520 521 //!Allocates many elements of size == 1. 522 //!Elements must be individually deallocated with deallocate_one() allocate_individual(std::size_t num_elements,multiallocation_chain & chain)523 void allocate_individual(std::size_t num_elements, multiallocation_chain &chain) 524 { 525 m_pool.allocate_nodes(num_elements, static_cast<typename pool_t::multiallocation_chain&>(chain)); 526 } 527 528 //!Deallocates memory previously allocated with allocate_one(). 529 //!You should never use deallocate_one to deallocate memory allocated 530 //!with other functions different from allocate_one(). Never throws deallocate_one(pointer p)531 void deallocate_one(pointer p) BOOST_NOEXCEPT_OR_NOTHROW 532 { 533 m_pool.deallocate_node(p); 534 } 535 deallocate_individual(multiallocation_chain & chain)536 void deallocate_individual(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW 537 { 538 m_pool.deallocate_nodes(chain); 539 } 540 541 //!Allocates many elements of size elem_size. 542 //!Elements must be individually deallocated with deallocate() allocate_many(size_type elem_size,std::size_t n_elements,multiallocation_chain & chain)543 void allocate_many(size_type elem_size, std::size_t n_elements, multiallocation_chain &chain) 544 { 545 BOOST_STATIC_ASSERT(( Version > 1 )); 546 if(BOOST_UNLIKELY(!dlmalloc_multialloc_nodes 547 (n_elements, elem_size*sizeof(T), BOOST_CONTAINER_DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<dlmalloc_memchain *>(&chain)))){ 548 boost::container::throw_bad_alloc(); 549 } 550 } 551 552 //!Allocates n_elements elements, each one of size elem_sizes[i] 553 //!Elements must be individually deallocated with deallocate() allocate_many(const size_type * elem_sizes,size_type n_elements,multiallocation_chain & chain)554 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain) 555 { 556 BOOST_STATIC_ASSERT(( Version > 1 )); 557 if(BOOST_UNLIKELY(!dlmalloc_multialloc_arrays 558 (n_elements, elem_sizes, sizeof(T), BOOST_CONTAINER_DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<dlmalloc_memchain *>(&chain)))){ 559 boost::container::throw_bad_alloc(); 560 } 561 } 562 deallocate_many(multiallocation_chain & chain)563 void deallocate_many(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW 564 { 565 dlmalloc_multidealloc(reinterpret_cast<dlmalloc_memchain *>(&chain)); 566 } 567 568 //!Deallocates all free blocks of the pool deallocate_free_blocks()569 void deallocate_free_blocks() BOOST_NOEXCEPT_OR_NOTHROW 570 { 571 m_pool.deallocate_free_blocks(); 572 } 573 574 //!Swaps allocators. Does not throw. If each allocator is placed in a 575 //!different memory segment, the result is undefined. swap(private_adaptive_pool &,private_adaptive_pool &)576 friend void swap(private_adaptive_pool &, private_adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW 577 {} 578 579 //!An allocator always compares to true, as memory allocated with one 580 //!instance can be deallocated by another instance operator ==(const private_adaptive_pool &,const private_adaptive_pool &)581 friend bool operator==(const private_adaptive_pool &, const private_adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW 582 { return true; } 583 584 //!An allocator always compares to false, as memory allocated with one 585 //!instance can be deallocated by another instance operator !=(const private_adaptive_pool &,const private_adaptive_pool &)586 friend bool operator!=(const private_adaptive_pool &, const private_adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW 587 { return false; } 588 589 private: priv_allocation_command(allocation_type command,std::size_t limit_size,size_type & prefer_in_recvd_out_size,pointer & reuse_ptr)590 pointer priv_allocation_command 591 (allocation_type command, std::size_t limit_size 592 ,size_type &prefer_in_recvd_out_size, pointer &reuse_ptr) 593 { 594 std::size_t const preferred_size = prefer_in_recvd_out_size; 595 dlmalloc_command_ret_t ret = {0 , 0}; 596 if(BOOST_UNLIKELY(limit_size > this->max_size() || preferred_size > this->max_size())){ 597 return pointer(); 598 } 599 std::size_t l_size = limit_size*sizeof(T); 600 std::size_t p_size = preferred_size*sizeof(T); 601 std::size_t r_size; 602 { 603 void* reuse_ptr_void = reuse_ptr; 604 ret = dlmalloc_allocation_command(command, sizeof(T), l_size, p_size, &r_size, reuse_ptr_void); 605 reuse_ptr = ret.second ? static_cast<T*>(reuse_ptr_void) : 0; 606 } 607 prefer_in_recvd_out_size = r_size/sizeof(T); 608 return (pointer)ret.first; 609 } 610 }; 611 612 } //namespace container { 613 } //namespace boost { 614 615 #include <boost/container/detail/config_end.hpp> 616 617 #endif //#ifndef BOOST_CONTAINER_ADAPTIVE_POOL_HPP 618