From 8e009a1d6673431fa9a48e0d5d467a76cfdd6ab2 Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 15 Sep 2016 00:46:59 +0200 Subject: [PATCH] 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 --- include/Nazara/Core/MemoryHelper.hpp | 35 +++++++++++++++++++++ include/Nazara/Core/MemoryHelper.inl | 46 ++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/include/Nazara/Core/MemoryHelper.hpp b/include/Nazara/Core/MemoryHelper.hpp index e2f295622..ff81ea083 100644 --- a/include/Nazara/Core/MemoryHelper.hpp +++ b/include/Nazara/Core/MemoryHelper.hpp @@ -7,6 +7,27 @@ #ifndef NAZARA_MEMORYHELPER_HPP #define NAZARA_MEMORYHELPER_HPP +#if defined(NAZARA_COMPILER_MSVC) + +#include + +#define NAZARA_ALLOCA(size) _alloca(size) +#define NAZARA_ALLOCA_SUPPORT + +#elif defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL) +#include + +#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 namespace Nz @@ -16,6 +37,20 @@ namespace Nz template T* PlacementNew(T* ptr, Args&&... args); + + class StackAllocation + { + public: + explicit StackAllocation(void* stackMemory); + ~StackAllocation(); + + void* GetPtr(); + + operator void*(); + + private: + void* m_ptr; + }; } #include diff --git a/include/Nazara/Core/MemoryHelper.inl b/include/Nazara/Core/MemoryHelper.inl index 23a745a44..d3fe43155 100644 --- a/include/Nazara/Core/MemoryHelper.inl +++ b/include/Nazara/Core/MemoryHelper.inl @@ -9,6 +9,7 @@ #define NAZARA_DEBUG_NEWREDEFINITION_DISABLE_REDEFINITION #endif +#include #include #include #include @@ -62,6 +63,51 @@ namespace Nz { return new (ptr) T(std::forward(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