Core/MemoryHelper: Add portable stack allocation
Former-commit-id: 50b96029c53d1784f028148f1ec15ae2cf98d036 [formerly eb3398905190df3d0dc77c8a3b54bb455059e39b] [formerly bfac4638e043161208ee7cf4735a788b1427f73e [formerly 1ee0a59ab5aa407c995afd287a9aaa4307075754]] Former-commit-id: 521c4248c638e5141534cd1d223d8f563693b821 [formerly 203f6191b3b24ccb2e04d330372973dadfb81e1d] Former-commit-id: 140a5c705819055b4d27f43156b079d6142049a5
This commit is contained in:
parent
357128f0b6
commit
8e009a1d66
|
|
@ -7,6 +7,27 @@
|
|||
#ifndef 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>
|
||||
|
||||
namespace Nz
|
||||
|
|
@ -16,6 +37,20 @@ namespace Nz
|
|||
|
||||
template<typename T, typename... 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>
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
#define NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION
|
||||
#endif
|
||||
|
||||
#include <Nazara/Core/MemoryHelper.hpp>
|
||||
#include <Nazara/Core/MemoryManager.hpp>
|
||||
#include <new>
|
||||
#include <utility>
|
||||
|
|
@ -62,6 +63,51 @@ namespace Nz
|
|||
{
|
||||
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>
|
||||
|
|
|
|||
Loading…
Reference in New Issue