From 0cfe2ca4a9687c1b1bbb6c9c9b193ae71a79d547 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Tue, 29 Sep 2015 15:27:57 +0200 Subject: [PATCH 1/6] Fix repository for JPEG2000 test data The data is now maintained in a git repository on Github. Signed-off-by: Stefan Weil --- CMakeLists.txt | 4 ++-- INSTALL | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7898d15c..73bed2c6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -291,8 +291,8 @@ if(BUILD_TESTING) include(CTest) # Search openjpeg data needed for the tests - # They could be found via svn on the OpenJPEG google code project - # svn checkout http://openjpeg.googlecode.com/svn/data (about 70 Mo) + # They could be found via git on the OpenJPEG GitHub code project + # git clone https://github.com/uclouvain/openjpeg-data.git find_path(OPJ_DATA_ROOT README-OPJ-Data PATHS $ENV{OPJ_DATA_ROOT} ${CMAKE_SOURCE_DIR}/../data NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH diff --git a/INSTALL b/INSTALL index 109ec4b1..16549f58 100644 --- a/INSTALL +++ b/INSTALL @@ -44,7 +44,7 @@ Main available cmake flags: cmake . -DBUILD_TESTING:BOOL=ON -DOPJ_DATA_ROOT:PATH='path/to/the/data/directory' make make Experimental - Note : JPEG2000 test files are available with 'svn checkout http://openjpeg.googlecode.com/svn/data' (about 70 Mo). + Note : JPEG2000 test files are available with 'git clone https://github.com/uclouvain/openjpeg-data.git'. If '-DOPJ_DATA_ROOT:PATH' option is omitted, test files will be automatically searched in '${CMAKE_SOURCE_DIR}/../data', corresponding to the location of the data directory when compiling from the trunk (and assuming the data directory has been checked out of course). From a1fc83cc255ff7697d6ca68ea4e1c42710771756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20M=C3=BChlstrasser?= Date: Mon, 19 Oct 2015 12:14:01 +0200 Subject: [PATCH 2/6] Fix HP compiler warning about redeclaration of function (#640) HP compiler warns: cc: "dwt.c", line 798: warning 562: Redeclaration of "opj_v4dwt_decode" with a different storage class specifier: "opj_v4dwt_decode" will have internal linkage. cc: "t2.c", line 1341: warning 562: Redeclaration of "opj_t2_init_seg" with a different storage class specifier: "opj_t2_init_seg" will have internal linkage. --- src/lib/openjp2/dwt.c | 2 +- src/lib/openjp2/t2.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/openjp2/dwt.c b/src/lib/openjp2/dwt.c index 92752f42..d63c120e 100644 --- a/src/lib/openjp2/dwt.c +++ b/src/lib/openjp2/dwt.c @@ -795,7 +795,7 @@ static void opj_v4dwt_decode_step2(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k, OPJ_IN /* */ /* Inverse 9-7 wavelet transform in 1-D. */ /* */ -void opj_v4dwt_decode(opj_v4dwt_t* restrict dwt) +static void opj_v4dwt_decode(opj_v4dwt_t* restrict dwt) { OPJ_INT32 a, b; if(dwt->cas == 0) { diff --git a/src/lib/openjp2/t2.c b/src/lib/openjp2/t2.c index 5af1a69a..ebc26b2d 100644 --- a/src/lib/openjp2/t2.c +++ b/src/lib/openjp2/t2.c @@ -1338,7 +1338,7 @@ static OPJ_BOOL opj_t2_skip_packet_data( opj_t2_t* p_t2, } -OPJ_BOOL opj_t2_init_seg( opj_tcd_cblk_dec_t* cblk, +static OPJ_BOOL opj_t2_init_seg( opj_tcd_cblk_dec_t* cblk, OPJ_UINT32 index, OPJ_UINT32 cblksty, OPJ_UINT32 first) From 0967d6485414e7038d51f11e4e57f7a4bf5b4572 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20M=C3=BChlstrasser?= Date: Tue, 20 Oct 2015 13:02:51 +0200 Subject: [PATCH 3/6] Generic aligned malloc implementation. Used for platforms where none of posix_memalign(), memalign() and _aligned_malloc() is available. --- src/lib/openjp2/opj_malloc.c | 80 +++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 6 deletions(-) diff --git a/src/lib/openjp2/opj_malloc.c b/src/lib/openjp2/opj_malloc.c index 3bbf80d8..e2ace0b8 100644 --- a/src/lib/openjp2/opj_malloc.c +++ b/src/lib/openjp2/opj_malloc.c @@ -32,6 +32,10 @@ #define OPJ_SKIP_POISON #include "opj_includes.h" +#ifndef SIZE_MAX +#define SIZE_MAX ((size_t) -1) +#endif + static INLINE void *opj_aligned_alloc_n(size_t alignment, size_t size) { void* ptr; @@ -59,8 +63,30 @@ static INLINE void *opj_aligned_alloc_n(size_t alignment, size_t size) #elif defined(HAVE__ALIGNED_MALLOC) ptr = _aligned_malloc(size, alignment); #else -/* TODO: _mm_malloc(x,y) */ -#error missing aligned alloc function + /* + * Generic aligned malloc implementation. + * Uses ptrdiff_t for the integer manipulation of the pointer, as + * uintptr_t is not available in C89. + */ + { + ptrdiff_t mask; + void *mem; + + /* Room for padding and extra pointer stored in front of allocated area */ + size_t overhead = (alignment - 1) + sizeof(void *); + + /* Avoid integer overflow */ + if (size > SIZE_MAX - overhead) + return NULL; + + mem = malloc(size + overhead); + if (!mem) + return mem; + + mask = ~(ptrdiff_t)(alignment - 1); + ptr = (void *) ((ptrdiff_t) (mem + overhead) & mask); + ((void**) ptr)[-1] = mem; + } #endif return ptr; } @@ -97,8 +123,46 @@ static INLINE void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t ne #elif defined(HAVE__ALIGNED_MALLOC) r_ptr = _aligned_realloc( ptr, new_size, alignment ); #else -/* TODO: _mm_malloc(x,y) */ -#error missing aligned realloc function + { + void *oldmem, *newmem; + size_t overhead = (alignment - 1) + sizeof(void *); + + if (new_size == 0) { + my_aligned_free(ptr); + return NULL; + } + + /* Avoid integer overflow */ + if (new_size > SIZE_MAX - overhead) + return NULL; + + oldmem = ((void**) ptr)[-1]; + newmem = realloc(oldmem, new_size + overhead); + if (!newmem) + return newmem; + + if (newmem == oldmem) { + r_ptr = ptr; + } + else { + ptrdiff_t old_offset, new_offset; + ptrdiff_t mask; + + /* realloc created a new copy, realign the copied memory block */ + old_offset = (char *) ptr - (char *) oldmem; + + mask = ~(ptrdiff_t)(alignment - 1); + r_ptr = (void *) ((ptrdiff_t) (newmem + overhead) & mask); + + new_offset = (char *) r_ptr - (char *) newmem; + + if (new_offset != old_offset) { + memmove((char *) newmem + new_offset, (char *) newmem + old_offset, + new_size); + } + ((void**) r_ptr)[-1] = newmem; + } + } #endif return r_ptr; } @@ -129,10 +193,14 @@ void * opj_aligned_realloc(void *ptr, size_t size) void opj_aligned_free(void* ptr) { -#ifdef HAVE__ALIGNED_MALLOC +#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN) + free( ptr ); +#elif defined(HAVE__ALIGNED_MALLOC) _aligned_free( ptr ); #else - free( ptr ); + /* Generic implementation has malloced pointer stored in front of used area */ + if (ptr) + free(((void**) ptr)[-1]); #endif } From eaf55b677c53d76a11d2b860034337afdf8d8817 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20M=C3=BChlstrasser?= Date: Tue, 20 Oct 2015 13:02:51 +0200 Subject: [PATCH 4/6] Generic aligned malloc implementation. Used for platforms where none of posix_memalign(), memalign() and _aligned_malloc() is available. --- src/lib/openjp2/opj_malloc.c | 80 +++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 6 deletions(-) diff --git a/src/lib/openjp2/opj_malloc.c b/src/lib/openjp2/opj_malloc.c index 3bbf80d8..e2ace0b8 100644 --- a/src/lib/openjp2/opj_malloc.c +++ b/src/lib/openjp2/opj_malloc.c @@ -32,6 +32,10 @@ #define OPJ_SKIP_POISON #include "opj_includes.h" +#ifndef SIZE_MAX +#define SIZE_MAX ((size_t) -1) +#endif + static INLINE void *opj_aligned_alloc_n(size_t alignment, size_t size) { void* ptr; @@ -59,8 +63,30 @@ static INLINE void *opj_aligned_alloc_n(size_t alignment, size_t size) #elif defined(HAVE__ALIGNED_MALLOC) ptr = _aligned_malloc(size, alignment); #else -/* TODO: _mm_malloc(x,y) */ -#error missing aligned alloc function + /* + * Generic aligned malloc implementation. + * Uses ptrdiff_t for the integer manipulation of the pointer, as + * uintptr_t is not available in C89. + */ + { + ptrdiff_t mask; + void *mem; + + /* Room for padding and extra pointer stored in front of allocated area */ + size_t overhead = (alignment - 1) + sizeof(void *); + + /* Avoid integer overflow */ + if (size > SIZE_MAX - overhead) + return NULL; + + mem = malloc(size + overhead); + if (!mem) + return mem; + + mask = ~(ptrdiff_t)(alignment - 1); + ptr = (void *) ((ptrdiff_t) (mem + overhead) & mask); + ((void**) ptr)[-1] = mem; + } #endif return ptr; } @@ -97,8 +123,46 @@ static INLINE void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t ne #elif defined(HAVE__ALIGNED_MALLOC) r_ptr = _aligned_realloc( ptr, new_size, alignment ); #else -/* TODO: _mm_malloc(x,y) */ -#error missing aligned realloc function + { + void *oldmem, *newmem; + size_t overhead = (alignment - 1) + sizeof(void *); + + if (new_size == 0) { + my_aligned_free(ptr); + return NULL; + } + + /* Avoid integer overflow */ + if (new_size > SIZE_MAX - overhead) + return NULL; + + oldmem = ((void**) ptr)[-1]; + newmem = realloc(oldmem, new_size + overhead); + if (!newmem) + return newmem; + + if (newmem == oldmem) { + r_ptr = ptr; + } + else { + ptrdiff_t old_offset, new_offset; + ptrdiff_t mask; + + /* realloc created a new copy, realign the copied memory block */ + old_offset = (char *) ptr - (char *) oldmem; + + mask = ~(ptrdiff_t)(alignment - 1); + r_ptr = (void *) ((ptrdiff_t) (newmem + overhead) & mask); + + new_offset = (char *) r_ptr - (char *) newmem; + + if (new_offset != old_offset) { + memmove((char *) newmem + new_offset, (char *) newmem + old_offset, + new_size); + } + ((void**) r_ptr)[-1] = newmem; + } + } #endif return r_ptr; } @@ -129,10 +193,14 @@ void * opj_aligned_realloc(void *ptr, size_t size) void opj_aligned_free(void* ptr) { -#ifdef HAVE__ALIGNED_MALLOC +#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN) + free( ptr ); +#elif defined(HAVE__ALIGNED_MALLOC) _aligned_free( ptr ); #else - free( ptr ); + /* Generic implementation has malloced pointer stored in front of used area */ + if (ptr) + free(((void**) ptr)[-1]); #endif } From d48be27f5aa018d398cf9369b2c664bee4ce6e19 Mon Sep 17 00:00:00 2001 From: mayeut Date: Wed, 21 Oct 2015 00:09:04 +0200 Subject: [PATCH 5/6] Generic aligned malloc implementation update. Update #642 --- src/lib/openjp2/opj_malloc.c | 84 ++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 33 deletions(-) diff --git a/src/lib/openjp2/opj_malloc.c b/src/lib/openjp2/opj_malloc.c index e2ace0b8..04ef055c 100644 --- a/src/lib/openjp2/opj_malloc.c +++ b/src/lib/openjp2/opj_malloc.c @@ -33,7 +33,7 @@ #include "opj_includes.h" #ifndef SIZE_MAX -#define SIZE_MAX ((size_t) -1) +# define SIZE_MAX ((size_t) -1) #endif static INLINE void *opj_aligned_alloc_n(size_t alignment, size_t size) @@ -42,6 +42,8 @@ static INLINE void *opj_aligned_alloc_n(size_t alignment, size_t size) /* alignment shall be power of 2 */ assert( (alignment != 0U) && ((alignment & (alignment - 1U)) == 0U)); + /* alignment shall be at least sizeof(void*) */ + assert( alignment >= sizeof(void*)); if (size == 0U) { /* prevent implementation defined behavior of realloc */ return NULL; @@ -65,26 +67,34 @@ static INLINE void *opj_aligned_alloc_n(size_t alignment, size_t size) #else /* * Generic aligned malloc implementation. - * Uses ptrdiff_t for the integer manipulation of the pointer, as - * uintptr_t is not available in C89. + * Uses size_t offset for the integer manipulation of the pointer, + * as uintptr_t is not available in C89 to do + * bitwise operations on the pointer itself. */ + alignment--; { - ptrdiff_t mask; - void *mem; + size_t offset; + OPJ_UINT8 *mem; /* Room for padding and extra pointer stored in front of allocated area */ - size_t overhead = (alignment - 1) + sizeof(void *); + size_t overhead = alignment + sizeof(void *); + + /* let's be extra careful */ + assert(alignment <= (SIZE_MAX - sizeof(void *))); /* Avoid integer overflow */ - if (size > SIZE_MAX - overhead) + if (size > (SIZE_MAX - overhead)) { return NULL; + } - mem = malloc(size + overhead); - if (!mem) + mem = (OPJ_UINT8*)malloc(size + overhead); + if (mem == NULL) { return mem; - - mask = ~(ptrdiff_t)(alignment - 1); - ptr = (void *) ((ptrdiff_t) (mem + overhead) & mask); + } + /* offset = ((alignment + 1U) - ((size_t)(mem + sizeof(void*)) & alignment)) & alignment; */ + /* Use the fact that alignment + 1U is a power of 2 */ + offset = ((alignment ^ ((size_t)(mem + sizeof(void*)) & alignment)) + 1U) & alignment; + ptr = (void *)(mem + sizeof(void*) + offset); ((void**) ptr)[-1] = mem; } #endif @@ -96,6 +106,8 @@ static INLINE void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t ne /* alignment shall be power of 2 */ assert( (alignment != 0U) && ((alignment & (alignment - 1U)) == 0U)); + /* alignment shall be at least sizeof(void*) */ + assert( alignment >= sizeof(void*)); if (new_size == 0U) { /* prevent implementation defined behavior of realloc */ return NULL; @@ -123,42 +135,47 @@ static INLINE void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t ne #elif defined(HAVE__ALIGNED_MALLOC) r_ptr = _aligned_realloc( ptr, new_size, alignment ); #else + if (ptr == NULL) { + return opj_aligned_alloc_n(alignment, new_size); + } + alignment--; { - void *oldmem, *newmem; - size_t overhead = (alignment - 1) + sizeof(void *); - - if (new_size == 0) { - my_aligned_free(ptr); - return NULL; - } + void *oldmem; + OPJ_UINT8 *newmem; + size_t overhead = alignment + sizeof(void *); + + /* let's be extra careful */ + assert(alignment <= (SIZE_MAX - sizeof(void *))); /* Avoid integer overflow */ - if (new_size > SIZE_MAX - overhead) + if (new_size > SIZE_MAX - overhead) { return NULL; - + } + oldmem = ((void**) ptr)[-1]; - newmem = realloc(oldmem, new_size + overhead); - if (!newmem) + newmem = (OPJ_UINT8*)realloc(oldmem, new_size + overhead); + if (newmem == NULL) { return newmem; + } if (newmem == oldmem) { r_ptr = ptr; } else { - ptrdiff_t old_offset, new_offset; - ptrdiff_t mask; + size_t old_offset; + size_t new_offset; /* realloc created a new copy, realign the copied memory block */ - old_offset = (char *) ptr - (char *) oldmem; + old_offset = (size_t)(ptr - oldmem); - mask = ~(ptrdiff_t)(alignment - 1); - r_ptr = (void *) ((ptrdiff_t) (newmem + overhead) & mask); - - new_offset = (char *) r_ptr - (char *) newmem; + /* offset = ((alignment + 1U) - ((size_t)(mem + sizeof(void*)) & alignment)) & alignment; */ + /* Use the fact that alignment + 1U is a power of 2 */ + new_offset = ((alignment ^ ((size_t)(newmem + sizeof(void*)) & alignment)) + 1U) & alignment; + new_offset += sizeof(void*); + r_ptr = (void *)(newmem + new_offset); if (new_offset != old_offset) { - memmove((char *) newmem + new_offset, (char *) newmem + old_offset, - new_size); + memmove(newmem + new_offset, newmem + old_offset, new_size); } ((void**) r_ptr)[-1] = newmem; } @@ -199,8 +216,9 @@ void opj_aligned_free(void* ptr) _aligned_free( ptr ); #else /* Generic implementation has malloced pointer stored in front of used area */ - if (ptr) + if (ptr != NULL) { free(((void**) ptr)[-1]); + } #endif } From b7a162348de2f1444377e2dca9f3cbf9d770b52d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20M=C3=BChlstrasser?= Date: Wed, 21 Oct 2015 09:01:31 +0200 Subject: [PATCH 6/6] Avoid pointer arithmetic with (void *) pointers. --- src/lib/openjp2/opj_malloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/openjp2/opj_malloc.c b/src/lib/openjp2/opj_malloc.c index 04ef055c..a4aea30f 100644 --- a/src/lib/openjp2/opj_malloc.c +++ b/src/lib/openjp2/opj_malloc.c @@ -166,7 +166,7 @@ static INLINE void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t ne size_t new_offset; /* realloc created a new copy, realign the copied memory block */ - old_offset = (size_t)(ptr - oldmem); + old_offset = (size_t)((OPJ_UINT8*)ptr - (OPJ_UINT8*)oldmem); /* offset = ((alignment + 1U) - ((size_t)(mem + sizeof(void*)) & alignment)) & alignment; */ /* Use the fact that alignment + 1U is a power of 2 */