RTT::MemoryPool< T > Class Template Reference

A memory pool in which allocate() and deallocate() are lock-free. More...

#include <rtt/MemoryPool.hpp>

List of all members.

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
minit

Detailed Description

template<class T>
class RTT::MemoryPool< T >

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.

Parameters:
T A class type for which memory will be allocated.
See also:
FixedSizeMemoryPool for a more memory efficient fixed size memory pool.

Definition at line 63 of file MemoryPool.hpp.


Constructor & Destructor Documentation

template<class T>
RTT::MemoryPool< T >::MemoryPool ( unsigned int  startsize = 4,
const T &  initial_value = T() 
) [inline]

Initialise the memory pool and already allocate some memory.

Parameters:
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.


Member Function Documentation

template<class T>
bool RTT::MemoryPool< T >::deallocate ( pointer  m  )  [inline]
template<class T>
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.

template<class T>
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().

template<class T>
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.


The documentation for this class was generated from the following file:
Generated on Thu Dec 23 13:22:43 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3