This commit is contained in:
Бранимир Караџић
2019-01-03 12:27:45 -08:00
parent 2b16a44b4d
commit 3df370aedc

View File

@@ -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() );
}