diff --git a/include/Nazara/Core/MemoryManager.hpp b/include/Nazara/Core/MemoryManager.hpp index a9481468d..a3d2d7774 100644 --- a/include/Nazara/Core/MemoryManager.hpp +++ b/include/Nazara/Core/MemoryManager.hpp @@ -16,12 +16,16 @@ class NAZARA_API NzMemoryManager public: static void* Allocate(std::size_t size, bool multi = false, const char* file = nullptr, unsigned int line = 0); + static void EnableAllocationLogging(bool logAllocations); + static void Free(void* pointer, bool multi = false); static unsigned int GetAllocatedBlockCount(); static std::size_t GetAllocatedSize(); static unsigned int GetAllocationCount(); + static bool IsAllocationLoggingEnabled(); + static void NextFree(const char* file, unsigned int line); private: diff --git a/src/Nazara/Core/MemoryManager.cpp b/src/Nazara/Core/MemoryManager.cpp index a91133234..a8cafac72 100644 --- a/src/Nazara/Core/MemoryManager.cpp +++ b/src/Nazara/Core/MemoryManager.cpp @@ -29,6 +29,7 @@ namespace unsigned int magic; }; + bool s_allocationLogging = false; bool s_initialized = false; const unsigned int s_magic = 0x51429EE; const char* s_MLTFileName = "NazaraLeaks.log"; @@ -111,6 +112,21 @@ void* NzMemoryManager::Allocate(std::size_t size, bool multi, const char* file, s_allocatedSize += size; s_allocationCount++; + if (s_allocationLogging) + { + char timeStr[23]; + TimeInfo(timeStr); + + FILE* log = std::fopen(s_MLTFileName, "a"); + + if (file) + std::fprintf(log, "%s Allocated %zu bytes at %s:%u\n", timeStr, size, file, line); + else + std::fprintf(log, "%s Allocated %zu bytes at unknown position\n", timeStr, size); + + std::fclose(log); + } + #if defined(NAZARA_PLATFORM_WINDOWS) LeaveCriticalSection(&s_mutex); #elif defined(NAZARA_PLATFORM_POSIX) @@ -120,6 +136,11 @@ void* NzMemoryManager::Allocate(std::size_t size, bool multi, const char* file, return reinterpret_cast(ptr) + sizeof(Block); } +void NzMemoryManager::EnableAllocationLogging(bool logAllocations) +{ + s_allocationLogging = logAllocations; +} + void NzMemoryManager::Free(void* pointer, bool multi) { if (!pointer) @@ -185,6 +206,11 @@ unsigned int NzMemoryManager::GetAllocationCount() return s_allocationCount; } +bool NzMemoryManager::IsAllocationLoggingEnabled() +{ + return s_allocationLogging; +} + void NzMemoryManager::NextFree(const char* file, unsigned int line) { s_nextFreeFile = file;