Files

395 lines
12 KiB
C++

/**
* @file include/ActivMiddleware/ActivLiteFramework/SmartPtrObjectPool.h
*
* @brief Header file for smart pointer managed object pool.
*/
#if (!defined ACTIV_MISC_SMART_PTR_OBJECT_POOL_H)
#define ACTIV_MISC_SMART_PTR_OBJECT_POOL_H
#include "ActivMiddleware/CxxYZ/functional.h"
#include "ActivMiddleware/CxxYZ/memory.h"
#include "ActivMiddleware/CxxYZ/mutex.h"
//! Activ namespace.
namespace Activ
{
//! SmartPtrObjectPool namespace.
namespace SmartPtrObjectPool
{
// Forward reference of pool type.
template<typename T, typename THREAD_POLICY, typename POOL_REF_POLICY>
class Pool;
// ---------------------------------------------------------------------------------------------------------------------------------
//! Pointers to objects keep a simple reference to the pool.
class SimplePoolRefPolicy
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Pointers to objects keep a weak_ptr to the pool. As such, this is safer than SimplePoolRefPolicy but
* at the cost of a weak_ptr -> shared_ptr promotion every time an object is returned to the pool.
*/
class SafePoolRefPolicy
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
namespace Detail
{
template<typename T, typename THREAD_POLICY, typename POOL_REF_POLICY>
class Deleter;
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T, typename THREAD_POLICY>
class Deleter<T, THREAD_POLICY, SimplePoolRefPolicy>
{
private:
typedef Pool<T, THREAD_POLICY, SimplePoolRefPolicy> ThisPool;
public:
// No default constructor; we have to have a reference to the pool.
explicit Deleter(ThisPool &pool) :
m_pool(pool)
{
}
void operator()(T * const pObject) const
{
m_pool.Return(pObject);
}
private:
ThisPool &m_pool;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T, typename THREAD_POLICY>
class Deleter<T, THREAD_POLICY, SafePoolRefPolicy>
{
private:
typedef Pool<T, THREAD_POLICY, SafePoolRefPolicy> ThisPool;
public:
Deleter()
{
}
explicit Deleter(ThisPool &pool) :
m_pPool(pool.shared_from_this())
{
// weak_from_this() only in C++17.
}
void operator()(T * const pObject) const
{
const std::shared_ptr<ThisPool> pPool(m_pPool.lock());
// If the pool itself is still alive, return the object to the pool. Otherwise delete it.
if (pPool)
pPool->Return(pObject);
else
delete pObject;
}
private:
CxxYZ::weak_ptr<ThisPool> m_pPool;
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T>
class PointerHelper;
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T, typename D>
class PointerHelper<CxxYZ::unique_ptr<T, D> >
{
public:
template<typename OBJECT_POOL>
static CxxYZ::unique_ptr<T, D> New(OBJECT_POOL &pool)
{
return Transfer(new T, pool);
}
template<typename OBJECT_POOL>
static CxxYZ::unique_ptr<T, D> Transfer(T * const pObject, OBJECT_POOL &pool)
{
return CxxYZ::unique_ptr<T, D>(pObject, typename OBJECT_POOL::Deleter(pool));
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T>
class PointerHelper<CxxYZ::shared_ptr<T> >
{
public:
template<typename OBJECT_POOL>
static CxxYZ::shared_ptr<T> New(OBJECT_POOL &pool)
{
return Transfer(new T, pool);
}
template<typename OBJECT_POOL>
static CxxYZ::shared_ptr<T> Transfer(T * const pObject, OBJECT_POOL &pool)
{
return CxxYZ::shared_ptr<T>(pObject, typename OBJECT_POOL::Deleter(pool));
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T>
class PointerHelper<Boost::intrusive_ptr<T> >
{
public:
template<typename OBJECT_POOL>
static Boost::intrusive_ptr<T> New(OBJECT_POOL &pool)
{
return Transfer(new T(pool), pool);
}
template<typename OBJECT_POOL>
static Boost::intrusive_ptr<T> Transfer(T * const pObject, OBJECT_POOL &)
{
return Boost::intrusive_ptr<T>(pObject);
}
};
} // namespace Detail
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Policy for single-threaded use of pool (user must guarantee the same thread calls Get() and Return(),
* or are otherwise synchronized).
*/
class SingleThreadPolicy
{
public:
class Scoped : private Boost::noncopyable
{
public:
Scoped(const SingleThreadPolicy &)
{
}
};
};
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Policy for multi-threaded use of pool (implicit mutex acquisition per Get() and Return()).
*/
class MultiThreadPolicy
{
public:
class Scoped : private Boost::noncopyable
{
public:
Scoped(const MultiThreadPolicy &multiThreadPolicy) :
m_lockGuard(multiThreadPolicy.m_mutex)
{
}
private:
const CxxYZ::lock_guard<CxxYZ::mutex> m_lockGuard;
};
private:
mutable CxxYZ::mutex m_mutex;
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Object pool.
*
* @tparam T type of the object to pool; must be default constructible for UniquePtr or SharedPtr usage. Must have a constructor
* taking a "Pool &" for IntrusivePtr usage.
* @tparam THREAD_POLICY whether to make the pool access thread safe.
* @tparam POOL_REF_POLICY whether to make the pointers returned by Get() aware of the pool's lifetime.
*/
template<typename T, typename THREAD_POLICY, typename POOL_REF_POLICY>
class Pool : public CxxYZ::enable_shared_from_this<Pool<T, THREAD_POLICY, POOL_REF_POLICY> >, private Boost::noncopyable
{
// Note: have to publically derive from enable_shared_from_this for it to work, even if it looks like the only
// place we use it is in Deleter which is a friend.
public:
//! Pooled type.
typedef T Type;
//! Object "deleter" (probably returns to pool).
typedef Detail::Deleter<T, THREAD_POLICY, POOL_REF_POLICY> Deleter;
//! unique_ptr to managed object.
typedef CxxYZ::unique_ptr<T, Deleter> UniquePtr;
//! shared_ptr to managed object.
typedef CxxYZ::shared_ptr<T> SharedPtr;
//! instrusive_ptr to managed object.
typedef Boost::intrusive_ptr<T> IntrusivePtr;
/**
* @brief Create a pool.
*
* @param maxFreeListSize the maximum number of objects to store in the internal free list.
*/
static CxxYZ::shared_ptr<Pool> Create(const size_t maxFreeListSize = Boost::integer_traits<size_t>::const_max);
/**
* @brief Get an object from the pool.
*
* @tparam P the pointer type of the object to return.
* UniquePtr: return a unique_ptr with a custom deleter that returns the object to the pool.
* SharedPtr: return a shared_ptr with a custom deleter that returns the object to the pool.
* IntrusivePtr: return an intrusive_ptr. The user will need to ensure type T implements the reference counting (and
* make intrusive_ptr_add_ref() and intrusive_ptr_release() available). T should store an instance of
* Pool::Deleter which it will need to invoke when the reference count reaches 0. T's constructor will need to
* take a reference to this Pool (to pass to the Deleter constructor).
*
* @return A pointer of type P to an object of type T.
*/
template<typename P = UniquePtr>
P Get();
//! Get the current free-list size.
size_t GetFreeListSize() const;
//! Get the number of object allocations that have been made.
size_t GetNumberOfAllocations() const;
private:
Pool(const size_t maxFreeListSize);
void Return(T * const pObject);
#if (!defined ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES)
Pool(Pool &&) = delete;
Pool &operator=(Pool &&) = delete;
#endif
const size_t m_maxFreeListSize;
THREAD_POLICY m_threadPolicy;
CxxYZ::vector<CxxYZ::unique_ptr<T> > m_freeList;
size_t m_numberOfAllocations;
friend Deleter;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T, typename THREAD_POLICY, typename POOL_REF_POLICY>
CxxYZ::shared_ptr<Pool<T, THREAD_POLICY, POOL_REF_POLICY> > Pool<T, THREAD_POLICY, POOL_REF_POLICY>::Create(
const size_t maxFreeListSize)
{
// make_shared() will have no access to our private constructor, so help it out.
class MakeHelper : public Pool
{
public:
MakeHelper(const size_t maxFreeListSize) :
Pool(maxFreeListSize)
{
}
};
return CxxYZ::make_shared<MakeHelper>(maxFreeListSize);
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T, typename THREAD_POLICY, typename POOL_REF_POLICY>
template<typename P>
inline P Pool<T, THREAD_POLICY, POOL_REF_POLICY>::Get()
{
typedef typename Detail::PointerHelper<P> PointerHelper;
{
typename THREAD_POLICY::Scoped scopedThreadPolicy(m_threadPolicy);
if (!m_freeList.empty())
{
CxxYZ::unique_ptr<T> pObject(CxxYZ::move(m_freeList.back()));
m_freeList.pop_back();
return PointerHelper::Transfer(pObject.release(), *this);
}
++m_numberOfAllocations;
}
return PointerHelper::New(*this);
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T, typename THREAD_POLICY, typename POOL_REF_POLICY>
inline size_t Pool<T, THREAD_POLICY, POOL_REF_POLICY>::GetFreeListSize() const
{
typename THREAD_POLICY::Scoped scopedThreadPolicy(m_threadPolicy);
return m_freeList.size();
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T, typename THREAD_POLICY, typename POOL_REF_POLICY>
inline size_t Pool<T, THREAD_POLICY, POOL_REF_POLICY>::GetNumberOfAllocations() const
{
typename THREAD_POLICY::Scoped scopedThreadPolicy(m_threadPolicy);
return m_numberOfAllocations;
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T, typename THREAD_POLICY, typename POOL_REF_POLICY>
inline Pool<T, THREAD_POLICY, POOL_REF_POLICY>::Pool(const size_t maxFreeListSize) :
m_maxFreeListSize(maxFreeListSize),
m_numberOfAllocations(0)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T, typename THREAD_POLICY, typename POOL_REF_POLICY>
inline void Pool<T, THREAD_POLICY, POOL_REF_POLICY>::Return(T * const pObject)
{
{
typename THREAD_POLICY::Scoped scopedThreadPolicy(m_threadPolicy);
// Note m_freeList stores regular unique_ptrs.
if (m_freeList.size() < m_maxFreeListSize)
{
m_freeList.emplace_back(pObject);
return;
}
}
delete pObject;
}
} // namespace SmartPtrObjectPool
} // namespace Activ
#endif // (!defined ACTIV_MISC_SMART_PTR_OBJECT_POOL_H)