alloc: rename 'size' to 'used'

This commit is contained in:
Danny Robson 2015-11-30 16:08:07 +11:00
parent da81fc8355
commit d232f1c871
6 changed files with 18 additions and 6 deletions

View File

@ -73,7 +73,7 @@ linear::capacity (void) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
size_t size_t
linear::size (void) const linear::used (void) const
{ {
return m_cursor - m_begin; return m_cursor - m_begin;
} }
@ -83,5 +83,5 @@ linear::size (void) const
size_t size_t
linear::remain (void) const linear::remain (void) const
{ {
return capacity () - size (); return capacity () - used ();
} }

View File

@ -38,7 +38,7 @@ namespace util { namespace alloc {
void reset (void); void reset (void);
size_t capacity (void) const; size_t capacity (void) const;
size_t size (void) const; size_t used (void) const;
size_t remain (void) const; size_t remain (void) const;
protected: protected:

View File

@ -111,7 +111,7 @@ stack::capacity (void) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
size_t size_t
stack::size (void) const stack::used (void) const
{ {
return m_cursor - m_begin; return m_cursor - m_begin;
} }
@ -121,5 +121,5 @@ stack::size (void) const
size_t size_t
stack::remain (void) const stack::remain (void) const
{ {
return capacity () - size (); return capacity () - used ();
} }

View File

@ -40,7 +40,7 @@ namespace util { namespace alloc {
void reset (void); void reset (void);
size_t capacity (void) const; size_t capacity (void) const;
size_t size (void) const; size_t used (void) const;
size_t remain (void) const; size_t remain (void) const;
private: private:

View File

@ -12,6 +12,8 @@ main (void)
alignas (std::max_align_t) char memory[BUFFER_SIZE]; alignas (std::max_align_t) char memory[BUFFER_SIZE];
util::alloc::linear store (std::begin (memory), std::end (memory)); util::alloc::linear store (std::begin (memory), std::end (memory));
tap.expect_eq (store.capacity (), BUFFER_SIZE, "bytes capacity matches");
tap.expect_throw<std::bad_alloc> ( tap.expect_throw<std::bad_alloc> (
[&] (void) { store.allocate (BUFFER_SIZE + 1, 1); }, [&] (void) { store.allocate (BUFFER_SIZE + 1, 1); },
"excessive allocation throws bad_alloc" "excessive allocation throws bad_alloc"
@ -22,6 +24,9 @@ main (void)
"maximum allocation succeeds" "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<std::bad_alloc> ( tap.expect_throw<std::bad_alloc> (
[&] (void) { store.allocate (1, 1); }, [&] (void) { store.allocate (1, 1); },
"minimum allocation fails after exhaustion" "minimum allocation fails after exhaustion"

View File

@ -34,6 +34,8 @@ main (void)
util::alloc::stack store (memory, memory + BUFFER_AVAILABLE); util::alloc::stack store (memory, memory + BUFFER_AVAILABLE);
tap.expect_eq (store.capacity (), BUFFER_AVAILABLE, "bytes capacity matches");
// larger than total allocations should throw // larger than total allocations should throw
tap.expect_throw<std::bad_alloc> ( tap.expect_throw<std::bad_alloc> (
[&store] (void) { store.allocate (BUFFER_AVAILABLE + 1, 1); }, [&store] (void) { store.allocate (BUFFER_AVAILABLE + 1, 1); },
@ -57,6 +59,11 @@ main (void)
"bad_alloc thrown on exhaustion" "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 // try many allocations again after resetting the allocator to zero usage
store.reset (); store.reset ();
tap.expect_nothrow ( tap.expect_nothrow (