Core/MemoryManager: Implement allocation filling

Former-commit-id: 12e6c293e6d51663971e3da4c160b12a6710430e
This commit is contained in:
Lynix 2015-06-28 16:03:31 +02:00
parent 318da0d960
commit 9c1441cc82
2 changed files with 25 additions and 0 deletions

View File

@ -16,6 +16,7 @@ class NAZARA_CORE_API NzMemoryManager
public: public:
static void* Allocate(std::size_t size, bool multi = false, const char* file = nullptr, unsigned int line = 0); 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 EnableAllocationLogging(bool logAllocations);
static void Free(void* pointer, bool multi = false); static void Free(void* pointer, bool multi = false);
@ -24,6 +25,7 @@ class NAZARA_CORE_API NzMemoryManager
static std::size_t GetAllocatedSize(); static std::size_t GetAllocatedSize();
static unsigned int GetAllocationCount(); static unsigned int GetAllocationCount();
static bool IsAllocationFillingEnabled();
static bool IsAllocationLoggingEnabled(); static bool IsAllocationLoggingEnabled();
static void NextFree(const char* file, unsigned int line); static void NextFree(const char* file, unsigned int line);

View File

@ -32,6 +32,7 @@ namespace
unsigned int magic; unsigned int magic;
}; };
bool s_allocationFilling = true;
bool s_allocationLogging = false; bool s_allocationLogging = false;
bool s_initialized = false; bool s_initialized = false;
const char* s_logFileName = "NazaraMemory.log"; 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_allocatedSize += size;
s_allocationCount++; s_allocationCount++;
if (s_allocationFilling)
{
nzUInt8* data = reinterpret_cast<nzUInt8*>(ptr) + sizeof(Block);
std::memset(data, 0xFF, size);
}
if (s_allocationLogging) if (s_allocationLogging)
{ {
char timeStr[23]; char timeStr[23];
@ -138,6 +145,11 @@ void* NzMemoryManager::Allocate(std::size_t size, bool multi, const char* file,
return reinterpret_cast<nzUInt8*>(ptr) + sizeof(Block); return reinterpret_cast<nzUInt8*>(ptr) + sizeof(Block);
} }
void NzMemoryManager::EnableAllocationFilling(bool allocationFilling)
{
s_allocationFilling = allocationFilling;
}
void NzMemoryManager::EnableAllocationLogging(bool logAllocations) void NzMemoryManager::EnableAllocationLogging(bool logAllocations)
{ {
s_allocationLogging = logAllocations; s_allocationLogging = logAllocations;
@ -195,6 +207,12 @@ void NzMemoryManager::Free(void* pointer, bool multi)
s_allocatedBlock--; s_allocatedBlock--;
s_allocatedSize -= ptr->size; s_allocatedSize -= ptr->size;
if (s_allocationFilling)
{
nzUInt8* data = reinterpret_cast<nzUInt8*>(ptr) + sizeof(Block);
std::memset(data, 0xFF, ptr->size);
}
std::free(ptr); std::free(ptr);
s_nextFreeFile = nullptr; s_nextFreeFile = nullptr;
@ -222,6 +240,11 @@ unsigned int NzMemoryManager::GetAllocationCount()
return s_allocationCount; return s_allocationCount;
} }
bool NzMemoryManager::IsAllocationFillingEnabled()
{
return s_allocationFilling;
}
bool NzMemoryManager::IsAllocationLoggingEnabled() bool NzMemoryManager::IsAllocationLoggingEnabled()
{ {
return s_allocationLogging; return s_allocationLogging;