diff --git a/alloc/linear.cpp b/alloc/linear.cpp index 80ed26a4..1f784a62 100644 --- a/alloc/linear.cpp +++ b/alloc/linear.cpp @@ -73,7 +73,7 @@ linear::capacity (void) const //----------------------------------------------------------------------------- size_t -linear::size (void) const +linear::used (void) const { return m_cursor - m_begin; } @@ -83,5 +83,5 @@ linear::size (void) const size_t linear::remain (void) const { - return capacity () - size (); + return capacity () - used (); } diff --git a/alloc/linear.hpp b/alloc/linear.hpp index 303789b4..d8724e86 100644 --- a/alloc/linear.hpp +++ b/alloc/linear.hpp @@ -38,7 +38,7 @@ namespace util { namespace alloc { void reset (void); size_t capacity (void) const; - size_t size (void) const; + size_t used (void) const; size_t remain (void) const; protected: diff --git a/alloc/stack.cpp b/alloc/stack.cpp index b33a6d2d..436aa249 100644 --- a/alloc/stack.cpp +++ b/alloc/stack.cpp @@ -111,7 +111,7 @@ stack::capacity (void) const //----------------------------------------------------------------------------- size_t -stack::size (void) const +stack::used (void) const { return m_cursor - m_begin; } @@ -121,5 +121,5 @@ stack::size (void) const size_t stack::remain (void) const { - return capacity () - size (); + return capacity () - used (); } diff --git a/alloc/stack.hpp b/alloc/stack.hpp index 055a6bec..0cedd583 100644 --- a/alloc/stack.hpp +++ b/alloc/stack.hpp @@ -40,7 +40,7 @@ namespace util { namespace alloc { void reset (void); size_t capacity (void) const; - size_t size (void) const; + size_t used (void) const; size_t remain (void) const; private: diff --git a/test/alloc/linear.cpp b/test/alloc/linear.cpp index ec95ada1..338e3437 100644 --- a/test/alloc/linear.cpp +++ b/test/alloc/linear.cpp @@ -12,6 +12,8 @@ main (void) alignas (std::max_align_t) char memory[BUFFER_SIZE]; util::alloc::linear store (std::begin (memory), std::end (memory)); + tap.expect_eq (store.capacity (), BUFFER_SIZE, "bytes capacity matches"); + tap.expect_throw ( [&] (void) { store.allocate (BUFFER_SIZE + 1, 1); }, "excessive allocation throws bad_alloc" @@ -22,6 +24,9 @@ main (void) "maximum allocation succeeds" ); + tap.expect_eq (store.used (), BUFFER_SIZE, "bytes used matches"); + tap.expect_eq (store.remain (), 0u, "bytes remain matches"); + tap.expect_throw ( [&] (void) { store.allocate (1, 1); }, "minimum allocation fails after exhaustion" diff --git a/test/alloc/stack.cpp b/test/alloc/stack.cpp index 24cca3a4..47356516 100644 --- a/test/alloc/stack.cpp +++ b/test/alloc/stack.cpp @@ -34,6 +34,8 @@ main (void) util::alloc::stack store (memory, memory + BUFFER_AVAILABLE); + tap.expect_eq (store.capacity (), BUFFER_AVAILABLE, "bytes capacity matches"); + // larger than total allocations should throw tap.expect_throw ( [&store] (void) { store.allocate (BUFFER_AVAILABLE + 1, 1); }, @@ -56,6 +58,11 @@ main (void) [&store] (void) { store.allocate (BUFFER_REQUEST); }, "bad_alloc thrown on exhaustion" ); + + // check byte counts are plausible. stacks use some extra memory for book + // keeping, so we need to use relational comparison rather than equality. + tap.expect_ge (store.used (), BUFFER_REQUEST, "bytes used matches"); + tap.expect_le (store.remain (), BUFFER_AVAILABLE - BUFFER_REQUEST, "bytes remain matches"); // try many allocations again after resetting the allocator to zero usage store.reset ();