Skip to content

Commit

Permalink
Fix alloca support on Clang on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLynix committed Feb 5, 2024
1 parent a447904 commit d09fd9e
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions include/NazaraUtils/MemoryHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@
#ifndef NAZARAUTILS_MEMORYHELPER_HPP
#define NAZARAUTILS_MEMORYHELPER_HPP

#if defined(NAZARA_COMPILER_MSVC) || defined(NAZARA_COMPILER_MINGW)
#include <NazaraUtils/Prerequisites.hpp>

#if defined(NAZARA_COMPILER_MSVC)

#include <malloc.h>

// with MSVC, using alloca with a size of zero returns a valid pointer
// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/alloca?view=msvc-170
#define NAZARA_ALLOCA(size) _alloca(size)
#define NAZARA_ALLOCA_SUPPORT

#elif defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL)
#elif defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC)

#ifdef NAZARA_PLATFORM_BSD
#include <stdlib.h>
#else
#include <alloca.h>
#endif
// https://clang.llvm.org/docs/LanguageExtensions.html
// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
#define NAZARA_ALLOCA(size) __builtin_alloca(((size) > 0) ? (size) : 1)
#define NAZARA_ALLOCA_SUPPORT

#elif defined(NAZARA_COMPILER_INTEL)

// with Clang/GCC, using alloca with a size of zero does nothing good
#define NAZARA_ALLOCA(size) alloca(((size) > 0) ? (size) : 1)
// https://www.intel.com/content/www/us/en/docs/cpp-compiler/developer-guide-reference/2021-10/miscellaneous-intrinsics-001.html
// same as MSVC but without the malloc.h include (not sure)
#define NAZARA_ALLOCA(size) _alloca(((size) > 0) ? (size) : 1)
#define NAZARA_ALLOCA_SUPPORT

#endif
Expand Down

0 comments on commit d09fd9e

Please sign in to comment.