summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bx/tests/nlalloc_test.cpp
diff options
context:
space:
mode:
author Julian Sikorski <belegdol+github@gmail.com>2019-10-13 13:50:38 +0200
committer R. Belmont <rb6502@users.noreply.github.com>2019-10-13 07:50:38 -0400
commit0837e7451a84f95c29dbdb9bd6b8b931fee1635d (patch)
tree626bcbd250a7fbebdf5958a288d2f438d4f9fb5a /3rdparty/bx/tests/nlalloc_test.cpp
parentc913ccb59d713ce0b91135520719ac5385e54358 (diff)
WIP: sync bgfx, bx and bimg with latest upstream (#5723)
* Sync with bgfx upstream revision b91d0b6 * Sync with bx upstream revision d60912b * Sync with bimg upstream revision bd81f60 * Add astc-codec decoder * Rename VertexDecl to VertexLayout * Rename UniformType enum Int1 to Sampler. * Add NVN stub * Fix unused-const-variable error on macOS * Drop redundant explicit language parameters buildoptions_cpp are only applied to c++ files and buildoptions_objcpp are only applied to objective c++ files. As such, hardcoding -x offers no benefit while preventing overrides (such as one needed by 3rdparty/bgfx/src/renderer_vk.cpp on macOS) from working. * Re-introduce -x c++ in places where C code is compiled as C++ to prevent clang from throwing a warning * Build bgfx as Objective-C++ on macOS It is needed due to included headers * Enable Direct3D12 and Vulkan bgfx rendering backends * Enable building of spirv shaders * Properly escape /c in cmd call * Comment out dx12 bgfx renderer * Honor VERBOSE setting during shaders build * Only invert hlsl shader XYZ_TO_sRGB matrix for opengl * Add spirv shaders * OpenGL ES needs transposed matrix too * Metal needs transposed matrix as well
Diffstat (limited to '3rdparty/bx/tests/nlalloc_test.cpp')
-rw-r--r--3rdparty/bx/tests/nlalloc_test.cpp92
1 files changed, 63 insertions, 29 deletions
diff --git a/3rdparty/bx/tests/nlalloc_test.cpp b/3rdparty/bx/tests/nlalloc_test.cpp
index 91b0354926f..14c44e5f23f 100644
--- a/3rdparty/bx/tests/nlalloc_test.cpp
+++ b/3rdparty/bx/tests/nlalloc_test.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright 2010-2018 Branimir Karadzic. All rights reserved.
+ * Copyright 2010-2019 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bx#license-bsd-2-clause
*/
@@ -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 };
}
}
@@ -131,38 +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
@@ -175,6 +172,9 @@ namespace bx
FreeList m_free;
uint32_t m_used;
};
+
+ constexpr uint32_t NonLocalAllocator::kMinBlockSize;
+
} // namespace bx
TEST_CASE("nlalloc")
@@ -185,11 +185,45 @@ 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);
+ REQUIRE(!isValid(blk2) );
+
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() );
+
+ nla.free(blk2);
+ REQUIRE(0 == nla.getUsed() );
}