implement a portable aligned realloc
authorMathieu Malaterre <mathieu.malaterre@gmail.com>
Sat, 10 Oct 2015 15:51:29 +0000 (17:51 +0200)
committerMathieu Malaterre <mathieu.malaterre@gmail.com>
Sat, 10 Oct 2015 15:51:29 +0000 (17:51 +0200)
src/lib/openjp2/opj_malloc.c
src/lib/openjp2/opj_malloc.h
src/lib/openjp2/tcd.c

index 62f7265e0adc1901c71b89dfc8e55a36785b8130..4c9de50f5e6cd790823916c033258cbcab097474 100644 (file)
 #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)
index 640946dd9ffd79eac2da5e2d81388dde0f2d1030..1b3fced91e55234d2955823afb7362dbf710e093 100644 (file)
@@ -68,6 +68,7 @@ Allocate memory aligned to a 16 byte boundary
 @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
 */
 void * opj_aligned_malloc(size_t size);
+void * opj_aligned_realloc(void *ptr, size_t size);
 void opj_aligned_free(void* ptr);
 
 /**
index 2fccff1c9c78d8b1dbb173bee63b248a2b66f876..a5a5d272796b38f59fa6263b6b8501ede6fc4939 100644 (file)
@@ -626,7 +626,7 @@ void opj_tcd_destroy(opj_tcd_t *tcd) {
 OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
 {
        if ((l_tilec->data == 00) || ((l_tilec->data_size_needed > l_tilec->data_size) && (l_tilec->ownsData == OPJ_FALSE))) {
-               l_tilec->data = (OPJ_INT32 *) opj_malloc(l_tilec->data_size_needed);
+               l_tilec->data = (OPJ_INT32 *) opj_aligned_malloc(l_tilec->data_size_needed);
                if (! l_tilec->data ) {
                        return OPJ_FALSE;
                }
@@ -635,11 +635,11 @@ OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
                l_tilec->ownsData = OPJ_TRUE;
        }
        else if (l_tilec->data_size_needed > l_tilec->data_size) {
-               OPJ_INT32 * new_data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_tilec->data_size_needed);
+               OPJ_INT32 * new_data = (OPJ_INT32 *) opj_aligned_realloc(l_tilec->data, l_tilec->data_size_needed);
                /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle tile datan"); */
                /* fprintf(stderr, "Not enough memory to handle tile data"); */
                if (! new_data) {
-                       opj_free(l_tilec->data);
+                       opj_aligned_free(l_tilec->data);
                        l_tilec->data = NULL;
                        l_tilec->data_size = 0;
                        l_tilec->data_size_needed = 0;