diff options
| author | Mathieu Malaterre <mathieu.malaterre@gmail.com> | 2015-10-10 17:51:29 +0200 |
|---|---|---|
| committer | Mathieu Malaterre <mathieu.malaterre@gmail.com> | 2015-10-10 17:51:29 +0200 |
| commit | d753441028e7c4f8efe84c043eff9c3e27e17c30 (patch) | |
| tree | 2e0dbf5b8552230ad7d1b65bf6869acc59251b14 /src/lib/openjp2/opj_malloc.c | |
| parent | 2d410fc74b6b1f4b031b6341f989a9bf7049b179 (diff) | |
implement a portable aligned realloc
Diffstat (limited to 'src/lib/openjp2/opj_malloc.c')
| -rw-r--r-- | src/lib/openjp2/opj_malloc.c | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/src/lib/openjp2/opj_malloc.c b/src/lib/openjp2/opj_malloc.c index 62f7265e..4c9de50f 100644 --- a/src/lib/openjp2/opj_malloc.c +++ b/src/lib/openjp2/opj_malloc.c @@ -32,10 +32,11 @@ #include "opj_malloc.h" #include "opj_config_private.h" #include <stdlib.h> +#include <string.h> +#include <inttypes.h> -static inline void *opj_aligned_alloc(size_t alignment, size_t size) +static inline void *opj_aligned_alloc_n(size_t alignment, size_t size) { -/* MacOSX / clang */ #if defined(HAVE_POSIX_MEMALIGN) // aligned_alloc requires c11, restrict to posix_memalign for now. Quote: // This function was introduced in POSIX 1003.1d. Although this function is @@ -59,12 +60,36 @@ static inline void *opj_aligned_alloc(size_t alignment, size_t size) #error missing aligned alloc function #endif } - +static inline void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t size) +{ +/* no portable aligned realloc */ +#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN) + /* glibc doc states one can mixed aligned malloc with realloc */ + void *r_ptr = realloc( ptr, size ); + /* fast path */ + if( (uintptr_t)r_ptr & alignment == 0 ) + return r_ptr; + /* this is non-trivial to implement a portable aligned realloc, so use a + * simple approach where we do not need a function that return the size of an + * allocated array (eg. _msize on Windows, malloc_size on MacOS, + * malloc_usable_size on systems with glibc) */ + void *a_ptr = opj_aligned_alloc_n(alignment, size); + /* memory may overlap, do not use memcpy */ + memmove(a_ptr, r_ptr, size); + free( r_ptr ); + return a_ptr; +/* _MSC_VER */ +#elif defined(HAVE__ALIGNED_MALLOC) + return _aligned_realloc( ptr, size, alignment ); +#else +/* TODO: _mm_malloc(x,y) */ +#error missing aligned realloc function +#endif +} void * opj_malloc(size_t size) { return malloc(size); } - void * opj_calloc(size_t numOfElements, size_t sizeOfElements) { return calloc(numOfElements, sizeOfElements); @@ -72,7 +97,11 @@ void * opj_calloc(size_t numOfElements, size_t sizeOfElements) void *opj_aligned_malloc(size_t size) { - return opj_aligned_alloc(16u,size); + return opj_aligned_alloc_n(16u,size); +} +void * opj_aligned_realloc(void *ptr, size_t size) +{ + return opj_aligned_realloc_n(ptr,16u,size); } void opj_aligned_free(void* ptr) |
