summaryrefslogtreecommitdiff
path: root/tools/slang-test/unit-test-memory-arena.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2018-09-13 14:02:33 -0400
committerGitHub <noreply@github.com>2018-09-13 14:02:33 -0400
commit929745d75f0607ab5b2218083ca4ccb493eb6032 (patch)
tree32ceddda261aaba108607b89aeb89ebafd424e00 /tools/slang-test/unit-test-memory-arena.cpp
parentf60135cec62c91a9d7923397fe8796d2b3eaa5cb (diff)
Feature/memory arena improvements (#633)
* First pass at MemoryArena. * First pass at RandomGenerator. * Extract TestContext into external source file. * Fix warning on printf. * Use enum classes for Test enums. OutputMode -> TestOutputMode. * First pass at FreeList unit test. * Auto registering tests. Improvements to RandomGenerator. * Remove the need for unitTest headers - cos can use registering. * Added unitTest for MemoryArena. * Do unit tests. * Fix typo. * Fix problem limiting errors from TestContext. * Refactor of MemoryArena * Removed the ability to rewind (to improve memory usage/simplify) * Better memory usage - around oversized blocks + Will keep allocating from a normal block if more than 1/3 memory left, or an oversided block is allocated * Better unitTest coverage for MemoryArena. * Fixes based on code review * Remove e prefix from enum class types for TestContext * Added extra checking for allocations sizes * Fixed some typos * Added std::is_pod test to allocateAndCopyArray * Add include for is_pod needed for linux build.
Diffstat (limited to 'tools/slang-test/unit-test-memory-arena.cpp')
-rw-r--r--tools/slang-test/unit-test-memory-arena.cpp49
1 files changed, 30 insertions, 19 deletions
diff --git a/tools/slang-test/unit-test-memory-arena.cpp b/tools/slang-test/unit-test-memory-arena.cpp
index 69eed520a..20a7e047d 100644
--- a/tools/slang-test/unit-test-memory-arena.cpp
+++ b/tools/slang-test/unit-test-memory-arena.cpp
@@ -123,15 +123,23 @@ static void memoryArenaUnitTest()
blocks.Add(arena.allocate(blockSize * 2));
blocks.Add(arena.allocate(100));
- while (blocks.Count())
+ arena.deallocateAll();
+ blocks.Add(arena.allocate(100));
+ blocks.Add(arena.allocate(blockSize * 2));
+
+ arena.reset();
+
{
- arena.deallocateLast(blocks.Last());
- blocks.RemoveLast();
+ uint32_t data[] = { 1, 2, 3 };
+
+ const uint32_t* copy = arena.allocateAndCopyArray(data, SLANG_COUNT_OF(data));
+
+ SLANG_CHECK(::memcmp(copy, data, sizeof(data)) == 0);
}
}
{
-
+ int count = 0;
const size_t blockSize = 1024;
for (TestMode mode = TestMode(0); int(mode) < int(TestMode::eCount); mode = TestMode(int(mode) + 1))
@@ -143,32 +151,30 @@ static void memoryArenaUnitTest()
List<Block> blocks;
- for (int i = 0; i < 1000; i++)
+ for (int i = 0; i < 10000; i++)
{
- int var = randGen.nextInt32() & 0x3ff;
+ count++;
+
+ const int var = randGen.nextInt32() & 0x3ff;
if (var < 3 && blocks.Count() > 0)
{
- if (var == 0)
- {
- // Do a single dealloc
- arena.deallocateLast(blocks.Last().m_data);
- blocks.RemoveLast();
- }
- else if (var == 1)
+ if (var == 1)
{
// Deallocate everything
arena.deallocateAll();
blocks.Clear();
+ }
+ else if (var == 2)
+ {
+ arena.reset();
+ blocks.Clear();
}
else
{
- // Do a multiple dealloc
- int index = randGen.nextInt32UpTo(int(blocks.Count()));
+ size_t usedMemory = arena.calcTotalMemoryUsed();
+ size_t allocatedMemory = arena.calcTotalMemoryAllocated();
- // Deallocate all afterwards
- arena.deallocateAllFrom(blocks[index].m_data);
-
- blocks.SetSize(index);
+ SLANG_CHECK(allocatedMemory >= usedMemory);
}
}
else
@@ -180,6 +186,11 @@ static void memoryArenaUnitTest()
{
sizeInBytes += blockSize;
}
+ else if ((randGen.nextInt32() & 0xff) < 2)
+ {
+ // Let's try for a block that's awkwardly sized
+ sizeInBytes = blockSize / 3 + 10;
+ }
const uint8_t value = uint8_t(randGen.nextInt32());