(MemoryManager) Added allocation logging

Former-commit-id: 6f7d5ae28b8a4bfecfc2dca5010524a3f2c88aaf
This commit is contained in:
Lynix 2015-02-03 14:43:58 +01:00
parent 0a324d9323
commit 29b61ec958
2 changed files with 30 additions and 0 deletions

View File

@ -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:

View File

@ -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<nzUInt8*>(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;