pool: capacity queries, comments

This commit is contained in:
Danny Robson 2016-01-19 18:29:13 +11:00
parent fcd10f1cea
commit bf5b0916e8
2 changed files with 71 additions and 28 deletions

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2011-2012 Danny Robson <danny@nerdcruft.net> * Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_POOL_HPP #ifndef __UTIL_POOL_HPP
@ -26,13 +26,15 @@ namespace util {
class pool : public nocopy { class pool : public nocopy {
protected: protected:
union node { union node {
node *_node;
char _data[sizeof (T)]; char _data[sizeof (T)];
node *_chain;
}; };
node *m_head; node *m_head; // root address of allocation
node *m_next; node *m_next; // next available entry in the linked list
size_t m_capacity;
const size_t m_capacity;
size_t m_remain;
public: public:
pool (unsigned int _capacity); pool (unsigned int _capacity);
@ -45,6 +47,8 @@ namespace util {
void release (T *data); void release (T *data);
size_t capacity (void) const; size_t capacity (void) const;
size_t remain (void) const;
bool empty (void) const;
// Indexing // Indexing
size_t index (const T*) const; size_t index (const T*) const;

View File

@ -11,82 +11,121 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2011-2012 Danny Robson <danny@nerdcruft.net> * Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#ifdef __UTIL_POOL_IPP #ifdef __UTIL_POOL_IPP
#error Double inclusion of pool.ipp #error
#endif #endif
#define __UTIL_POOL_IPP #define __UTIL_POOL_IPP
namespace util { namespace util {
//-------------------------------------------------------------------------
template <typename T> template <typename T>
pool<T>::pool (unsigned int _capacity): pool<T>::pool (unsigned int _capacity):
m_capacity (_capacity) m_capacity (_capacity),
m_remain (_capacity)
{ {
static_assert (sizeof (T) >= sizeof (uintptr_t), static_assert (sizeof (T) >= sizeof (uintptr_t),
"pool<T>'s chained block system requires that T be at least pointer sized"); "pool<T>'s chained block system requires that T be at least pointer sized");
m_head = static_cast<node *> (operator new (sizeof (T) * m_capacity)); // allocate the memory and note the base address for deletion in destructor
m_next = m_head; m_next = m_head = new node[m_capacity]; // static_cast<node *> (operator new (sizeof (T) * m_capacity));
// initialise the linked list of nodes
for (unsigned int i = 0; i < m_capacity - 1; ++i) for (unsigned int i = 0; i < m_capacity - 1; ++i)
m_next[i]._chain = &m_next[i + 1]; m_next[i]._node = m_next + i + 1;
m_next[m_capacity - 1]._chain = NULL; m_next[m_capacity - 1]._node = nullptr;
} }
//-------------------------------------------------------------------------
template <typename T> template <typename T>
pool<T>::~pool () { pool<T>::~pool ()
CHECK (m_next != NULL); {
// don't check if everything's been returned as pools are often used
unsigned int doomed_count = 0; // for PODs which don't need to be destructed via calling release.
for (node *cursor = m_next; cursor != NULL; cursor = cursor->_chain) delete [] m_head;
++doomed_count;
CHECK_EQ (doomed_count, m_capacity);
operator delete (m_head);
} }
//-------------------------------------------------------------------------
template <typename T> template <typename T>
size_t size_t
pool<T>::capacity (void) const pool<T>::capacity (void) const
{ return m_capacity; } {
return m_capacity;
}
//-------------------------------------------------------------------------
template <typename T>
size_t
pool<T>::remain (void) const
{
return m_remain;
}
//-------------------------------------------------------------------------
template <typename T>
bool
pool<T>::empty (void) const
{
return m_remain == 0;
}
//-------------------------------------------------------------------------
template <typename T> template <typename T>
template <typename ...Args> template <typename ...Args>
T* T*
pool<T>::acquire (Args&... args) { pool<T>::acquire (Args&... args)
{
// double check we have enough capacity left
if (!m_next) if (!m_next)
throw std::bad_alloc (); throw std::bad_alloc ();
CHECK_GT (m_remain, 0);
node *newnext = m_next->_chain; // save what will become the next node shortly. it could be overwritten
T *data = (T*)&m_next->_data; // in the constructor we're about to call.
node *newnext = m_next->_node;
T *data = reinterpret_cast<T*> (m_next->_data);
// try to construct the returnable object.
try { try {
new (data) T (args...); new (data) T (args...);
} catch (...) { } catch (...) {
m_next->_chain = newnext; // the constructor may have overwritten the node linkages before
// throwing. fix this up before forwarding the exception.
m_next->_node = newnext;
throw; throw;
} }
// the object is valid. save the new linked list head and bump the
// stats for availability.
m_next = newnext; m_next = newnext;
m_remain--;
return data; return data;
} }
//-------------------------------------------------------------------------
template <typename T> template <typename T>
void void
pool<T>::release (T *data) { pool<T>::release (T *data)
{
CHECK_LT (m_remain, m_capacity);
data->~T(); data->~T();
node *newnode = reinterpret_cast<node *> (data); node *newnode = reinterpret_cast<node *> (data);
newnode->_chain = m_next; newnode->_node = m_next;
m_next = newnode; m_next = newnode;
m_remain++;
} }
} }