summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMathieu Malaterre <mathieu.malaterre@gmail.com>2015-10-10 17:51:29 +0200
committerMathieu Malaterre <mathieu.malaterre@gmail.com>2015-10-10 17:51:29 +0200
commitd753441028e7c4f8efe84c043eff9c3e27e17c30 (patch)
tree2e0dbf5b8552230ad7d1b65bf6869acc59251b14 /src
parent2d410fc74b6b1f4b031b6341f989a9bf7049b179 (diff)
implement a portable aligned realloc
Diffstat (limited to 'src')
-rw-r--r--src/lib/openjp2/opj_malloc.c39
-rw-r--r--src/lib/openjp2/opj_malloc.h1
-rw-r--r--src/lib/openjp2/tcd.c6
3 files changed, 38 insertions, 8 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)
diff --git a/src/lib/openjp2/opj_malloc.h b/src/lib/openjp2/opj_malloc.h
index 640946dd..1b3fced9 100644
--- a/src/lib/openjp2/opj_malloc.h
+++ b/src/lib/openjp2/opj_malloc.h
@@ -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);
/**
diff --git a/src/lib/openjp2/tcd.c b/src/lib/openjp2/tcd.c
index 2fccff1c..a5a5d272 100644
--- a/src/lib/openjp2/tcd.c
+++ b/src/lib/openjp2/tcd.c
@@ -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;