Boost.Pool C++ Reference
Provides a template type boost::object_pool<T, UserAllocator> that can be used for fast and efficient memory allocation of objects of type T. It also provides automatic destruction of non-deallocated objects.
Provides class pool: a fast memory allocator that guarantees proper alignment of all allocated chunks, and which extends and generalizes the framework provided by the simple segregated storage solution. Also provides two UserAllocator classes which can be used in conjuction with pool. UserAllocator used as template parameter for pool and object_pool. Uses malloc and free internally. An unsigned integral type that can represent the size of the largest object to be allocated. std::size_t A signed integral type that can represent the difference of any two pointers. std::ptrdiff_t char *const size_type voidchar *const Allocator used as the default template parameter for a UserAllocator template parameter. Uses new and delete. An unsigned integral type that can represent the size of the largest object to be allocated. std::size_t A signed integral type that can represent the difference of any two pointers. std::ptrdiff_t char *const size_typeAttempts to allocate n bytes from the system. Returns 0 if out-of-memory voidchar *constAttempts to de-allocate block. Block must have been previously returned from a call to UserAllocator::malloc.
C++ Standard Library compatible pool-based allocators. This header provides two template types - pool_allocator and fast_pool_allocator - that can be used for fast and efficient memory allocation in conjunction with the C++ Standard Library containers.These types both satisfy the Standard Allocator requirements [20.1.5] and the additional requirements in [20.1.5/4], so they can be used with either Standard or user-supplied containers.In addition, the fast_pool_allocator also provides an additional allocation and an additional deallocation function: ExpressionReturn TypeSemantic Equivalence PoolAlloc::allocate()T *PoolAlloc::allocate(1) PoolAlloc::deallocate(p)voidPoolAlloc::deallocate(p, 1) The typedef user_allocator publishes the value of the UserAllocator template parameter.NotesIf the allocation functions run out of memory, they will throw std::bad_alloc.The underlying Pool type used by the allocators is accessible through the Singleton Pool Interface. The identifying tag used for pool_allocator is pool_allocator_tag, and the tag used for fast_pool_allocator is fast_pool_allocator_tag. All template parameters of the allocators (including implementation-specific ones) determine the type of the underlying Pool, with the exception of the first parameter T, whose size is used instead.Since the size of T is used to determine the type of the underlying Pool, each allocator for different types of the same size will share the same underlying pool. The tag class prevents pools from being shared between pool_allocator and fast_pool_allocator. For example, on a system where sizeof(int) == sizeof(void *), pool_allocator<int> and pool_allocator<void *> will both allocate/deallocate from/to the same pool.If there is only one thread running before main() starts and after main() ends, then both allocators are completely thread-safe.Compiler and STL NotesA number of common STL libraries contain bugs in their using of allocators. Specifically, they pass null pointers to the deallocate function, which is explicitly forbidden by the Standard [20.1.5 Table 32]. PoolAlloc will work around these libraries if it detects them; currently, workarounds are in place for: Borland C++ (Builder and command-line compiler) with default (RogueWave) library, ver. 5 and earlier, STLport (with any compiler), ver. 4.0 and earlier. voidUserAllocatorMutexNextSizeMaxSizeSpecialization of fast_pool_allocator<void>. Specialization of fast_pool_allocator<void> required to make the allocator standard-conforming. Nested class rebind allows for transformation from fast_pool_allocator<T> to fast_pool_allocator. Nested class rebind allows for transformation from fast_pool_allocator<T> to fast_pool_allocator via the member typedef other. fast_pool_allocator< U, UserAllocator, Mutex, NextSize, MaxSize > void * const void * void Simple tag type used by fast_pool_allocator as a template parameter to the underlying singleton_pool. voidUserAllocatorMutexNextSizeMaxSizeSpecialization of pool_allocator<void>. Specialization of pool_allocator for type void: required by the standard to make this a conforming allocator type. Nested class rebind allows for transformation from pool_allocator<T> to pool_allocator. Nested class rebind allows for transformation from pool_allocator<T> to pool_allocator via the member typedef other. pool_allocator< U, UserAllocator, Mutex, NextSize, MaxSize > void * const void * void Simple tag type used by pool_allocator as an argument to the underlying singleton_pool.
Forward declarations of all public (non-implemention) classes. A C++ Standard Library conforming allocator geared towards allocating single chunks. While class template pool_allocator is a more general-purpose solution geared towards efficiently servicing requests for any number of contiguous chunks, fast_pool_allocator is also a general-purpose solution, but is geared towards efficiently servicing requests for one chunk at a time; it will work for contiguous chunks, but not as well as pool_allocator.If you are seriously concerned about performance, use fast_pool_allocator when dealing with containers such as std::list, and use pool_allocator when dealing with containers such as std::vector.The template parameters 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 Pool when it is created.MaxSize Limit on the maximum size used.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. voidconst pointerconst value_type & voidconst pointerDestroy ptr using destructor. boolconst fast_pool_allocator & boolconst fast_pool_allocator & Ensures construction of the underlying 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 . const fast_pool_allocator< U, UserAllocator, Mutex, NextSize, MaxSize > &Ensures construction of the underlying 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 . pointerreference const_pointerconst_reference size_type pointerconst size_type pointerconst size_typeconst void * constAllocate memory . pointerAllocate memory. voidconst pointerconst size_typeDeallocate memory. voidconst pointerdeallocate/free boost::pool< UserAllocator >A template class that can be used for fast and efficient memory allocation of objects. It also provides automatic destruction of non-deallocated objects. T The type of object to allocate/deallocate. T must have a non-throwing destructor.UserAllocator Defines the allocator that the underlying Pool will use to allocate memory from the system. See User Allocators for details.Class object_pool is a template class that can be used for fast and efficient memory allocation of objects. It also provides automatic destruction of non-deallocated objects.When the object pool is destroyed, then the destructor for type T is called for each allocated T that has not yet been deallocated. O(N).Whenever an object of type ObjectPool needs memory from the system, it will request it from its UserAllocator template parameter. The amount requested is determined using a doubling algorithm; that is, each time more system memory is allocated, the amount of system memory requested is doubled. Users may control the doubling algorithm by the parameters passed to the object_pool's constructor. pool< UserAllocator > & The underlying boost:: pool storage used by *this. const pool< UserAllocator > & The underlying boost:: pool storage used by *this. void *&void *const The next memory block after ptr (for the sake of code readability :) element_type *Allocates memory that can hold one object of type ElementType.If out of memory, returns 0.Amortized O(1). voidelement_type *constDe-Allocates memory that holds a chunk of type ElementType.Note that p may not be 0. Note that the destructor for p is not called. O(N). boolelement_type *const Returns false if chunk 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.This function may NOT be used to reliably test random pointer values! true if chunk was allocated from *this or may be returned as the result of a future allocation from *this. element_type * A pointer to an object of type T, allocated in memory from the underlying pool and default constructed. The returned objected can be freed by a call to destroy. Otherwise the returned object will be automatically destroyed when *this is destroyed. element_type *Arg1 &... ArgN & Since the number and type of arguments to this function is totally arbitrary, a simple system has been set up to automatically generate template construct functions. This system is based on the macro preprocessor m4, which is standard on UNIX systems and also available for Win32 systems. detail/pool_construct.m4, when run with m4, will create the file detail/pool_construct.ipp, which only defines the construct functions for the proper number of arguments. The number of arguments may be passed into the file as an m4 macro, NumberOfArguments; if not provided, it will default to 3. For each different number of arguments (1 to NumberOfArguments), a template function is generated. There are the same number of template parameters as there are arguments, and each argument's type is a reference to that (possibly cv-qualified) template argument. Each possible permutation of the cv-qualifications is also generated. Because each permutation is generated for each possible number of arguments, the included file size grows exponentially in terms of the number of constructor arguments, not linearly. For the sake of rational compile times, only use as many arguments as you need. detail/pool_construct.bat and detail/pool_construct.sh are also provided to call m4, defining NumberOfArguments to be their command-line parameter. See these files for more details. A pointer to an object of type T, allocated in memory from the underlying pool and constructed from arguments Arg1 to ArgN. The returned objected can be freed by a call to destroy. Otherwise the returned object will be automatically destroyed when *this is destroyed. voidelement_type *constDestroys an object allocated with construct.Equivalent to:p->~ElementType(); this->free(p); p must have been previously allocated from *this via a call to construct. size_type The number of chunks that will be allocated next time we run out of memory. voidconst size_typewanted next_size (must not be zero). Set a new number of chunks to allocate the next time we run out of memory. const size_type32const size_type0Constructs a new (empty by default) ObjectPool. next_size != 0. boost::simple_segregated_storage< UserAllocator::size_type >A fast memory allocator that guarantees proper alignment of all allocated chunks. Whenever an object of type pool needs memory from the system, it will request it from its UserAllocator template parameter. The amount requested is determined using a doubling algorithm; that is, each time more system memory is allocated, the amount of system memory requested is doubled.Users may control the doubling algorithm by using the following extensions:Users may pass an additional constructor parameter to pool. This parameter is of type size_type, and is the number of chunks to request from the system the first time that object needs to allocate system memory. The default is 32. This parameter may not be 0.Users may also pass an optional third parameter to pool's constructor. This parameter is of type size_type, and sets a maximum size for allocated chunks. When this parameter takes the default value of 0, then there is no upper limit on chunk size.Finally, if the doubling algorithm results in no memory being allocated, the pool will backtrack just once, halving the chunk size and trying again.UserAllocator type - the method that the Pool will use to allocate memory from the system.There are essentially two ways to use class pool: the client can call malloc() and free() to allocate and free single chunks of memory, this is the most efficient way to use a pool, but does not allow for the efficient allocation of arrays of chunks. Alternatively, the client may call ordered_malloc() and ordered_free(), in which case the free list is maintained in an ordered state, and efficient allocation of arrays of chunks are possible. However, this latter option can suffer from poor performance when large numbers of allocations are performed. void * No memory in any of our storages; make a new storage, Allocates chunk in newly malloc aftert resize. 0 if out-of-memory. Called if malloc/ordered_malloc needs to resize the free list. pointer to chunk. void *Called if malloc needs to resize the free list. No memory in any of our storages; make a new storage, pointer to new chunk. simple_segregated_storage< size_type > & pointer to store. const simple_segregated_storage< size_type > & pointer to store. details::PODptr< size_type >void *constfinds which POD in the list 'chunk' was allocated from. find which PODptr storage memory that this chunk is from. the PODptr that holds this chunk. size_typeCalculated size of the memory chunks that will be allocated by this Pool. allocated size. boolvoid *constchunk to check if is from this pool. char *constmemory chunk at i with element sizeof_i. const size_typeelement size (size of the chunk area of that block, not the total size of that block). Returns false if chunk 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. true if chunk was allocated or may be returned. as the result of a future allocation. void *&void *const Pointer dereferenced. (Provided and used for the sake of code readability :) boolpool must be ordered. Frees every memory block that doesn't have any allocated chunks. true if at least one memory block was freed. boolpool must be ordered. Frees every memory block.This function invalidates any pointers previously returned by allocation functions of t. true if at least one memory block was freed. size_typeNumber of chunks to request from the system the next time that object needs to allocate system memory. This value should never be 0. next_size; voidconst size_typeSet number of chunks to request from the system the next time that object needs to allocate system memory. This value should never be set to 0. nnext_size. size_type max_size. voidconst size_typeSet max_size. size_type the requested size passed into the constructor. (This value will not change during the lifetime of a Pool object). void *Allocates a chunk of memory. Searches in the list of memory blocks for a block that has a free chunk, and returns that free chunk if found. Otherwise, creates a new memory block, adds its free list to pool's free list, a free chunk from that block. If a new memory block cannot be allocated, returns 0. Amortized O(1). void *Same as malloc, only merges the free lists, to preserve order. Amortized O(1). a free chunk from that block. If a new memory block cannot be allocated, returns 0. Amortized O(1). void *size_typeGets address of a chunk n, allocating new memory if not already available. Address of chunk n if allocated ok. 0 if not enough memory for n chunks. voidvoid *constSame as malloc, only allocates enough contiguous chunks to cover n * requested_size bytes. Amortized O(n). Deallocates a chunk of memory. Note that chunk may not be 0. O(1).Chunk must have been previously returned by t.malloc() or t.ordered_malloc(). Assumes that chunk actually refers to a block of chunks spanning n * partition_sz bytes. deallocates each chunk in that block. Note that chunk may not be 0. O(n). a free chunk from that block. If a new memory block cannot be allocated, returns 0. Amortized O(1). voidvoid *constSame as above, but is order-preserving.Note that chunk may not be 0. O(N) with respect to the size of the free list. chunk must have been previously returned by t.malloc() or t.ordered_malloc(). voidvoid *constconst size_typeAssumes that chunk actually refers to a block of chunks.chunk must have been previously returned by t.ordered_malloc(n) spanning n * partition_sz bytes. Deallocates each chunk in that block. Note that chunk may not be 0. O(n). voidvoid *constconst size_typeAssumes that chunk actually refers to a block of chunks spanning n * partition_sz bytes; deallocates each chunk in that block.Note that chunk may not be 0. Order-preserving. O(N + n) where N is the size of the free list. chunk must have been previously returned by t.malloc() or t.ordered_malloc(). boolvoid *const Returns true if chunk was allocated from u or may be returned as the result of a future allocation from u. Returns false if chunk 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. const size_typeRequested chunk size const size_type32parameter is of type size_type, is the number of chunks to request from the system the first time that object needs to allocate system memory. The default is 32. This parameter may not be 0. const size_type0is the maximum number of chunks to allocate in one block. Constructs a new empty Pool that can be used to allocate chunks of size RequestedSize. Destructs the Pool, freeing its list of memory blocks. A C++ Standard Library conforming allocator, based on an underlying pool. 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.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. boolconst pool_allocator & boolconst 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) . 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 . pointerreference const_pointerconst_reference size_type voidconst pointerconst value_type & voidconst pointer pointerconst size_type pointerconst size_typebytes to allocate. const void * constallocate n bytes voidconst pointerlocation to deallocate from. const size_typenumber of bytes to deallocate. Deallocate n bytes from ptr
Simple Segregated Storage. A simple segregated storage implementation: simple segregated storage is the basic idea behind the Boost Pool library. Simple segregated storage is the simplest, and probably the fastest, memory allocation/deallocation algorithm. It begins by partitioning a memory block into fixed-size chunks. Where the block comes from is not important until implementation time. A Pool is some object that uses Simple Segregated Storage in this fashion. Simple Segregated Storage is the simplest, and probably the fastest, memory allocation/deallocation algorithm. It is responsible for partitioning a memory block into fixed-size chunks: where the block comes from is determined by the client of the class. Template class simple_segregated_storage controls access to a free list of memory chunks. Please note that this is a very simple class, with preconditions on almost all its functions. It is intended to be the fastest and smallest possible quick memory allocator - e.g., something to use in embedded systems. This class delegates many difficult preconditions to the user (i.e., alignment issues).An object of type simple_segregated_storage<SizeType> is empty if its free list is empty. If it is not empty, then it is ordered if its free list is ordered. A free list is ordered if repeated calls to malloc() will result in a constantly-increasing sequence of values, as determined by std::less<void *>. A member function is order-preserving if the free list maintains its order orientation (that is, an ordered free list is still ordered after the member function call). SizeType const simple_segregated_storage & voidconst simple_segregated_storage & void *void *&size_typesize_type (n > 0), (start != 0), (nextof(start) != 0) (start != 0) The function attempts to find n contiguous chunks of size partition_size in the free list, starting at start. If it succeds, it returns the last chunk in that contiguous sequence, so that the sequence is known by [start, {retval}] If it fails, it does do either because it's at the end of the free list or hits a non-contiguous chunk. In either case, it will return 0, and set start to the last considered chunk. You are at the end of the free list if nextof(start) == 0. Otherwise, start points to the last chunk in the contiguous sequence, and nextof(start) points to the first chunk in the next contiguous sequence (assuming an ordered free list). void *void *Traverses the free list referred to by "first", and returns the iterator previous to where "ptr" would go if it was in the free list. Returns 0 if "ptr" would go at the beginning of the free list (i.e., before "first"). Note that this function finds the location previous to where ptr would go if it was in the free list. It does not find the entry in the free list before ptr (unless ptr is already in the free list). Specifically, find_prev(0) will return 0, not the last entry in the free list. location previous to where ptr would go if it was in the free list. void *&void *constThe return value is just *ptr cast to the appropriate type. ptr must not be 0. (For the sake of code readability :)As an example, let us assume that we want to truncate the free list after the first chunk. That is, we want to set *first to 0; this will result in a free list with only one entry. The normal way to do this is to first cast first to a pointer to a pointer to void, and then dereference and assign (*static_cast<void **>(first) = 0;). This can be done more easily through the use of this convenience function (nextof(first) = 0;). dereferenced pointer. voidvoid *constconst size_typeconst size_typeAdd block Segregate this block and merge its free list into the free list referred to by "first". Same as segregate. !empty() voidvoid *constconst size_typeconst size_typeadd block (ordered into list) This (slower) version of add_block segregates the block and merges its free list into our free list in the proper order. bool true only if simple_segregated_storage is empty. void *Create a chunk. !empty() Increment the "first" pointer to point to the next chunk. voidvoid *constFree a chunk. chunk was previously returned from a malloc() referring to the same free list. !empty() voidvoid *constThis (slower) implementation of 'free' places the memory back in the list in its proper order. chunk was previously returned from a malloc() referring to the same free list !empty(). void *size_typesize_typeAttempts to find a contiguous sequence of n partition_sz-sized chunks. If found, removes them all from the free list and returns a pointer to the first. If not found, returns 0. It is strongly recommended (but not required) that the free list be ordered, as this algorithm will fail to find a contiguous sequence unless it is contiguous in the free list as well. Order-preserving. O(N) with respect to the size of the free list. voidvoid *constconst size_typeconst size_type If you're allocating/deallocating n a lot, you should be using an ordered pool. chunks was previously allocated from *this with the same values for n and partition_size. !empty() voidvoid *constconst size_typeconst size_typeFree n chunks from order list. chunks was previously allocated from *this with the same values for n and partition_size. n should not be zero (n == 0 has no effect). Construct empty storage area. empty() void *void *size_typesize_typevoid *0Segregate block into chunks. npartition_sz >= sizeof(void *) npartition_sz = sizeof(void *) * i, for some integer i nsz >= npartition_sz Block is properly aligned for an array of object of size npartition_sz and array of void *. The requirements above guarantee that any pointer to a chunk (which is a pointer to an element in an array of npartition_sz) may be cast to void **.
The singleton_pool class allows other pool interfaces for types of the same size to share the same underlying pool. Header singleton_pool.hpp provides a template class singleton_pool, which provides access to a pool as a singleton object. The singleton_pool class allows other pool interfaces for types of the same size to share the same pool. Template parameters are as follows:Tag User-specified type to uniquely identify this pool: allows different unbounded sets of singleton pools to exist.RequestedSize The size of each chunk returned by member function malloc().UserAllocator User allocator, default = default_user_allocator_new_delete.Mutex This class is the type of mutex to use to protect simultaneous access to the underlying Pool. Can be any Boost.Thread Mutex type or boost::details::pool::null_mutex. It is exposed so that users may declare some singleton pools normally (i.e., with synchronization), but some singleton pools without synchronization (by specifying boost::details::pool::null_mutex) for efficiency reasons. The member typedef mutex exposes the value of this template parameter. The default for this parameter is boost::details::pool::default_mutex which is a synonym for either boost::details::pool::null_mutex (when threading support is turned off in the compiler (so BOOST_HAS_THREADS is not set), or threading support has ben explicitly disabled with BOOST_DISABLE_THREADS (Boost-wide disabling of threads) or BOOST_POOL_NO_MT (this library only)) or for boost::mutex (when threading support is enabled in the compiler).NextSize The value of this parameter is passed to the underlying Pool when it is created and specifies the number of chunks to allocate in the first allocation request (defaults to 32). The member typedef static const value next_size exposes the value of this template parameter.MaxSizeThe value of this parameter is passed to the underlying Pool when it is created and specifies the maximum number of chunks to allocate in any single allocation request (defaults to 0).Notes:The underlying pool p referenced by the static functions in singleton_pool is actually declared in a way that is:1 Thread-safe if there is only one thread running before main() begins and after main() ends – all of the static functions of singleton_pool synchronize their access to p.2 Guaranteed to be constructed before it is used – thus, the simple static object in the synopsis above would actually be an incorrect implementation. The actual implementation to guarantee this is considerably more complicated.3 Note too that a different underlying pool p exists for each different set of template parameters, including implementation-specific ones.4 The underlying pool is constructed "as if" by:pool<UserAllocator> p(RequestedSize, NextSize, MaxSize);The underlying pool constructed by the singleton is never freed. This means that memory allocated by a singleton_pool can be still used after main() has completed, but may mean that some memory checking programs will complain about leaks from singleton_pool. void The Tag template parameter uniquely identifies this pool and allows different unbounded sets of singleton pools to exist. For example, the pool allocators use two tag classes to ensure that the two different allocator types never share the same underlying singleton pool. Tag is never actually used by singleton_pool. Tag The type of mutex used to synchonise access to this pool (default details::pool::default_mutex). Mutex The user-allocator used by this pool, default = default_user_allocator_new_delete. UserAllocator size_type of user allocator. pool< UserAllocator >::size_type difference_type of user allocator. pool< UserAllocator >::difference_type const unsignedThe size of each chunk allocated by this pool. const unsignedThe number of chunks to allocate on the first allocation. pool< UserAllocator >For exposition only! void *Equivalent to SingletonPool::p.malloc(); synchronized. void *Equivalent to SingletonPool::p.ordered_malloc(); synchronized. void *const size_typeEquivalent to SingletonPool::p.ordered_malloc(n); synchronized. boolvoid *constEquivalent to SingletonPool::p.is_from(chunk); synchronized. true if chunk is from SingletonPool::is_from(chunk) voidvoid *constEquivalent to SingletonPool::p.free(chunk); synchronized. voidvoid *constEquivalent to SingletonPool::p.ordered_free(chunk); synchronized. voidvoid *constconst size_typeEquivalent to SingletonPool::p.free(chunk, n); synchronized. voidvoid *constconst size_typeEquivalent to SingletonPool::p.ordered_free(chunk, n); synchronized. boolEquivalent to SingletonPool::p.release_memory(); synchronized. boolEquivalent to SingletonPool::p.purge_memory(); synchronized. pool_type &