522 lines
13 KiB
C++
522 lines
13 KiB
C++
/**
|
|
* @file include/ActivMiddleware/Misc/ObjectPool.h
|
|
*
|
|
* @brief Classes to cache a number of objects to avoid constructing/destructing them continually.
|
|
*/
|
|
|
|
#if (!defined ACTIV_MISC_OBJECT_POOL_H)
|
|
#define ACTIV_MISC_OBJECT_POOL_H
|
|
|
|
#include "ActivMiddleware/Misc/FunctionObjects.h"
|
|
#include "ActivMiddleware/Misc/Singleton.h"
|
|
#include "ActivMiddleware/Misc/StlHelper.h"
|
|
#include "ActivMiddleware/ActivBase/StatusCodeException.h"
|
|
|
|
#include ACTIV_BOOST_INCLUDE(range/algorithm_ext/push_back.hpp)
|
|
|
|
#include <set>
|
|
#include <vector>
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
namespace Detail
|
|
{
|
|
|
|
ACTIV_BOOST_TTI_HAS_MEMBER_FUNCTION(GetMemoryUsage)
|
|
|
|
template<typename T>
|
|
class HasGetMemoryUsage : public Boost::mpl::or_<
|
|
has_member_function_GetMemoryUsage<const T, size_t>,
|
|
has_member_function_GetMemoryUsage<const T, const size_t> >
|
|
{
|
|
};
|
|
|
|
} // namespace Detail
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Object pool.
|
|
*
|
|
* This pool provides no built-in thread safety. It provides no object-tracking; all objects allocated from
|
|
* the pool must be manually returned to the pool to avoid memory leaks.
|
|
*
|
|
* T the type of the object to cache; must be default constructible.
|
|
*/
|
|
template <typename T>
|
|
class ObjectPool
|
|
{
|
|
public:
|
|
typedef T ObjectType;
|
|
|
|
/**
|
|
* @brief Pool statistics.
|
|
*/
|
|
class Stats
|
|
{
|
|
public:
|
|
size_t m_initialFreeListSize;
|
|
size_t m_currentFreeListSize;
|
|
size_t m_maximumFreeListSize;
|
|
size_t m_maximumAllowedFreeListSize;
|
|
};
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param initialFreeListSize how many objects to create initially.
|
|
* @param maximumAllowedFreeListSize maximum allowed free list size, or 0 for no upper bound.
|
|
*
|
|
* @throw Exception invalid parameters.
|
|
*/
|
|
ObjectPool(const size_t initialFreeListSize = 0, const size_t maximumAllowedFreeListSize = 0) :
|
|
m_maximumAllowedFreeListSize(maximumAllowedFreeListSize)
|
|
{
|
|
if ((0 != maximumAllowedFreeListSize) && (initialFreeListSize > maximumAllowedFreeListSize))
|
|
ACTIV_THROW(StatusCodeException, STATUS_CODE_INVALID_PARAMETER);
|
|
|
|
m_freeList.reserve(initialFreeListSize);
|
|
|
|
for (size_t i = 0; i < initialFreeListSize; ++i)
|
|
{
|
|
T *pObject = new (std::nothrow) T;
|
|
|
|
if (0 != pObject)
|
|
m_freeList.push_back(pObject);
|
|
}
|
|
|
|
m_initialFreeListSize = m_freeList.size();
|
|
m_maximumFreeListSize = m_initialFreeListSize;
|
|
}
|
|
|
|
/**
|
|
* @brief Get an object from the pool.
|
|
*
|
|
* If the free list is non-empty, an object from it is returned, *without* invoking the object's constructor. Otherwise,
|
|
* a new object is allocated from the heap (and therefore it's constructor is called).
|
|
*
|
|
* @return Pointer to an object, or 0 if the pool and heap are empty.
|
|
*/
|
|
T *Get()
|
|
{
|
|
T *pObject;
|
|
|
|
if (!m_freeList.empty())
|
|
{
|
|
pObject = m_freeList.back();
|
|
m_freeList.pop_back();
|
|
}
|
|
else
|
|
{
|
|
pObject = new (std::nothrow) T;
|
|
if (0 == pObject)
|
|
return 0;
|
|
}
|
|
|
|
return pObject;
|
|
}
|
|
|
|
/**
|
|
* @brief Return an in use object to the pool.
|
|
*
|
|
* The object will be deleted if the free list is already full. Otherwise, it is added to the free list *without*
|
|
* invoking the object's destructor.
|
|
*
|
|
* @param pObject Pointer to object to return.
|
|
*
|
|
* @return true
|
|
*/
|
|
bool Return(T * const pObject)
|
|
{
|
|
if ((0 == m_maximumAllowedFreeListSize) || (m_freeList.size() < m_maximumAllowedFreeListSize))
|
|
{
|
|
m_freeList.push_back(pObject);
|
|
|
|
if (m_freeList.size() > m_maximumFreeListSize)
|
|
m_maximumFreeListSize = m_freeList.size();
|
|
}
|
|
else
|
|
{
|
|
delete pObject;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
//! Trim the free-list to its initial size.
|
|
void TrimFreeList()
|
|
{
|
|
if (m_freeList.size() > m_initialFreeListSize)
|
|
{
|
|
std::for_each(m_freeList.begin() + m_initialFreeListSize, m_freeList.end(), DeleteObject());
|
|
m_freeList.resize(m_initialFreeListSize);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Apply a function object to all objects in the free-list.
|
|
*
|
|
* @param fn
|
|
*/
|
|
template<typename F>
|
|
void ApplyToFreeList(const F &fn)
|
|
{
|
|
std::for_each(m_freeList.begin(), m_freeList.end(), fn);
|
|
}
|
|
|
|
/**
|
|
* @brief Get statistics.
|
|
*
|
|
* @return Stats.
|
|
*/
|
|
Stats GetStats() const
|
|
{
|
|
Stats stats;
|
|
|
|
stats.m_initialFreeListSize = m_initialFreeListSize;
|
|
stats.m_currentFreeListSize = m_freeList.size();
|
|
stats.m_maximumFreeListSize = m_maximumFreeListSize;
|
|
stats.m_maximumAllowedFreeListSize = m_maximumAllowedFreeListSize;
|
|
|
|
return stats;
|
|
}
|
|
|
|
/**
|
|
* @brief Get the approximate memory usage of this object and objects in the free-list.
|
|
*
|
|
* Note this can be a slow operation.
|
|
*
|
|
* @return Approximate memory usage in bytes.
|
|
*/
|
|
#if (ACTIV_CPLUSPLUS >= ACTIV_CPLUSPLUS_11)
|
|
template<typename U = T>
|
|
typename std::enable_if<Detail::HasGetMemoryUsage<U>::value, size_t>::type GetMemoryUsage() const
|
|
{
|
|
ACTIV_BOOST_STATIC_ASSERT(Detail::HasGetMemoryUsage<T>::value);
|
|
return GetMemoryUsageImpl<T>();
|
|
}
|
|
#else
|
|
size_t GetMemoryUsage() const
|
|
{
|
|
ACTIV_BOOST_STATIC_ASSERT(Detail::HasGetMemoryUsage<T>::value);
|
|
return GetMemoryUsageImpl<T>();
|
|
}
|
|
#endif
|
|
|
|
protected:
|
|
typedef std::vector<T *> ObjectList;
|
|
|
|
const size_t m_maximumAllowedFreeListSize;
|
|
ObjectList m_freeList; ///< List of free objects.
|
|
|
|
private:
|
|
template<typename U>
|
|
typename Boost::enable_if<Detail::HasGetMemoryUsage<U>, size_t>::type GetMemoryUsageImpl() const
|
|
{
|
|
size_t memoryUsage = sizeof(*this);
|
|
|
|
memoryUsage += m_freeList.capacity() * sizeof(typename ObjectList::value_type);
|
|
|
|
for (typename ObjectList::const_iterator iter = m_freeList.begin(), iterEnd = m_freeList.end(); iter != iterEnd; ++iter)
|
|
{
|
|
const T * const pObject = *iter;
|
|
if (0 != pObject)
|
|
memoryUsage += pObject->GetMemoryUsage();
|
|
}
|
|
|
|
return memoryUsage;
|
|
}
|
|
|
|
size_t m_initialFreeListSize;
|
|
size_t m_maximumFreeListSize;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Object pool with object tracking.
|
|
*
|
|
* This pool provides no built-in thread safety.
|
|
*
|
|
* T the type of the object to cache; must be default constructible.
|
|
*/
|
|
template <typename T>
|
|
class TrackedObjectPool : public ObjectPool<T>
|
|
{
|
|
private:
|
|
typedef ObjectPool<T> BasePool;
|
|
|
|
public:
|
|
/**
|
|
* @brief Pool statistics.
|
|
*/
|
|
class Stats : public BasePool::Stats
|
|
{
|
|
public:
|
|
size_t m_currentInUseCount;
|
|
size_t m_maximumInUseCount;
|
|
};
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param initialFreeListSize how many objects to create initially.
|
|
* @param maximumAllowedFreeListSize maximum allowed free list size, or 0 for no upper bound.
|
|
*/
|
|
TrackedObjectPool(const size_t initialFreeListSize = 0, const size_t maximumAllowedFreeListSize = 0) :
|
|
ObjectPool<T>(initialFreeListSize, maximumAllowedFreeListSize),
|
|
m_maximumInUseCount(0)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @brief Destructor.
|
|
*
|
|
* All objects in the pool, or allocated, are destroyed.
|
|
*/
|
|
~TrackedObjectPool()
|
|
{
|
|
DestroyAll();
|
|
}
|
|
|
|
/**
|
|
* @brief Get an object from the pool.
|
|
*
|
|
* If the free list is non-empty, an object from it is returned, *without* invoking the object's constructor. Otherwise,
|
|
* a new object is allocated.
|
|
*
|
|
* @return Pointer to an object, or 0 if the heap is empty.
|
|
*/
|
|
T *Get()
|
|
{
|
|
T * const pObject = BasePool::Get();
|
|
|
|
(void) m_inUseSet.insert(pObject);
|
|
|
|
if (m_inUseSet.size() > m_maximumInUseCount)
|
|
m_maximumInUseCount = m_inUseSet.size();
|
|
|
|
return pObject;
|
|
}
|
|
|
|
/**
|
|
* @brief Return an in use object to the pool.
|
|
*
|
|
* The object will be deleted if the free list is already full. Otherwise, it is added to the free list *without*
|
|
* invoking the object's destructor.
|
|
*
|
|
* @param pObject Pointer to object to return.
|
|
*
|
|
* @return true if object was returned to the pool or deleted, false if object was not allocated by the pool.
|
|
*/
|
|
bool Return(T * const pObject)
|
|
{
|
|
typename ObjectSet::iterator iter = m_inUseSet.find(pObject);
|
|
|
|
if (m_inUseSet.end() == iter)
|
|
return false;
|
|
|
|
m_inUseSet.erase(iter);
|
|
|
|
return BasePool::Return(pObject);
|
|
}
|
|
|
|
/**
|
|
* @brief Return all in use objects to the pool (invalidates all in use pointers).
|
|
*
|
|
* Some objects may be deleted if the free list maximum size isn't sufficient to hold all in use objects.
|
|
*/
|
|
void ReturnAll()
|
|
{
|
|
// Move from the in use set to the free list.
|
|
Boost::range::push_back(this->m_freeList, m_inUseSet);
|
|
m_inUseSet.clear();
|
|
|
|
// And trim if necessary.
|
|
if ((0 != this->m_maximumAllowedFreeListSize) && (this->m_freeList.size() > this->m_maximumAllowedFreeListSize))
|
|
{
|
|
std::for_each(this->m_freeList.begin() + this->m_maximumAllowedFreeListSize, this->m_freeList.end(), DeleteObject());
|
|
this->m_freeList.erase(this->m_freeList.begin() + this->m_maximumAllowedFreeListSize, this->m_freeList.end());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Destroy all objects allocated by the pool (those currently in use and in the free list).
|
|
*/
|
|
void DestroyAll()
|
|
{
|
|
std::for_each(m_inUseSet.begin(), m_inUseSet.end(), DeleteObject());
|
|
m_inUseSet.clear();
|
|
|
|
std::for_each(this->m_freeList.begin(), this->m_freeList.end(), DeleteObject());
|
|
this->m_freeList.clear();
|
|
}
|
|
|
|
/**
|
|
* @brief Get statistics.
|
|
*
|
|
* @return Stats.
|
|
*/
|
|
Stats GetStats() const
|
|
{
|
|
Stats stats;
|
|
|
|
static_cast<typename BasePool::Stats &>(stats) = BasePool::GetStats();
|
|
|
|
stats.m_currentInUseCount = m_inUseSet.size();
|
|
stats.m_maximumInUseCount = m_maximumInUseCount;
|
|
|
|
return stats;
|
|
}
|
|
|
|
/**
|
|
* @brief Get the approximate memory usage of this object, objects in the free-list and objects in use.
|
|
*
|
|
* Note this can be a slow operation.
|
|
*
|
|
* @return Approximate memory usage in bytes.
|
|
*/
|
|
size_t GetMemoryUsage() const
|
|
{
|
|
size_t memoryUsage = ObjectPool<T>::GetMemoryUsage();
|
|
|
|
memoryUsage += sizeof(*this) - sizeof(ObjectPool<T>);
|
|
|
|
// This will be low; we can't easily account for node size.
|
|
memoryUsage += m_inUseSet.size() * sizeof(typename ObjectSet::value_type);
|
|
|
|
for (typename ObjectSet::const_iterator iter = m_inUseSet.begin(), iterEnd = m_inUseSet.end(); iter != iterEnd; ++iter)
|
|
{
|
|
const T * const pObject = *iter;
|
|
if (0 != pObject)
|
|
memoryUsage += pObject->GetMemoryUsage();
|
|
}
|
|
|
|
return memoryUsage;
|
|
}
|
|
|
|
private:
|
|
typedef std::set<T *> ObjectSet;
|
|
|
|
ObjectSet m_inUseSet; ///< Set of objects in currently use.
|
|
size_t m_maximumInUseCount;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Object pool singleton.
|
|
*
|
|
* This pool provides no built-in thread safety.
|
|
*
|
|
* T the type of pool to create (ObjectPool or TrackedObjectPool).
|
|
* INITIAL_FREE_LIST_SIZE the initial size of the free list.
|
|
* MAX_FREE_LIST_SIZE the maximum size of the free list. 0 is no upper bound.
|
|
*/
|
|
template <typename T, size_t INITIAL_FREE_LIST_SIZE = 0, size_t MAX_FREE_LIST_SIZE = 0>
|
|
class ObjectPoolSingleton : public Singleton<ObjectPoolSingleton<T> >, public T
|
|
{
|
|
public:
|
|
ACTIV_BOOST_STATIC_ASSERT((0 == MAX_FREE_LIST_SIZE) || (INITIAL_FREE_LIST_SIZE <= MAX_FREE_LIST_SIZE));
|
|
|
|
/**
|
|
* @brief Get a reference to the singleton instance.
|
|
*
|
|
* @return Reference.
|
|
*/
|
|
static ObjectPoolSingleton &GetInstance();
|
|
|
|
private:
|
|
/**
|
|
* @brief Private constructor (singleton class).
|
|
*/
|
|
ObjectPoolSingleton() :
|
|
T(INITIAL_FREE_LIST_SIZE, MAX_FREE_LIST_SIZE)
|
|
{
|
|
}
|
|
|
|
friend class Singleton<ObjectPoolSingleton<T> >;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <typename T, size_t INITIAL_FREE_LIST_SIZE, size_t MAX_FREE_LIST_SIZE>
|
|
ObjectPoolSingleton<T, INITIAL_FREE_LIST_SIZE, MAX_FREE_LIST_SIZE> &
|
|
ObjectPoolSingleton<T, INITIAL_FREE_LIST_SIZE, MAX_FREE_LIST_SIZE>::GetInstance()
|
|
{
|
|
static ObjectPoolSingleton ObjectPoolSingleton;
|
|
|
|
return ObjectPoolSingleton;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Helper class to get an object from an ObjectPool and return it when the helper goes out of scope.
|
|
*
|
|
* T is the ObjectPool type.
|
|
*/
|
|
template<typename T>
|
|
class ScopedPooledObject
|
|
{
|
|
public:
|
|
typedef typename T::ObjectType ObjectType;
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @throw Exception if can't allocate an object from the pool.
|
|
*/
|
|
ScopedPooledObject(T &objectPool) :
|
|
m_objectPool(objectPool),
|
|
m_p(objectPool.Get())
|
|
{
|
|
if (0 == m_p)
|
|
ACTIV_THROW(StatusCodeException, STATUS_CODE_HEAP_EMPTY);
|
|
}
|
|
|
|
/**
|
|
* @brief Destructor.
|
|
*
|
|
* Returns the object to the pool.
|
|
*/
|
|
~ScopedPooledObject()
|
|
{
|
|
(void) m_objectPool.Return(m_p);
|
|
}
|
|
|
|
/**
|
|
* @brief Object accessor.
|
|
*
|
|
* @return Reference to the object.
|
|
*/
|
|
ObjectType &operator()()
|
|
{
|
|
return *m_p;
|
|
}
|
|
|
|
/**
|
|
* @brief Object accessor.
|
|
*
|
|
* @return Const reference to the object.
|
|
*/
|
|
const ObjectType &operator()() const
|
|
{
|
|
return *m_p;
|
|
}
|
|
|
|
private:
|
|
T & m_objectPool;
|
|
ObjectType * const m_p;
|
|
};
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_MISC_OBJECT_POOL_H)
|