A memory pool in which allocate() and deallocate() are lock-free. More...
#include <rtt/MemoryPool.hpp>
Classes | |
struct | Item |
Adds a reference count. More... | |
Public Types | |
typedef T * | pointer |
typedef unsigned int | size_type |
Public Member Functions | |
MemoryPool (unsigned int startsize=4, const T &initial_value=T()) | |
Initialise the memory pool and already allocate some memory. | |
~MemoryPool () | |
Release all memory from the pool. | |
size_type | size () const |
Returns the number of elements currently available. | |
size_type | capacity () const |
Returns the maximum number of elements. | |
void | reserve () |
Reserve one additional element in the pool, allocating extra memory if needed. | |
void | shrink () |
Unreserve one additional element in the pool. | |
pointer | allocate () |
Acquire and lock() a pointer to previously reserve()'d memory of type T. | |
bool | lock (pointer m) |
Increase the reference count of a piece of memory. | |
bool | unlock (pointer m) |
Decrease the reference count of a piece of memory. | |
bool | deallocate (pointer m) |
Deallocate and unlock() a piece of memory. | |
size_type | useCount (pointer m) |
Returns how many times a piece of memory is used. | |
Protected Types | |
typedef AtomicQueue< void * > | QueueType |
The pool stores memory as void pointers. | |
typedef boost::shared_ptr < QueueType > | QueueTypePtr |
typedef std::vector< std::pair < QueueTypePtr, Item * > > | PoolType |
A vector of memory pools. | |
Protected Member Functions | |
void | make_pool (size_type growsize) |
For each additional pool, also store the location of the Item pointer. | |
Protected Attributes | |
unsigned int | used_cap |
unsigned int | alloc_cnt |
PoolType | mpool |
T | minit |
A memory pool in which allocate() and deallocate() are lock-free.
This implementation is for a variable amount of memory for value_types.
Use reserve() or shrink() to reserve or remove the reservation of memory for a type T. It returns in allocate objects of type T*. Any number of concurrent threads may invoke allocate(), deallocate(), lock() and unlock(). However, reserve() and shrink() may not be concurrently invoked.
T | A class type for which memory will be allocated. |
Definition at line 63 of file MemoryPool.hpp.
RTT::MemoryPool< T >::MemoryPool | ( | unsigned int | startsize = 4 , |
|
const T & | initial_value = T() | |||
) | [inline] |
Initialise the memory pool and already allocate some memory.
startsize | The initial size of the pool. The amount of memory allocated will be doubled when growing. For example, if the startsize is 4 and 5 items or more are needed, 8 new additional will be allocated, totalling in 12 available items. If later on 13 items or more are needed, 16 new additional items are allocated, totalling in 12+16=28 items and so on. Must be at least 1. | |
initial_value | The initial value for all the data returned the first time by allocate(). This parameter is usefull if T requires a first time initialisation, for example, when storing an std::vector. The data stored in this pool is only destructed when the MemoryPool is destructed, so no destructor is invoked in deallocate(). When the same data is returned in allocate() it will still contain the last value. |
Definition at line 131 of file MemoryPool.hpp.
bool RTT::MemoryPool< T >::deallocate | ( | pointer | m | ) | [inline] |
Deallocate and unlock() a piece of memory.
Returns false if already released.
Definition at line 234 of file MemoryPool.hpp.
Referenced by RTT::SortedList< DataType_ >::erase(), RTT::SingleList< DataType_ >::erase(), RTT::SortedList< DataType_ >::insert(), RTT::SingleList< DataType_ >::insert(), and RTT::MemoryPool< Node >::unlock().
bool RTT::MemoryPool< T >::lock | ( | pointer | m | ) | [inline] |
Increase the reference count of a piece of memory.
Returns false if already released.
Definition at line 214 of file MemoryPool.hpp.
void RTT::MemoryPool< T >::shrink | ( | ) | [inline] |
Unreserve one additional element in the pool.
The memory is not physically freed, that only happens in the destructor.
Definition at line 189 of file MemoryPool.hpp.
Referenced by RTT::SortedList< DataType_ >::shrink(), and RTT::SingleList< DataType_ >::shrink().
bool RTT::MemoryPool< T >::unlock | ( | pointer | m | ) | [inline] |
Decrease the reference count of a piece of memory.
Returns false if already released.
Definition at line 226 of file MemoryPool.hpp.