static_mem_pool< _Sz, _Gid > Class Template Reference

#include <static_mem_pool.h>

Inheritance diagram for static_mem_pool< _Sz, _Gid >:

mem_pool_base

Detailed Description

template<size_t _Sz, int _Gid = -1>
class static_mem_pool< _Sz, _Gid >

Singleton class template to manage the allocation/deallocation of memory blocks of one specific size.

Parameters:
_Sz size of elements in the static_mem_pool
_Gid group id of a static_mem_pool: if it is negative, simultaneous accesses to this static_mem_pool will be protected from each other; otherwise no protection is given

Definition at line 112 of file static_mem_pool.h.


Static Public Member Functions

static void * alloc_sys (size_t __size)
static void dealloc_sys (void *__ptr)

Private Types

typedef class_level_lock
< static_mem_pool< _Sz, _Gid >
,(_Gid< 0)>::lock lock;public:static
static_mem_pool &instance(){lock
__guard;if(!_S_instance_p){_S_instance_p=_S_create_instance();}return
*_S_instance_p;}static
static_mem_pool
&instance_known(){assert(_S_instance_p!=NULL);return
*_S_instance_p;}void *allocate(){{lock
__guard;if(_S_memory_block_p){void
*__result=_S_memory_block_p;_S_memory_block_p=_S_memory_block_p-> 
_M_next

Private Member Functions

return _S_alloc_sys (_S_align(_Sz))
void deallocate (void *__ptr)
const static_mem_pooloperator= (const static_mem_pool &)
virtual void recycle ()
 static_mem_pool (const static_mem_pool &)
 static_mem_pool ()
 ~static_mem_pool ()

Static Private Member Functions

static size_t _S_align (size_t __size)
static void * _S_alloc_sys (size_t __size)
static static_mem_pool_S_create_instance ()

Private Attributes

return __result

Static Private Attributes

static bool _S_destroyed = false
static static_mem_pool_S_instance_p = _S_create_instance()
static mem_pool_base::_Block_list_S_memory_block_p = NULL

Member Typedef Documentation

template<size_t _Sz, int _Gid = -1>
typedef class_level_lock<static_mem_pool<_Sz, _Gid>, (_Gid < 0)>::lock lock;public: static static_mem_pool& instance() { lock __guard; if (!_S_instance_p) { _S_instance_p = _S_create_instance(); } return *_S_instance_p; } static static_mem_pool& instance_known() { assert(_S_instance_p != NULL); return *_S_instance_p; } void* allocate() { { lock __guard; if (_S_memory_block_p) { void* __result = _S_memory_block_p; _S_memory_block_p = _S_memory_block_p-> static_mem_pool< _Sz, _Gid >::_M_next [private]

Definition at line 161 of file static_mem_pool.h.


Constructor & Destructor Documentation

template<size_t _Sz, int _Gid = -1>
static_mem_pool< _Sz, _Gid >::static_mem_pool (  )  [inline, private]

Definition at line 183 of file static_mem_pool.h.

References _STATIC_MEM_POOL_TRACE.

Referenced by static_mem_pool< _Sz, _Gid >::_S_create_instance().

00184     {
00185         _STATIC_MEM_POOL_TRACE(true, "static_mem_pool<" << _Sz << ','
00186                                      << _Gid << "> is created");
00187     }

template<size_t _Sz, int _Gid = -1>
static_mem_pool< _Sz, _Gid >::~static_mem_pool (  )  [inline, private]

Definition at line 188 of file static_mem_pool.h.

References mem_pool_base::_Block_list::_M_next, static_mem_pool< _Sz, _Gid >::_S_destroyed, static_mem_pool< _Sz, _Gid >::_S_instance_p, static_mem_pool< _Sz, _Gid >::_S_memory_block_p, _STATIC_MEM_POOL_TRACE, and mem_pool_base::dealloc_sys().

00189     {
00190 #   ifdef _DEBUG
00191         // Empty the pool to avoid false memory leakage alarms.  This is
00192         // generally not necessary for release binaries.
00193         _Block_list* __block = _S_memory_block_p;
00194         while (__block)
00195         {
00196             _Block_list* __next = __block->_M_next;
00197             dealloc_sys(__block);
00198             __block = __next;
00199         }
00200         _S_memory_block_p = NULL;
00201 #   endif
00202         _S_instance_p = NULL;
00203         _S_destroyed = true;
00204         _STATIC_MEM_POOL_TRACE(false, "static_mem_pool<" << _Sz << ','
00205                                       << _Gid << "> is destroyed");
00206     }

template<size_t _Sz, int _Gid = -1>
static_mem_pool< _Sz, _Gid >::static_mem_pool ( const static_mem_pool< _Sz, _Gid > &   )  [private]


Member Function Documentation

template<size_t _Sz, int _Gid = -1>
static size_t static_mem_pool< _Sz, _Gid >::_S_align ( size_t  __size  )  [inline, static, private]

Definition at line 207 of file static_mem_pool.h.

00208     {
00209         return __size >= sizeof(_Block_list) ? __size : sizeof(_Block_list);
00210     }

template<size_t _Sz, int _Gid>
void * static_mem_pool< _Sz, _Gid >::_S_alloc_sys ( size_t  __size  )  [inline, static, private]

Definition at line 262 of file static_mem_pool.h.

References static_mem_pool< _Sz, _Gid >::__result, mem_pool_base::alloc_sys(), static_mem_pool_set::instance(), and static_mem_pool_set::recycle().

00263 {
00264     static_mem_pool_set::lock __guard;
00265     void* __result = mem_pool_base::alloc_sys(__size);
00266     if (!__result)
00267     {
00268         static_mem_pool_set::instance().recycle();
00269         __result = mem_pool_base::alloc_sys(__size);
00270     }
00271     return __result;
00272 }

template<size_t _Sz, int _Gid = -1>
return static_mem_pool< _Sz, _Gid >::_S_alloc_sys ( _S_align(_Sz)   )  [private]

template<size_t _Sz, int _Gid>
static_mem_pool< _Sz, _Gid > * static_mem_pool< _Sz, _Gid >::_S_create_instance (  )  [inline, static, private]

Definition at line 275 of file static_mem_pool.h.

References static_mem_pool< _Sz, _Gid >::_S_destroyed, _STATIC_MEM_POOL_TRACE, static_mem_pool_set::add(), static_mem_pool_set::instance(), and static_mem_pool< _Sz, _Gid >::static_mem_pool().

00276 {
00277     if (_S_destroyed)
00278         throw std::runtime_error("dead reference detected");
00279 
00280     static_mem_pool_set::instance();  // Force its creation
00281     static_mem_pool* __inst_p = new static_mem_pool();
00282     try
00283     {
00284         static_mem_pool_set::instance().add(__inst_p);
00285     }
00286     catch (...)
00287     {
00288         _STATIC_MEM_POOL_TRACE(true,
00289                 "Exception occurs in static_mem_pool_set::add");
00290         // The strange cast below is to work around a bug in GCC 2.95.3
00291         delete static_cast<mem_pool_base*>(__inst_p);
00292         throw;
00293     }
00294     return __inst_p;
00295 }

void * mem_pool_base::alloc_sys ( size_t  __size  )  [static, inherited]

Definition at line 60 of file mem_pool_base.cpp.

References _MEM_POOL_ALLOCATE.

Referenced by static_mem_pool< _Sz, _Gid >::_S_alloc_sys(), and fixed_mem_pool< _Tp >::initialize().

00061 {
00062     return _MEM_POOL_ALLOCATE(__size);
00063 }

void mem_pool_base::dealloc_sys ( void *  __ptr  )  [static, inherited]

template<size_t _Sz, int _Gid = -1>
void static_mem_pool< _Sz, _Gid >::deallocate ( void *  __ptr  )  [inline, private]

Deallocates memory by putting the memory block into the pool.

Parameters:
__ptr pointer to memory to be deallocated

Definition at line 172 of file static_mem_pool.h.

References mem_pool_base::_Block_list::_M_next, and static_mem_pool< _Sz, _Gid >::_S_memory_block_p.

00173     {
00174         assert(__ptr != NULL);
00175         lock __guard;
00176         _Block_list* __block = reinterpret_cast<_Block_list*>(__ptr);
00177         __block->_M_next = _S_memory_block_p;
00178         _S_memory_block_p = __block;
00179     }

template<size_t _Sz, int _Gid = -1>
const static_mem_pool& static_mem_pool< _Sz, _Gid >::operator= ( const static_mem_pool< _Sz, _Gid > &   )  [private]

template<size_t _Sz, int _Gid>
void static_mem_pool< _Sz, _Gid >::recycle (  )  [inline, private, virtual]

Recycles half of the free memory blocks in the memory pool to the system. It is called when a memory request to the system (in other instances of the static memory pool) fails.

Implements mem_pool_base.

Definition at line 236 of file static_mem_pool.h.

References mem_pool_base::_Block_list::_M_next, static_mem_pool< _Sz, _Gid >::_S_memory_block_p, _STATIC_MEM_POOL_TRACE, and mem_pool_base::dealloc_sys().

00237 {
00238     // Only here the global lock in static_mem_pool_set is obtained
00239     // before the pool-specific lock.  However, no race conditions are
00240     // found so far.
00241     lock __guard;
00242     _Block_list* __block = _S_memory_block_p;
00243     while (__block)
00244     {
00245         if (_Block_list* __temp = __block->_M_next)
00246         {
00247             _Block_list* __next = __temp->_M_next;
00248             __block->_M_next = __next;
00249             dealloc_sys(__temp);
00250             __block = __next;
00251         }
00252         else
00253         {
00254             break;
00255         }
00256     }
00257     _STATIC_MEM_POOL_TRACE(false, "static_mem_pool<" << _Sz << ','
00258                                   << _Gid << "> is recycled");
00259 }


Field Documentation

template<size_t _Sz, int _Gid = -1>
return static_mem_pool< _Sz, _Gid >::__result [private]

Definition at line 162 of file static_mem_pool.h.

Referenced by static_mem_pool< _Sz, _Gid >::_S_alloc_sys().

template<size_t _Sz, int _Gid = -1>
bool static_mem_pool< _Sz, _Gid >::_S_destroyed = false [inline, static, private]

template<size_t _Sz, int _Gid = -1>
static_mem_pool< _Sz, _Gid > * static_mem_pool< _Sz, _Gid >::_S_instance_p = _S_create_instance() [inline, static, private]

Definition at line 215 of file static_mem_pool.h.

Referenced by static_mem_pool< _Sz, _Gid >::~static_mem_pool().

template<size_t _Sz, int _Gid = -1>
mem_pool_base::_Block_list * static_mem_pool< _Sz, _Gid >::_S_memory_block_p = NULL [inline, static, private]


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

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