implement a portable aligned realloc

This commit is contained in:
Mathieu Malaterre 2015-10-10 17:51:29 +02:00
parent 2d410fc74b
commit d753441028
3 changed files with 38 additions and 8 deletions

View File

@ -32,10 +32,11 @@
#include "opj_malloc.h" #include "opj_malloc.h"
#include "opj_config_private.h" #include "opj_config_private.h"
#include <stdlib.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) #if defined(HAVE_POSIX_MEMALIGN)
// aligned_alloc requires c11, restrict to posix_memalign for now. Quote: // aligned_alloc requires c11, restrict to posix_memalign for now. Quote:
// This function was introduced in POSIX 1003.1d. Although this function is // 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 #error missing aligned alloc function
#endif #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) void * opj_malloc(size_t size)
{ {
return malloc(size); return malloc(size);
} }
void * opj_calloc(size_t numOfElements, size_t sizeOfElements) void * opj_calloc(size_t numOfElements, size_t sizeOfElements)
{ {
return calloc(numOfElements, 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) 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) void opj_aligned_free(void* ptr)

View 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 @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_malloc(size_t size);
void * opj_aligned_realloc(void *ptr, size_t size);
void opj_aligned_free(void* ptr); void opj_aligned_free(void* ptr);
/** /**

View 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) 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))) { 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 ) { if (! l_tilec->data ) {
return OPJ_FALSE; 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; l_tilec->ownsData = OPJ_TRUE;
} }
else if (l_tilec->data_size_needed > l_tilec->data_size) { 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"); */ /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle tile datan"); */
/* fprintf(stderr, "Not enough memory to handle tile data"); */ /* fprintf(stderr, "Not enough memory to handle tile data"); */
if (! new_data) { if (! new_data) {
opj_free(l_tilec->data); opj_aligned_free(l_tilec->data);
l_tilec->data = NULL; l_tilec->data = NULL;
l_tilec->data_size = 0; l_tilec->data_size = 0;
l_tilec->data_size_needed = 0; l_tilec->data_size_needed = 0;