#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
#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);
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)
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;
}
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;