From 3df370aedc35b55c23e8863f0ccce88d290f5e47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Thu, 3 Jan 2019 12:27:45 -0800 Subject: [PATCH] Cleanup. --- tests/nlalloc_test.cpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/tests/nlalloc_test.cpp b/tests/nlalloc_test.cpp index 91b0354..34ac8ab 100644 --- a/tests/nlalloc_test.cpp +++ b/tests/nlalloc_test.cpp @@ -34,7 +34,7 @@ namespace bx { struct Blk { - static const uint64_t kInvalid = UINT64_MAX; + static constexpr uint64_t kInvalid = UINT64_MAX; Blk() : ptr(kInvalid) @@ -66,6 +66,8 @@ namespace bx class NonLocalAllocator { public: + static constexpr uint32_t kMinBlockSize = 16u; + NonLocalAllocator() { reset(); @@ -102,13 +104,13 @@ namespace bx Blk alloc(uint32_t _size) { - _size = max(_size, 16u); + _size = max(_size, kMinBlockSize); for (FreeList::iterator it = m_free.begin(), itEnd = m_free.end(); it != itEnd; ++it) { if (it->size >= _size) { - uint64_t ptr = it->ptr; + const uint64_t ptr = it->ptr; if (it->size != _size) { @@ -121,6 +123,7 @@ namespace bx } m_used += _size; + return Blk{ ptr, _size }; } } @@ -142,7 +145,8 @@ namespace bx m_free.begin() , uint32_t(m_free.end() - m_free.begin() ) , sizeof(Blk) - , [](const void* _a, const void* _b) -> int32_t { + , [](const void* _a, const void* _b) -> int32_t + { const Blk& lhs = *(const Blk*)(_a); const Blk& rhs = *(const Blk*)(_b); return lhs < rhs ? -1 : 1; @@ -175,6 +179,9 @@ namespace bx FreeList m_free; uint32_t m_used; }; + + constexpr uint32_t NonLocalAllocator::kMinBlockSize; + } // namespace bx TEST_CASE("nlalloc") @@ -190,6 +197,16 @@ TEST_CASE("nlalloc") blk = nla.alloc(100); REQUIRE(isValid(blk) ); + bx::Blk blk2 = nla.alloc(1); + REQUIRE(!isValid(blk2) ); + nla.free(blk); REQUIRE(0 == nla.getUsed() ); + + blk2 = nla.alloc(1); + REQUIRE(isValid(blk2) ); + REQUIRE(bx::NonLocalAllocator::kMinBlockSize == nla.getUsed() ); + + nla.free(blk2); + REQUIRE(0 == nla.getUsed() ); }