diff options
author | Chris Robinson <[email protected]> | 2016-03-29 00:44:58 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2016-03-29 00:44:58 -0700 |
commit | 2ccc1d1d8a6ecc5c025e99518038fcc7a001d949 (patch) | |
tree | 9e9c2c60e8215c3431a2f3065639e69b2a9ce16c /common | |
parent | ca309e685e88244c3b380acd5eabbc65364665b2 (diff) |
Move the aligned malloc functions to the common lib
Diffstat (limited to 'common')
-rw-r--r-- | common/almalloc.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/common/almalloc.c b/common/almalloc.c new file mode 100644 index 00000000..8c1c5794 --- /dev/null +++ b/common/almalloc.c @@ -0,0 +1,62 @@ + +#include "config.h" + +#include "almalloc.h" + +#include <stdlib.h> +#include <string.h> +#ifdef HAVE_MALLOC_H +#include <malloc.h> +#endif +#ifdef HAVE_WINDOWS_H +#include <windows.h> +#endif + +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(((ptrdiff_t)ret&(alignment-1)) != 0) + *(ret++) = 0x55; + } + 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) + free(ptr); +#elif defined(HAVE__ALIGNED_MALLOC) + _aligned_free(ptr); +#else + if(ptr != NULL) + { + char *finder = ptr; + do { + --finder; + } while(*finder == 0x55); + free(finder); + } +#endif +} |