Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is an old version of boost. Click here for the latest version's documentation home page.
C++ Boost

object_pool - Boost Object Pool Allocator


object_pool.hpp provides a template type that can be used for fast and efficient memory allocation. It also provides automatic destruction of non-deallocated objects. For information on other pool-based interfaces, see the other pool interfaces.


template <typename ElementType, typename UserAllocator = default_user_allocator_new_delete>
class object_pool
    object_pool(const object_pool &);
    void operator=(const object_pool &);

    typedef ElementType element_type;
    typedef UserAllocator user_allocator;
    typedef typename pool<UserAllocator>::size_type size_type;
    typedef typename pool<UserAllocator>::difference_type difference_type;


    element_type * malloc();
    void free(element_type * p);
    bool is_from(element_type * p) const;

    element_type * construct();
    // other construct() functions
    void destroy(element_type * p);

Template Parameters


The template parameter is the type of object to allocate/deallocate. It must have a non-throwing destructor.


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


Symbol Table
ObjectPoolobject_pool<ElementType, UserAllocator>
tvalue of type ObjectPool
uvalue of type const ObjectPool
pvalue of type ElementType *


Constructors, Destructors, and Testing
ExpressionReturn TypeNotes
ObjectPool()not usedConstructs a new empty ObjectPool
(&t)->~ObjectPool()not usedDestructs the ObjectPool; ~ElementType() is called for each allocated ElementType that has not been deallocated. O(N).
u.is_from(p)boolReturns true if p was allocated from u or may be returned as the result of a future allocation from u. Returns false if p was allocated from some other pool or may be returned as the result of a future allocation from some other pool. Otherwise, the return value is meaningless; note that this function may not be used to reliably test random pointer values.

Allocation and Deallocation
ExpressionReturn TypePre-ConditionSemantic EquivalenceNotes
t.malloc()ElementType *Allocates memory that can hold an object of type ElementType. If out of memory, returns 0. Amortized O(1). usedp must have been previously allocated from tDeallocates a chunk of memory. Note that p may not be 0. Note that the destructor for p is not called. O(N).
t.construct(???)ElementType *ElementType must have a constructor matching ???; the number of parameters given must not exceed what is supported through pool_constructAllocates and initializes an object of type ElementType. If out of memory, returns 0. Amortized O(1).
t.destroy(p)not usedp must have been previously allocated from tp->~ElementType();;


Implementation Details

Copyright © 2000, 2001 Stephen Cleary (scleary AT jerviswebb DOT com)

This file can be redistributed and/or modified under the terms found in copyright.html

This software and its documentation is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.