...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
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 { private: object_pool(const object_pool &); void operator=(const object_pool &); public: typedef ElementType element_type; typedef UserAllocator user_allocator; typedef typename pool<UserAllocator>::size_type size_type; typedef typename pool<UserAllocator>::difference_type difference_type; object_pool(); ~object_pool(); 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); };
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 | Meaning |
---|---|
ObjectPool | object_pool<ElementType, UserAllocator> |
t | value of type ObjectPool |
u | value of type const ObjectPool |
p | value of type ElementType * |
Expression | Type |
---|---|
ObjectPool::element_type | ElementType |
ObjectPool::user_allocator | UserAllocator |
ObjectPool::size_type | pool<UserAllocator>::size_type |
ObjectPool::difference_type | pool<UserAllocator>::difference_type |
Expression | Return Type | Notes |
---|---|---|
ObjectPool() | not used | Constructs a new empty ObjectPool |
(&t)->~ObjectPool() | not used | Destructs the ObjectPool; ~ElementType() is called for each allocated ElementType that has not been deallocated. O(N). |
u.is_from(p) | bool | Returns 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. |
Expression | Return Type | Pre-Condition | Semantic Equivalence | Notes |
---|---|---|---|---|
t.malloc() | ElementType * | Allocates memory that can hold an object of type ElementType. If out of memory, returns 0. Amortized O(1). | ||
t.free(p) | not used | p must have been previously allocated from t | Deallocates 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_construct | Allocates and initializes an object of type ElementType. If out of memory, returns 0. Amortized O(1). | |
t.destroy(p) | not used | p must have been previously allocated from t | p->~ElementType(); t.free(p); |
Revised 05 December, 2006
Copyright © 2000, 2001 Stephen Cleary (scleary AT jerviswebb DOT com)
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)