Updated MemoryManager
Renamed MemoryLeakTracker[.hpp|.cpp] to MemoryManager[.hpp|.cpp] Added MemoryManager::GetAllocatedBlockCount() Added MemoryManager::GetAllocatedSize() Optimized MemoryManager (no longer allocates a time buffer) Former-commit-id: 0e9c720cbb48fbed7006db3bd1000ebbc22c9583
This commit is contained in:
@@ -3,11 +3,9 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/Config.hpp>
|
||||
#if NAZARA_CORE_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG)
|
||||
|
||||
#define NAZARA_DEBUG_MEMORYLEAKTRACKER_DISABLE_REDEFINITION
|
||||
|
||||
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp>
|
||||
#if NAZARA_CORE_MEMORYMANAGER || defined(NAZARA_DEBUG)
|
||||
#define NAZARA_DEBUG_MEMORYMANAGER_DISABLE_REDEFINITION
|
||||
#include <Nazara/Core/Debug/MemoryManager.hpp>
|
||||
#include <new>
|
||||
|
||||
void* operator new(std::size_t size)
|
||||
|
||||
@@ -2,30 +2,20 @@
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#define NAZARA_DEBUG_MEMORYLEAKTRACKER_DISABLE_REDEFINITION
|
||||
#define NAZARA_DEBUG_MEMORYMANAGER_DISABLE_REDEFINITION
|
||||
|
||||
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp>
|
||||
#include <Nazara/Core/Debug/MemoryManager.hpp>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <stdexcept>
|
||||
|
||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||
#include <windows.h>
|
||||
#include <windows.h>
|
||||
#elif defined(NAZARA_PLATFORM_POSIX)
|
||||
#include <pthread.h>
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
// C'est malheureux mais le spécificateur %z de (f)printf n'est pas supporté partout
|
||||
#ifdef NAZARA_COMPILER_MINGW
|
||||
#ifdef NAZARA_PLATFORM_x64
|
||||
#define SIZE_T_SPECIFIER "%llu"
|
||||
#else
|
||||
#define SIZE_T_SPECIFIER "%u"
|
||||
#endif
|
||||
#else
|
||||
#define SIZE_T_SPECIFIER "%zu" // Standard
|
||||
#endif
|
||||
// Le seul fichier n'ayant pas à inclure Debug.hpp
|
||||
|
||||
namespace
|
||||
@@ -41,27 +31,29 @@ namespace
|
||||
unsigned int magic;
|
||||
};
|
||||
|
||||
bool initialized = false;
|
||||
const unsigned int magic = 0x51429EE;
|
||||
const char* MLTFileName = "NazaraLeaks.log";
|
||||
const char* nextFreeFile = "(Internal error)";
|
||||
unsigned int nextFreeLine = 0;
|
||||
bool s_initialized = false;
|
||||
const unsigned int s_magic = 0x51429EE;
|
||||
const char* s_MLTFileName = "NazaraLeaks.log";
|
||||
const char* s_nextFreeFile = "(Internal error)";
|
||||
unsigned int s_nextFreeLine = 0;
|
||||
|
||||
Block ptrList =
|
||||
Block s_list =
|
||||
{
|
||||
0,
|
||||
nullptr,
|
||||
&ptrList,
|
||||
&ptrList,
|
||||
&s_list,
|
||||
&s_list,
|
||||
false,
|
||||
0,
|
||||
magic
|
||||
s_magic
|
||||
};
|
||||
unsigned int s_allocatedBlock = 0;
|
||||
std::size_t s_allocatedSize = 0;
|
||||
|
||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||
CRITICAL_SECTION mutex;
|
||||
CRITICAL_SECTION s_mutex;
|
||||
#elif defined(NAZARA_PLATFORM_POSIX)
|
||||
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_mutex_t s_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -76,21 +68,21 @@ NzMemoryManager::~NzMemoryManager()
|
||||
|
||||
void* NzMemoryManager::Allocate(std::size_t size, bool multi, const char* file, unsigned int line)
|
||||
{
|
||||
if (!initialized)
|
||||
if (!s_initialized)
|
||||
Initialize();
|
||||
|
||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||
EnterCriticalSection(&mutex);
|
||||
EnterCriticalSection(&s_mutex);
|
||||
#elif defined(NAZARA_PLATFORM_POSIX)
|
||||
pthread_mutex_lock(&mutex);
|
||||
pthread_mutex_lock(&s_mutex);
|
||||
#endif
|
||||
|
||||
Block* ptr = reinterpret_cast<Block*>(std::malloc(size+sizeof(Block)));
|
||||
if (!ptr)
|
||||
{
|
||||
// Pas d'information de temps (Car nécessitant une allocation)
|
||||
FILE* log = std::fopen(MLTFileName, "a");
|
||||
std::fprintf(log, "Failed to allocate memory (" SIZE_T_SPECIFIER " bytes)\n", size);
|
||||
FILE* log = std::fopen(s_MLTFileName, "a");
|
||||
std::fprintf(log, "Failed to allocate memory (%zu bytes)\n", size);
|
||||
std::fclose(log);
|
||||
|
||||
return nullptr; // Impossible d'envoyer une exception car cela allouerait de la mémoire avec new (boucle infinie)
|
||||
@@ -100,20 +92,23 @@ void* NzMemoryManager::Allocate(std::size_t size, bool multi, const char* file,
|
||||
ptr->file = file;
|
||||
ptr->line = line;
|
||||
ptr->size = size;
|
||||
ptr->magic = magic;
|
||||
ptr->magic = s_magic;
|
||||
|
||||
ptr->prev = ptrList.prev;
|
||||
ptr->next = &ptrList;
|
||||
ptrList.prev->next = ptr;
|
||||
ptrList.prev = ptr;
|
||||
ptr->prev = s_list.prev;
|
||||
ptr->next = &s_list;
|
||||
s_list.prev->next = ptr;
|
||||
s_list.prev = ptr;
|
||||
|
||||
s_allocatedBlock++;
|
||||
s_allocatedSize += size;
|
||||
|
||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||
LeaveCriticalSection(&mutex);
|
||||
LeaveCriticalSection(&s_mutex);
|
||||
#elif defined(NAZARA_PLATFORM_POSIX)
|
||||
pthread_mutex_unlock(&mutex);
|
||||
pthread_mutex_unlock(&s_mutex);
|
||||
#endif
|
||||
|
||||
return reinterpret_cast<char*>(ptr)+sizeof(Block);
|
||||
return reinterpret_cast<nzUInt8*>(ptr) + sizeof(Block);
|
||||
}
|
||||
|
||||
void NzMemoryManager::Free(void* pointer, bool multi)
|
||||
@@ -121,144 +116,148 @@ void NzMemoryManager::Free(void* pointer, bool multi)
|
||||
if (!pointer)
|
||||
return;
|
||||
|
||||
Block* ptr = reinterpret_cast<Block*>(reinterpret_cast<char*>(pointer)-sizeof(Block));
|
||||
if (ptr->magic != magic)
|
||||
Block* ptr = reinterpret_cast<Block*>(reinterpret_cast<nzUInt8*>(pointer) - sizeof(Block));
|
||||
if (ptr->magic != s_magic)
|
||||
return;
|
||||
|
||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||
EnterCriticalSection(&mutex);
|
||||
EnterCriticalSection(&s_mutex);
|
||||
#elif defined(NAZARA_PLATFORM_POSIX)
|
||||
pthread_mutex_lock(&mutex);
|
||||
pthread_mutex_lock(&s_mutex);
|
||||
#endif
|
||||
|
||||
if (ptr->array != multi)
|
||||
{
|
||||
char* time = TimeInfo();
|
||||
char timeStr[23];
|
||||
TimeInfo(timeStr);
|
||||
|
||||
FILE* log = std::fopen(MLTFileName, "a");
|
||||
FILE* log = std::fopen(s_MLTFileName, "a");
|
||||
|
||||
if (nextFreeFile)
|
||||
if (s_nextFreeFile)
|
||||
{
|
||||
if (multi)
|
||||
std::fprintf(log, "%s Warning: delete[] after new at %s:%u\n", time, nextFreeFile, nextFreeLine);
|
||||
std::fprintf(log, "%s Warning: delete[] after new at %s:%u\n", timeStr, s_nextFreeFile, s_nextFreeLine);
|
||||
else
|
||||
std::fprintf(log, "%s Warning: delete after new[] at %s:%u\n", time, nextFreeFile, nextFreeLine);
|
||||
std::fprintf(log, "%s Warning: delete after new[] at %s:%u\n", timeStr, s_nextFreeFile, s_nextFreeLine);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (multi)
|
||||
std::fprintf(log, "%s Warning: delete[] after new at unknown position\n", time);
|
||||
std::fprintf(log, "%s Warning: delete[] after new at unknown position\n", timeStr);
|
||||
else
|
||||
std::fprintf(log, "%s Warning: delete after new[] at unknown position\n", time);
|
||||
std::fprintf(log, "%s Warning: delete after new[] at unknown position\n", timeStr);
|
||||
}
|
||||
|
||||
std::fclose(log);
|
||||
|
||||
std::free(time);
|
||||
}
|
||||
|
||||
ptr->magic = 0;
|
||||
ptr->magic = 0; // Évitons des problèmes
|
||||
ptr->prev->next = ptr->next;
|
||||
ptr->next->prev = ptr->prev;
|
||||
|
||||
s_allocatedBlock--;
|
||||
s_allocatedSize -= ptr->size;
|
||||
|
||||
std::free(ptr);
|
||||
|
||||
nextFreeFile = nullptr;
|
||||
nextFreeLine = 0;
|
||||
s_nextFreeFile = nullptr;
|
||||
s_nextFreeLine = 0;
|
||||
|
||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||
LeaveCriticalSection(&mutex);
|
||||
LeaveCriticalSection(&s_mutex);
|
||||
#elif defined(NAZARA_PLATFORM_POSIX)
|
||||
pthread_mutex_unlock(&mutex);
|
||||
pthread_mutex_unlock(&s_mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
unsigned int NzMemoryManager::GetAllocatedBlockCount()
|
||||
{
|
||||
return s_allocatedBlock;
|
||||
}
|
||||
|
||||
std::size_t NzMemoryManager::GetAllocatedSize()
|
||||
{
|
||||
return s_allocatedSize;
|
||||
}
|
||||
|
||||
void NzMemoryManager::NextFree(const char* file, unsigned int line)
|
||||
{
|
||||
nextFreeFile = file;
|
||||
nextFreeLine = line;
|
||||
s_nextFreeFile = file;
|
||||
s_nextFreeLine = line;
|
||||
}
|
||||
|
||||
void NzMemoryManager::Initialize()
|
||||
{
|
||||
char* time = TimeInfo();
|
||||
char timeStr[23];
|
||||
TimeInfo(timeStr);
|
||||
|
||||
FILE* file = std::fopen(MLTFileName, "w");
|
||||
std::fprintf(file, "%s ==============================\n", time);
|
||||
std::fprintf(file, "%s Nazara Memory Leak Tracker \n", time);
|
||||
std::fprintf(file, "%s ==============================\n", time);
|
||||
FILE* file = std::fopen(s_MLTFileName, "w");
|
||||
std::fprintf(file, "%s ==============================\n", timeStr);
|
||||
std::fprintf(file, "%s Nazara Memory Leak Tracker \n", timeStr);
|
||||
std::fprintf(file, "%s ==============================\n", timeStr);
|
||||
std::fclose(file);
|
||||
|
||||
std::free(time);
|
||||
|
||||
if (std::atexit(Uninitialize) != 0)
|
||||
{
|
||||
static NzMemoryManager manager;
|
||||
}
|
||||
|
||||
#ifdef NAZARA_PLATFORM_WINDOWS
|
||||
InitializeCriticalSection(&mutex);
|
||||
InitializeCriticalSection(&s_mutex);
|
||||
#endif
|
||||
|
||||
initialized = true;
|
||||
s_initialized = true;
|
||||
}
|
||||
|
||||
char* NzMemoryManager::TimeInfo()
|
||||
void NzMemoryManager::TimeInfo(char buffer[23])
|
||||
{
|
||||
char* buffer = reinterpret_cast<char*>(std::malloc(23*sizeof(char)));
|
||||
|
||||
time_t currentTime = std::time(nullptr);
|
||||
std::strftime(buffer, 23, "%d/%m/%Y - %H:%M:%S:", std::localtime(¤tTime));
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void NzMemoryManager::Uninitialize()
|
||||
{
|
||||
#ifdef NAZARA_PLATFORM_WINDOWS
|
||||
DeleteCriticalSection(&mutex);
|
||||
DeleteCriticalSection(&s_mutex);
|
||||
#endif
|
||||
|
||||
FILE* log = std::fopen(MLTFileName, "a");
|
||||
FILE* log = std::fopen(s_MLTFileName, "a");
|
||||
|
||||
char* time = TimeInfo();
|
||||
char timeStr[23];
|
||||
TimeInfo(timeStr);
|
||||
|
||||
std::fprintf(log, "%s Application finished, checking leaks...\n", time);
|
||||
std::fprintf(log, "%s Application finished, checking leaks...\n", timeStr);
|
||||
|
||||
if (ptrList.next == &ptrList)
|
||||
if (s_allocatedBlock == 0)
|
||||
{
|
||||
std::fprintf(log, "%s ==============================\n", time);
|
||||
std::fprintf(log, "%s No leak detected \n", time);
|
||||
std::fprintf(log, "%s ==============================", time);
|
||||
std::fprintf(log, "%s ==============================\n", timeStr);
|
||||
std::fprintf(log, "%s No leak detected \n", timeStr);
|
||||
std::fprintf(log, "%s ==============================", timeStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::fprintf(log, "%s ==============================\n", time);
|
||||
std::fprintf(log, "%s Leaks have been detected \n", time);
|
||||
std::fprintf(log, "%s ==============================\n\n", time);
|
||||
std::fprintf(log, "%s ==============================\n", timeStr);
|
||||
std::fprintf(log, "%s Leaks have been detected \n", timeStr);
|
||||
std::fprintf(log, "%s ==============================\n\n", timeStr);
|
||||
std::fputs("Leak list:\n", log);
|
||||
|
||||
std::size_t totalSize = 0;
|
||||
Block* ptr = ptrList.next;
|
||||
unsigned int count = 0;
|
||||
while (ptr != &ptrList)
|
||||
Block* ptr = s_list.next;
|
||||
while (ptr != &s_list)
|
||||
{
|
||||
count++;
|
||||
totalSize += ptr->size;
|
||||
if (ptr->file)
|
||||
std::fprintf(log, "-0x%p -> " SIZE_T_SPECIFIER " bytes allocated at %s:%u\n", reinterpret_cast<char*>(ptr)+sizeof(Block), ptr->size, ptr->file, ptr->line);
|
||||
std::fprintf(log, "-0x%p -> %zu bytes allocated at %s:%u\n", reinterpret_cast<nzUInt8*>(ptr) + sizeof(Block), ptr->size, ptr->file, ptr->line);
|
||||
else
|
||||
std::fprintf(log, "-0x%p -> " SIZE_T_SPECIFIER " bytes allocated at unknown position\n", reinterpret_cast<char*>(ptr)+sizeof(Block), ptr->size);
|
||||
std::fprintf(log, "-0x%p -> %zu bytes allocated at unknown position\n", reinterpret_cast<nzUInt8*>(ptr) + sizeof(Block), ptr->size);
|
||||
|
||||
void* pointer = ptr;
|
||||
ptr = ptr->next;
|
||||
|
||||
std::free(pointer);
|
||||
}
|
||||
|
||||
std::fprintf(log, "\n%u blocks leaked (" SIZE_T_SPECIFIER " bytes)", count, totalSize);
|
||||
std::fprintf(log, "\n%u blocks leaked (%zu bytes)", s_allocatedBlock, s_allocatedSize);
|
||||
}
|
||||
|
||||
std::free(time);
|
||||
std::fclose(log);
|
||||
}
|
||||
|
||||
@@ -272,13 +271,13 @@ void* operator new[](std::size_t size, const char* file, unsigned int line)
|
||||
return NzMemoryManager::Allocate(size, true, file, line);
|
||||
}
|
||||
|
||||
void operator delete(void* ptr, const char* file, unsigned int line) throw()
|
||||
void operator delete(void* ptr, const char* file, unsigned int line) noexcept
|
||||
{
|
||||
NzMemoryManager::NextFree(file, line);
|
||||
NzMemoryManager::Free(ptr, false);
|
||||
}
|
||||
|
||||
void operator delete[](void* ptr, const char* file, unsigned int line) throw()
|
||||
void operator delete[](void* ptr, const char* file, unsigned int line) noexcept
|
||||
{
|
||||
NzMemoryManager::NextFree(file, line);
|
||||
NzMemoryManager::Free(ptr, true);
|
||||
Reference in New Issue
Block a user