util-memory: Avoid MSVC specific C++ and use Std++ if available

This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2019-09-04 03:22:05 +02:00
parent 82faab4380
commit da9124fc1a
1 changed files with 15 additions and 12 deletions

View File

@ -20,21 +20,22 @@
#include "util-memory.hpp" #include "util-memory.hpp"
#include <cstdlib> #include <cstdlib>
#include <stdexcept> #include <stdexcept>
#include <stdlib.h>
#define USE_STD_ALLOC_FREE #if defined(_MSC_VER) && (_MSC_VER <= 2100)
//#define USE_MSC_ALLOC
#ifdef _MSC_VER #elif defined(_cplusplus) && (__cplusplus >= 201103L)
#define D_ALIGNED_ALLOC(a, s) _aligned_malloc(s, a) #define USE_STD_ALLOC
#define D_ALIGNED_FREE _aligned_free
#else
#define D_ALIGNED_ALLOC(a, s) aligned_alloc(s, a)
#define D_ALIGNED_FREE free
#endif #endif
using namespace std;
void* util::malloc_aligned(size_t align, size_t size) void* util::malloc_aligned(size_t align, size_t size)
{ {
#ifdef USE_STD_ALLOC_FREE #ifdef USE_MSC_ALLOC
return D_ALIGNED_ALLOC(align, size); return _aligned_malloc(size, align);
#elif defined(USE_STD_ALLOC)
return aligned_alloc(size, align);
#else #else
// Ensure that we have space for the pointer and the data. // Ensure that we have space for the pointer and the data.
size_t asize = aligned_offset(align, size + (sizeof(void*) * 2)); size_t asize = aligned_offset(align, size + (sizeof(void*) * 2));
@ -55,8 +56,10 @@ void* util::malloc_aligned(size_t align, size_t size)
void util::free_aligned(void* mem) void util::free_aligned(void* mem)
{ {
#ifdef USE_STD_ALLOC_FREE #ifdef USE_MSC_ALLOC
D_ALIGNED_FREE(mem); _aligned_free(mem);
#elif defined(USE_STD_ALLOC_FREE)
free(mem);
#else #else
void* ptr = reinterpret_cast<void*>(*reinterpret_cast<intptr_t*>(static_cast<char*>(mem) - sizeof(void*))); void* ptr = reinterpret_cast<void*>(*reinterpret_cast<intptr_t*>(static_cast<char*>(mem) - sizeof(void*)));
free(ptr); free(ptr);