Big config/debug update

Added config checkers
Macro no longer use suffixes
Moved MemoryManager to upper directory
Renamed *_MEMORYMANAGER to *_MANAGE_MEMORY
Renamed AUDIO_STREAMEDBUFFERCOUNT to AUDIO_STREAMED_BUFFER_COUNT
Renamed CORE_REAL_PRECISION to CORE_DECIMAL_DIGITS
Renamed DEBUG_MEMORYMANAGER_DISABLE_REDEFINITION to
DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION
Renamed GRAPHICS_MAX_LIGHTPERPASS to GRAPHICS_MAX_LIGHT_PER_PASS
Renamed UTILITY_FORCE_DECLARATION_STRIDE_MULTIPLE_OF_32 to
UTILITY_VERTEX_DECLARATION_FORCE_STRIDE_MULTIPLE_OF_32


Former-commit-id: 81ef836ac9f092ac471f60e544bb7c7c6370593c
This commit is contained in:
Lynix
2014-07-08 10:56:37 +02:00
parent f819beb747
commit c4b10dddda
64 changed files with 471 additions and 206 deletions

View File

@@ -1,283 +0,0 @@
// Copyright (C) 2014 Jérôme Leclercq
// 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_MEMORYMANAGER_DISABLE_REDEFINITION
#include <Nazara/Core/Debug/MemoryManager.hpp>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <stdexcept>
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <windows.h>
#elif defined(NAZARA_PLATFORM_POSIX)
#include <pthread.h>
#endif
// Le seul fichier n'ayant pas à inclure Debug.hpp
namespace
{
struct Block
{
std::size_t size;
const char* file;
Block* prev;
Block* next;
bool array;
unsigned int line;
unsigned int magic;
};
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 s_list =
{
0,
nullptr,
&s_list,
&s_list,
false,
0,
s_magic
};
unsigned int s_allocationCount = 0;
unsigned int s_allocatedBlock = 0;
std::size_t s_allocatedSize = 0;
#if defined(NAZARA_PLATFORM_WINDOWS)
CRITICAL_SECTION s_mutex;
#elif defined(NAZARA_PLATFORM_POSIX)
pthread_mutex_t s_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
}
NzMemoryManager::NzMemoryManager()
{
}
NzMemoryManager::~NzMemoryManager()
{
Uninitialize();
}
void* NzMemoryManager::Allocate(std::size_t size, bool multi, const char* file, unsigned int line)
{
if (!s_initialized)
Initialize();
#if defined(NAZARA_PLATFORM_WINDOWS)
EnterCriticalSection(&s_mutex);
#elif defined(NAZARA_PLATFORM_POSIX)
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(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)
}
ptr->array = multi;
ptr->file = file;
ptr->line = line;
ptr->size = size;
ptr->magic = s_magic;
ptr->prev = s_list.prev;
ptr->next = &s_list;
s_list.prev->next = ptr;
s_list.prev = ptr;
s_allocatedBlock++;
s_allocatedSize += size;
s_allocationCount++;
#if defined(NAZARA_PLATFORM_WINDOWS)
LeaveCriticalSection(&s_mutex);
#elif defined(NAZARA_PLATFORM_POSIX)
pthread_mutex_unlock(&s_mutex);
#endif
return reinterpret_cast<nzUInt8*>(ptr) + sizeof(Block);
}
void NzMemoryManager::Free(void* pointer, bool multi)
{
if (!pointer)
return;
Block* ptr = reinterpret_cast<Block*>(reinterpret_cast<nzUInt8*>(pointer) - sizeof(Block));
if (ptr->magic != s_magic)
return;
#if defined(NAZARA_PLATFORM_WINDOWS)
EnterCriticalSection(&s_mutex);
#elif defined(NAZARA_PLATFORM_POSIX)
pthread_mutex_lock(&s_mutex);
#endif
if (ptr->array != multi)
{
char timeStr[23];
TimeInfo(timeStr);
FILE* log = std::fopen(s_MLTFileName, "a");
const char* error = (multi) ? "delete[] after new" : "delete after new[]";
if (s_nextFreeFile)
std::fprintf(log, "%s Warning: %s at %s:%u\n", timeStr, error, s_nextFreeFile, s_nextFreeLine);
else
std::fprintf(log, "%s Warning: %s at unknown position\n", error, timeStr);
std::fclose(log);
}
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);
s_nextFreeFile = nullptr;
s_nextFreeLine = 0;
#if defined(NAZARA_PLATFORM_WINDOWS)
LeaveCriticalSection(&s_mutex);
#elif defined(NAZARA_PLATFORM_POSIX)
pthread_mutex_unlock(&s_mutex);
#endif
}
unsigned int NzMemoryManager::GetAllocatedBlockCount()
{
return s_allocatedBlock;
}
std::size_t NzMemoryManager::GetAllocatedSize()
{
return s_allocatedSize;
}
unsigned int NzMemoryManager::GetAllocationCount()
{
return s_allocationCount;
}
void NzMemoryManager::NextFree(const char* file, unsigned int line)
{
s_nextFreeFile = file;
s_nextFreeLine = line;
}
void NzMemoryManager::Initialize()
{
char timeStr[23];
TimeInfo(timeStr);
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);
if (std::atexit(Uninitialize) != 0)
{
static NzMemoryManager manager;
}
#ifdef NAZARA_PLATFORM_WINDOWS
InitializeCriticalSection(&s_mutex);
#endif
s_initialized = true;
}
void NzMemoryManager::TimeInfo(char buffer[23])
{
time_t currentTime = std::time(nullptr);
std::strftime(buffer, 23, "%d/%m/%Y - %H:%M:%S:", std::localtime(&currentTime));
}
void NzMemoryManager::Uninitialize()
{
#ifdef NAZARA_PLATFORM_WINDOWS
DeleteCriticalSection(&s_mutex);
#endif
FILE* log = std::fopen(s_MLTFileName, "a");
char timeStr[23];
TimeInfo(timeStr);
std::fprintf(log, "%s Application finished, checking leaks...\n", timeStr);
if (s_allocatedBlock == 0)
{
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", timeStr);
std::fprintf(log, "%s Leaks have been detected \n", timeStr);
std::fprintf(log, "%s ==============================\n\n", timeStr);
std::fputs("Leak list:\n", log);
Block* ptr = s_list.next;
while (ptr != &s_list)
{
if (ptr->file)
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 -> %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 (%zu bytes)", s_allocatedBlock, s_allocatedSize);
}
std::fclose(log);
}
void* operator new(std::size_t size, const char* file, unsigned int line)
{
return NzMemoryManager::Allocate(size, false, file, line);
}
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) noexcept
{
NzMemoryManager::NextFree(file, line);
NzMemoryManager::Free(ptr, false);
}
void operator delete[](void* ptr, const char* file, unsigned int line) noexcept
{
NzMemoryManager::NextFree(file, line);
NzMemoryManager::Free(ptr, true);
}

View File

@@ -3,10 +3,10 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Config.hpp>
#if NAZARA_CORE_MEMORYMANAGER || defined(NAZARA_DEBUG)
#define NAZARA_DEBUG_MEMORYMANAGER_DISABLE_REDEFINITION
#include <Nazara/Core/Debug/MemoryManager.hpp>
#include <new>
#if NAZARA_CORE_MANAGE_MEMORY
#include <Nazara/Core/MemoryManager.hpp>
#include <new> // Nécessaire ?
void* operator new(std::size_t size)
{
@@ -27,4 +27,5 @@ void operator delete[](void* pointer) noexcept
{
NzMemoryManager::Free(pointer, true);
}
#endif
#endif // NAZARA_CORE_MANAGE_MEMORY

View File

@@ -0,0 +1,36 @@
// Copyright (C) 2014 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Config.hpp>
#if NAZARA_CORE_MANAGE_MEMORY
#define NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION
#include <Nazara/Core/Debug/NewRedefinition.hpp>
#include <Nazara/Core/MemoryManager.hpp>
#include <new> // Nécessaire ?
void* operator new(std::size_t size, const char* file, unsigned int line)
{
return NzMemoryManager::Allocate(size, false, file, line);
}
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) noexcept
{
NzMemoryManager::NextFree(file, line);
NzMemoryManager::Free(ptr, false);
}
void operator delete[](void* ptr, const char* file, unsigned int line) noexcept
{
NzMemoryManager::NextFree(file, line);
NzMemoryManager::Free(ptr, true);
}
#endif // NAZARA_CORE_MANAGE_MEMORY