From 49c4ae482ef5843992ec6138faebde8d55ceac15 Mon Sep 17 00:00:00 2001 From: Kearwood Gilbert Date: Sun, 27 Sep 2026 22:17:59 -0700 Subject: [PATCH] Implement Heap::reset() --- include/heap.h | 3 +++ src/heap.cpp | 31 +++++++++++++++++++------------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/include/heap.h b/include/heap.h index c7e7f04..891bc2a 100644 --- a/include/heap.h +++ b/include/heap.h @@ -60,6 +60,9 @@ public: // Free the allocation at `address` void free(std::byte* address); + // Reset the heap, freeing all memory and potentially releasing comitted pages. + void reset(); + // Get the actual used size. // This may differ from the sum of allocations due to alignment requirements. size_t getUsed() const; diff --git a/src/heap.cpp b/src/heap.cpp index 4019280..a61e273 100644 --- a/src/heap.cpp +++ b/src/heap.cpp @@ -118,18 +118,7 @@ bool Heap::init(size_t minSize, size_t maxSize) if (!m_region.resize(m_minSize)) { return false; } - TLSFIndex* index = (TLSFIndex*)m_region.getAddress(); - memset(index, 0, sizeof(TLSFIndex)); - - // Start with one free block, filling the entire Region - TLSFBlock* block = (TLSFBlock*)(m_region.getAddress() + sizeof(TLSFIndex)); - block->prevPhys = nullptr; - block->nextFree = nullptr; - block->prevFree = nullptr; - block->size = m_region.getMaxSize(); - block->size |= 0b10; // T=1: Last Block, F=1: Free Block - - insertFreeBlock(block); + reset(); return true; } @@ -330,4 +319,22 @@ void Heap::free(std::byte* address) { } +// Reset (or initialize) the heap, freeing all memory and potentially releasing comitted pages. +void Heap::reset() +{ + // Clear the index, representing no free blocks + TLSFIndex* index = (TLSFIndex*)m_region.getAddress(); + memset(index, 0, sizeof(TLSFIndex)); + + // Start with one free block, filling the entire Region + TLSFBlock* block = (TLSFBlock*)(m_region.getAddress() + sizeof(TLSFIndex)); + block->prevPhys = nullptr; + block->nextFree = nullptr; + block->prevFree = nullptr; + block->size = m_region.getMaxSize(); + block->size |= 0b10; // T=1: Last Block, F=1: Free Block + + insertFreeBlock(block); +} + } // namespace mimir