diff --git a/include/Nazara/Core/MemoryManager.hpp b/include/Nazara/Core/MemoryManager.hpp index 23e9982d9..fb17539e9 100644 --- a/include/Nazara/Core/MemoryManager.hpp +++ b/include/Nazara/Core/MemoryManager.hpp @@ -16,6 +16,7 @@ class NAZARA_CORE_API NzMemoryManager public: static void* Allocate(std::size_t size, bool multi = false, const char* file = nullptr, unsigned int line = 0); + static void EnableAllocationFilling(bool allocationFilling); static void EnableAllocationLogging(bool logAllocations); static void Free(void* pointer, bool multi = false); @@ -24,6 +25,7 @@ class NAZARA_CORE_API NzMemoryManager static std::size_t GetAllocatedSize(); static unsigned int GetAllocationCount(); + static bool IsAllocationFillingEnabled(); static bool IsAllocationLoggingEnabled(); static void NextFree(const char* file, unsigned int line); diff --git a/src/Nazara/Core/MemoryManager.cpp b/src/Nazara/Core/MemoryManager.cpp index 9820d24b6..353f2c5bb 100644 --- a/src/Nazara/Core/MemoryManager.cpp +++ b/src/Nazara/Core/MemoryManager.cpp @@ -32,6 +32,7 @@ namespace unsigned int magic; }; + bool s_allocationFilling = true; bool s_allocationLogging = false; bool s_initialized = false; const char* s_logFileName = "NazaraMemory.log"; @@ -114,6 +115,12 @@ void* NzMemoryManager::Allocate(std::size_t size, bool multi, const char* file, s_allocatedSize += size; s_allocationCount++; + if (s_allocationFilling) + { + nzUInt8* data = reinterpret_cast(ptr) + sizeof(Block); + std::memset(data, 0xFF, size); + } + if (s_allocationLogging) { char timeStr[23]; @@ -138,6 +145,11 @@ void* NzMemoryManager::Allocate(std::size_t size, bool multi, const char* file, return reinterpret_cast(ptr) + sizeof(Block); } +void NzMemoryManager::EnableAllocationFilling(bool allocationFilling) +{ + s_allocationFilling = allocationFilling; +} + void NzMemoryManager::EnableAllocationLogging(bool logAllocations) { s_allocationLogging = logAllocations; @@ -195,6 +207,12 @@ void NzMemoryManager::Free(void* pointer, bool multi) s_allocatedBlock--; s_allocatedSize -= ptr->size; + if (s_allocationFilling) + { + nzUInt8* data = reinterpret_cast(ptr) + sizeof(Block); + std::memset(data, 0xFF, ptr->size); + } + std::free(ptr); s_nextFreeFile = nullptr; @@ -222,6 +240,11 @@ unsigned int NzMemoryManager::GetAllocationCount() return s_allocationCount; } +bool NzMemoryManager::IsAllocationFillingEnabled() +{ + return s_allocationFilling; +} + bool NzMemoryManager::IsAllocationLoggingEnabled() { return s_allocationLogging;