9 #ifndef MRPT_MEMORY_POOL_H 10 #define MRPT_MEMORY_POOL_H 37 template <
class DATA_PARAMS,
class POOLABLE_DATA>
41 typedef std::list<std::pair<DATA_PARAMS,POOLABLE_DATA*> >
TList;
47 CGenericMemoryPool(
const size_t max_pool_entries,
bool &was_destroyed ) : m_maxPoolEntries(max_pool_entries), m_was_destroyed(was_destroyed)
49 m_was_destroyed =
false;
61 static bool was_destroyed =
false;
63 return was_destroyed ? NULL : &inst;
74 if (m_pool.empty())
return NULL;
77 for (
typename TList::iterator it=m_pool.begin();it!=m_pool.end();++it) {
78 if (it->first.isSuitable(params))
80 POOLABLE_DATA * ret = it->second;
98 if (m_pool.begin()->second)
delete m_pool.begin()->second;
99 m_pool.erase(m_pool.begin());
102 m_pool.push_back(
typename TList::value_type(params,block) );
107 m_was_destroyed =
true;
This class provides simple critical sections functionality.
A class acquiring a CCriticalSection at its constructor, and releasing it at destructor.
mrpt::synch::CCriticalSection m_pool_cs
void dump_to_pool(const DATA_PARAMS ¶ms, POOLABLE_DATA *block)
Saves the passed data block (characterized by params) to the pool.
std::list< std::pair< DATA_PARAMS, POOLABLE_DATA * > > TList
POOLABLE_DATA * request_memory(const DATA_PARAMS ¶ms)
Request a block of data which fulfils the size requirements stated in params.
A generic system for versatile memory pooling.
void setMemoryPoolMaxSize(const size_t maxNumEntries)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
bool & m_was_destroyed
With this trick we get rid of the "global destruction order fiasco" ;-)
static CGenericMemoryPool< DATA_PARAMS, POOLABLE_DATA > * getInstance(const size_t max_pool_entries=5)
Construct-on-first-use (~singleton) pattern: Return the unique instance of this class for a given tem...
size_t getMemoryPoolMaxSize() const
CGenericMemoryPool(const size_t max_pool_entries, bool &was_destroyed)