Add tests for Heap allocator
Some checks failed
CMake on multiple platforms / build (Release, cl, cl, windows-latest) (push) Has been cancelled
CMake on multiple platforms / build (Release, clang, clang++, macos-latest) (push) Has been cancelled
CMake on multiple platforms / build (Release, clang, clang++, ubuntu-latest) (push) Has been cancelled
CMake on multiple platforms / build (Release, gcc, g++, ubuntu-latest) (push) Has been cancelled

Add Heap::getMaxSize()
Add Heap::getUsed()
Add minSize argument to Heap::init()
This commit is contained in:
2026-09-15 22:48:03 -07:00
parent 76062cdf03
commit 033bf5d6ef
6 changed files with 49 additions and 4 deletions

View File

@@ -35,6 +35,7 @@
#include <stdint.h> #include <stdint.h>
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
#define NOMINMAX
#include <Windows.h> #include <Windows.h>
#endif #endif

View File

@@ -46,7 +46,7 @@ public:
Heap(); Heap();
~Heap(); ~Heap();
bool init(size_t maxSize = 1ULL << 32); bool init(size_t minSize = 1ULL << 24, size_t maxSize = 1ULL << 32);
// Allocate `size` bytes // Allocate `size` bytes
std::byte* alloc(size_t size); std::byte* alloc(size_t size);
@@ -60,8 +60,18 @@ public:
// Free the allocation at `address` // Free the allocation at `address`
void free(std::byte* address); void free(std::byte* address);
// Get the actual used size.
// This may differ from the sum of allocations due to alignment requirements.
size_t getUsed() const;
// Get the actual maximum size.
// This may be greater than the maxSize passed into init, due to page size alignment.
size_t getMaxSize() const;
private: private:
Region m_region; Region m_region;
size_t m_minSize;
size_t m_usedSize;
// Add a free block to the index // Add a free block to the index
void insertFreeBlock(TLSFBlock* block); void insertFreeBlock(TLSFBlock* block);

View File

@@ -32,6 +32,7 @@
#include "../include/mimir.h" #include "../include/mimir.h"
#include "mimir_impl.h" #include "mimir_impl.h"
#include <algorithm>
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include <bit> #include <bit>
@@ -94,6 +95,8 @@ uint64_t blockSizeToIndex(uint64_t size)
*/ */
Heap::Heap() Heap::Heap()
: m_minSize(0)
, m_usedSize(0)
{ {
} }
@@ -101,12 +104,18 @@ Heap::~Heap()
{ {
} }
bool Heap::init(size_t maxSize) bool Heap::init(size_t minSize, size_t maxSize)
{ {
assert(maxSize >= minSize);
assert(m_usedSize == 0);
m_minSize = sizeof(TLSFIndex) + 16;
m_minSize = std::max(m_minSize, minSize);
m_minSize = KRAKEN_MEM_ROUND_UP_PAGE(m_minSize);
if (!m_region.init(maxSize)) { if (!m_region.init(maxSize)) {
return false; return false;
} }
if (!m_region.resize(sizeof(TLSFIndex) + 16)) { if (!m_region.resize(m_minSize)) {
return false; return false;
} }
TLSFIndex* index = (TLSFIndex*)m_region.getAddress(); TLSFIndex* index = (TLSFIndex*)m_region.getAddress();
@@ -124,6 +133,20 @@ bool Heap::init(size_t maxSize)
return true; return true;
} }
// Get the actual maximum size.
// This may be greater than the maxSize passed into init, due to page size alignment.
size_t Heap::getMaxSize() const
{
return m_region.getSize();
}
// Get the actual used size.
// This may differ from the sum of allocations due to alignment requirements.
size_t Heap::getUsed() const
{
return m_usedSize;
}
void Heap::insertFreeBlock(TLSFBlock* block) void Heap::insertFreeBlock(TLSFBlock* block)
{ {
// Add the block to the index // Add the block to the index
@@ -281,6 +304,7 @@ std::byte* Heap::alloc(size_t size)
} }
block->size = size; block->size = size;
m_usedSize += size;
return (std::byte*)block + 16; return (std::byte*)block + 16;
} }

View File

@@ -39,6 +39,7 @@
#endif #endif
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
#define NOMINMAX
#include <Windows.h> #include <Windows.h>
#include <memoryapi.h> #include <memoryapi.h>
#endif #endif

View File

@@ -14,8 +14,9 @@ enable_testing()
add_executable( add_executable(
mimir_test mimir_test
"region_test.cpp"
"arena_test.cpp" "arena_test.cpp"
"heap_test.cpp"
"region_test.cpp"
"test_util.cpp") "test_util.cpp")
target_link_libraries( target_link_libraries(
mimir_test mimir_test

View File

@@ -6,6 +6,14 @@
namespace mimir_tests { namespace mimir_tests {
TEST(Arena, InitialState)
{
mimir::init();
mimir::Arena arena;
EXPECT_TRUE(arena.init());
EXPECT_EQ(arena.getUsed(), 0);
}
TEST(Arena, OutOfMemory) TEST(Arena, OutOfMemory)
{ {
mimir::init(); mimir::init();