MRPT  1.9.9
mrpt::system::CGenericMemoryPool< DATA_PARAMS, POOLABLE_DATA > Class Template Reference

Detailed Description

template<class DATA_PARAMS, class POOLABLE_DATA>
class mrpt::system::CGenericMemoryPool< DATA_PARAMS, POOLABLE_DATA >

A generic system for versatile memory pooling.

This class implements the singleton pattern so a unique instance exists for each combination of template parameters. All methods are thread-safe.

Basic usage:

  • When needed, call request_memory() to check the availability of memory in the pool.
    • At your class destructor, donate the memory to the pool with dump_to_pool().

Notice that memory requests are checked against memory blocks in the pool via a user-defined function:

bool POOLABLE_DATA::isSuitable(const POOLABLE_DATA & req) const { ... }

For an example of how to handle a memory pool, see the class mrpt::obs::CObservation3DRangeScan

Template Parameters
POOLABLE_DATAA struct with user-defined objects which actually contain the memory blocks (e.g. one or more std::vector).
DATA_PARAMSA struct with user information about each memory block (e.g. size of a std::vector)

Definition at line 43 of file CGenericMemoryPool.h.

#include <mrpt/system/CGenericMemoryPool.h>

Public Member Functions

size_t getMemoryPoolMaxSize () const
 
void setMemoryPoolMaxSize (const size_t maxNumEntries)
 
POOLABLE_DATA * request_memory (const DATA_PARAMS &params)
 Request a block of data which fulfils the size requirements stated in params. More...
 
void dump_to_pool (const DATA_PARAMS &params, POOLABLE_DATA *block)
 Saves the passed data block (characterized by params) to the pool. More...
 
 ~CGenericMemoryPool ()
 

Static Public Member Functions

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 template arguments, or nullptr if it was once created but it's been destroyed (which means we're in the program global destruction phase). More...
 

Private Types

using TList = std::list< std::pair< DATA_PARAMS, POOLABLE_DATA * > >
 

Private Member Functions

 CGenericMemoryPool (const size_t max_pool_entries, bool &was_destroyed)
 

Private Attributes

TList m_pool
 
std::mutex m_pool_cs
 
size_t m_maxPoolEntries
 
bool & m_was_destroyed
 With this trick we get rid of the "global destruction order fiasco" ;-) More...
 

Member Typedef Documentation

◆ TList

template<class DATA_PARAMS, class POOLABLE_DATA>
using mrpt::system::CGenericMemoryPool< DATA_PARAMS, POOLABLE_DATA >::TList = std::list<std::pair<DATA_PARAMS, POOLABLE_DATA*> >
private

Definition at line 46 of file CGenericMemoryPool.h.

Constructor & Destructor Documentation

◆ CGenericMemoryPool()

template<class DATA_PARAMS, class POOLABLE_DATA>
mrpt::system::CGenericMemoryPool< DATA_PARAMS, POOLABLE_DATA >::CGenericMemoryPool ( const size_t  max_pool_entries,
bool &  was_destroyed 
)
inlineprivate

◆ ~CGenericMemoryPool()

Member Function Documentation

◆ dump_to_pool()

template<class DATA_PARAMS, class POOLABLE_DATA>
void mrpt::system::CGenericMemoryPool< DATA_PARAMS, POOLABLE_DATA >::dump_to_pool ( const DATA_PARAMS &  params,
POOLABLE_DATA *  block 
)
inline

Saves the passed data block (characterized by params) to the pool.

If the overall size of the pool is above the limit, the oldest entry is removed.

Note
It is a responsibility of the user to allocate in dynamic memory the "POOLABLE_DATA" object with "new".

Definition at line 116 of file CGenericMemoryPool.h.

References mrpt::system::CGenericMemoryPool< DATA_PARAMS, POOLABLE_DATA >::m_maxPoolEntries, mrpt::system::CGenericMemoryPool< DATA_PARAMS, POOLABLE_DATA >::m_pool, and mrpt::system::CGenericMemoryPool< DATA_PARAMS, POOLABLE_DATA >::m_pool_cs.

Referenced by mrpt::opengl::CTexturedObject::loadTextureInOpenGL(), mempool_donate_range_matrix(), and mempool_donate_xyz_buffers().

◆ getInstance()

template<class DATA_PARAMS, class POOLABLE_DATA>
static CGenericMemoryPool<DATA_PARAMS, POOLABLE_DATA>* mrpt::system::CGenericMemoryPool< DATA_PARAMS, POOLABLE_DATA >::getInstance ( const size_t  max_pool_entries = 5)
inlinestatic

Construct-on-first-use (~singleton) pattern: Return the unique instance of this class for a given template arguments, or nullptr if it was once created but it's been destroyed (which means we're in the program global destruction phase).

Definition at line 72 of file CGenericMemoryPool.h.

Referenced by mrpt::opengl::CTexturedObject::loadTextureInOpenGL(), mempool_donate_range_matrix(), mempool_donate_xyz_buffers(), mrpt::obs::CObservationRGBD360::rangeImage_setSize(), mrpt::obs::CObservation3DRangeScan::rangeImage_setSize(), reserveDataBuffer(), and mrpt::obs::CObservation3DRangeScan::resizePoints3DVectors().

◆ getMemoryPoolMaxSize()

template<class DATA_PARAMS, class POOLABLE_DATA>
size_t mrpt::system::CGenericMemoryPool< DATA_PARAMS, POOLABLE_DATA >::getMemoryPoolMaxSize ( ) const
inline

◆ request_memory()

template<class DATA_PARAMS, class POOLABLE_DATA>
POOLABLE_DATA* mrpt::system::CGenericMemoryPool< DATA_PARAMS, POOLABLE_DATA >::request_memory ( const DATA_PARAMS &  params)
inline

Request a block of data which fulfils the size requirements stated in params.

Notice that the decision on the suitability of each pool'ed block is done by DATA_PARAMS::isSuitable().

Returns
The block of data, or nullptr if none suitable was found in the pool.
Note
It is a responsibility of the user to free with "delete" the "POOLABLE_DATA" object itself once the memory has been extracted from its elements.

Definition at line 91 of file CGenericMemoryPool.h.

References mrpt::system::CGenericMemoryPool< DATA_PARAMS, POOLABLE_DATA >::m_pool, and mrpt::system::CGenericMemoryPool< DATA_PARAMS, POOLABLE_DATA >::m_pool_cs.

Referenced by mrpt::obs::CObservationRGBD360::rangeImage_setSize(), mrpt::obs::CObservation3DRangeScan::rangeImage_setSize(), reserveDataBuffer(), and mrpt::obs::CObservation3DRangeScan::resizePoints3DVectors().

◆ setMemoryPoolMaxSize()

template<class DATA_PARAMS, class POOLABLE_DATA>
void mrpt::system::CGenericMemoryPool< DATA_PARAMS, POOLABLE_DATA >::setMemoryPoolMaxSize ( const size_t  maxNumEntries)
inline

Member Data Documentation

◆ m_maxPoolEntries

◆ m_pool

◆ m_pool_cs

◆ m_was_destroyed

template<class DATA_PARAMS, class POOLABLE_DATA>
bool& mrpt::system::CGenericMemoryPool< DATA_PARAMS, POOLABLE_DATA >::m_was_destroyed
private



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020