Implement Heap::reset()
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
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
This commit is contained in:
@@ -60,6 +60,9 @@ public:
|
|||||||
// Free the allocation at `address`
|
// Free the allocation at `address`
|
||||||
void free(std::byte* address);
|
void free(std::byte* address);
|
||||||
|
|
||||||
|
// Reset the heap, freeing all memory and potentially releasing comitted pages.
|
||||||
|
void reset();
|
||||||
|
|
||||||
// Get the actual used size.
|
// Get the actual used size.
|
||||||
// This may differ from the sum of allocations due to alignment requirements.
|
// This may differ from the sum of allocations due to alignment requirements.
|
||||||
size_t getUsed() const;
|
size_t getUsed() const;
|
||||||
|
|||||||
31
src/heap.cpp
31
src/heap.cpp
@@ -118,18 +118,7 @@ bool Heap::init(size_t minSize, size_t maxSize)
|
|||||||
if (!m_region.resize(m_minSize)) {
|
if (!m_region.resize(m_minSize)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
TLSFIndex* index = (TLSFIndex*)m_region.getAddress();
|
reset();
|
||||||
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);
|
|
||||||
return true;
|
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
|
} // namespace mimir
|
||||||
|
|||||||
Reference in New Issue
Block a user