pool: rename remain as size for consistency

This commit is contained in:
Danny Robson 2016-02-04 12:41:23 +11:00
parent b70a84c9be
commit d81b9f12fd
3 changed files with 15 additions and 11 deletions

View File

@ -22,6 +22,10 @@
#include <cstdlib> #include <cstdlib>
namespace util { namespace util {
/// a simple pre-allocated pool for storage of PODs.
///
/// non-POD types can be stored, but there are no guarantees for calling
/// item destructors at pool destruction time.
template <typename T> template <typename T>
class pool : public nocopy { class pool : public nocopy {
protected: protected:
@ -34,7 +38,7 @@ namespace util {
node *m_next; // next available entry in the linked list node *m_next; // next available entry in the linked list
const size_t m_capacity; const size_t m_capacity;
size_t m_remain; size_t m_size;
public: public:
pool (unsigned int _capacity); pool (unsigned int _capacity);
@ -47,7 +51,7 @@ 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; size_t size (void) const;
bool empty (void) const; bool empty (void) const;
// Indexing // Indexing

View File

@ -26,7 +26,7 @@ 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) m_size (0u)
{ {
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");
@ -63,9 +63,9 @@ namespace util {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
template <typename T> template <typename T>
size_t size_t
pool<T>::remain (void) const pool<T>::size (void) const
{ {
return m_remain; return m_size;
} }
@ -74,7 +74,7 @@ namespace util {
bool bool
pool<T>::empty (void) const pool<T>::empty (void) const
{ {
return m_remain == 0; return m_size == m_capacity;
} }
@ -87,7 +87,7 @@ namespace util {
// double check we have enough capacity left // 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); CHECK_LT (m_size, m_capacity);
// save what will become the next node shortly. it could be overwritten // save what will become the next node shortly. it could be overwritten
// in the constructor we're about to call. // in the constructor we're about to call.
@ -107,7 +107,7 @@ namespace util {
// the object is valid. save the new linked list head and bump the // the object is valid. save the new linked list head and bump the
// stats for availability. // stats for availability.
m_next = newnext; m_next = newnext;
m_remain--; m_size++;
return data; return data;
} }
@ -118,14 +118,14 @@ namespace util {
void void
pool<T>::release (T *data) pool<T>::release (T *data)
{ {
CHECK_LT (m_remain, m_capacity); CHECK_NEZ (m_size);
data->~T(); data->~T();
node *newnode = reinterpret_cast<node *> (data); node *newnode = reinterpret_cast<node *> (data);
newnode->_node = m_next; newnode->_node = m_next;
m_next = newnode; m_next = newnode;
m_remain++; m_size--;
} }
} }

View File

@ -35,7 +35,7 @@ check_unique_ptr (util::TAP::logger &tap)
for (auto i: uintset) for (auto i: uintset)
uintpool.release (i); uintpool.release (i);
tap.expect_eq (uintset.size (), uintpool.remain (), "re-inserted maximum elements"); tap.expect_eq (uintpool.size (), 0u, "re-inserted maximum elements");
uintset.clear (); uintset.clear ();
// Do the above one more time to ensure that releasing works right // Do the above one more time to ensure that releasing works right