MIRA
Classes | Public Types | Public Member Functions | List of all members
PoolAllocator< T > Class Template Reference

Provides a pool allocator that is compatible to STL allocators. More...

#include <utils/PoolAllocator.h>

Inheritance diagram for PoolAllocator< T >:
Inheritance graph
[legend]

Public Types

typedef T value_type
 Element type. More...
 
typedef std::size_t size_type
 Quantities of elements. More...
 
typedef std::ptrdiff_t difference_type
 Difference between two pointers. More...
 
typedef T * pointer
 Pointer to element. More...
 
typedef T & reference
 Reference to element. More...
 
typedef const T * const_pointer
 Reference to element. More...
 
typedef const T & const_reference
 Constant reference to element. More...
 

Public Member Functions

 PoolAllocator (size_type poolCapacity=32, bool growPools=true, size_type itemSize=sizeof(T))
 Creates a new PoolAllocator. More...
 
 ~PoolAllocator ()
 Destructor. More...
 
 PoolAllocator (PoolAllocator &&other) noexcept
 
PoolAllocatoroperator= (PoolAllocator &&other) noexcept
 
void swap (PoolAllocator &other)
 
size_type allocatedMemory () const
 
size_type usedMemory () const
 
size_type unusedMemory () const
 
pointer address (reference x) const
 Returns the address of x. More...
 
const_pointer address (const_reference x) const
 Returns the address of x. More...
 
size_type max_size () const MIRA_NOEXCEPT_OR_NOTHROW
 Returns the maximum number of elements of type T (the template parameter) that could be allocated by a call to member allocate. More...
 
size_type itemSize () const MIRA_NOEXCEPT_OR_NOTHROW
 Returns the size of each item in bytes. More...
 
pointer allocate (size_type n, const void *hint=0)
 Allocate a block of storage and returns the pointer, note that the object is not constructed. More...
 
void construct (pointer p, const_reference val)
 Constructs an object of type T on the location p using its copy constructor to initialize its value to val. More...
 
void destroy (T *p)
 Destroy an object. Notice that this does not deallocate space for the element. More...
 
void deallocate (pointer p, size_type n)
 Releases a block of storage previously allocated with member allocate and not yet released. More...
 
T * construct ()
 Allocates and constructs a new object and returns its pointer. More...
 
T * construct (const_reference val)
 Allocates a new feature and returns its pointer This method is provided for convenience. More...
 
void destruct (pointer p)
 Destroys the object and deallocates the memory. More...
 

Detailed Description

template<typename T>
class mira::PoolAllocator< T >

Provides a pool allocator that is compatible to STL allocators.

This allocator is suitable whenever MANY smaller objects are allocated (e.g. several million objects with a size of 32 bytes).

This allocator always allocates pools of objects instead of each object separately. This reduces the management overhead for each object. If the current memory pool is exhausted, another pool is allocated. There are two modes for the allocation of new pools:

  1. each memory pool has the same constant size that is specified within the constructor
  2. the size of each newly created pool is doubled (starting with the size specified in the constructor) This behavior can be configured using the optional flag in the constructor.

This allocator uses the "Simple Segregated Storage" pattern, i.e. it manages a list of deallocated objects (free list) within the allocated memory blocks, i.e. no extra memory is reserved for handling the free list.

Note
Note that this allocator is able to allocate only one object with each call to its allocate method, which however is no real restriction in practice.
This allocator is 4-5 times faster than the boost pool allocators and fast pool allocator, which should not be used.

Member Typedef Documentation

◆ value_type

typedef T value_type

Element type.

◆ size_type

typedef std::size_t size_type

Quantities of elements.

◆ difference_type

typedef std::ptrdiff_t difference_type

Difference between two pointers.

◆ pointer

typedef T* pointer

Pointer to element.

◆ reference

typedef T& reference

Reference to element.

◆ const_pointer

typedef const T* const_pointer

Reference to element.

◆ const_reference

typedef const T& const_reference

Constant reference to element.

Constructor & Destructor Documentation

◆ PoolAllocator() [1/2]

PoolAllocator ( size_type  poolCapacity = 32,
bool  growPools = true,
size_type  itemSize = sizeof(T) 
)
inline

Creates a new PoolAllocator.

The (initial) size of the pools (in number of objects) is specified as first parameter. The second parameter specifies the mode for the allocation of new pools:

  • If it is set to false, each memory pool has the same constant size that is specified by the first parameter.
  • If it is set to true, the size of each newly created pool is doubled, starting with the size specified by the first parameter The itemSize parameter allows to specify the size in bytes of each item manually, by default sizeof(T) is used.

◆ ~PoolAllocator()

~PoolAllocator ( )
inline

Destructor.

◆ PoolAllocator() [2/2]

PoolAllocator ( PoolAllocator< T > &&  other)
inlinenoexcept

Member Function Documentation

◆ operator=()

PoolAllocator& operator= ( PoolAllocator< T > &&  other)
inlinenoexcept

◆ swap()

void swap ( PoolAllocator< T > &  other)
inline

◆ allocatedMemory()

size_type allocatedMemory ( ) const
inline

◆ usedMemory()

size_type usedMemory ( ) const
inline

◆ unusedMemory()

size_type unusedMemory ( ) const
inline

◆ address() [1/2]

pointer address ( reference  x) const
inline

Returns the address of x.

◆ address() [2/2]

const_pointer address ( const_reference  x) const
inline

Returns the address of x.

◆ max_size()

size_type max_size ( ) const
inline

Returns the maximum number of elements of type T (the template parameter) that could be allocated by a call to member allocate.

Note
Note that this allocator can allocate one object only with each call to allocate (max_size() returns 1)!

◆ itemSize()

size_type itemSize ( ) const
inline

Returns the size of each item in bytes.

◆ allocate()

pointer allocate ( size_type  n,
const void *  hint = 0 
)
inline

Allocate a block of storage and returns the pointer, note that the object is not constructed.

Note
Note that this allocator can allocate one object only with each call to allocate - trying to allocate more than one object will fail (return a NULL pointer)!

◆ construct() [1/3]

void construct ( pointer  p,
const_reference  val 
)
inline

Constructs an object of type T on the location p using its copy constructor to initialize its value to val.

◆ destroy()

void destroy ( T *  p)
inline

Destroy an object. Notice that this does not deallocate space for the element.

◆ deallocate()

void deallocate ( pointer  p,
size_type  n 
)
inline

Releases a block of storage previously allocated with member allocate and not yet released.

◆ construct() [2/3]

T* construct ( )
inline

Allocates and constructs a new object and returns its pointer.

This method is provided for convenience but may be up to 10% faster than the STL conform equivalent:

T* obj = a.allocate(1);
a.construct(obj, T());

◆ construct() [3/3]

T* construct ( const_reference  val)
inline

Allocates a new feature and returns its pointer This method is provided for convenience.

It is equivalent to:

T* obj = a.allocate(1);
a.construct(obj, val);

◆ destruct()

void destruct ( pointer  p)
inline

Destroys the object and deallocates the memory.

Provided for convenience and as counterpart to the two above construct methods.


The documentation for this class was generated from the following file: