fixed_mem_pool< _Tp > Class Template Reference

#include <fixed_mem_pool.h>


Detailed Description

template<class _Tp>
class fixed_mem_pool< _Tp >

Class template to manipulate a fixed-size memory pool. Please notice that only allocate and deallocate are protected by a lock.

Parameters:
_Tp class to use the fixed_mem_pool

Definition at line 76 of file fixed_mem_pool.h.


Public Types

typedef class_level_lock
< fixed_mem_pool< _Tp >
>::lock 
lock

Static Public Member Functions

static void * allocate ()
static void deallocate (void *)
static int deinitialize ()
static int get_alloc_count ()
static bool initialize (size_t __size)
static bool is_initialized ()

Static Protected Member Functions

static bool bad_alloc_handler ()

Static Private Member Functions

static size_t _S_align (size_t __size)

Static Private Attributes

static int _S_alloc_cnt = 0
static void * _S_first_avail_ptr = NULL
static void * _S_mem_pool_ptr = NULL

Member Typedef Documentation

template<class _Tp>
typedef class_level_lock<fixed_mem_pool<_Tp> >::lock fixed_mem_pool< _Tp >::lock

Definition at line 79 of file fixed_mem_pool.h.


Member Function Documentation

template<class _Tp>
size_t fixed_mem_pool< _Tp >::_S_align ( size_t  __size  )  [inline, static, private]

Aligns the memory block size.

Parameters:
__size size to be aligned
Returns:
aligned value of __size

Definition at line 236 of file fixed_mem_pool.h.

References MEM_POOL_ALIGNMENT.

Referenced by fixed_mem_pool< _Tp >::initialize().

00237 {
00238     return (__size + MEM_POOL_ALIGNMENT - 1)
00239            / MEM_POOL_ALIGNMENT * MEM_POOL_ALIGNMENT;
00240 }

template<class _Tp>
void * fixed_mem_pool< _Tp >::allocate (  )  [inline, static]

Allocates a memory block from the memory pool.

Returns:
pointer to the allocated memory block

Definition at line 113 of file fixed_mem_pool.h.

References fixed_mem_pool< _Tp >::_S_alloc_cnt, fixed_mem_pool< _Tp >::_S_first_avail_ptr, and fixed_mem_pool< _Tp >::bad_alloc_handler().

00114 {
00115     lock __guard;
00116     for (;;)
00117     {
00118         if (void* __result = _S_first_avail_ptr)
00119         {
00120             _S_first_avail_ptr = *(void**)_S_first_avail_ptr;
00121             ++_S_alloc_cnt;
00122             return __result;
00123         }
00124         else
00125             if (!bad_alloc_handler())
00126                 return NULL;
00127     }
00128 }

template<class _Tp>
bool fixed_mem_pool< _Tp >::bad_alloc_handler (  )  [inline, static, protected]

Bad allocation handler. Called when there are no memory blocks available in the memory pool. If this function returns false (default behaviour if not explicitly specialized), it indicates that it can do nothing and allocate() should return NULL; if this function returns true, it indicates that it has freed some memory blocks and allocate() should try allocating again.

Definition at line 224 of file fixed_mem_pool.h.

Referenced by fixed_mem_pool< _Tp >::allocate().

00225 {
00226     return false;
00227 }

template<class _Tp>
void fixed_mem_pool< _Tp >::deallocate ( void *  __block_ptr  )  [inline, static]

Deallocates a memory block and returns it to the memory pool.

Parameters:
__block_ptr pointer to the memory block to return

Definition at line 136 of file fixed_mem_pool.h.

References fixed_mem_pool< _Tp >::_S_alloc_cnt, and fixed_mem_pool< _Tp >::_S_first_avail_ptr.

00137 {
00138     if (__block_ptr == NULL)
00139         return;
00140     lock __guard;
00141     assert(_S_alloc_cnt != 0);
00142     --_S_alloc_cnt;
00143     *(void**)__block_ptr = _S_first_avail_ptr;
00144     _S_first_avail_ptr = __block_ptr;
00145 }

template<class _Tp>
int fixed_mem_pool< _Tp >::deinitialize (  )  [inline, static]

Deinitializes the memory pool.

Returns:
0 if all memory blocks are returned and the memory pool successfully freed; or a non-zero value indicating number of memory blocks still in allocation

Definition at line 182 of file fixed_mem_pool.h.

References fixed_mem_pool< _Tp >::_S_alloc_cnt, fixed_mem_pool< _Tp >::_S_first_avail_ptr, fixed_mem_pool< _Tp >::_S_mem_pool_ptr, mem_pool_base::dealloc_sys(), and fixed_mem_pool< _Tp >::is_initialized().

00183 {
00184     if (_S_alloc_cnt != 0)
00185         return _S_alloc_cnt;
00186     assert(is_initialized());
00187     mem_pool_base::dealloc_sys(_S_mem_pool_ptr);
00188     _S_mem_pool_ptr = NULL;
00189     _S_first_avail_ptr = NULL;
00190     return 0;
00191 }

template<class _Tp>
int fixed_mem_pool< _Tp >::get_alloc_count (  )  [inline, static]

Gets the allocation count.

Returns:
the number of memory blocks still in allocation

Definition at line 199 of file fixed_mem_pool.h.

References fixed_mem_pool< _Tp >::_S_alloc_cnt.

00200 {
00201     return _S_alloc_cnt;
00202 }

template<class _Tp>
bool fixed_mem_pool< _Tp >::initialize ( size_t  __size  )  [inline, static]

Initializes the memory pool.

Parameters:
__size number of memory blocks to put in the memory pool
Returns:
true if successful; false if memory insufficient

Definition at line 154 of file fixed_mem_pool.h.

References fixed_mem_pool< _Tp >::_S_align(), fixed_mem_pool< _Tp >::_S_first_avail_ptr, fixed_mem_pool< _Tp >::_S_mem_pool_ptr, mem_pool_base::alloc_sys(), and fixed_mem_pool< _Tp >::is_initialized().

00155 {
00156     size_t __block_size = _S_align(sizeof(_Tp));
00157     assert(!is_initialized());
00158     assert(__size > 0 && __block_size >= sizeof(void*));
00159     _S_mem_pool_ptr = mem_pool_base::alloc_sys(__size * __block_size);
00160     _S_first_avail_ptr = _S_mem_pool_ptr;
00161     if (_S_mem_pool_ptr == NULL)
00162         return false;
00163     char* __block = (char*)_S_mem_pool_ptr;
00164     while (--__size != 0)
00165     {
00166         char* __next = __block + __block_size;
00167         *(void**)__block = __next;
00168         __block = __next;
00169     }
00170     *(void**)__block = NULL;
00171     return true;
00172 }

template<class _Tp>
bool fixed_mem_pool< _Tp >::is_initialized (  )  [inline, static]

Is the memory pool initialized?

Returns:
true if it is successfully initialized; false otherwise

Definition at line 210 of file fixed_mem_pool.h.

References fixed_mem_pool< _Tp >::_S_mem_pool_ptr.

Referenced by fixed_mem_pool< _Tp >::deinitialize(), and fixed_mem_pool< _Tp >::initialize().

00211 {
00212     return _S_mem_pool_ptr != NULL;;
00213 }


Field Documentation

template<class _Tp>
int fixed_mem_pool< _Tp >::_S_alloc_cnt = 0 [inline, static, private]

template<class _Tp>
void * fixed_mem_pool< _Tp >::_S_first_avail_ptr = NULL [inline, static, private]

template<class _Tp>
void * fixed_mem_pool< _Tp >::_S_mem_pool_ptr = NULL [inline, static, private]

Pointer to the allocated chunk of memory.

Definition at line 90 of file fixed_mem_pool.h.

Referenced by fixed_mem_pool< _Tp >::deinitialize(), fixed_mem_pool< _Tp >::initialize(), and fixed_mem_pool< _Tp >::is_initialized().


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

Generated on Wed May 5 00:06:41 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6