Core/MemoryHelper: Add portable stack allocation
Former-commit-id: f1c38cd50d028ab9c3b7868178d457a3bde30158 [formerly 09f688b51171507d684db1a6e865f65202295ca3] [formerly da2fe586a5e71591f7869a24b0178d3b3554de67 [formerly 99d022f578a47294795819cddc85c1c41a08a943]] Former-commit-id: d92a834a6538f1a8d74bbaebb898d9abf48135ca [formerly 6db2b81497d7252583dda33c4a1bf03a77c8ba43] Former-commit-id: fa05a671eed209de23671b8d396217f0851011d1
This commit is contained in:
parent
c6767a0fbd
commit
da03c9fc3b
|
|
@ -7,6 +7,27 @@
|
||||||
#ifndef NAZARA_MEMORYHELPER_HPP
|
#ifndef NAZARA_MEMORYHELPER_HPP
|
||||||
#define NAZARA_MEMORYHELPER_HPP
|
#define NAZARA_MEMORYHELPER_HPP
|
||||||
|
|
||||||
|
#if defined(NAZARA_COMPILER_MSVC)
|
||||||
|
|
||||||
|
#include <malloc.h>
|
||||||
|
|
||||||
|
#define NAZARA_ALLOCA(size) _alloca(size)
|
||||||
|
#define NAZARA_ALLOCA_SUPPORT
|
||||||
|
|
||||||
|
#elif defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL)
|
||||||
|
#include <alloca.h>
|
||||||
|
|
||||||
|
#define NAZARA_ALLOCA(size) alloca(size)
|
||||||
|
#define NAZARA_ALLOCA_SUPPORT
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef NAZARA_ALLOCA_SUPPORT
|
||||||
|
#define NazaraStackAllocation(size) Nz::StackAllocation(NAZARA_ALLOCA(size))
|
||||||
|
#else
|
||||||
|
#define NazaraStackAllocation(size) Nz::StackAllocation(Nz::OperatorNew(size))
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
|
|
@ -16,6 +37,20 @@ namespace Nz
|
||||||
|
|
||||||
template<typename T, typename... Args>
|
template<typename T, typename... Args>
|
||||||
T* PlacementNew(T* ptr, Args&&... args);
|
T* PlacementNew(T* ptr, Args&&... args);
|
||||||
|
|
||||||
|
class StackAllocation
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit StackAllocation(void* stackMemory);
|
||||||
|
~StackAllocation();
|
||||||
|
|
||||||
|
void* GetPtr();
|
||||||
|
|
||||||
|
operator void*();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void* m_ptr;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <Nazara/Core/MemoryHelper.inl>
|
#include <Nazara/Core/MemoryHelper.inl>
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
#define NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION
|
#define NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <Nazara/Core/MemoryHelper.hpp>
|
||||||
#include <Nazara/Core/MemoryManager.hpp>
|
#include <Nazara/Core/MemoryManager.hpp>
|
||||||
#include <new>
|
#include <new>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
@ -62,6 +63,51 @@ namespace Nz
|
||||||
{
|
{
|
||||||
return new (ptr) T(std::forward<Args>(args)...);
|
return new (ptr) T(std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \ingroup core
|
||||||
|
* \class Nz::StackAllocation
|
||||||
|
* \brief Core class that represents a stack allocation
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Constructs a StackAllocation object with a pointer to a memory allocated with NAZARA_ALLOCA or OperatorNew is alloca is not supported
|
||||||
|
*
|
||||||
|
* \param ptr Pointer to raw memory
|
||||||
|
*/
|
||||||
|
inline StackAllocation::StackAllocation(void* stackMemory) :
|
||||||
|
m_ptr(stackMemory)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Destructs the object and release memory if necessary
|
||||||
|
*/
|
||||||
|
inline StackAllocation::~StackAllocation()
|
||||||
|
{
|
||||||
|
#ifndef NAZARA_ALLOCA_SUPPORT
|
||||||
|
OperatorDelete(m_ptr);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Access the internal pointer
|
||||||
|
* \return internal memory pointer
|
||||||
|
*/
|
||||||
|
inline void* StackAllocation::GetPtr()
|
||||||
|
{
|
||||||
|
return m_ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Access the internal pointer
|
||||||
|
* \return internal memory pointer
|
||||||
|
*/
|
||||||
|
inline StackAllocation::operator void*()
|
||||||
|
{
|
||||||
|
return m_ptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <Nazara/Core/DebugOff.hpp>
|
#include <Nazara/Core/DebugOff.hpp>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue