Ticket #2696: pool.patch

File pool.patch, 9.3 KB (added by Nikolay Mladenov, 14 years ago)
  • pool.hpp

     
    154154    const size_type requested_size;
    155155    size_type next_size;
    156156    size_type start_size;
     157    size_type max_size;
    157158
    158159    // finds which POD in the list 'chunk' was allocated from
    159160    details::PODptr<size_type> find_POD(void * const chunk) const;
     
    192193    // The second parameter here is an extension!
    193194    // pre: npartition_size != 0 && nnext_size != 0
    194195    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)
    197199    { }
    198200
    199201    ~pool() { purge_memory(); }
     
    210212    // These functions are extensions!
    211213    size_type get_next_size() const { return next_size; }
    212214    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; }
    213217    size_type get_requested_size() const { return requested_size; }
    214218
    215219    // Both malloc and ordered_malloc do a quick inlined check first for any
     
    438442  if (ptr == 0)
    439443    return 0;
    440444  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);
    442451
    443452  //  initialize it,
    444453  store().add_block(node.begin(), node.element_size(), partition_size);
     
    462471  if (ptr == 0)
    463472    return 0;
    464473  const details::PODptr<size_type> node(ptr, POD_size);
    465   next_size <<= 1;
    466474
     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
    467481  //  initialize it,
    468482  //  (we can use "add_block" here because we know that
    469483  //  the free list is empty, so we don't have to use
    470484  //  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);
    472486
    473487  //  insert it into the list,
    474488  //   handle border case
     
    528542  //  the free list is empty, so we don't have to use
    529543  //  the slower ordered version)
    530544  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,
    532546        node.element_size() - num_chunks * partition_size, partition_size);
    533547
    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);
    535553
    536554  //  insert it into the list,
    537555  //   handle border case
  • poolfwd.hpp

     
    4646template <typename Tag, unsigned RequestedSize,
    4747    typename UserAllocator = default_user_allocator_new_delete,
    4848    typename Mutex = details::pool::default_mutex,
    49     unsigned NextSize = 32>
     49    unsigned NextSize = 32,
     50    unsigned MaxSize = 0>
    5051struct singleton_pool;
    5152
    5253//
     
    5758template <typename T,
    5859    typename UserAllocator = default_user_allocator_new_delete,
    5960    typename Mutex = details::pool::default_mutex,
    60     unsigned NextSize = 32>
     61    unsigned NextSize = 32,
     62    unsigned MaxSize = 0>
    6163class pool_allocator;
    6264
    6365struct fast_pool_allocator_tag;
     
    6567template <typename T,
    6668    typename UserAllocator = default_user_allocator_new_delete,
    6769    typename Mutex = details::pool::default_mutex,
    68     unsigned NextSize = 32>
     70    unsigned NextSize = 32,
     71    unsigned MaxSize = 0>
    6972class fast_pool_allocator;
    7073
    7174} // namespace boost
  • singleton_pool.hpp

     
    2727template <typename Tag, unsigned RequestedSize,
    2828    typename UserAllocator,
    2929    typename Mutex,
    30     unsigned NextSize>
     30    unsigned NextSize,
     31    unsigned MaxSize>
    3132struct singleton_pool
    3233{
    3334  public:
  • object_pool.hpp

     
    5353
    5454  public:
    5555    // 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) { }
    5858
    5959    ~object_pool();
    6060
  • pool_alloc.hpp

     
    3535template <typename T,
    3636    typename UserAllocator,
    3737    typename Mutex,
    38     unsigned NextSize>
     38    unsigned NextSize,
     39    unsigned MaxSize>
    3940class pool_allocator
    4041{
    4142  public:
     
    5455    template <typename U>
    5556    struct rebind
    5657    {
    57       typedef pool_allocator<U, UserAllocator, Mutex, NextSize> other;
     58      typedef pool_allocator<U, UserAllocator, Mutex, NextSize,MaxSize> other;
    5859    };
    5960
    6061  public:
     
    7475
    7576    // not explicit, mimicking std::allocator [20.4.1]
    7677    template <typename U>
    77     pool_allocator(const pool_allocator<U, UserAllocator, Mutex, NextSize> &)
     78    pool_allocator(const pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> &)
    7879    {
    7980      // Required to ensure construction of singleton_pool IFF an
    8081      // instace of this allocator is constructed during global
     
    109110    {
    110111      const pointer ret = static_cast<pointer>(
    111112          singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
    112               NextSize>::ordered_malloc(n) );
     113              NextSize, MaxSize>::ordered_malloc(n) );
    113114      if (ret == 0)
    114115        boost::throw_exception(std::bad_alloc());
    115116      return ret;
     
    123124        return;
    124125#endif
    125126      singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
    126           NextSize>::ordered_free(ptr, n);
     127          NextSize, MaxSize>::ordered_free(ptr, n);
    127128    }
    128129};
    129130
     
    147148template <typename T,
    148149    typename UserAllocator,
    149150    typename Mutex,
    150     unsigned NextSize>
     151    unsigned NextSize,
     152    unsigned MaxSize>
    151153class fast_pool_allocator
    152154{
    153155  public:
     
    166168    template <typename U>
    167169    struct rebind
    168170    {
    169       typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize> other;
     171      typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
    170172    };
    171173
    172174  public:
     
    187189    // not explicit, mimicking std::allocator [20.4.1]
    188190    template <typename U>
    189191    fast_pool_allocator(
    190         const fast_pool_allocator<U, UserAllocator, Mutex, NextSize> &)
     192        const fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> &)
    191193    {
    192194      // Required to ensure construction of singleton_pool IFF an
    193195      // instace of this allocator is constructed during global
     
    223225      const pointer ret = (n == 1) ?
    224226          static_cast<pointer>(
    225227              singleton_pool<fast_pool_allocator_tag, sizeof(T),
    226                   UserAllocator, Mutex, NextSize>::malloc() ) :
     228                  UserAllocator, Mutex, NextSize, MaxSize>::malloc() ) :
    227229          static_cast<pointer>(
    228230              singleton_pool<fast_pool_allocator_tag, sizeof(T),
    229                   UserAllocator, Mutex, NextSize>::ordered_malloc(n) );
     231                  UserAllocator, Mutex, NextSize, MaxSize>::ordered_malloc(n) );
    230232      if (ret == 0)
    231233        boost::throw_exception(std::bad_alloc());
    232234      return ret;
     
    237239    {
    238240      const pointer ret = static_cast<pointer>(
    239241          singleton_pool<fast_pool_allocator_tag, sizeof(T),
    240               UserAllocator, Mutex, NextSize>::malloc() );
     242              UserAllocator, Mutex, NextSize, MaxSize>::malloc() );
    241243      if (ret == 0)
    242244        boost::throw_exception(std::bad_alloc());
    243245      return ret;
     
    250252#endif
    251253      if (n == 1)
    252254        singleton_pool<fast_pool_allocator_tag, sizeof(T),
    253             UserAllocator, Mutex, NextSize>::free(ptr);
     255            UserAllocator, Mutex, NextSize, MaxSize>::free(ptr);
    254256      else
    255257        singleton_pool<fast_pool_allocator_tag, sizeof(T),
    256             UserAllocator, Mutex, NextSize>::free(ptr, n);
     258            UserAllocator, Mutex, NextSize, MaxSize>::free(ptr, n);
    257259    }
    258260    static void deallocate(const pointer ptr)
    259261    {
    260262      singleton_pool<fast_pool_allocator_tag, sizeof(T),
    261           UserAllocator, Mutex, NextSize>::free(ptr);
     263          UserAllocator, Mutex, NextSize, MaxSize>::free(ptr);
    262264    }
    263265};
    264266