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