| Home | Libraries | People | FAQ | More

## Class template pool_allocator

boost::pool_allocator — A C++ Standard Library conforming allocator, based on an underlying pool.

## Synopsis

    // In header: <boost/pool/pool_alloc.hpp>

    template<typename T, typename UserAllocator, typename Mutex,
             unsigned NextSize, unsigned MaxSize>
    class pool_allocator {
    public:
      // types
      typedef T                                      value_type;       // value_type of template parameter T.
      typedef UserAllocator                          user_allocator;   // allocator that defines the method that the underlying Pool will use to allocate memory from the system.
      typedef Mutex                                  mutex;            // typedef mutex publishes the value of the template parameter Mutex.
      typedef value_type *                           pointer;
      typedef const value_type *                     const_pointer;
      typedef value_type &                           reference;
      typedef const value_type &                     const_reference;
      typedef pool< UserAllocator >::size_type       size_type;
      typedef pool< UserAllocator >::difference_type difference_type;

      // member classes/structs/unions

      // Nested class rebind allows for transformation from pool_allocator<T> to pool_allocator<underline>. </underline>
      template<typename U>
      struct rebind {
        // types
        typedef pool_allocator< U, UserAllocator, Mutex, NextSize, MaxSize > other;
      };

      // construct/copy/destruct
      pool_allocator();
      template<typename U>
        pool_allocator(const pool_allocator< U, UserAllocator, Mutex, NextSize, MaxSize > &);

      // public member functions
      bool operator==(const pool_allocator &) const;
      bool operator!=(const pool_allocator &) const;

      // public static functions
      static pointer address(reference);
      static const_pointer address(const_reference);
      static size_type max_size();
      static void construct(const pointer, const value_type &);
      static void destroy(const pointer);
      static pointer allocate(const size_type);
      static pointer allocate(const size_type, const void * const);
      static void deallocate(const pointer, const size_type);

      // public data members
      static const unsigned next_size;  // next_size publishes the values of the template parameter NextSize.
    };

## Description

Template parameters for pool_allocator are defined as follows:

T Type of object to allocate/deallocate.

UserAllocator. Defines the method that the underlying Pool will use to allocate memory from the system. See User Allocators for details.

Mutex Allows the user to determine the type of synchronization to be used on the underlying singleton_pool.

NextSize The value of this parameter is passed to the underlying singleton_pool when it is created.

MaxSize Limit on the maximum size used.

| Note
The underlying singleton_pool used by the this allocator constructs a pool instance that is never freed. This means that memory allocated by the allocator can be still used after main() has completed, but may mean that some memory checking programs will complain about leaks.

### `pool_allocator` public construct/copy/destruct

1.     pool_allocator();

   Results in default construction of the underlying `singleton_pool` IFF an instance of this allocator is constructed during global initialization ( required to ensure construction of `singleton_pool` IFF an instance of this allocator is constructed during global initialization. See ticket #2359 for a complete explanation at http://svn.boost.org/trac/boost/ticket/2359) .

2.     template<typename U>
         pool_allocator(const pool_allocator< U, UserAllocator, Mutex, NextSize, MaxSize > &);

   Results in the default construction of the underlying `singleton_pool`, this is required to ensure construction of `singleton_pool` IFF an instance of this allocator is constructed during global initialization. See ticket #2359 for a complete explanation at http://svn.boost.org/trac/boost/ticket/2359 .

### `pool_allocator` public member functions

1.     bool operator==(const pool_allocator &) const;

2.     bool operator!=(const pool_allocator &) const;

### `pool_allocator` public static functions

1.     static pointer address(reference r);

2.     static const_pointer address(const_reference s);

3.     static size_type max_size();

4.     static void construct(const pointer ptr, const value_type & t);

5.     static void destroy(const pointer ptr);

6.     static pointer allocate(const size_type n);

7.     static pointer allocate(const size_type n, const void * const);

   allocate n bytes

   Parameters: |
   `n` | bytes to allocate.

8.     static void deallocate(const pointer ptr, const size_type n);

   Deallocate n bytes from ptr

   Parameters: |
   `n` | number of bytes to deallocate.
   `ptr` | location to deallocate from.

### Specializations

- Class template pool_allocator<void, UserAllocator, Mutex, NextSize, MaxSize>