Added memory helpers

This will protect some memory-related code from new redefinition


Former-commit-id: 337e69e70acd0bf8dbc3ba1657c6bb0bddfd5df4
This commit is contained in:
Lynix 2014-07-22 17:26:31 +02:00
parent fba2e5ae01
commit e6dea541a7
7 changed files with 79 additions and 28 deletions

View File

@ -0,0 +1,20 @@
// 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
#pragma once
#ifndef NAZARA_MEMORYHELPER_HPP
#define NAZARA_MEMORYHELPER_HPP
#include <cstddef>
void NzOperatorDelete(void* ptr);
void* NzOperatorNew(std::size_t size);
template<typename T, typename... Args>
T* NzPlacementNew(void* ptr, Args... args);
#include <Nazara/Core/MemoryHelper.inl>
#endif // NAZARA_MEMORYHELPER_HPP

View File

@ -0,0 +1,45 @@
// 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
// Je ne suis pas fier des cinq lignes qui suivent mais difficile de faire autrement pour le moment...
#ifdef NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION
#define NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION_DEFINED
#else
#define NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION
#endif
#include <Nazara/Core/MemoryManager.hpp>
#include <new>
#include <Nazara/Core/Debug.hpp>
inline void NzOperatorDelete(void* ptr)
{
#if NAZARA_CORE_MANAGE_MEMORY
NzMemoryManager::Free(ptr);
#else
operator delete(ptr);
#endif
}
inline void* NzOperatorNew(std::size_t size)
{
#if NAZARA_CORE_MANAGE_MEMORY
return NzMemoryManager::Allocate(size);
#else
return operator new(size);
#endif
}
template<typename T, typename... Args>
T* NzPlacementNew(void* ptr, Args... args)
{
return new (ptr) T(args...);
}
#include <Nazara/Core/DebugOff.hpp>
// Si c'est nous qui avons défini la constante, alors il nous faut l'enlever (Pour éviter que le moteur entier n'en souffre)
#ifndef NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION_DEFINED
#undef NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION
#endif

View File

@ -2,14 +2,7 @@
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
// Je ne suis pas fier des cinq lignes qui suivent mais difficile de faire autrement pour le moment...
#ifdef NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION
#define NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION_DEFINED
#else
#define NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION
#endif
#include <new>
#include <Nazara/Core/MemoryHelper.hpp>
#include <Nazara/Core/Debug.hpp>
template<unsigned int blockSize, bool canGrow>
@ -58,7 +51,7 @@ void* NzMemoryPool<blockSize, canGrow>::Allocate(unsigned int size)
}
}
return operator new(size);
return NzOperatorNew(size);
}
template<unsigned int blockSize, bool canGrow>
@ -93,7 +86,7 @@ void NzMemoryPool<blockSize, canGrow>::Free(void* ptr)
if (m_next)
m_next->Free(ptr);
else
operator delete(ptr);
NzOperatorDelete(ptr);
}
}
}
@ -111,8 +104,3 @@ unsigned int NzMemoryPool<blockSize, canGrow>::GetSize() const
}
#include <Nazara/Core/DebugOff.hpp>
// Si c'est nous qui avons défini la constante, alors il nous faut l'enlever (Pour éviter que le moteur entier n'en souffre)
#ifndef NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION_DEFINED
#undef NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION
#endif

View File

@ -4,6 +4,7 @@
#include <Nazara/Core/Error.hpp>
#include <iostream>
#include <Nazara/Core/MemoryHelper.hpp>
#include <Nazara/Lua/Debug.hpp>
template<class T>
@ -31,7 +32,7 @@ void NzLuaClass<T>::Register(NzLuaInstance& lua)
// Ainsi c'est Lua qui va s'occuper de la destruction pour nous :-)
// De même, l'utilisation d'un shared_ptr permet de garder la structure en vie même si l'instance est libérée avant le LuaClass
void* info = lua.PushUserdata(sizeof(std::shared_ptr<ClassInfo>));
new (info) std::shared_ptr<ClassInfo>(m_info);
NzPlacementNew<std::shared_ptr<ClassInfo>>(info, m_info);
// On créé la table qui contiendra une méthode (Le finalizer) pour libérer le ClassInfo
lua.PushTable(0, 1);

View File

@ -5,12 +5,12 @@
#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 ?
#define NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION
#include <Nazara/Core/Debug/NewRedefinition.hpp>
void* operator new(std::size_t size, const char* file, unsigned int line)
{
return NzMemoryManager::Allocate(size, false, file, line);

View File

@ -2,11 +2,9 @@
// This file is part of the "Nazara Engine - Core module"
// 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
#define NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION
#include <Nazara/Core/ParameterList.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/MemoryHelper.hpp>
#include <cstring>
#include <limits>
#include <new>
@ -309,7 +307,7 @@ void NzParameterList::SetParameter(const NzString& name, const NzString& value)
parameter.type = nzParameterType_String;
new (&parameter.value.stringVal) NzString(value);
NzPlacementNew<NzString>(&parameter.value.stringVal, value);
}
void NzParameterList::SetParameter(const NzString& name, const char* value)
@ -322,7 +320,7 @@ void NzParameterList::SetParameter(const NzString& name, const char* value)
parameter.type = nzParameterType_String;
new (&parameter.value.stringVal) NzString(value);
NzPlacementNew<NzString>(&parameter.value.stringVal, value);
}
void NzParameterList::SetParameter(const NzString& name, void* value)
@ -405,7 +403,7 @@ NzParameterList& NzParameterList::operator=(const NzParameterList& list)
case nzParameterType_String:
parameter.type = nzParameterType_String;
new (&parameter.value.stringVal) NzString(it->second.value.stringVal);
NzPlacementNew<NzString>(&parameter.value.stringVal, it->second.value.stringVal);
break;
case nzParameterType_Userdata:

View File

@ -2,8 +2,6 @@
// This file is part of the "Nazara Engine - Lua scripting module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#define NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION
#include <Nazara/Lua/LuaInstance.hpp>
#include <Lua/lauxlib.h>
#include <Lua/lua.h>
@ -11,6 +9,7 @@
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/File.hpp>
#include <Nazara/Core/MemoryHelper.hpp>
#include <Nazara/Core/MemoryStream.hpp>
#include <Nazara/Core/StringStream.hpp>
#include <cstdlib>
@ -621,7 +620,7 @@ void NzLuaInstance::PushCFunction(NzLuaCFunction func, int upvalueCount)
void NzLuaInstance::PushFunction(NzLuaFunction func)
{
NzLuaFunction* luaFunc = reinterpret_cast<NzLuaFunction*>(lua_newuserdata(m_state, sizeof(NzLuaFunction)));
new (luaFunc) NzLuaFunction(std::move(func));
NzPlacementNew<NzLuaFunction>(luaFunc, std::move(func));
lua_pushcclosure(m_state, ProxyFunc, 1);
}