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:
Lynix 2014-04-21 22:13:31 +02:00
parent 77fc34ee7d
commit c606599e19
40 changed files with 179 additions and 190 deletions

View File

@ -29,8 +29,8 @@
/// Chaque modification d'un paramètre du module nécessite une recompilation de celui-ci /// Chaque modification d'un paramètre du module nécessite une recompilation de celui-ci
// Utilise un tracker pour repérer les éventuels leaks (Ralentit l'exécution) // Utilise un manager de mémoire pour gérer les allocations dynamiques (détecte les leaks, ralenti l'exécution)
#define NAZARA_MODULENAME_MEMORYLEAKTRACKER 0 #define NAZARA_MODULENAME_MEMORYMANAGER 0
// Active les tests de sécurité basés sur le code (Conseillé pour le développement) // Active les tests de sécurité basés sur le code (Conseillé pour le développement)
#define NAZARA_MODULENAME_SAFE 1 #define NAZARA_MODULENAME_SAFE 1

View File

@ -3,7 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/ModuleName/Config.hpp> #include <Nazara/ModuleName/Config.hpp>
#if NAZARA_MODULENAME_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_MODULENAME_MEMORYMANAGER || defined(NAZARA_DEBUG)
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp> #include <Nazara/Core/Debug/MemoryLeakTracker.hpp>
#define delete NzMemoryManager::NextFree(__FILE__, __LINE__), delete #define delete NzMemoryManager::NextFree(__FILE__, __LINE__), delete

View File

@ -2,7 +2,7 @@
// This file is part of the "Nazara Engine - Module name" // This file is part of the "Nazara Engine - Module name"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#if NAZARA_MODULENAME_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_MODULENAME_MEMORYMANAGER || defined(NAZARA_DEBUG)
#undef delete #undef delete
#undef new #undef new
#endif #endif

View File

@ -3,7 +3,8 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/ModuleName/Config.hpp> #include <Nazara/ModuleName/Config.hpp>
#if NAZARA_MODULENAME_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_MODULENAME_MEMORYMANAGER || defined(NAZARA_DEBUG)
#define NAZARA_DEBUG_MEMORYMANAGER_DISABLE_REDEFINITION
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp> #include <Nazara/Core/Debug/MemoryLeakTracker.hpp>
#include <new> #include <new>

View File

@ -29,8 +29,8 @@
/// Chaque modification d'un paramètre du module nécessite une recompilation de celui-ci /// Chaque modification d'un paramètre du module nécessite une recompilation de celui-ci
// Utilise un tracker pour repérer les éventuels leaks (Ralentit l'exécution) // Utilise un manager de mémoire pour gérer les allocations dynamiques (détecte les leaks, ralenti l'exécution)
#define NAZARA_AUDIO_MEMORYLEAKTRACKER 0 #define NAZARA_AUDIO_MEMORYMANAGER 0
// Active les tests de sécurité basés sur le code (Conseillé pour le développement) // Active les tests de sécurité basés sur le code (Conseillé pour le développement)
#define NAZARA_AUDIO_SAFE 1 #define NAZARA_AUDIO_SAFE 1

View File

@ -3,6 +3,6 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Audio/Config.hpp> #include <Nazara/Audio/Config.hpp>
#if NAZARA_AUDIO_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_AUDIO_MEMORYMANAGER || defined(NAZARA_DEBUG)
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp> #include <Nazara/Core/Debug/MemoryManager.hpp>
#endif #endif

View File

@ -2,7 +2,7 @@
// This file is part of the "Nazara Engine - Audio module" // This file is part of the "Nazara Engine - Audio module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#if NAZARA_AUDIO_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_AUDIO_MEMORYMANAGER || defined(NAZARA_DEBUG)
#undef delete #undef delete
#undef new #undef new
#endif #endif

View File

@ -44,8 +44,8 @@
// Incorpore la table Unicode Character Data (Nécessaires pour faire fonctionner le flag NzString::HandleUTF8) // Incorpore la table Unicode Character Data (Nécessaires pour faire fonctionner le flag NzString::HandleUTF8)
#define NAZARA_CORE_INCLUDE_UNICODEDATA 0 #define NAZARA_CORE_INCLUDE_UNICODEDATA 0
// Utilise un tracker pour repérer les éventuels leaks (Ralentit l'exécution) // Utilise un manager de mémoire pour gérer les allocations dynamiques (détecte les leaks, ralenti l'exécution)
#define NAZARA_CORE_MEMORYLEAKTRACKER 0 #define NAZARA_CORE_MEMORYMANAGER 0
// Précision des réels lors de la transformation en texte (Max. chiffres après la virgule) // Précision des réels lors de la transformation en texte (Max. chiffres après la virgule)
#define NAZARA_CORE_REAL_PRECISION 6 #define NAZARA_CORE_REAL_PRECISION 6

View File

@ -3,6 +3,6 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Config.hpp> #include <Nazara/Core/Config.hpp>
#if NAZARA_CORE_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_CORE_MEMORYMANAGER || defined(NAZARA_DEBUG)
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp> #include <Nazara/Core/Debug/MemoryManager.hpp>
#endif #endif

View File

@ -2,10 +2,8 @@
// This file is part of the "Nazara Engine - Core module" // This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once #ifndef NAZARA_DEBUG_MEMORYMANAGER_HPP
#define NAZARA_DEBUG_MEMORYMANAGER_HPP
#ifndef NAZARA_DEBUG_MEMORYLEAKTRACKER_HPP
#define NAZARA_DEBUG_MEMORYLEAKTRACKER_HPP
#include <Nazara/Prerequesites.hpp> #include <Nazara/Prerequesites.hpp>
#include <cstdio> #include <cstdio>
@ -15,7 +13,12 @@ class NAZARA_API NzMemoryManager
{ {
public: public:
static void* Allocate(std::size_t size, bool multi, const char* file = nullptr, unsigned int line = 0); static void* Allocate(std::size_t size, bool multi, const char* file = nullptr, unsigned int line = 0);
static void Free(void* pointer, bool multi); static void Free(void* pointer, bool multi);
static unsigned int GetAllocatedBlockCount();
static std::size_t GetAllocatedSize();
static void NextFree(const char* file, unsigned int line); static void NextFree(const char* file, unsigned int line);
private: private:
@ -23,18 +26,18 @@ class NAZARA_API NzMemoryManager
~NzMemoryManager(); ~NzMemoryManager();
static void Initialize(); static void Initialize();
static char* TimeInfo(); static void TimeInfo(char buffer[23]);
static void Uninitialize(); static void Uninitialize();
}; };
NAZARA_API void* operator new(std::size_t size, const char* file, unsigned int line); NAZARA_API void* operator new(std::size_t size, const char* file, unsigned int line);
NAZARA_API void* operator new[](std::size_t size, const char* file, unsigned int line); NAZARA_API void* operator new[](std::size_t size, const char* file, unsigned int line);
NAZARA_API void operator delete(void* ptr, const char* file, unsigned int line) throw(); NAZARA_API void operator delete(void* ptr, const char* file, unsigned int line) noexcept;
NAZARA_API void operator delete[](void* ptr, const char* file, unsigned int line) throw(); NAZARA_API void operator delete[](void* ptr, const char* file, unsigned int line) noexcept;
#endif // NAZARA_DEBUG_MEMORYLEAKTRACKER_HPP #endif // NAZARA_DEBUG_MEMORYMANAGER_HPP
#ifndef NAZARA_DEBUG_MEMORYLEAKTRACKER_DISABLE_REDEFINITION #ifndef NAZARA_DEBUG_MEMORYMANAGER_DISABLE_REDEFINITION
#define delete NzMemoryManager::NextFree(__FILE__, __LINE__), delete #define delete NzMemoryManager::NextFree(__FILE__, __LINE__), delete
#define new new(__FILE__, __LINE__) #define new new(__FILE__, __LINE__)
#endif #endif

View File

@ -2,7 +2,7 @@
// This file is part of the "Nazara Engine - Core module" // This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#if NAZARA_CORE_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_CORE_MEMORYMANAGER || defined(NAZARA_DEBUG)
#undef delete #undef delete
#undef new #undef new
#endif #endif

View File

@ -32,8 +32,8 @@
// À partir de combien d'instances d'un même mesh/matériau l'instancing doit-il être utilisé ? // À partir de combien d'instances d'un même mesh/matériau l'instancing doit-il être utilisé ?
#define NAZARA_GRAPHICS_INSTANCING_MIN_INSTANCES_COUNT 10 #define NAZARA_GRAPHICS_INSTANCING_MIN_INSTANCES_COUNT 10
// Utilise un tracker pour repérer les éventuels leaks (Ralentit l'exécution) // Utilise un manager de mémoire pour gérer les allocations dynamiques (détecte les leaks, ralenti l'exécution)
#define NAZARA_GRAPHICS_MEMORYLEAKTRACKER 0 #define NAZARA_GRAPHICS_MEMORYMANAGER 0
// Active les tests de sécurité basés sur le code (Conseillé pour le développement) // Active les tests de sécurité basés sur le code (Conseillé pour le développement)
#define NAZARA_GRAPHICS_SAFE 1 #define NAZARA_GRAPHICS_SAFE 1

View File

@ -3,6 +3,6 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/Config.hpp> #include <Nazara/Graphics/Config.hpp>
#if NAZARA_GRAPHICS_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_GRAPHICS_MEMORYMANAGER || defined(NAZARA_DEBUG)
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp> #include <Nazara/Core/Debug/MemoryManager.hpp>
#endif #endif

View File

@ -2,7 +2,7 @@
// This file is part of the "Nazara Engine - Graphics module" // This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#if NAZARA_GRAPHICS_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_GRAPHICS_MEMORYMANAGER || defined(NAZARA_DEBUG)
#undef delete #undef delete
#undef new #undef new
#endif #endif

View File

@ -29,8 +29,8 @@
/// Chaque modification d'un paramètre du module nécessite une recompilation de celui-ci /// Chaque modification d'un paramètre du module nécessite une recompilation de celui-ci
// Utilise un tracker pour repérer les éventuels leaks (Ralentit l'exécution) // Utilise un manager de mémoire pour gérer les allocations dynamiques (détecte les leaks, ralenti l'exécution)
#define NAZARA_LUA_MEMORYLEAKTRACKER 0 #define NAZARA_LUA_MEMORYMANAGER 0
// Active les tests de sécurité basés sur le code (Conseillé pour le développement) // Active les tests de sécurité basés sur le code (Conseillé pour le développement)
#define NAZARA_LUA_SAFE 1 #define NAZARA_LUA_SAFE 1

View File

@ -3,6 +3,6 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Lua/Config.hpp> #include <Nazara/Lua/Config.hpp>
#if NAZARA_LUA_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_LUA_MEMORYMANAGER || defined(NAZARA_DEBUG)
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp> #include <Nazara/Core/Debug/MemoryManager.hpp>
#endif #endif

View File

@ -2,7 +2,7 @@
// This file is part of the "Nazara Engine - Lua scripting module" // This file is part of the "Nazara Engine - Lua scripting module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#if NAZARA_LUA_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_LUA_MEMORYMANAGER || defined(NAZARA_DEBUG)
#undef delete #undef delete
#undef new #undef new
#endif #endif

View File

@ -29,8 +29,8 @@
/// Chaque modification d'un paramètre du module nécessite une recompilation de celui-ci /// Chaque modification d'un paramètre du module nécessite une recompilation de celui-ci
// Utilise un tracker pour repérer les éventuels leaks (Ralentit l'exécution) // Utilise un manager de mémoire pour gérer les allocations dynamiques (détecte les leaks, ralenti l'exécution)
#define NAZARA_NOISE_MEMORYLEAKTRACKER 0 #define NAZARA_NOISE_MEMORYMANAGER 0
// Active les tests de sécurité basés sur le code (Conseillé pour le développement) // Active les tests de sécurité basés sur le code (Conseillé pour le développement)
#define NAZARA_NOISE_SAFE 1 #define NAZARA_NOISE_SAFE 1

View File

@ -3,6 +3,6 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Noise/Config.hpp> #include <Nazara/Noise/Config.hpp>
#if NAZARA_NOISE_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_NOISE_MEMORYMANAGER || defined(NAZARA_DEBUG)
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp> #include <Nazara/Core/Debug/MemoryManager.hpp>
#endif #endif

View File

@ -2,7 +2,7 @@
// This file is part of the "Nazara Engine - Noise module" // This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#if NAZARA_NOISE_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_NOISE_MEMORYMANAGER || defined(NAZARA_DEBUG)
#undef delete #undef delete
#undef new #undef new
#endif #endif

View File

@ -29,8 +29,8 @@
/// Chaque modification d'un paramètre du module nécessite une recompilation de celui-ci /// Chaque modification d'un paramètre du module nécessite une recompilation de celui-ci
// Utilise un tracker pour repérer les éventuels leaks (Ralentit l'exécution) // Utilise un manager de mémoire pour gérer les allocations dynamiques (détecte les leaks, ralenti l'exécution)
#define NAZARA_PHYSICS_MEMORYLEAKTRACKER 0 #define NAZARA_PHYSICS_MEMORYMANAGER 0
// Active les tests de sécurité basés sur le code (Conseillé pour le développement) // Active les tests de sécurité basés sur le code (Conseillé pour le développement)
#define NAZARA_PHYSICS_SAFE 1 #define NAZARA_PHYSICS_SAFE 1

View File

@ -3,6 +3,6 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Physics/Config.hpp> #include <Nazara/Physics/Config.hpp>
#if NAZARA_PHYSICS_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_PHYSICS_MEMORYMANAGER || defined(NAZARA_DEBUG)
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp> #include <Nazara/Core/Debug/MemoryManager.hpp>
#endif #endif

View File

@ -2,7 +2,7 @@
// This file is part of the "Nazara Engine - Physics module" // This file is part of the "Nazara Engine - Physics module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#if NAZARA_PHYSICS_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_PHYSICS_MEMORYMANAGER || defined(NAZARA_DEBUG)
#undef delete #undef delete
#undef new #undef new
#endif #endif

View File

@ -32,8 +32,8 @@
// La taille du buffer d'Instancing (définit le nombre maximum d'instances en un rendu) // La taille du buffer d'Instancing (définit le nombre maximum d'instances en un rendu)
#define NAZARA_RENDERER_INSTANCE_BUFFER_SIZE 8192*64 // 8192 matrices 4x4 flottantes #define NAZARA_RENDERER_INSTANCE_BUFFER_SIZE 8192*64 // 8192 matrices 4x4 flottantes
// Utilise un tracker pour repérer les éventuels leaks (Ralentit l'exécution) // Utilise un manager de mémoire pour gérer les allocations dynamiques (détecte les leaks, ralenti l'exécution)
#define NAZARA_RENDERER_MEMORYLEAKTRACKER 0 #define NAZARA_RENDERER_MEMORYMANAGER 0
// Active le paramère debug des paramètres des contextes par défaut (Perte de performances mais capable de recevoir des messages d'OpenGL) // Active le paramère debug des paramètres des contextes par défaut (Perte de performances mais capable de recevoir des messages d'OpenGL)
#define NAZARA_RENDERER_OPENGL_DEBUG 0 #define NAZARA_RENDERER_OPENGL_DEBUG 0

View File

@ -3,6 +3,6 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Renderer/Config.hpp> #include <Nazara/Renderer/Config.hpp>
#if NAZARA_RENDERER_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_RENDERER_MEMORYMANAGER || defined(NAZARA_DEBUG)
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp> #include <Nazara/Core/Debug/MemoryManager.hpp>
#endif #endif

View File

@ -2,7 +2,7 @@
// This file is part of the "Nazara Engine - Renderer module" // This file is part of the "Nazara Engine - Renderer module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#if NAZARA_RENDERER_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_RENDERER_MEMORYMANAGER || defined(NAZARA_DEBUG)
#undef delete #undef delete
#undef new #undef new
#endif #endif

View File

@ -32,8 +32,8 @@
// Force les buffers à posséder un stride multiple de 32 bytes (Gain de performances sur certaines cartes/plus de consommation mémoire) // Force les buffers à posséder un stride multiple de 32 bytes (Gain de performances sur certaines cartes/plus de consommation mémoire)
#define NAZARA_UTILITY_FORCE_DECLARATION_STRIDE_MULTIPLE_OF_32 0 ///FIXME: Ne peut pas être utilisé pour l'instant #define NAZARA_UTILITY_FORCE_DECLARATION_STRIDE_MULTIPLE_OF_32 0 ///FIXME: Ne peut pas être utilisé pour l'instant
// Utilise un tracker pour repérer les éventuels leaks (Ralentit l'exécution) // Utilise un manager de mémoire pour gérer les allocations dynamiques (détecte les leaks, ralenti l'exécution)
#define NAZARA_UTILITY_MEMORYLEAKTRACKER 0 #define NAZARA_UTILITY_MEMORYMANAGER 0
// Le skinning doit-il prendre avantage du multi-threading ? (Boost de performances sur les processeurs multi-coeurs) // Le skinning doit-il prendre avantage du multi-threading ? (Boost de performances sur les processeurs multi-coeurs)
#define NAZARA_UTILITY_MULTITHREADED_SKINNING 0 #define NAZARA_UTILITY_MULTITHREADED_SKINNING 0

View File

@ -3,6 +3,6 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Config.hpp> #include <Nazara/Utility/Config.hpp>
#if NAZARA_UTILITY_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_UTILITY_MEMORYMANAGER || defined(NAZARA_DEBUG)
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp> #include <Nazara/Core/Debug/MemoryManager.hpp>
#endif #endif

View File

@ -2,7 +2,7 @@
// This file is part of the "Nazara Engine - Utility module" // This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#if NAZARA_UTILITY_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_UTILITY_MEMORYMANAGER || defined(NAZARA_DEBUG)
#undef delete #undef delete
#undef new #undef new
#endif #endif

View File

@ -3,11 +3,9 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Audio/Config.hpp> #include <Nazara/Audio/Config.hpp>
#if NAZARA_AUDIO_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_AUDIO_MEMORYMANAGER || defined(NAZARA_DEBUG)
#define NAZARA_DEBUG_MEMORYMANAGER_DISABLE_REDEFINITION
#define NAZARA_DEBUG_MEMORYLEAKTRACKER_DISABLE_REDEFINITION #include <Nazara/Core/Debug/MemoryManager.hpp>
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp>
#include <new> #include <new>
void* operator new(std::size_t size) void* operator new(std::size_t size)

View File

@ -3,11 +3,9 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Config.hpp> #include <Nazara/Core/Config.hpp>
#if NAZARA_CORE_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_CORE_MEMORYMANAGER || defined(NAZARA_DEBUG)
#define NAZARA_DEBUG_MEMORYMANAGER_DISABLE_REDEFINITION
#define NAZARA_DEBUG_MEMORYLEAKTRACKER_DISABLE_REDEFINITION #include <Nazara/Core/Debug/MemoryManager.hpp>
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp>
#include <new> #include <new>
void* operator new(std::size_t size) void* operator new(std::size_t size)

View File

@ -2,30 +2,20 @@
// This file is part of the "Nazara Engine - Core module" // This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp // 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 <cstdio>
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <stdexcept> #include <stdexcept>
#if defined(NAZARA_PLATFORM_WINDOWS) #if defined(NAZARA_PLATFORM_WINDOWS)
#include <windows.h> #include <windows.h>
#elif defined(NAZARA_PLATFORM_POSIX) #elif defined(NAZARA_PLATFORM_POSIX)
#include <pthread.h> #include <pthread.h>
#endif #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 // Le seul fichier n'ayant pas à inclure Debug.hpp
namespace namespace
@ -41,27 +31,29 @@ namespace
unsigned int magic; unsigned int magic;
}; };
bool initialized = false; bool s_initialized = false;
const unsigned int magic = 0x51429EE; const unsigned int s_magic = 0x51429EE;
const char* MLTFileName = "NazaraLeaks.log"; const char* s_MLTFileName = "NazaraLeaks.log";
const char* nextFreeFile = "(Internal error)"; const char* s_nextFreeFile = "(Internal error)";
unsigned int nextFreeLine = 0; unsigned int s_nextFreeLine = 0;
Block ptrList = Block s_list =
{ {
0, 0,
nullptr, nullptr,
&ptrList, &s_list,
&ptrList, &s_list,
false, false,
0, 0,
magic s_magic
}; };
unsigned int s_allocatedBlock = 0;
std::size_t s_allocatedSize = 0;
#if defined(NAZARA_PLATFORM_WINDOWS) #if defined(NAZARA_PLATFORM_WINDOWS)
CRITICAL_SECTION mutex; CRITICAL_SECTION s_mutex;
#elif defined(NAZARA_PLATFORM_POSIX) #elif defined(NAZARA_PLATFORM_POSIX)
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t s_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif #endif
} }
@ -76,21 +68,21 @@ NzMemoryManager::~NzMemoryManager()
void* NzMemoryManager::Allocate(std::size_t size, bool multi, const char* file, unsigned int line) void* NzMemoryManager::Allocate(std::size_t size, bool multi, const char* file, unsigned int line)
{ {
if (!initialized) if (!s_initialized)
Initialize(); Initialize();
#if defined(NAZARA_PLATFORM_WINDOWS) #if defined(NAZARA_PLATFORM_WINDOWS)
EnterCriticalSection(&mutex); EnterCriticalSection(&s_mutex);
#elif defined(NAZARA_PLATFORM_POSIX) #elif defined(NAZARA_PLATFORM_POSIX)
pthread_mutex_lock(&mutex); pthread_mutex_lock(&s_mutex);
#endif #endif
Block* ptr = reinterpret_cast<Block*>(std::malloc(size+sizeof(Block))); Block* ptr = reinterpret_cast<Block*>(std::malloc(size+sizeof(Block)));
if (!ptr) if (!ptr)
{ {
// Pas d'information de temps (Car nécessitant une allocation) // Pas d'information de temps (Car nécessitant une allocation)
FILE* log = std::fopen(MLTFileName, "a"); FILE* log = std::fopen(s_MLTFileName, "a");
std::fprintf(log, "Failed to allocate memory (" SIZE_T_SPECIFIER " bytes)\n", size); std::fprintf(log, "Failed to allocate memory (%zu bytes)\n", size);
std::fclose(log); std::fclose(log);
return nullptr; // Impossible d'envoyer une exception car cela allouerait de la mémoire avec new (boucle infinie) 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->file = file;
ptr->line = line; ptr->line = line;
ptr->size = size; ptr->size = size;
ptr->magic = magic; ptr->magic = s_magic;
ptr->prev = ptrList.prev; ptr->prev = s_list.prev;
ptr->next = &ptrList; ptr->next = &s_list;
ptrList.prev->next = ptr; s_list.prev->next = ptr;
ptrList.prev = ptr; s_list.prev = ptr;
s_allocatedBlock++;
s_allocatedSize += size;
#if defined(NAZARA_PLATFORM_WINDOWS) #if defined(NAZARA_PLATFORM_WINDOWS)
LeaveCriticalSection(&mutex); LeaveCriticalSection(&s_mutex);
#elif defined(NAZARA_PLATFORM_POSIX) #elif defined(NAZARA_PLATFORM_POSIX)
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&s_mutex);
#endif #endif
return reinterpret_cast<char*>(ptr)+sizeof(Block); return reinterpret_cast<nzUInt8*>(ptr) + sizeof(Block);
} }
void NzMemoryManager::Free(void* pointer, bool multi) void NzMemoryManager::Free(void* pointer, bool multi)
@ -121,144 +116,148 @@ void NzMemoryManager::Free(void* pointer, bool multi)
if (!pointer) if (!pointer)
return; return;
Block* ptr = reinterpret_cast<Block*>(reinterpret_cast<char*>(pointer)-sizeof(Block)); Block* ptr = reinterpret_cast<Block*>(reinterpret_cast<nzUInt8*>(pointer) - sizeof(Block));
if (ptr->magic != magic) if (ptr->magic != s_magic)
return; return;
#if defined(NAZARA_PLATFORM_WINDOWS) #if defined(NAZARA_PLATFORM_WINDOWS)
EnterCriticalSection(&mutex); EnterCriticalSection(&s_mutex);
#elif defined(NAZARA_PLATFORM_POSIX) #elif defined(NAZARA_PLATFORM_POSIX)
pthread_mutex_lock(&mutex); pthread_mutex_lock(&s_mutex);
#endif #endif
if (ptr->array != multi) 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) 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 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 else
{ {
if (multi) 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 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::fclose(log);
std::free(time);
} }
ptr->magic = 0; ptr->magic = 0; // Évitons des problèmes
ptr->prev->next = ptr->next; ptr->prev->next = ptr->next;
ptr->next->prev = ptr->prev; ptr->next->prev = ptr->prev;
s_allocatedBlock--;
s_allocatedSize -= ptr->size;
std::free(ptr); std::free(ptr);
nextFreeFile = nullptr; s_nextFreeFile = nullptr;
nextFreeLine = 0; s_nextFreeLine = 0;
#if defined(NAZARA_PLATFORM_WINDOWS) #if defined(NAZARA_PLATFORM_WINDOWS)
LeaveCriticalSection(&mutex); LeaveCriticalSection(&s_mutex);
#elif defined(NAZARA_PLATFORM_POSIX) #elif defined(NAZARA_PLATFORM_POSIX)
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&s_mutex);
#endif #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) void NzMemoryManager::NextFree(const char* file, unsigned int line)
{ {
nextFreeFile = file; s_nextFreeFile = file;
nextFreeLine = line; s_nextFreeLine = line;
} }
void NzMemoryManager::Initialize() void NzMemoryManager::Initialize()
{ {
char* time = TimeInfo(); char timeStr[23];
TimeInfo(timeStr);
FILE* file = std::fopen(MLTFileName, "w"); FILE* file = std::fopen(s_MLTFileName, "w");
std::fprintf(file, "%s ==============================\n", time); std::fprintf(file, "%s ==============================\n", timeStr);
std::fprintf(file, "%s Nazara Memory Leak Tracker \n", time); std::fprintf(file, "%s Nazara Memory Leak Tracker \n", timeStr);
std::fprintf(file, "%s ==============================\n", time); std::fprintf(file, "%s ==============================\n", timeStr);
std::fclose(file); std::fclose(file);
std::free(time);
if (std::atexit(Uninitialize) != 0) if (std::atexit(Uninitialize) != 0)
{ {
static NzMemoryManager manager; static NzMemoryManager manager;
} }
#ifdef NAZARA_PLATFORM_WINDOWS #ifdef NAZARA_PLATFORM_WINDOWS
InitializeCriticalSection(&mutex); InitializeCriticalSection(&s_mutex);
#endif #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); time_t currentTime = std::time(nullptr);
std::strftime(buffer, 23, "%d/%m/%Y - %H:%M:%S:", std::localtime(&currentTime)); std::strftime(buffer, 23, "%d/%m/%Y - %H:%M:%S:", std::localtime(&currentTime));
return buffer;
} }
void NzMemoryManager::Uninitialize() void NzMemoryManager::Uninitialize()
{ {
#ifdef NAZARA_PLATFORM_WINDOWS #ifdef NAZARA_PLATFORM_WINDOWS
DeleteCriticalSection(&mutex); DeleteCriticalSection(&s_mutex);
#endif #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 ==============================\n", timeStr);
std::fprintf(log, "%s No leak detected \n", time); std::fprintf(log, "%s No leak detected \n", timeStr);
std::fprintf(log, "%s ==============================", time); std::fprintf(log, "%s ==============================", timeStr);
} }
else else
{ {
std::fprintf(log, "%s ==============================\n", time); std::fprintf(log, "%s ==============================\n", timeStr);
std::fprintf(log, "%s Leaks have been detected \n", time); std::fprintf(log, "%s Leaks have been detected \n", timeStr);
std::fprintf(log, "%s ==============================\n\n", time); std::fprintf(log, "%s ==============================\n\n", timeStr);
std::fputs("Leak list:\n", log); std::fputs("Leak list:\n", log);
std::size_t totalSize = 0; Block* ptr = s_list.next;
Block* ptr = ptrList.next; while (ptr != &s_list)
unsigned int count = 0;
while (ptr != &ptrList)
{ {
count++;
totalSize += ptr->size;
if (ptr->file) 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 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; void* pointer = ptr;
ptr = ptr->next; ptr = ptr->next;
std::free(pointer); 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); 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); 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::NextFree(file, line);
NzMemoryManager::Free(ptr, false); 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::NextFree(file, line);
NzMemoryManager::Free(ptr, true); NzMemoryManager::Free(ptr, true);

View File

@ -3,7 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
// Notre utilisation du placement new n'est pas (encore ?) compatible avec les définitions du MLT // Notre utilisation du placement new n'est pas (encore ?) compatible avec les définitions du MLT
#define NAZARA_DEBUG_MEMORYLEAKTRACKER_DISABLE_REDEFINITION #define NAZARA_DEBUG_MEMORYMANAGER_DISABLE_REDEFINITION
#include <Nazara/Core/ParameterList.hpp> #include <Nazara/Core/ParameterList.hpp>
#include <Nazara/Core/Error.hpp> #include <Nazara/Core/Error.hpp>

View File

@ -3,11 +3,9 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/Config.hpp> #include <Nazara/Graphics/Config.hpp>
#if NAZARA_GRAPHICS_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_GRAPHICS_MEMORYMANAGER || defined(NAZARA_DEBUG)
#define NAZARA_DEBUG_MEMORYMANAGER_DISABLE_REDEFINITION
#define NAZARA_DEBUG_MEMORYLEAKTRACKER_DISABLE_REDEFINITION #include <Nazara/Core/Debug/MemoryManager.hpp>
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp>
#include <new> #include <new>
void* operator new(std::size_t size) void* operator new(std::size_t size)

View File

@ -3,11 +3,9 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Lua/Config.hpp> #include <Nazara/Lua/Config.hpp>
#if NAZARA_LUA_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_LUA_MEMORYMANAGER || defined(NAZARA_DEBUG)
#define NAZARA_DEBUG_MEMORYMANAGER_DISABLE_REDEFINITION
#define NAZARA_DEBUG_MEMORYLEAKTRACKER_DISABLE_REDEFINITION #include <Nazara/Core/Debug/MemoryManager.hpp>
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp>
#include <new> #include <new>
void* operator new(std::size_t size) void* operator new(std::size_t size)

View File

@ -2,6 +2,8 @@
// This file is part of the "Nazara Engine - Lua scripting module" // This file is part of the "Nazara Engine - Lua scripting module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#define NAZARA_DEBUG_MEMORYMANAGER_DISABLE_REDEFINITION
#include <Nazara/Lua/LuaInstance.hpp> #include <Nazara/Lua/LuaInstance.hpp>
#include <Lua/lauxlib.h> #include <Lua/lauxlib.h>
#include <Lua/lua.h> #include <Lua/lua.h>

View File

@ -3,11 +3,9 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Noise/Config.hpp> #include <Nazara/Noise/Config.hpp>
#if NAZARA_NOISE_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_NOISE_MEMORYMANAGER || defined(NAZARA_DEBUG)
#define NAZARA_DEBUG_MEMORYMANAGER_DISABLE_REDEFINITION
#define NAZARA_DEBUG_MEMORYLEAKTRACKER_DISABLE_REDEFINITION #include <Nazara/Core/Debug/MemoryManager.hpp>
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp>
#include <new> #include <new>
void* operator new(std::size_t size) void* operator new(std::size_t size)

View File

@ -3,11 +3,9 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Physics/Config.hpp> #include <Nazara/Physics/Config.hpp>
#if NAZARA_PHYSICS_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_PHYSICS_MEMORYMANAGER || defined(NAZARA_DEBUG)
#define NAZARA_DEBUG_MEMORYMANAGER_DISABLE_REDEFINITION
#define NAZARA_DEBUG_MEMORYLEAKTRACKER_DISABLE_REDEFINITION #include <Nazara/Core/Debug/MemoryManager.hpp>
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp>
#include <new> #include <new>
void* operator new(std::size_t size) void* operator new(std::size_t size)

View File

@ -3,11 +3,9 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Renderer/Config.hpp> #include <Nazara/Renderer/Config.hpp>
#if NAZARA_RENDERER_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_RENDERER_MEMORYMANAGER || defined(NAZARA_DEBUG)
#define NAZARA_DEBUG_MEMORYMANAGER_DISABLE_REDEFINITION
#define NAZARA_DEBUG_MEMORYLEAKTRACKER_DISABLE_REDEFINITION #include <Nazara/Core/Debug/MemoryManager.hpp>
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp>
#include <new> #include <new>
void* operator new(std::size_t size) void* operator new(std::size_t size)

View File

@ -3,11 +3,9 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Config.hpp> #include <Nazara/Utility/Config.hpp>
#if NAZARA_UTILITY_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) #if NAZARA_UTILITY_MEMORYMANAGER || defined(NAZARA_DEBUG)
#define NAZARA_DEBUG_MEMORYMANAGER_DISABLE_REDEFINITION
#define NAZARA_DEBUG_MEMORYLEAKTRACKER_DISABLE_REDEFINITION #include <Nazara/Core/Debug/MemoryManager.hpp>
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp>
#include <new> #include <new>
void* operator new(std::size_t size) void* operator new(std::size_t size)