From d81b9f12fd93b5c6dfb404c7767ecc121870415a Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 4 Feb 2016 12:41:23 +1100 Subject: [PATCH] pool: rename remain as size for consistency --- pool.hpp | 8 ++++++-- pool.ipp | 16 ++++++++-------- test/pool.cpp | 2 +- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/pool.hpp b/pool.hpp index f4aed52c..1f3bf598 100644 --- a/pool.hpp +++ b/pool.hpp @@ -22,6 +22,10 @@ #include 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 class pool : public nocopy { protected: @@ -34,7 +38,7 @@ namespace util { node *m_next; // next available entry in the linked list const size_t m_capacity; - size_t m_remain; + size_t m_size; public: pool (unsigned int _capacity); @@ -47,7 +51,7 @@ namespace util { void release (T *data); size_t capacity (void) const; - size_t remain (void) const; + size_t size (void) const; bool empty (void) const; // Indexing diff --git a/pool.ipp b/pool.ipp index f9261c33..1cafaae0 100644 --- a/pool.ipp +++ b/pool.ipp @@ -26,7 +26,7 @@ namespace util { template pool::pool (unsigned int _capacity): m_capacity (_capacity), - m_remain (_capacity) + m_size (0u) { static_assert (sizeof (T) >= sizeof (uintptr_t), "pool's chained block system requires that T be at least pointer sized"); @@ -63,9 +63,9 @@ namespace util { //------------------------------------------------------------------------- template size_t - pool::remain (void) const + pool::size (void) const { - return m_remain; + return m_size; } @@ -74,7 +74,7 @@ namespace util { bool pool::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 if (!m_next) 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 // 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 // stats for availability. m_next = newnext; - m_remain--; + m_size++; return data; } @@ -118,14 +118,14 @@ namespace util { void pool::release (T *data) { - CHECK_LT (m_remain, m_capacity); + CHECK_NEZ (m_size); data->~T(); node *newnode = reinterpret_cast (data); newnode->_node = m_next; m_next = newnode; - m_remain++; + m_size--; } } diff --git a/test/pool.cpp b/test/pool.cpp index 00c79a94..81c412e4 100644 --- a/test/pool.cpp +++ b/test/pool.cpp @@ -35,7 +35,7 @@ check_unique_ptr (util::TAP::logger &tap) for (auto i: uintset) 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 (); // Do the above one more time to ensure that releasing works right