/** * @file include/ActivMiddleware/Misc/PooledBuffer.h * * @brief Boost::pool helper. */ #if (!defined ACTIV_MISC_POOLED_BUFFER_H) #define ACTIV_MISC_POOLED_BUFFER_H #include "ActivMiddleware/ActivMiddleware.h" #include ACTIV_BOOST_INCLUDE(smart_ptr/intrusive_ptr.hpp) #include ACTIV_BOOST_INCLUDE(smart_ptr/intrusive_ref_counter.hpp) #include ACTIV_BOOST_INCLUDE(pool/pool.hpp) //! Activ namespace. namespace Activ { /** * @brief Pooled buffer helper. * * POOL is a Boost::pool class. * ADD_INTRUSIVE_REFCOUNT whether to add a Boost::intrusive_ptr reference count to the buffer class. * * This class can be used to allocate buffers from a Boost::pool, or with malloc/free if the Boost::pool buffer * size is not sufficient. * * Provides Pointer and ConstPointer types. Will be intrusive if ADD_INTRUSIVE_REFCOUNT. */ template class PooledBuffer : public Boost::mpl::if_c, Boost::thread_unsafe_counter>, Empty>::type { public: typedef POOL Pool; typedef typename Boost::mpl::if_c< ADD_INTRUSIVE_REFCOUNT, Boost::intrusive_ptr, PooledBuffer * >::type Pointer; typedef typename Boost::mpl::if_c< ADD_INTRUSIVE_REFCOUNT, Boost::intrusive_ptr, const PooledBuffer * >::type ConstPointer; /** * @brief Constructor. * * No buffer is allocated from the pool. * * @param pool to be used for future allocations. */ PooledBuffer(Pool &pool); /** * @brief Constructor. * * @param pool * @param bufferLength initial buffer size to allocate. If <= the pool's buffer size, allocate from pool, else malloc. * * @throw std::bad_alloc on memory allocation failure. */ PooledBuffer(Pool &pool, const size_t bufferLength); /** * @brief Destructor. */ ~PooledBuffer(); /** * @brief Allocate a buffer. * * If a buffer is already allocated, and it is of sufficient size to hold @a bufferLength, it is reused. * * @param bufferLength buffer size to allocate. If <= the pool's buffer size, allocate from pool, else malloc. */ bool Allocate(const size_t bufferLength); /** * @brief Copy data in to the start of the buffer. * * A buffer is allocated as required. * * @param pData * @param dataLength * * @return Boolean as to whether the copy was successful. */ bool Copy(const void *pData, const size_t dataLength); /** * @brief Get the pool. * * @return Pool reference. */ Pool &GetPool(); /** * @brief Get const pointer to underlying buffer. * * @return Pointer to buffer. */ const void *GetBuffer() const; /** * @brief Get pointer to underlying buffer. * * @return Pointer to buffer. */ void *GetBuffer(); /** * @brief Get size of underlying buffer. * * @return Buffer size. */ size_t GetBufferSize() const; /** * @brief Get amount of buffer in use. * * @return Buffer length in use. */ size_t GetLength() const; /** * @brief Set the amount of the buffer in use. * * @param dataLength cannot be larger than the underlying buffer size. */ void SetLength(const size_t dataLength); /** * @brief Assignment operator. * * This object's buffer (if any) is freed, and this object takes ownership of @a rhs's buffer. rhs will * no longer have a buffer. * * The pool used by @a rhs must be the same as this pool. * * @param rhs */ PooledBuffer &operator=(PooledBuffer &rhs); private: // No copy ctor. We'd need a proxy class to be able to do a non-const copy constructor anyway. PooledBuffer(const PooledBuffer &); /** * Allocate buffer from pool or malloc. * * @param bufferLength */ bool InternalAllocate(const size_t bufferLength); /** * @brief Return buffer to pool or free(). */ void Free(); Pool & m_pool; void * m_pBuffer; size_t m_bufferSize; size_t m_dataLength; ///< Buffer length actually in use. }; // --------------------------------------------------------------------------------------------------------------------------------- template inline PooledBuffer::PooledBuffer(Pool &pool) : m_pool(pool), m_pBuffer(0), m_bufferSize(0), m_dataLength(0) { } // --------------------------------------------------------------------------------------------------------------------------------- template inline PooledBuffer::PooledBuffer(Pool &pool, const size_t bufferLength) : m_pool(pool), m_pBuffer(0), m_bufferSize(0), m_dataLength(0) { if (bufferLength > 0) { if (!InternalAllocate(bufferLength)) throw std::bad_alloc(); } } // --------------------------------------------------------------------------------------------------------------------------------- template inline PooledBuffer::~PooledBuffer() { if (0 != m_pBuffer) Free(); } // --------------------------------------------------------------------------------------------------------------------------------- template inline bool PooledBuffer::Allocate(const size_t bufferLength) { if (ACTIV_UNLIKELY(0 == bufferLength)) { if (0 != m_bufferSize) { Free(); m_dataLength = 0; } return true; } if (0 != m_bufferSize) { if (bufferLength <= m_bufferSize) { // Just reuse existing buffer. m_dataLength = 0; return true; } Free(); } return InternalAllocate(bufferLength); } // --------------------------------------------------------------------------------------------------------------------------------- template inline bool PooledBuffer::Copy(const void *pData, const size_t dataLength) { if (ACTIV_LIKELY(0 != pData)) { // There's actual data to copy in. Allocate a buffer if necessary. if (ACTIV_UNLIKELY(!Allocate(dataLength))) return false; std::memcpy(m_pBuffer, pData, dataLength); } else { // Setting NULL pointer, just free up an existing buffer. assert(0 == dataLength); if (0 != m_pBuffer) Free(); } m_dataLength = dataLength; return true; } // --------------------------------------------------------------------------------------------------------------------------------- template inline POOL &PooledBuffer::GetPool() { return m_pool; } // --------------------------------------------------------------------------------------------------------------------------------- template inline const void *PooledBuffer::GetBuffer() const { return m_pBuffer; } // --------------------------------------------------------------------------------------------------------------------------------- template inline void *PooledBuffer::GetBuffer() { return m_pBuffer; } // --------------------------------------------------------------------------------------------------------------------------------- template inline size_t PooledBuffer::GetBufferSize() const { return m_bufferSize; } // --------------------------------------------------------------------------------------------------------------------------------- template inline size_t PooledBuffer::GetLength() const { return m_dataLength; } // --------------------------------------------------------------------------------------------------------------------------------- template inline void PooledBuffer::SetLength(const size_t dataLength) { if (ACTIV_UNLIKELY(dataLength > m_bufferSize)) { // Whoops. We're trying to set the length beyond the buffer size. Not allowed - this API // is just for truncating a buffer to the "built" size of a datagram. assert(false); return; } m_dataLength = dataLength; } // --------------------------------------------------------------------------------------------------------------------------------- template inline PooledBuffer &PooledBuffer::operator=(PooledBuffer &rhs) { if (ACTIV_UNLIKELY(&rhs == this)) return *this; assert(&m_pool == &rhs.m_pool); // Transfer ownership of rhs's buffer to this. if (0 != m_pBuffer) Free(); m_pBuffer = rhs.m_pBuffer; m_bufferSize = rhs.m_bufferSize; m_dataLength = rhs.m_dataLength; rhs.m_pBuffer = 0; rhs.m_bufferSize = 0; rhs.m_dataLength = 0; return *this; } // --------------------------------------------------------------------------------------------------------------------------------- template inline bool PooledBuffer::InternalAllocate(const size_t bufferLength) { assert(0 == m_pBuffer); void *pBuffer; size_t actualBufferLength = m_pool.get_requested_size(); if (bufferLength <= actualBufferLength) { pBuffer = m_pool.malloc(); } else { actualBufferLength = bufferLength; pBuffer = std::malloc(actualBufferLength); } if (0 == pBuffer) return false; m_pBuffer = pBuffer; m_bufferSize = actualBufferLength; m_dataLength = 0; return true; } // --------------------------------------------------------------------------------------------------------------------------------- template inline void PooledBuffer::Free() { assert(0 != m_pBuffer); if (m_bufferSize <= m_pool.get_requested_size()) m_pool.free(m_pBuffer); else std::free(m_pBuffer); m_pBuffer = 0; m_bufferSize = 0; } } // namespace Activ #endif // (!defined ACTIV_MISC_POOLED_BUFFER_H)