Ticket #2696: pool.patch
File pool.patch, 9.3 KB (added by , 14 years ago) |
---|
-
pool.hpp
154 154 const size_type requested_size; 155 155 size_type next_size; 156 156 size_type start_size; 157 size_type max_size; 157 158 158 159 // finds which POD in the list 'chunk' was allocated from 159 160 details::PODptr<size_type> find_POD(void * const chunk) const; … … 192 193 // The second parameter here is an extension! 193 194 // pre: npartition_size != 0 && nnext_size != 0 194 195 explicit pool(const size_type nrequested_size, 195 const size_type nnext_size = 32) 196 :list(0, 0), requested_size(nrequested_size), next_size(nnext_size), start_size(nnext_size) 196 const size_type nnext_size = 32, 197 const size_type nmax_size = 0) 198 :list(0, 0), requested_size(nrequested_size), next_size(nnext_size), start_size(nnext_size),max_size(nmax_size) 197 199 { } 198 200 199 201 ~pool() { purge_memory(); } … … 210 212 // These functions are extensions! 211 213 size_type get_next_size() const { return next_size; } 212 214 void set_next_size(const size_type nnext_size) { next_size = start_size = nnext_size; } 215 size_type get_max_size() const { return max_size; } 216 void set_max_size(const size_type nmax_size) { max_size = nmax_size; } 213 217 size_type get_requested_size() const { return requested_size; } 214 218 215 219 // Both malloc and ordered_malloc do a quick inlined check first for any … … 438 442 if (ptr == 0) 439 443 return 0; 440 444 const details::PODptr<size_type> node(ptr, POD_size); 441 next_size <<= 1; 445 446 BOOST_USING_STD_MIN(); 447 if(!max_size) 448 next_size <<= 1; 449 else if( next_size*partition_size/requested_size < max_size) 450 next_size = min BOOST_PREVENT_MACRO_SUBSTITUTION(next_size << 1, max_size*requested_size/ partition_size); 442 451 443 452 // initialize it, 444 453 store().add_block(node.begin(), node.element_size(), partition_size); … … 462 471 if (ptr == 0) 463 472 return 0; 464 473 const details::PODptr<size_type> node(ptr, POD_size); 465 next_size <<= 1;466 474 475 BOOST_USING_STD_MIN(); 476 if(!max_size) 477 next_size <<= 1; 478 else if( next_size*partition_size/requested_size < max_size) 479 next_size = min BOOST_PREVENT_MACRO_SUBSTITUTION(next_size << 1, max_size*requested_size/ partition_size); 480 467 481 // initialize it, 468 482 // (we can use "add_block" here because we know that 469 483 // the free list is empty, so we don't have to use 470 484 // the slower ordered version) 471 store().add_ block(node.begin(), node.element_size(), partition_size);485 store().add_ordered_block(node.begin(), node.element_size(), partition_size); 472 486 473 487 // insert it into the list, 474 488 // handle border case … … 528 542 // the free list is empty, so we don't have to use 529 543 // the slower ordered version) 530 544 if (next_size > num_chunks) 531 store().add_ block(node.begin() + num_chunks * partition_size,545 store().add_ordered_block(node.begin() + num_chunks * partition_size, 532 546 node.element_size() - num_chunks * partition_size, partition_size); 533 547 534 next_size <<= 1; 548 BOOST_USING_STD_MIN(); 549 if(!max_size) 550 next_size <<= 1; 551 else if( next_size*partition_size/requested_size < max_size) 552 next_size = min BOOST_PREVENT_MACRO_SUBSTITUTION(next_size << 1, max_size*requested_size/ partition_size); 535 553 536 554 // insert it into the list, 537 555 // handle border case -
poolfwd.hpp
46 46 template <typename Tag, unsigned RequestedSize, 47 47 typename UserAllocator = default_user_allocator_new_delete, 48 48 typename Mutex = details::pool::default_mutex, 49 unsigned NextSize = 32> 49 unsigned NextSize = 32, 50 unsigned MaxSize = 0> 50 51 struct singleton_pool; 51 52 52 53 // … … 57 58 template <typename T, 58 59 typename UserAllocator = default_user_allocator_new_delete, 59 60 typename Mutex = details::pool::default_mutex, 60 unsigned NextSize = 32> 61 unsigned NextSize = 32, 62 unsigned MaxSize = 0> 61 63 class pool_allocator; 62 64 63 65 struct fast_pool_allocator_tag; … … 65 67 template <typename T, 66 68 typename UserAllocator = default_user_allocator_new_delete, 67 69 typename Mutex = details::pool::default_mutex, 68 unsigned NextSize = 32> 70 unsigned NextSize = 32, 71 unsigned MaxSize = 0> 69 72 class fast_pool_allocator; 70 73 71 74 } // namespace boost -
singleton_pool.hpp
27 27 template <typename Tag, unsigned RequestedSize, 28 28 typename UserAllocator, 29 29 typename Mutex, 30 unsigned NextSize> 30 unsigned NextSize, 31 unsigned MaxSize> 31 32 struct singleton_pool 32 33 { 33 34 public: -
object_pool.hpp
53 53 54 54 public: 55 55 // This constructor parameter is an extension! 56 explicit object_pool(const size_type next_size = 32 )57 :pool<UserAllocator>(sizeof(T), next_size ) { }56 explicit object_pool(const size_type next_size = 32, const size_type max_size = 0) 57 :pool<UserAllocator>(sizeof(T), next_size, max_size) { } 58 58 59 59 ~object_pool(); 60 60 -
pool_alloc.hpp
35 35 template <typename T, 36 36 typename UserAllocator, 37 37 typename Mutex, 38 unsigned NextSize> 38 unsigned NextSize, 39 unsigned MaxSize> 39 40 class pool_allocator 40 41 { 41 42 public: … … 54 55 template <typename U> 55 56 struct rebind 56 57 { 57 typedef pool_allocator<U, UserAllocator, Mutex, NextSize > other;58 typedef pool_allocator<U, UserAllocator, Mutex, NextSize,MaxSize> other; 58 59 }; 59 60 60 61 public: … … 74 75 75 76 // not explicit, mimicking std::allocator [20.4.1] 76 77 template <typename U> 77 pool_allocator(const pool_allocator<U, UserAllocator, Mutex, NextSize > &)78 pool_allocator(const pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> &) 78 79 { 79 80 // Required to ensure construction of singleton_pool IFF an 80 81 // instace of this allocator is constructed during global … … 109 110 { 110 111 const pointer ret = static_cast<pointer>( 111 112 singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex, 112 NextSize >::ordered_malloc(n) );113 NextSize, MaxSize>::ordered_malloc(n) ); 113 114 if (ret == 0) 114 115 boost::throw_exception(std::bad_alloc()); 115 116 return ret; … … 123 124 return; 124 125 #endif 125 126 singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex, 126 NextSize >::ordered_free(ptr, n);127 NextSize, MaxSize>::ordered_free(ptr, n); 127 128 } 128 129 }; 129 130 … … 147 148 template <typename T, 148 149 typename UserAllocator, 149 150 typename Mutex, 150 unsigned NextSize> 151 unsigned NextSize, 152 unsigned MaxSize> 151 153 class fast_pool_allocator 152 154 { 153 155 public: … … 166 168 template <typename U> 167 169 struct rebind 168 170 { 169 typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize > other;171 typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other; 170 172 }; 171 173 172 174 public: … … 187 189 // not explicit, mimicking std::allocator [20.4.1] 188 190 template <typename U> 189 191 fast_pool_allocator( 190 const fast_pool_allocator<U, UserAllocator, Mutex, NextSize > &)192 const fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> &) 191 193 { 192 194 // Required to ensure construction of singleton_pool IFF an 193 195 // instace of this allocator is constructed during global … … 223 225 const pointer ret = (n == 1) ? 224 226 static_cast<pointer>( 225 227 singleton_pool<fast_pool_allocator_tag, sizeof(T), 226 UserAllocator, Mutex, NextSize >::malloc() ) :228 UserAllocator, Mutex, NextSize, MaxSize>::malloc() ) : 227 229 static_cast<pointer>( 228 230 singleton_pool<fast_pool_allocator_tag, sizeof(T), 229 UserAllocator, Mutex, NextSize >::ordered_malloc(n) );231 UserAllocator, Mutex, NextSize, MaxSize>::ordered_malloc(n) ); 230 232 if (ret == 0) 231 233 boost::throw_exception(std::bad_alloc()); 232 234 return ret; … … 237 239 { 238 240 const pointer ret = static_cast<pointer>( 239 241 singleton_pool<fast_pool_allocator_tag, sizeof(T), 240 UserAllocator, Mutex, NextSize >::malloc() );242 UserAllocator, Mutex, NextSize, MaxSize>::malloc() ); 241 243 if (ret == 0) 242 244 boost::throw_exception(std::bad_alloc()); 243 245 return ret; … … 250 252 #endif 251 253 if (n == 1) 252 254 singleton_pool<fast_pool_allocator_tag, sizeof(T), 253 UserAllocator, Mutex, NextSize >::free(ptr);255 UserAllocator, Mutex, NextSize, MaxSize>::free(ptr); 254 256 else 255 257 singleton_pool<fast_pool_allocator_tag, sizeof(T), 256 UserAllocator, Mutex, NextSize >::free(ptr, n);258 UserAllocator, Mutex, NextSize, MaxSize>::free(ptr, n); 257 259 } 258 260 static void deallocate(const pointer ptr) 259 261 { 260 262 singleton_pool<fast_pool_allocator_tag, sizeof(T), 261 UserAllocator, Mutex, NextSize >::free(ptr);263 UserAllocator, Mutex, NextSize, MaxSize>::free(ptr); 262 264 } 263 265 }; 264 266