From 28593579394d9dcf489ed190dca9b14cdc2340d9 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Wed, 15 Aug 2012 05:50:40 -0700 Subject: Add wrapper methods to ensure aligned allocations --- Alc/helpers.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'Alc/helpers.c') 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)) { -- cgit v1.2.3