|
@@ -5,35 +5,53 @@
|
|
|
|
|
|
|
|
#include <Tests/Framework/Framework.h>
|
|
#include <Tests/Framework/Framework.h>
|
|
|
#include <AnKi/Util/BuddyAllocatorBuilder.h>
|
|
#include <AnKi/Util/BuddyAllocatorBuilder.h>
|
|
|
|
|
+#include <tuple>
|
|
|
|
|
|
|
|
namespace anki {
|
|
namespace anki {
|
|
|
|
|
|
|
|
|
|
+/// Check if all memory has the same value.
|
|
|
|
|
+static int memvcmp(const void* memory, U8 val, PtrSize size)
|
|
|
|
|
+{
|
|
|
|
|
+ const U8* mm = static_cast<const U8*>(memory);
|
|
|
|
|
+ return (*mm == val) && memcmp(mm, mm + 1, size - 1) == 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
ANKI_TEST(Util, BuddyAllocatorBuilder)
|
|
ANKI_TEST(Util, BuddyAllocatorBuilder)
|
|
|
{
|
|
{
|
|
|
HeapAllocator<U8> alloc(allocAligned, nullptr);
|
|
HeapAllocator<U8> alloc(allocAligned, nullptr);
|
|
|
|
|
|
|
|
// Simple
|
|
// Simple
|
|
|
{
|
|
{
|
|
|
- BuddyAllocatorBuilder<4, Mutex> buddy(alloc, 4);
|
|
|
|
|
|
|
+ BuddyAllocatorBuilder<32, Mutex> buddy(alloc, 32);
|
|
|
|
|
|
|
|
Array<U32, 2> addr;
|
|
Array<U32, 2> addr;
|
|
|
- Bool success = buddy.allocate(1, addr[0]);
|
|
|
|
|
- success = buddy.allocate(3, addr[1]);
|
|
|
|
|
- (void)success;
|
|
|
|
|
|
|
+ const Array<U32, 2> sizes = {58, 198010775};
|
|
|
|
|
+ const Array<U32, 2> alignments = {21, 17};
|
|
|
|
|
+ Bool success = buddy.allocate(sizes[0], alignments[0], addr[0]);
|
|
|
|
|
+ ANKI_TEST_EXPECT_EQ(success, true);
|
|
|
|
|
+ success = buddy.allocate(sizes[1], alignments[1], addr[1]);
|
|
|
|
|
+ ANKI_TEST_EXPECT_EQ(success, true);
|
|
|
|
|
|
|
|
// buddy.debugPrint();
|
|
// buddy.debugPrint();
|
|
|
|
|
|
|
|
- buddy.free(addr[0], 1);
|
|
|
|
|
- buddy.free(addr[1], 3);
|
|
|
|
|
|
|
+ buddy.free(addr[0], sizes[0], alignments[0]);
|
|
|
|
|
+ buddy.free(addr[1], sizes[1], alignments[1]);
|
|
|
|
|
|
|
|
// printf("\n");
|
|
// printf("\n");
|
|
|
// buddy.debugPrint();
|
|
// buddy.debugPrint();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Fuzzy
|
|
|
|
|
|
|
+ // Fuzzy with alignment
|
|
|
{
|
|
{
|
|
|
BuddyAllocatorBuilder<32, Mutex> buddy(alloc, 32);
|
|
BuddyAllocatorBuilder<32, Mutex> buddy(alloc, 32);
|
|
|
- std::vector<std::pair<U32, U32>> allocations;
|
|
|
|
|
|
|
+ std::vector<std::tuple<U32, U32, U32, U8>> allocations;
|
|
|
|
|
+
|
|
|
|
|
+ U8* backingMemory = static_cast<U8*>(malloc(MAX_U32));
|
|
|
|
|
+ for(PtrSize i = 0; i < MAX_U32; ++i)
|
|
|
|
|
+ {
|
|
|
|
|
+ backingMemory[i] = i % MAX_U8;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
for(U32 it = 0; it < 10000; ++it)
|
|
for(U32 it = 0; it < 10000; ++it)
|
|
|
{
|
|
{
|
|
|
if((getRandom() % 2) == 0)
|
|
if((getRandom() % 2) == 0)
|
|
@@ -41,10 +59,14 @@ ANKI_TEST(Util, BuddyAllocatorBuilder)
|
|
|
// Do an allocation
|
|
// Do an allocation
|
|
|
U32 addr;
|
|
U32 addr;
|
|
|
const U32 size = max<U32>(getRandom() % 256_MB, 1);
|
|
const U32 size = max<U32>(getRandom() % 256_MB, 1);
|
|
|
- const Bool success = buddy.allocate(size, addr);
|
|
|
|
|
|
|
+ const U32 alignment = max<U32>(getRandom() % 24, 1);
|
|
|
|
|
+ const Bool success = buddy.allocate(size, alignment, addr);
|
|
|
|
|
+ // printf("al %u %u\n", size, alignment);
|
|
|
if(success)
|
|
if(success)
|
|
|
{
|
|
{
|
|
|
- allocations.push_back({addr, size});
|
|
|
|
|
|
|
+ const U8 bufferValue = getRandom() % MAX_U8;
|
|
|
|
|
+ memset(backingMemory + addr, bufferValue, size);
|
|
|
|
|
+ allocations.push_back({addr, size, alignment, bufferValue});
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
@@ -53,12 +75,22 @@ ANKI_TEST(Util, BuddyAllocatorBuilder)
|
|
|
if(allocations.size())
|
|
if(allocations.size())
|
|
|
{
|
|
{
|
|
|
const PtrSize randPos = getRandom() % allocations.size();
|
|
const PtrSize randPos = getRandom() % allocations.size();
|
|
|
- buddy.free(allocations[randPos].first, allocations[randPos].second);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ const U32 address = std::get<0>(allocations[randPos]);
|
|
|
|
|
+ const U32 size = std::get<1>(allocations[randPos]);
|
|
|
|
|
+ const U32 alignment = std::get<2>(allocations[randPos]);
|
|
|
|
|
+ const U8 bufferValue = std::get<3>(allocations[randPos]);
|
|
|
|
|
+
|
|
|
|
|
+ ANKI_TEST_EXPECT_EQ(memvcmp(backingMemory + address, bufferValue, size), 1);
|
|
|
|
|
+
|
|
|
|
|
+ // printf("fr %u %u\n", size, alignment);
|
|
|
|
|
+ buddy.free(address, size, alignment);
|
|
|
|
|
|
|
|
allocations.erase(allocations.begin() + randPos);
|
|
allocations.erase(allocations.begin() + randPos);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ free(backingMemory);
|
|
|
|
|
|
|
|
// Get the fragmentation
|
|
// Get the fragmentation
|
|
|
PtrSize userAllocatedSize, realAllocatedSize;
|
|
PtrSize userAllocatedSize, realAllocatedSize;
|
|
@@ -71,7 +103,7 @@ ANKI_TEST(Util, BuddyAllocatorBuilder)
|
|
|
// Remove the remaining
|
|
// Remove the remaining
|
|
|
for(const auto& pair : allocations)
|
|
for(const auto& pair : allocations)
|
|
|
{
|
|
{
|
|
|
- buddy.free(pair.first, pair.second);
|
|
|
|
|
|
|
+ buddy.free(std::get<0>(pair), std::get<1>(pair), std::get<2>(pair));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|