This commit is contained in:
Ilusha 2024-03-15 23:42:10 +03:00
parent 00d8fa0886
commit 65cb26a627
34 changed files with 288 additions and 264 deletions

View file

@ -13,9 +13,11 @@ static bool init(const tp::ModuleManifest* self) {
return true;
}
static void deinit(const tp::ModuleManifest* self) { tp::HeapAllocGlobal::checkLeaks(); }
static void deinit(const tp::ModuleManifest* self) {
tp::HeapAllocGlobal::checkLeaks();
}
static tp::ModuleManifest* sModuleDependencies[] = { &tp::gModuleUtils, nullptr };
static tp::ModuleManifest* sModuleDependencies[] = { nullptr };
tp::ModuleManifest tp::gModuleAllocators = ModuleManifest("Allocators", init, deinit, sModuleDependencies);
void* operator new(size_t aSize) { return tp::HeapAllocGlobal::allocate(aSize); }

View file

@ -3,7 +3,7 @@
#include "HeapAllocatorGlobal.hpp"
#include "PrivateConfig.hpp"
#include "Debugging.hpp"
#include "Callstack.hpp"
#include "Utils.hpp"
#include <cstdio>
@ -33,7 +33,11 @@ tp::ualni tp::HeapAllocGlobal::mNumAllocations = 0;
std::mutex tp::HeapAllocGlobal::mMutex;
bool tp::HeapAllocGlobal::mIgnore = true;
// ----------------------- Debug Implementation ---------------------------- //
#ifdef MEM_STACK_TRACE
tp::CallStackCapture tp::HeapAllocGlobal::mCallstack;
#endif
// ----------------------- Debug Implementation ---------------------------- //
// |----------------|
// | MemHead |
// |----------------|
@ -105,8 +109,7 @@ ALLOCATE:
// 3) Trace the stack
#ifdef MEM_STACK_TRACE
// check if somewhat decides to call new within static variable initialization
if (gCSCapture) head->mCallStack = gCSCapture->getSnapshot();
else head->mCallStack = nullptr;
head->mCallStack = mCallstack.getSnapshot();
#endif
mMutex.unlock();
@ -146,12 +149,16 @@ void HeapAllocGlobal::deallocate(void* aPtr) {
if (!head->mIgnored) {
// 3) Check the wrap
if (memCompareVal(wrap_top, WRAP_SIZE, WRAP_VAL)) {
CallStackCapture::printSnapshot(head->mCallStack);
#ifdef MEM_STACK_TRACE
mCallstack.printSnapshot(head->mCallStack);
#endif
ASSERT(!"Allocated Block Wrap Corrupted!")
}
if (memCompareVal(wrap_bottom, WRAP_SIZE, WRAP_VAL)) {
CallStackCapture::printSnapshot(head->mCallStack);
#ifdef MEM_STACK_TRACE
mCallstack.printSnapshot(head->mCallStack);
#endif
ASSERT(!"Allocated Block Wrap Corrupted!")
}
@ -176,7 +183,7 @@ bool HeapAllocGlobal::checkLeaks() {
#ifdef MEM_STACK_TRACE
for (auto iter = mEntry; iter; iter = iter->mPrev) {
if (!iter->mIgnored) CallStackCapture::printSnapshot(iter->mCallStack);
if (!iter->mIgnored) mCallstack.printSnapshot(iter->mCallStack);
}
#endif