|
@@ -34,7 +34,7 @@ namespace bx
|
|
|
{
|
|
{
|
|
|
struct Blk
|
|
struct Blk
|
|
|
{
|
|
{
|
|
|
- static const uint64_t kInvalid = UINT64_MAX;
|
|
|
|
|
|
|
+ static constexpr uint64_t kInvalid = UINT64_MAX;
|
|
|
|
|
|
|
|
Blk()
|
|
Blk()
|
|
|
: ptr(kInvalid)
|
|
: ptr(kInvalid)
|
|
@@ -66,6 +66,8 @@ namespace bx
|
|
|
class NonLocalAllocator
|
|
class NonLocalAllocator
|
|
|
{
|
|
{
|
|
|
public:
|
|
public:
|
|
|
|
|
+ static constexpr uint32_t kMinBlockSize = 16u;
|
|
|
|
|
+
|
|
|
NonLocalAllocator()
|
|
NonLocalAllocator()
|
|
|
{
|
|
{
|
|
|
reset();
|
|
reset();
|
|
@@ -102,13 +104,13 @@ namespace bx
|
|
|
|
|
|
|
|
Blk alloc(uint32_t _size)
|
|
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)
|
|
for (FreeList::iterator it = m_free.begin(), itEnd = m_free.end(); it != itEnd; ++it)
|
|
|
{
|
|
{
|
|
|
if (it->size >= _size)
|
|
if (it->size >= _size)
|
|
|
{
|
|
{
|
|
|
- uint64_t ptr = it->ptr;
|
|
|
|
|
|
|
+ const uint64_t ptr = it->ptr;
|
|
|
|
|
|
|
|
if (it->size != _size)
|
|
if (it->size != _size)
|
|
|
{
|
|
{
|
|
@@ -121,6 +123,7 @@ namespace bx
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
m_used += _size;
|
|
m_used += _size;
|
|
|
|
|
+
|
|
|
return Blk{ ptr, _size };
|
|
return Blk{ ptr, _size };
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -142,7 +145,8 @@ namespace bx
|
|
|
m_free.begin()
|
|
m_free.begin()
|
|
|
, uint32_t(m_free.end() - m_free.begin() )
|
|
, uint32_t(m_free.end() - m_free.begin() )
|
|
|
, sizeof(Blk)
|
|
, 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& lhs = *(const Blk*)(_a);
|
|
|
const Blk& rhs = *(const Blk*)(_b);
|
|
const Blk& rhs = *(const Blk*)(_b);
|
|
|
return lhs < rhs ? -1 : 1;
|
|
return lhs < rhs ? -1 : 1;
|
|
@@ -175,6 +179,9 @@ namespace bx
|
|
|
FreeList m_free;
|
|
FreeList m_free;
|
|
|
uint32_t m_used;
|
|
uint32_t m_used;
|
|
|
};
|
|
};
|
|
|
|
|
+
|
|
|
|
|
+ constexpr uint32_t NonLocalAllocator::kMinBlockSize;
|
|
|
|
|
+
|
|
|
} // namespace bx
|
|
} // namespace bx
|
|
|
|
|
|
|
|
TEST_CASE("nlalloc")
|
|
TEST_CASE("nlalloc")
|
|
@@ -190,6 +197,16 @@ TEST_CASE("nlalloc")
|
|
|
blk = nla.alloc(100);
|
|
blk = nla.alloc(100);
|
|
|
REQUIRE(isValid(blk) );
|
|
REQUIRE(isValid(blk) );
|
|
|
|
|
|
|
|
|
|
+ bx::Blk blk2 = nla.alloc(1);
|
|
|
|
|
+ REQUIRE(!isValid(blk2) );
|
|
|
|
|
+
|
|
|
nla.free(blk);
|
|
nla.free(blk);
|
|
|
REQUIRE(0 == nla.getUsed() );
|
|
REQUIRE(0 == nla.getUsed() );
|
|
|
|
|
+
|
|
|
|
|
+ blk2 = nla.alloc(1);
|
|
|
|
|
+ REQUIRE(isValid(blk2) );
|
|
|
|
|
+ REQUIRE(bx::NonLocalAllocator::kMinBlockSize == nla.getUsed() );
|
|
|
|
|
+
|
|
|
|
|
+ nla.free(blk2);
|
|
|
|
|
+ REQUIRE(0 == nla.getUsed() );
|
|
|
}
|
|
}
|