aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2014-03-25 18:16:03 -0700
committerChris Robinson <[email protected]>2014-03-25 18:16:03 -0700
commit045959e9c00979128679ca77b10b929182fe3397 (patch)
treecba018b5a2aecfae723e372694a0281fdf936614
parent03fd2b826658bae6bd975957c4af26ae42481756 (diff)
Use C99 VLA instead of alloca when available
-rw-r--r--CMakeLists.txt37
-rw-r--r--OpenAL32/Include/alMain.h6
-rw-r--r--OpenAL32/sample_cvt.c12
-rw-r--r--config.h.in3
4 files changed, 39 insertions, 19 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d80fbfcc..37aa57dd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -159,6 +159,17 @@ IF(CMAKE_COMPILER_IS_GNUCC)
SET(CMAKE_REQUIRED_FLAGS "${OLD_REQUIRED_FLAGS}")
ENDIF()
+# Check if we have C99 variable length arrays
+CHECK_C_SOURCE_COMPILES(
+"int main(int argc, char *argv[])
+ {
+ volatile int tmp[argc];
+ tmp[0] = argv[0][0];
+ return tmp[0];
+ }"
+HAVE_C99_VLA)
+
+
# Add definitions, compiler switches, etc.
INCLUDE_DIRECTORIES("${OpenAL_SOURCE_DIR}/include" "${OpenAL_BINARY_DIR}")
IF(CMAKE_VERSION VERSION_LESS "2.8.8")
@@ -302,8 +313,10 @@ ENDIF()
CHECK_C_SOURCE_COMPILES("int foo(const char *str, ...) __attribute__((format(printf, 1, 2)));
int main() {return 0;}" HAVE_GCC_FORMAT)
+IF(NOT HAVE_C99_VLA)
+ CHECK_INCLUDE_FILE(alloca.h HAVE_ALLOCA_H)
+ENDIF()
CHECK_INCLUDE_FILE(malloc.h HAVE_MALLOC_H)
-CHECK_INCLUDE_FILE(alloca.h HAVE_ALLOCA_H)
CHECK_INCLUDE_FILE(strings.h HAVE_STRINGS_H)
CHECK_INCLUDE_FILE(cpuid.h HAVE_CPUID_H)
CHECK_INCLUDE_FILE(sys/sysconf.h HAVE_SYS_SYSCONF_H)
@@ -327,18 +340,20 @@ CHECK_SYMBOL_EXISTS(aligned_alloc stdlib.h HAVE_ALIGNED_ALLOC)
CHECK_SYMBOL_EXISTS(posix_memalign stdlib.h HAVE_POSIX_MEMALIGN)
CHECK_SYMBOL_EXISTS(_aligned_malloc malloc.h HAVE__ALIGNED_MALLOC)
CHECK_SYMBOL_EXISTS(lrintf math.h HAVE_LRINTF)
-IF(HAVE_ALLOCA_H)
- CHECK_SYMBOL_EXISTS(alloca alloca.h HAVE_ALLOCA)
-ELSEIF(HAVE_MALLOC_H)
- CHECK_SYMBOL_EXISTS(alloca malloc.h HAVE_ALLOCA)
-ENDIF()
-IF(NOT HAVE_ALLOCA)
- CHECK_SYMBOL_EXISTS(_alloca malloc.h HAVE__ALLOCA)
- IF(NOT HAVE__ALLOCA)
- MESSAGE(FATAL_ERROR "No alloca function found, please report!")
+IF(NOT HAVE_C99_VLA)
+ IF(HAVE_ALLOCA_H)
+ CHECK_SYMBOL_EXISTS(alloca alloca.h HAVE_ALLOCA)
+ ELSEIF(HAVE_MALLOC_H)
+ CHECK_SYMBOL_EXISTS(alloca malloc.h HAVE_ALLOCA)
ENDIF()
+ IF(NOT HAVE_ALLOCA)
+ CHECK_SYMBOL_EXISTS(_alloca malloc.h HAVE__ALLOCA)
+ IF(NOT HAVE__ALLOCA)
+ MESSAGE(FATAL_ERROR "No alloca function found, please report!")
+ ENDIF()
- ADD_DEFINITIONS(-Dalloca=_alloca)
+ ADD_DEFINITIONS(-Dalloca=_alloca)
+ ENDIF()
ENDIF()
IF(HAVE_FLOAT_H)
diff --git a/OpenAL32/Include/alMain.h b/OpenAL32/Include/alMain.h
index 04514b5e..3d1f062a 100644
--- a/OpenAL32/Include/alMain.h
+++ b/OpenAL32/Include/alMain.h
@@ -314,6 +314,12 @@ typedef ptrdiff_t ALsizeiptrEXT;
#define FORCE_ALIGN
#endif
+#ifdef HAVE_C99_VLA
+#define DECL_VLA(T, _name, _size) T _name[(_size)]
+#else
+#define DECL_VLA(T, _name, _size) T *_name = alloca((_size) * sizeof(T))
+#endif
+
#ifndef PATH_MAX
#ifdef MAX_PATH
#define PATH_MAX MAX_PATH
diff --git a/OpenAL32/sample_cvt.c b/OpenAL32/sample_cvt.c
index a4c59d9d..903ec8ca 100644
--- a/OpenAL32/sample_cvt.c
+++ b/OpenAL32/sample_cvt.c
@@ -917,10 +917,9 @@ static void Convert_##T##_ALima4(T *dst, const ALima4 *src, ALuint numchans, \
ALuint len, ALuint align) \
{ \
ALsizei byte_align = ((align-1)/2 + 4) * numchans; \
+ DECL_VLA(ALshort, tmp, align*numchans); \
ALuint i, j, k; \
- ALshort *tmp; \
\
- tmp = alloca(align*numchans*sizeof(*tmp)); \
for(i = 0;i < len;i += align) \
{ \
DecodeIMA4Block(tmp, src, numchans, align); \
@@ -968,10 +967,9 @@ static void Convert_ALima4_##T(ALima4 *dst, const T *src, ALuint numchans, \
ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
ALint index[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
ALsizei byte_align = ((align-1)/2 + 4) * numchans; \
+ DECL_VLA(ALshort, tmp, align*numchans); \
ALuint i, j, k; \
- ALshort *tmp; \
\
- tmp = alloca(align*numchans*sizeof(*tmp)); \
for(i = 0;i < len;i += align) \
{ \
for(j = 0;j < align;j++) \
@@ -1020,10 +1018,9 @@ static void Convert_##T##_ALmsadpcm(T *dst, const ALmsadpcm *src, \
ALuint align) \
{ \
ALsizei byte_align = ((align-2)/2 + 7) * numchans; \
+ DECL_VLA(ALshort, tmp, align*numchans); \
ALuint i, j, k; \
- ALshort *tmp; \
\
- tmp = alloca(align*numchans*sizeof(*tmp)); \
for(i = 0;i < len;i += align) \
{ \
DecodeMSADPCMBlock(tmp, src, numchans, align); \
@@ -1071,10 +1068,9 @@ static void Convert_ALmsadpcm_##T(ALmsadpcm *dst, const T *src, \
{ \
ALint sample[MAX_INPUT_CHANNELS] = {0,0,0,0,0,0,0,0}; \
ALsizei byte_align = ((align-2)/2 + 7) * numchans; \
+ DECL_VLA(ALshort, tmp, align*numchans); \
ALuint i, j, k; \
- ALshort *tmp; \
\
- tmp = alloca(align*numchans*sizeof(*tmp)); \
for(i = 0;i < len;i += align) \
{ \
for(j = 0;j < align;j++) \
diff --git a/config.h.in b/config.h.in
index a0c11f36..c80a7d56 100644
--- a/config.h.in
+++ b/config.h.in
@@ -89,6 +89,9 @@
/* Define to the size of a long long int type */
#cmakedefine SIZEOF_LONG_LONG ${SIZEOF_LONG_LONG}
+/* Define if we have C99 variable-length array support */
+#cmakedefine HAVE_C99_VLA
+
/* Define if we have GCC's destructor attribute */
#cmakedefine HAVE_GCC_DESTRUCTOR