From 8397b518d6a1c02ad02a947a2acc24dcc945edd2 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, 14 Feb 2019 20:34:56 -0800 Subject: [PATCH] Cleanup. --- tests/nlalloc_test.cpp | 69 ++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/tests/nlalloc_test.cpp b/tests/nlalloc_test.cpp index d3c24fb..14c44e5 100644 --- a/tests/nlalloc_test.cpp +++ b/tests/nlalloc_test.cpp @@ -134,39 +134,32 @@ namespace bx void free(const Blk& _blk) { + BX_CHECK(isValid(_blk), "Freeing invalid block!"); + m_used -= _blk.size; - m_free.push_back(_blk); - compact(); - } - bool compact() - { - bx::quickSort( - m_free.begin() - , uint32_t(m_free.end() - m_free.begin() ) - , sizeof(Blk) - , [](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; - }); - - for (FreeList::iterator it = m_free.begin(), next = it, itEnd = m_free.end(); next != itEnd;) + FreeList::iterator it = m_free.begin(); + FreeList::iterator itEnd = m_free.end(); + for (; it != itEnd; ++it) { - if ( (it->ptr + it->size) == next->ptr) + if ( (_blk.ptr + _blk.size) == it->ptr) { - it->size += next->size; - next = m_free.erase(next); + it->ptr = _blk.ptr; + it->size += _blk.size; + break; } - else + + if (_blk.ptr > it->ptr) { - it = next; - ++next; + m_free.insert(it, _blk); + break; } } - return 0 == m_used; + if (it == itEnd) + { + m_free.push_back(_blk); + } } uint32_t getUsed() const @@ -192,9 +185,9 @@ TEST_CASE("nlalloc") blk = nla.alloc(100); REQUIRE(!isValid(blk) ); - nla.add(bx::Blk{0x1000, 100}); + nla.add(bx::Blk{0x1000, 1024}); - blk = nla.alloc(100); + blk = nla.alloc(1024); REQUIRE(isValid(blk) ); bx::Blk blk2 = nla.alloc(1); @@ -203,6 +196,30 @@ TEST_CASE("nlalloc") nla.free(blk); REQUIRE(0 == nla.getUsed() ); + { + bx::Blk blk3 = nla.alloc(123); + REQUIRE(isValid(blk3) ); + + bx::Blk blk4 = nla.alloc(134); + REQUIRE(isValid(blk4) ); + + bx::Blk blk5 = nla.alloc(145); + REQUIRE(isValid(blk5) ); + + bx::Blk blk6 = nla.alloc(156); + REQUIRE(isValid(blk6) ); + + bx::Blk blk7 = nla.alloc(167); + REQUIRE(isValid(blk7) ); + + nla.free(blk3); + nla.free(blk5); + nla.free(blk7); + nla.free(blk4); + nla.free(blk6); + REQUIRE(0 == nla.getUsed() ); + } + blk2 = nla.alloc(1); REQUIRE(isValid(blk2) ); REQUIRE(bx::NonLocalAllocator::kMinBlockSize == nla.getUsed() );