/** * @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 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 class Deleter; // --------------------------------------------------------------------------------------------------------------------------------- template class Deleter { private: typedef Pool 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 class Deleter { private: typedef Pool 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 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 m_pPool; }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- template class PointerHelper; // --------------------------------------------------------------------------------------------------------------------------------- template class PointerHelper > { public: template static CxxYZ::unique_ptr New(OBJECT_POOL &pool) { return Transfer(new T, pool); } template static CxxYZ::unique_ptr Transfer(T * const pObject, OBJECT_POOL &pool) { return CxxYZ::unique_ptr(pObject, typename OBJECT_POOL::Deleter(pool)); } }; // --------------------------------------------------------------------------------------------------------------------------------- template class PointerHelper > { public: template static CxxYZ::shared_ptr New(OBJECT_POOL &pool) { return Transfer(new T, pool); } template static CxxYZ::shared_ptr Transfer(T * const pObject, OBJECT_POOL &pool) { return CxxYZ::shared_ptr(pObject, typename OBJECT_POOL::Deleter(pool)); } }; // --------------------------------------------------------------------------------------------------------------------------------- template class PointerHelper > { public: template static Boost::intrusive_ptr New(OBJECT_POOL &pool) { return Transfer(new T(pool), pool); } template static Boost::intrusive_ptr Transfer(T * const pObject, OBJECT_POOL &) { return Boost::intrusive_ptr(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 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 class Pool : public CxxYZ::enable_shared_from_this >, 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 Deleter; //! unique_ptr to managed object. typedef CxxYZ::unique_ptr UniquePtr; //! shared_ptr to managed object. typedef CxxYZ::shared_ptr SharedPtr; //! instrusive_ptr to managed object. typedef Boost::intrusive_ptr IntrusivePtr; /** * @brief Create a pool. * * @param maxFreeListSize the maximum number of objects to store in the internal free list. */ static CxxYZ::shared_ptr Create(const size_t maxFreeListSize = Boost::integer_traits::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 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 > m_freeList; size_t m_numberOfAllocations; friend Deleter; }; // --------------------------------------------------------------------------------------------------------------------------------- template CxxYZ::shared_ptr > Pool::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(maxFreeListSize); } // --------------------------------------------------------------------------------------------------------------------------------- template template inline P Pool::Get() { typedef typename Detail::PointerHelper

PointerHelper; { typename THREAD_POLICY::Scoped scopedThreadPolicy(m_threadPolicy); if (!m_freeList.empty()) { CxxYZ::unique_ptr pObject(CxxYZ::move(m_freeList.back())); m_freeList.pop_back(); return PointerHelper::Transfer(pObject.release(), *this); } ++m_numberOfAllocations; } return PointerHelper::New(*this); } // --------------------------------------------------------------------------------------------------------------------------------- template inline size_t Pool::GetFreeListSize() const { typename THREAD_POLICY::Scoped scopedThreadPolicy(m_threadPolicy); return m_freeList.size(); } // --------------------------------------------------------------------------------------------------------------------------------- template inline size_t Pool::GetNumberOfAllocations() const { typename THREAD_POLICY::Scoped scopedThreadPolicy(m_threadPolicy); return m_numberOfAllocations; } // --------------------------------------------------------------------------------------------------------------------------------- template inline Pool::Pool(const size_t maxFreeListSize) : m_maxFreeListSize(maxFreeListSize), m_numberOfAllocations(0) { } // --------------------------------------------------------------------------------------------------------------------------------- template inline void Pool::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)