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

@ -5,11 +5,11 @@ file(GLOB SOURCES "./private/*.cpp")
file(GLOB HEADERS "./public/*.hpp")
add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS})
target_include_directories(${PROJECT_NAME} PUBLIC ./public/)
target_link_libraries(${PROJECT_NAME} PUBLIC Utils)
target_link_libraries(${PROJECT_NAME} PUBLIC Callstack)
### -------------------------- Tests -------------------------- ###
enable_testing()
file(GLOB TEST_SOURCES "./tests/*.cpp")
add_executable(${PROJECT_NAME}Tests ${TEST_SOURCES})
target_link_libraries(${PROJECT_NAME}Tests ${PROJECT_NAME} UnitTest++)
add_test(NAME ${PROJECT_NAME}Tests COMMAND ${PROJECT_NAME}Tests)
add_executable(Tests${PROJECT_NAME} ${TEST_SOURCES})
target_link_libraries(Tests${PROJECT_NAME} ${PROJECT_NAME} UnitTest++)
add_test(NAME Tests${PROJECT_NAME} COMMAND Tests${PROJECT_NAME})

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

View file

@ -1,6 +1,6 @@
#pragma once
#include "Module.hpp"
#include "Callstack.hpp"
#include <mutex>
namespace tp {
@ -12,6 +12,9 @@ namespace tp {
static struct MemHead* mEntry;
static std::mutex mMutex;
static bool mIgnore;
#ifdef MEM_STACK_TRACE // Save stack on allocation call
static CallStackCapture mCallstack;
#endif
#endif
public:

View file

@ -6,5 +6,4 @@
#define MEM_CLEAR_ON_DEALLOC // Clear data on free
#define MEM_CLEAR_ON_DEALLOC_VAL 0xAA // Clear data on free
#define MEM_CLEAR_ON_ALLOC_VAL 0xCC // Clear data on free
#define MEM_STACK_TRACE // Save stack on allocation call
#define MEM_STACK_TRACE_MAX_DEPTH 32 // Call stack max depth

View file

@ -234,7 +234,7 @@ SUITE(Allocators) {
int main() {
tp::ModuleManifest* deps[] = { &tp::gModuleAllocators, &tp::gModuleUtils, nullptr };
tp::ModuleManifest* deps[] = { &tp::gModuleAllocators, nullptr };
tp::ModuleManifest testModule("AllocatorsTest", nullptr, nullptr, deps);
if (!testModule.initialize()) {