diff options
author | Chris Robinson <[email protected]> | 2012-08-15 05:50:40 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2012-08-15 05:50:40 -0700 |
commit | 28593579394d9dcf489ed190dca9b14cdc2340d9 (patch) | |
tree | f5e09e3df841b68e706a60a5637c9d579c9e946e | |
parent | 2cbb565d09425115590ad8845dda2a3e353d74df (diff) |
Add wrapper methods to ensure aligned allocations
-rw-r--r-- | Alc/helpers.c | 48 | ||||
-rw-r--r-- | CMakeLists.txt | 4 | ||||
-rw-r--r-- | OpenAL32/Include/alMain.h | 4 | ||||
-rw-r--r-- | config.h.in | 9 |
4 files changed, 65 insertions, 0 deletions
diff --git a/Alc/helpers.c b/Alc/helpers.c index 82bc5ec6..26a32b69 100644 --- a/Alc/helpers.c +++ b/Alc/helpers.c @@ -110,6 +110,54 @@ void FillCPUCaps(ALuint capfilter) } +void *al_malloc(size_t alignment, size_t size) +{ +#if defined(HAVE_ALIGNED_ALLOC) + size = (size+(alignment-1))&~(alignment-1); + return aligned_alloc(alignment, size); +#elif defined(HAVE_POSIX_MEMALIGN) + void *ret; + if(posix_memalign(&ret, alignment, size) == 0) + return ret; + return NULL; +#elif defined(HAVE__ALIGNED_MALLOC) + return _aligned_malloc(size, alignment); +#else + char *ret = malloc(size+alignment); + if(ret != NULL) + { + *(ret++) = 0x00; + while(((ALintptrEXT)ret&(alignment-1)) != 0) + *(ret++) = 0xAA; + } + return ret; +#endif +} + +void *al_calloc(size_t alignment, size_t size) +{ + void *ret = al_malloc(alignment, size); + if(ret) memset(ret, 0, size); + return ret; +} + +void al_free(void *ptr) +{ +#if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN) || defined(HAVE__ALIGNED_MALLOC) + free(ptr); +#else + if(ptr != NULL) + { + char *finder = ptr; + do { + --finder; + } while(*finder == 0xAA); + free(finder); + } + return ret; +#endif +} + #ifdef _WIN32 void pthread_once(pthread_once_t *once, void (*callback)(void)) { diff --git a/CMakeLists.txt b/CMakeLists.txt index f2265b87..c8ecfd8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -256,6 +256,10 @@ IF(HAVE_LIBM) ENDIF() +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(powf math.h HAVE_POWF) CHECK_SYMBOL_EXISTS(powf math.h HAVE_POWF) CHECK_SYMBOL_EXISTS(sqrtf math.h HAVE_SQRTF) CHECK_SYMBOL_EXISTS(cosf math.h HAVE_COSF) diff --git a/OpenAL32/Include/alMain.h b/OpenAL32/Include/alMain.h index 5665a458..8b1bcc0f 100644 --- a/OpenAL32/Include/alMain.h +++ b/OpenAL32/Include/alMain.h @@ -685,6 +685,10 @@ static __inline void UnlockContext(ALCcontext *context) { UnlockDevice(context->Device); } +void *al_malloc(size_t alignment, size_t size); +void *al_calloc(size_t alignment, size_t size); +void al_free(void *ptr); + ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr); ALuint StopThread(ALvoid *thread); diff --git a/config.h.in b/config.h.in index 8f9c14c8..2254adff 100644 --- a/config.h.in +++ b/config.h.in @@ -7,6 +7,15 @@ #define ALIGN(x) ${ALIGN_DECL} +/* Define if we have the C11 aligned_alloc function */ +#cmakedefine HAVE_ALIGNED_ALLOC + +/* Define if we have the posix_memalign function */ +#cmakedefine HAVE_POSIX_MEMALIGN + +/* Define if we have the _aligned_malloc function */ +#cmakedefine HAVE__ALIGNED_MALLOC + /* Define if we have SSE CPU extensions */ #cmakedefine HAVE_SSE |