From 0dc4914b4e5b93ab1d765a541fb342835ada51b5 Mon Sep 17 00:00:00 2001 From: Mathieu Malaterre Date: Fri, 9 Oct 2015 22:03:35 +0200 Subject: [PATCH 01/18] cleanup header file and move to implementation --- CMakeLists.txt | 14 +++ src/lib/openjp2/CMakeLists.txt | 1 + src/lib/openjp2/opj_config_private.h.cmake.in | 13 ++- src/lib/openjp2/opj_malloc.c | 96 ++++++++++++++++ src/lib/openjp2/opj_malloc.h | 106 ++---------------- 5 files changed, 132 insertions(+), 98 deletions(-) create mode 100644 src/lib/openjp2/opj_malloc.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 7898d15c..68814d23 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -225,6 +225,20 @@ CHECK_INCLUDE_FILE("unistd.h" HAVE_UNISTD_H) include(TestLargeFiles) OPJ_TEST_LARGE_FILES(OPJ_HAVE_LARGEFILES) +include(CheckIncludeFiles) +check_include_files(malloc.h HAVE_MALLOC_H) +include(CheckSymbolExists) +# aligned_alloc +set(CMAKE_REQUIRED_FLAGS "-std=c11") +check_symbol_exists(aligned_alloc stdlib.h HAVE_ALIGNED_ALLOC) +unset(CMAKE_REQUIRED_FLAGS) +# _aligned_alloc https://msdn.microsoft.com/en-us/library/8z34s9c6.aspx +check_symbol_exists(_aligned_malloc malloc.h HAVE__ALIGNED_MALLOC) +# memalign +check_symbol_exists(memalign malloc.h HAVE_MEMALIGN) +# posix_memalign +check_symbol_exists(posix_memalign stdlib.h HAVE_POSIX_MEMALIGN) + #----------------------------------------------------------------------------- # Build Library if(BUILD_JPIP_SERVER) diff --git a/src/lib/openjp2/CMakeLists.txt b/src/lib/openjp2/CMakeLists.txt index 3129bf53..500e905c 100644 --- a/src/lib/openjp2/CMakeLists.txt +++ b/src/lib/openjp2/CMakeLists.txt @@ -50,6 +50,7 @@ set(OPENJPEG_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/opj_codec.h ${CMAKE_CURRENT_SOURCE_DIR}/opj_includes.h ${CMAKE_CURRENT_SOURCE_DIR}/opj_intmath.h + ${CMAKE_CURRENT_SOURCE_DIR}/opj_malloc.c ${CMAKE_CURRENT_SOURCE_DIR}/opj_malloc.h ${CMAKE_CURRENT_SOURCE_DIR}/opj_stdint.h ) diff --git a/src/lib/openjp2/opj_config_private.h.cmake.in b/src/lib/openjp2/opj_config_private.h.cmake.in index 8a02c79d..97c9fdc5 100644 --- a/src/lib/openjp2/opj_config_private.h.cmake.in +++ b/src/lib/openjp2/opj_config_private.h.cmake.in @@ -17,6 +17,17 @@ #cmakedefine _FILE_OFFSET_BITS @_FILE_OFFSET_BITS@ #cmakedefine OPJ_HAVE_FSEEKO @OPJ_HAVE_FSEEKO@ +/* find whether or not have */ +#cmakedefine HAVE_MALLOC_H +/* check if function `aligned_alloc` exists */ +#cmakedefine HAVE_ALIGNED_ALLOC +/* check if function `_aligned_malloc` exists */ +#cmakedefine HAVE__ALIGNED_MALLOC +/* check if function `memalign` exists */ +#cmakedefine HAVE_MEMALIGN +/* check if function `posix_memalign` exists */ +#cmakedefine HAVE_POSIX_MEMALIGN + /* Byte order. */ /* All compilers that support Mac OS X define either __BIG_ENDIAN__ or __LITTLE_ENDIAN__ to match the endianness of the architecture being @@ -28,4 +39,4 @@ On other platforms we use the result of the TRY_RUN. */ #cmakedefine OPJ_BIG_ENDIAN #elif defined(__BIG_ENDIAN__) # define OPJ_BIG_ENDIAN -#endif \ No newline at end of file +#endif diff --git a/src/lib/openjp2/opj_malloc.c b/src/lib/openjp2/opj_malloc.c new file mode 100644 index 00000000..a170d852 --- /dev/null +++ b/src/lib/openjp2/opj_malloc.c @@ -0,0 +1,96 @@ +/* + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third + * party and contributor rights, including patent rights, and no such rights + * are granted under this license. + * + * Copyright (c) 2015, Mathieu Malaterre + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#define OPJ_SKIP_POISON +#include "opj_malloc.h" +#include + +static inline void *opj_aligned_alloc(size_t alignment, size_t size) +{ +#ifndef HAVE_ALIGNED_ALLOC + /* older linux */ +#ifdef HAVE_MEMALIGN + assert( size % alignment == 0 ); + return memalign( alignment, size ); +#endif /* HAVE_MEMALIGN */ + +/* _MSC_VER */ +#ifdef HAVE__ALIGNED_MALLOC + return _aligned_malloc( alignment, size ); +#endif /* HAVE__ALIGNED_MALLOC */ + +/* MacOSX / clang */ +#if defined(HAVE_POSIX_MEMALIGN) && !defined(HAVE_MEMALIGN) + void* ptr; + if (posix_memalign (&ptr, alignment, size)) + { + ptr = NULL; + } + return ptr; +#endif /* HAVE_POSIX_MEMALIGN */ + +#else /* HAVE_ALIGNED_ALLOC */ + return aligned_alloc( alignment, size ); +#endif /* HAVE_ALIGNED_ALLOC */ +/* TODO: _mm_malloc(x,y) */ +} + +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(size,16); +} + +void opj_aligned_free(void* ptr) +{ +#ifdef HAVE__ALIGNED_MALLOC + _aligned_free( ptr ); +#else + free( ptr ); +#endif +} + +void * opj_realloc(void * m, size_t s) +{ + return realloc(m,s); +} +void opj_free(void * m) +{ + free(m); +} diff --git a/src/lib/openjp2/opj_malloc.h b/src/lib/openjp2/opj_malloc.h index 517707f9..640946dd 100644 --- a/src/lib/openjp2/opj_malloc.h +++ b/src/lib/openjp2/opj_malloc.h @@ -31,6 +31,8 @@ */ #ifndef __OPJ_MALLOC_H #define __OPJ_MALLOC_H + +#include /** @file opj_malloc.h @brief Internal functions @@ -50,16 +52,7 @@ Allocate an uninitialized memory block @param size Bytes to allocate @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available */ -#ifdef ALLOC_PERF_OPT -void * OPJ_CALLCONV opj_malloc(size_t size); -#else -/* prevent assertion on overflow for MSVC */ -#ifdef _MSC_VER -#define opj_malloc(size) ((size_t)(size) >= (size_t)-0x100 ? NULL : malloc(size)) -#else -#define opj_malloc(size) malloc(size) -#endif -#endif +void * opj_malloc(size_t size); /** Allocate a memory block with elements initialized to 0 @@ -67,83 +60,15 @@ Allocate a memory block with elements initialized to 0 @param size Bytes per block to allocate @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available */ -#ifdef ALLOC_PERF_OPT -void * OPJ_CALLCONV opj_calloc(size_t _NumOfElements, size_t _SizeOfElements); -#else -/* prevent assertion on overflow for MSVC */ -#ifdef _MSC_VER -#define opj_calloc(num, size) ((size_t)(num) != 0 && (size_t)(num) >= (size_t)-0x100 / (size_t)(size) ? NULL : calloc(num, size)) -#else -#define opj_calloc(num, size) calloc(num, size) -#endif -#endif +void * opj_calloc(size_t numOfElements, size_t sizeOfElements); /** Allocate memory aligned to a 16 byte boundary @param size Bytes to allocate @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available */ -/* FIXME: These should be set with cmake tests, but we're currently not requiring use of cmake */ -#ifdef _WIN32 - /* Someone should tell the mingw people that their malloc.h ought to provide _mm_malloc() */ - #ifdef __GNUC__ - #include - #define HAVE_MM_MALLOC - #else /* MSVC, Intel C++ */ - #include - #ifdef _mm_malloc - #define HAVE_MM_MALLOC - #endif - #endif -#else /* Not _WIN32 */ - #if defined(__sun) - #define HAVE_MEMALIGN - #elif defined(__FreeBSD__) - #define HAVE_POSIX_MEMALIGN - /* Linux x86_64 and OSX always align allocations to 16 bytes */ - #elif !defined(__amd64__) && !defined(__APPLE__) && !defined(_AIX) - #define HAVE_MEMALIGN - #include - #endif -#endif - -#define opj_aligned_malloc(size) malloc(size) -#define opj_aligned_free(m) free(m) - -#ifdef HAVE_MM_MALLOC - #undef opj_aligned_malloc - #define opj_aligned_malloc(size) _mm_malloc(size, 16) - #undef opj_aligned_free - #define opj_aligned_free(m) _mm_free(m) -#endif - -#ifdef HAVE_MEMALIGN - extern void* memalign(size_t, size_t); - #undef opj_aligned_malloc - #define opj_aligned_malloc(size) memalign(16, (size)) - #undef opj_aligned_free - #define opj_aligned_free(m) free(m) -#endif - -#ifdef HAVE_POSIX_MEMALIGN - #undef opj_aligned_malloc - extern int posix_memalign(void**, size_t, size_t); - - static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){ - void* mem = NULL; - posix_memalign(&mem, 16, size); - return mem; - } - #undef opj_aligned_free - #define opj_aligned_free(m) free(m) -#endif - -#ifdef ALLOC_PERF_OPT - #undef opj_aligned_malloc - #define opj_aligned_malloc(size) opj_malloc(size) - #undef opj_aligned_free - #define opj_aligned_free(m) opj_free(m) -#endif +void * opj_aligned_malloc(size_t size); +void opj_aligned_free(void* ptr); /** Reallocate memory blocks. @@ -151,28 +76,15 @@ Reallocate memory blocks. @param s New size in bytes @return Returns a void pointer to the reallocated (and possibly moved) memory block */ -#ifdef ALLOC_PERF_OPT -void * OPJ_CALLCONV opj_realloc(void * m, size_t s); -#else -/* prevent assertion on overflow for MSVC */ -#ifdef _MSC_VER -#define opj_realloc(m, s) ((size_t)(s) >= (size_t)-0x100 ? NULL : realloc(m, s)) -#else -#define opj_realloc(m, s) realloc(m, s) -#endif -#endif +void * opj_realloc(void * m, size_t s); /** Deallocates or frees a memory block. @param m Previously allocated memory block to be freed */ -#ifdef ALLOC_PERF_OPT -void OPJ_CALLCONV opj_free(void * m); -#else -#define opj_free(m) free(m) -#endif +void opj_free(void * m); -#ifdef __GNUC__ +#if defined(__GNUC__) && !defined(OPJ_SKIP_POISON) #pragma GCC poison malloc calloc realloc free #endif From 2d410fc74b6b1f4b031b6341f989a9bf7049b179 Mon Sep 17 00:00:00 2001 From: Mathieu Malaterre Date: Sat, 10 Oct 2015 14:54:21 +0200 Subject: [PATCH 02/18] do not use aligned_alloc since it requires c11 --- CMakeLists.txt | 10 +++------- src/lib/openjp2/opj_malloc.c | 36 +++++++++++++++++------------------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 68814d23..2ef847fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -225,20 +225,16 @@ CHECK_INCLUDE_FILE("unistd.h" HAVE_UNISTD_H) include(TestLargeFiles) OPJ_TEST_LARGE_FILES(OPJ_HAVE_LARGEFILES) +# Allocating Aligned Memory Blocks include(CheckIncludeFiles) check_include_files(malloc.h HAVE_MALLOC_H) include(CheckSymbolExists) -# aligned_alloc -set(CMAKE_REQUIRED_FLAGS "-std=c11") -check_symbol_exists(aligned_alloc stdlib.h HAVE_ALIGNED_ALLOC) -unset(CMAKE_REQUIRED_FLAGS) # _aligned_alloc https://msdn.microsoft.com/en-us/library/8z34s9c6.aspx check_symbol_exists(_aligned_malloc malloc.h HAVE__ALIGNED_MALLOC) -# memalign -check_symbol_exists(memalign malloc.h HAVE_MEMALIGN) # posix_memalign check_symbol_exists(posix_memalign stdlib.h HAVE_POSIX_MEMALIGN) - +# memalign (obsolete) +check_symbol_exists(memalign malloc.h HAVE_MEMALIGN) #----------------------------------------------------------------------------- # Build Library if(BUILD_JPIP_SERVER) diff --git a/src/lib/openjp2/opj_malloc.c b/src/lib/openjp2/opj_malloc.c index a170d852..62f7265e 100644 --- a/src/lib/openjp2/opj_malloc.c +++ b/src/lib/openjp2/opj_malloc.c @@ -30,36 +30,34 @@ */ #define OPJ_SKIP_POISON #include "opj_malloc.h" +#include "opj_config_private.h" #include static inline void *opj_aligned_alloc(size_t alignment, size_t size) { -#ifndef HAVE_ALIGNED_ALLOC - /* older linux */ -#ifdef HAVE_MEMALIGN - assert( size % alignment == 0 ); - return memalign( alignment, size ); -#endif /* HAVE_MEMALIGN */ - -/* _MSC_VER */ -#ifdef HAVE__ALIGNED_MALLOC - return _aligned_malloc( alignment, size ); -#endif /* HAVE__ALIGNED_MALLOC */ - /* MacOSX / clang */ -#if defined(HAVE_POSIX_MEMALIGN) && !defined(HAVE_MEMALIGN) +#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 + // superseded by aligned_alloc, it is more portable to older POSIX systems + // that do not support ISO C11. void* ptr; if (posix_memalign (&ptr, alignment, size)) { ptr = NULL; } return ptr; -#endif /* HAVE_POSIX_MEMALIGN */ - -#else /* HAVE_ALIGNED_ALLOC */ - return aligned_alloc( alignment, size ); -#endif /* HAVE_ALIGNED_ALLOC */ + /* older linux */ +#elif defined(HAVE_MEMALIGN) + assert( size % alignment == 0 ); + return memalign( alignment, size ); +/* _MSC_VER */ +#elif defined(HAVE__ALIGNED_MALLOC) + return _aligned_malloc( alignment, size ); +#else /* TODO: _mm_malloc(x,y) */ +#error missing aligned alloc function +#endif } void * opj_malloc(size_t size) @@ -74,7 +72,7 @@ void * opj_calloc(size_t numOfElements, size_t sizeOfElements) void *opj_aligned_malloc(size_t size) { - return opj_aligned_alloc(size,16); + return opj_aligned_alloc(16u,size); } void opj_aligned_free(void* ptr) From d753441028e7c4f8efe84c043eff9c3e27e17c30 Mon Sep 17 00:00:00 2001 From: Mathieu Malaterre Date: Sat, 10 Oct 2015 17:51:29 +0200 Subject: [PATCH 03/18] implement a portable aligned realloc --- src/lib/openjp2/opj_malloc.c | 39 +++++++++++++++++++++++++++++++----- src/lib/openjp2/opj_malloc.h | 1 + src/lib/openjp2/tcd.c | 6 +++--- 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 +#include +#include -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; From dc869c2985c8ee346089e61414899ae7aa2e00f5 Mon Sep 17 00:00:00 2001 From: Mathieu Malaterre Date: Sat, 10 Oct 2015 18:38:08 +0200 Subject: [PATCH 04/18] Add paranoid sentinels --- src/lib/openjp2/mct.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib/openjp2/mct.c b/src/lib/openjp2/mct.c index 8b0276f3..9bb01da4 100644 --- a/src/lib/openjp2/mct.c +++ b/src/lib/openjp2/mct.c @@ -81,6 +81,10 @@ void opj_mct_encode( { OPJ_SIZE_T i; const OPJ_SIZE_T len = n; + /* buffer are aligned on 16 bytes */ + assert( (uintptr_t)c0 & 16 == 0 ); + assert( (uintptr_t)c1 & 16 == 0 ); + assert( (uintptr_t)c2 & 16 == 0 ); for(i = 0; i < (len & ~3U); i += 4) { __m128i y, u, v; From ab8929262a66852e8b17fb63b79bca1671a77a1e Mon Sep 17 00:00:00 2001 From: Mathieu Malaterre Date: Sat, 10 Oct 2015 21:03:44 +0200 Subject: [PATCH 05/18] Fix an issue with parenthesis --- src/lib/openjp2/mct.c | 6 +++--- src/lib/openjp2/opj_malloc.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/openjp2/mct.c b/src/lib/openjp2/mct.c index 9bb01da4..6b678345 100644 --- a/src/lib/openjp2/mct.c +++ b/src/lib/openjp2/mct.c @@ -82,9 +82,9 @@ void opj_mct_encode( OPJ_SIZE_T i; const OPJ_SIZE_T len = n; /* buffer are aligned on 16 bytes */ - assert( (uintptr_t)c0 & 16 == 0 ); - assert( (uintptr_t)c1 & 16 == 0 ); - assert( (uintptr_t)c2 & 16 == 0 ); + assert( ((uintptr_t)c0 & 0xf) == 0 ); + assert( ((uintptr_t)c1 & 0xf) == 0 ); + assert( ((uintptr_t)c2 & 0xf) == 0 ); for(i = 0; i < (len & ~3U); i += 4) { __m128i y, u, v; diff --git a/src/lib/openjp2/opj_malloc.c b/src/lib/openjp2/opj_malloc.c index 4c9de50f..85a5716b 100644 --- a/src/lib/openjp2/opj_malloc.c +++ b/src/lib/openjp2/opj_malloc.c @@ -67,7 +67,7 @@ static inline void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t si /* 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 ) + 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 From dd81b5892d5d0709ab981c9311adca81a7a4cd52 Mon Sep 17 00:00:00 2001 From: Mathieu Malaterre Date: Mon, 12 Oct 2015 21:24:10 +0200 Subject: [PATCH 06/18] minor tweaks to the code --- src/lib/openjp2/mct.c | 6 +++--- src/lib/openjp2/opj_malloc.c | 19 ++++++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/lib/openjp2/mct.c b/src/lib/openjp2/mct.c index 6b678345..02259679 100644 --- a/src/lib/openjp2/mct.c +++ b/src/lib/openjp2/mct.c @@ -82,9 +82,9 @@ void opj_mct_encode( OPJ_SIZE_T i; const OPJ_SIZE_T len = n; /* buffer are aligned on 16 bytes */ - assert( ((uintptr_t)c0 & 0xf) == 0 ); - assert( ((uintptr_t)c1 & 0xf) == 0 ); - assert( ((uintptr_t)c2 & 0xf) == 0 ); + assert( ((size_t)c0 & 0xf) == 0 ); + assert( ((size_t)c1 & 0xf) == 0 ); + assert( ((size_t)c2 & 0xf) == 0 ); for(i = 0; i < (len & ~3U); i += 4) { __m128i y, u, v; diff --git a/src/lib/openjp2/opj_malloc.c b/src/lib/openjp2/opj_malloc.c index 85a5716b..30d89258 100644 --- a/src/lib/openjp2/opj_malloc.c +++ b/src/lib/openjp2/opj_malloc.c @@ -34,14 +34,15 @@ #include #include #include +#include static inline void *opj_aligned_alloc_n(size_t alignment, size_t size) { #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 - // superseded by aligned_alloc, it is more portable to older POSIX systems - // that do not support ISO C11. + /* aligned_alloc requires c11, restrict to posix_memalign for now. Quote: + * This function was introduced in POSIX 1003.1d. Although this function is + * superseded by aligned_alloc, it is more portable to older POSIX systems + * that do not support ISO C11. */ void* ptr; if (posix_memalign (&ptr, alignment, size)) { @@ -50,7 +51,7 @@ static inline void *opj_aligned_alloc_n(size_t alignment, size_t size) return ptr; /* older linux */ #elif defined(HAVE_MEMALIGN) - assert( size % alignment == 0 ); + assert( size & (alignment - 1u) == 0 ); return memalign( alignment, size ); /* _MSC_VER */ #elif defined(HAVE__ALIGNED_MALLOC) @@ -62,18 +63,22 @@ static inline void *opj_aligned_alloc_n(size_t alignment, size_t size) } static inline void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t size) { + void *a_ptr; /* 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 ); + assert( alignment > 0 ); /* fast path */ - if( ((uintptr_t)r_ptr & alignment) == 0 ) + /* we simply use `size_t` to cast, since we are only interest in binary AND + * operator */ + if( ((size_t)r_ptr & (alignment - 1u)) == 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); + a_ptr = opj_aligned_alloc_n(alignment, size); /* memory may overlap, do not use memcpy */ memmove(a_ptr, r_ptr, size); free( r_ptr ); From 08238dbed6c5f3051e7631faa6e314512b510366 Mon Sep 17 00:00:00 2001 From: Matthieu Darbois Date: Tue, 13 Oct 2015 23:24:10 +0200 Subject: [PATCH 07/18] Add appveyor status badge & license badge --- README.md | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 17b8957b..9a3fe731 100644 --- a/README.md +++ b/README.md @@ -6,17 +6,20 @@ OpenJPEG is an open-source JPEG 2000 codec written in C language. It has been developed in order to promote the use of [JPEG 2000](http://www.jpeg.org/jpeg2000), a still-image compression standard from the Joint Photographic Experts Group ([JPEG](http://www.jpeg.org)). Since April 2015, it is officially recognized by ISO/IEC and ITU-T as a [JPEG 2000 Reference Software](http://www.itu.int/rec/T-REC-T.804-201504-I!Amd2). ## Who can use the code ? +[![badge-license]][link-license] -Anyone. As the OpenJPEG code is released under the [2-clauses BSD license](https://github.com/uclouvain/openjpeg/blob/master/LICENSE), anyone can use or modify the code, even for commercial applications. The only restriction is to retain the copyright in the sources or in the binaries documentation. Of course, if you modified the code in a way that might be of interest for other users, you are encouraged to share it (through a [github pull request](https://github.com/uclouvain/openjpeg/pulls) or by filling an [issue](https://github.com/uclouvain/openjpeg/issues)) but this is not a requirement. +Anyone. As the OpenJPEG code is released under the [BSD 2-Clause][link-license] license, anyone can use or modify the code, even for commercial applications. The only restriction is to retain the copyright in the sources or in the binaries documentation. Of course, if you modified the code in a way that might be of interest for other users, you are encouraged to share it (through a [github pull request](https://github.com/uclouvain/openjpeg/pulls) or by filling an [issue](https://github.com/uclouvain/openjpeg/issues)) but this is not a requirement. ## How to install and use OpenJPEG ? API Documentation needs a major refactoring. Meanwhile, you can check [installation](https://github.com/uclouvain/openjpeg/wiki/Installation) instructions and [codec documentation](https://github.com/uclouvain/openjpeg/wiki/DocJ2KCodec). ## Current Status -[![Build Status](https://travis-ci.org/uclouvain/openjpeg.svg?branch=master)](https://travis-ci.org/uclouvain/openjpeg) +[![badge-build]][link-build] + +[![badge-msvc-build]][link-msvc-build] + +[![badge-coverity]][link-coverity] -[![Coverity Scan Build Status](https://scan.coverity.com/projects/6383/badge.svg)](https://scan.coverity.com/projects/uclouvain-openjpeg) - ## Who are the developers ? The library is developed and maintained by the Image and Signal Processing Group ([ISPGroup](http://sites.uclouvain.be/ispgroup/)), in the Université catholique de Louvain ([UCL](http://www.uclouvain.be/en-index.html), with the support of the [CNES](https://cnes.fr/), the [CS](http://www.c-s.fr/) company and the [intoPIX](http://www.intopix.com) company. The JPWL module has been developed by the Digital Signal Processing Lab ([DSPLab](http://dsplab.diei.unipg.it/)) of the University of Perugia, Italy ([UNIPG](http://www.unipg.it/)). @@ -47,7 +50,7 @@ The library is developed and maintained by the Image and Signal Processing Group * tests: configuration files and utilities for the openjpeg test suite. All test images are located in [openjpeg-data](https://github.com/uclouvain/openjpeg-data) repository. * cmake: cmake related files -See [LICENSE](https://github.com/uclouvain/openjpeg/blob/master/LICENSE) for license and copyright information. +See [LICENSE][link-license] for license and copyright information. See [INSTALL](https://github.com/uclouvain/openjpeg/blob/master/INSTALL) for installation procedures. @@ -59,8 +62,18 @@ OpenJPEG strives to provide a stable API/ABI for your applications. As such it only exposes a limited subset of its functions. It uses a mechanism of exporting/hiding functions. If you are unsure which functions you can use in your applications, you should compile OpenJPEG using something similar to gcc: -`fvisibility=hidden` compilation flag. +`-fvisibility=hidden` compilation flag. See also: http://gcc.gnu.org/wiki/Visibility On windows, MSVC directly supports export/hiding function and as such the only API available is the one supported by OpenJPEG. + +[comment-license]: https://img.shields.io/github/license/uclouvain/openjpeg.svg "https://img.shields.io/badge/license-BSD_2--Clause-blue.svg" +[badge-license]: https://img.shields.io/badge/license-BSD_2--Clause-blue.svg "License" +[link-license]: https://github.com/uclouvain/openjpeg/blob/master/LICENSE "License" +[badge-build]: https://travis-ci.org/uclouvain/openjpeg.svg?branch=master "Build Status" +[link-build]: https://travis-ci.org/uclouvain/openjpeg "Build Status" +[badge-msvc-build]: https://ci.appveyor.com/api/projects/status/github/uclouvain/openjpeg?branch=master&svg=true "Windows Build Status" +[link-msvc-build]: https://ci.appveyor.com/project/uclouvain/openjpeg/branch/master "Windows Build Status" +[badge-coverity]: https://scan.coverity.com/projects/6383/badge.svg "Coverity Scan Build Status" +[link-coverity]: https://scan.coverity.com/projects/uclouvain-openjpeg "Coverity Scan Build Status" From 65f78eaa73cf4e8a7d3d94840259da0b3cf2fa30 Mon Sep 17 00:00:00 2001 From: Matthieu Darbois Date: Tue, 13 Oct 2015 23:54:18 +0200 Subject: [PATCH 08/18] Use SPDX full name & identifier for license --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9a3fe731..d4e20dba 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ OpenJPEG is an open-source JPEG 2000 codec written in C language. It has been de ## Who can use the code ? [![badge-license]][link-license] -Anyone. As the OpenJPEG code is released under the [BSD 2-Clause][link-license] license, anyone can use or modify the code, even for commercial applications. The only restriction is to retain the copyright in the sources or in the binaries documentation. Of course, if you modified the code in a way that might be of interest for other users, you are encouraged to share it (through a [github pull request](https://github.com/uclouvain/openjpeg/pulls) or by filling an [issue](https://github.com/uclouvain/openjpeg/issues)) but this is not a requirement. +Anyone. As the OpenJPEG code is released under the [BSD 2-clause "Simplified" License][link-license], anyone can use or modify the code, even for commercial applications. The only restriction is to retain the copyright in the sources or in the binaries documentation. Of course, if you modified the code in a way that might be of interest for other users, you are encouraged to share it (through a [github pull request](https://github.com/uclouvain/openjpeg/pulls) or by filling an [issue](https://github.com/uclouvain/openjpeg/issues)) but this is not a requirement. ## How to install and use OpenJPEG ? API Documentation needs a major refactoring. Meanwhile, you can check [installation](https://github.com/uclouvain/openjpeg/wiki/Installation) instructions and [codec documentation](https://github.com/uclouvain/openjpeg/wiki/DocJ2KCodec). @@ -68,9 +68,9 @@ See also: http://gcc.gnu.org/wiki/Visibility On windows, MSVC directly supports export/hiding function and as such the only API available is the one supported by OpenJPEG. -[comment-license]: https://img.shields.io/github/license/uclouvain/openjpeg.svg "https://img.shields.io/badge/license-BSD_2--Clause-blue.svg" -[badge-license]: https://img.shields.io/badge/license-BSD_2--Clause-blue.svg "License" -[link-license]: https://github.com/uclouvain/openjpeg/blob/master/LICENSE "License" +[comment-license]: https://img.shields.io/github/license/uclouvain/openjpeg.svg "https://img.shields.io/badge/license-BSD--2--Clause-blue.svg" +[badge-license]: https://img.shields.io/badge/license-BSD--2--Clause-blue.svg "BSD 2-clause "Simplified" License" +[link-license]: https://github.com/uclouvain/openjpeg/blob/master/LICENSE "BSD 2-clause "Simplified" License" [badge-build]: https://travis-ci.org/uclouvain/openjpeg.svg?branch=master "Build Status" [link-build]: https://travis-ci.org/uclouvain/openjpeg "Build Status" [badge-msvc-build]: https://ci.appveyor.com/api/projects/status/github/uclouvain/openjpeg?branch=master&svg=true "Windows Build Status" From 9729405a8f481bab0a3d638949ee148ab341ee68 Mon Sep 17 00:00:00 2001 From: mayeut Date: Wed, 14 Oct 2015 22:43:39 +0200 Subject: [PATCH 09/18] Add ABI check for PR --- tools/abi-tracker/openjpeg.json | 1 + tools/travis-ci/abi-check.sh | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/tools/abi-tracker/openjpeg.json b/tools/abi-tracker/openjpeg.json index 14653472..2cfa4438 100644 --- a/tools/abi-tracker/openjpeg.json +++ b/tools/abi-tracker/openjpeg.json @@ -6,6 +6,7 @@ "Maintainer": "Antonin D.", "MaintainerUrl": "http://www.openjpeg.org/", "Package": "version.", + "Configure": "-DCMAKE_C_FLAGS='-fvisibility=hidden -g -Og -w -fpermissive'", "Versions": [ { diff --git a/tools/travis-ci/abi-check.sh b/tools/travis-ci/abi-check.sh index 1a904d27..3eb6e18f 100755 --- a/tools/travis-ci/abi-check.sh +++ b/tools/travis-ci/abi-check.sh @@ -55,7 +55,12 @@ cd tracker # Let's create all we need grep -v Git ${OPJ_SOURCE_DIR}/tools/abi-tracker/openjpeg.json > ./openjpeg.json abi-monitor ${OPJ_LIMIT_ABI_BUILDS} -get openjpeg.json -cp -f ${OPJ_SOURCE_DIR}/tools/abi-tracker/openjpeg.json ./openjpeg.json +if [ "${OPJ_LIMIT_ABI_BUILDS}" != "" ]; then + cp -f ${OPJ_SOURCE_DIR}/tools/abi-tracker/openjpeg.json ./openjpeg.json +else + # Old versions of openjpeg don't like -fvisibility=hidden... + grep -v Configure ${OPJ_SOURCE_DIR}/tools/abi-tracker/openjpeg.json > ./openjpeg.json +fi cp -rf ${OPJ_SOURCE_DIR} src/openjpeg/current abi-monitor ${OPJ_LIMIT_ABI_BUILDS} -build openjpeg.json abi-tracker -build openjpeg.json @@ -66,8 +71,11 @@ EXIT_CODE=0 abi-compliance-checker -l openjpeg -old $(find ./abi_dump/openjpeg/2.1 -name '*.dump') -new $(find ./abi_dump/openjpeg/current -name '*.dump') -header openjpeg.h -api -s || EXIT_CODE=1 # Check ABI -# Disabled for now, problems with symbol visibility... -# abi-compliance-checker -l openjpeg -old $(find ./abi_dump/openjpeg/2.1 -name '*.dump') -new $(find ./abi_dump/openjpeg/current -name '*.dump') -header openjpeg.h -abi -s || EXIT_CODE=1 +if [ "${OPJ_LIMIT_ABI_BUILDS}" != "" ]; then + abi-compliance-checker -l openjpeg -old $(find ./abi_dump/openjpeg/2.1 -name '*.dump') -new $(find ./abi_dump/openjpeg/current -name '*.dump') -header openjpeg.h -abi -s || EXIT_CODE=1 +else + echo "Disable ABI check for now, problems with symbol visibility..." +fi rm -rf src installed From e734f0522a6b583a030c3968df04c090a549312a Mon Sep 17 00:00:00 2001 From: mayeut Date: Thu, 15 Oct 2015 00:17:05 +0200 Subject: [PATCH 10/18] Remove useless environment variables --- appveyor.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 255f4b83..d5cc1620 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -5,9 +5,6 @@ branches: skip_tags: true clone_depth: 50 environment: - OPJ_NONCOMMERCIAL: 1 - OPJ_CI_SKIP_TESTS: 0 - OPJ_DO_SUBMIT: 1 matrix: - OPJ_CI_ARCH: x86 OPJ_CI_VSCOMNTOOLS: $(VS140COMNTOOLS) From fd424cfb66a37e5abf456179a618240f166cc218 Mon Sep 17 00:00:00 2001 From: mayeut Date: Thu, 15 Oct 2015 00:58:58 +0200 Subject: [PATCH 11/18] travis-ci: Include add ons in matrix --- .travis.yml | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8fc6169f..07dedbc4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,10 @@ matrix: - os: linux compiler: gcc env: OPJ_CI_ARCH=i386 OPJ_CI_BUILD_CONFIGURATION=Release + addons: + apt: + packages: + - gcc-multilib - os: linux compiler: gcc env: OPJ_CI_ARCH=x86_64 OPJ_CI_BUILD_CONFIGURATION=Debug @@ -20,30 +24,36 @@ matrix: - os: linux compiler: x86_64-w64-mingw32-gcc env: OPJ_CI_ARCH=x86_64 OPJ_CI_BUILD_CONFIGURATION=Release + addons: + apt: + packages: + - gcc-mingw-w64-base + - binutils-mingw-w64-x86-64 + - gcc-mingw-w64-x86-64 + - gcc-mingw-w64 - os: linux compiler: x86_64-w64-mingw32-gcc env: OPJ_CI_ARCH=i386 OPJ_CI_BUILD_CONFIGURATION=Release + addons: + apt: + packages: + - gcc-mingw-w64-base + - binutils-mingw-w64-i686 + - gcc-mingw-w64-i686 + - gcc-mingw-w64 - os: linux compiler: gcc-4.8 env: OPJ_CI_ABI_CHECK=1 - -addons: - apt: - sources: - - ubuntu-toolchain-r-test - packages: - - gcc-4.8 - - gcc-multilib - - gcc-mingw-w64-base - - binutils-mingw-w64-i686 - - binutils-mingw-w64-x86-64 - - gcc-mingw-w64-i686 - - gcc-mingw-w64-x86-64 - - gcc-mingw-w64 - - libelf-dev - - elfutils - - texinfo - - exuberant-ctags + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - gcc-4.8 + - libelf-dev + - elfutils + - texinfo + - exuberant-ctags install: - ./tools/travis-ci/install.sh From b3c581760f8b3da08af27f8121d23beb3a9e48eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20M=C3=BChlstrasser?= Date: Thu, 15 Oct 2015 10:53:33 +0200 Subject: [PATCH 12/18] Fix OpenJPEG GitHub issue #633. "opj_includes.h" must be included before system headers, otherwise inconsistent definitions of configuration macros lead to build failures on AIX. --- src/lib/openjp2/opj_clock.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/openjp2/opj_clock.c b/src/lib/openjp2/opj_clock.c index 0df99ef0..bb4cae73 100644 --- a/src/lib/openjp2/opj_clock.c +++ b/src/lib/openjp2/opj_clock.c @@ -29,6 +29,8 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include "opj_includes.h" + #ifdef _WIN32 #include #else @@ -36,7 +38,6 @@ #include #include #endif /* _WIN32 */ -#include "opj_includes.h" OPJ_FLOAT64 opj_clock(void) { #ifdef _WIN32 From b1a8e1adcb7e5ac35320dcfab226e5f68eaf81af Mon Sep 17 00:00:00 2001 From: Matthieu Darbois Date: Thu, 15 Oct 2015 23:31:42 +0200 Subject: [PATCH 13/18] Change link for appveyor --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d4e20dba..c40f19e5 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,6 @@ API available is the one supported by OpenJPEG. [badge-build]: https://travis-ci.org/uclouvain/openjpeg.svg?branch=master "Build Status" [link-build]: https://travis-ci.org/uclouvain/openjpeg "Build Status" [badge-msvc-build]: https://ci.appveyor.com/api/projects/status/github/uclouvain/openjpeg?branch=master&svg=true "Windows Build Status" -[link-msvc-build]: https://ci.appveyor.com/project/uclouvain/openjpeg/branch/master "Windows Build Status" +[link-msvc-build]: https://ci.appveyor.com/project/detonin/openjpeg/branch/master "Windows Build Status" [badge-coverity]: https://scan.coverity.com/projects/6383/badge.svg "Coverity Scan Build Status" [link-coverity]: https://scan.coverity.com/projects/uclouvain-openjpeg "Coverity Scan Build Status" From 83249c318fa9a133cb992973f84e15d3ba3182e0 Mon Sep 17 00:00:00 2001 From: mayeut Date: Sat, 17 Oct 2015 01:30:23 +0200 Subject: [PATCH 14/18] Fixed crash on encoding Update #624 Update #625 --- src/lib/openjp2/j2k.c | 16 ++++++++++++++-- src/lib/openjp2/opj_malloc.c | 3 +-- src/lib/openjp2/tcd.c | 13 +++++-------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/lib/openjp2/j2k.c b/src/lib/openjp2/j2k.c index c8d8cf1a..a8dc22b9 100644 --- a/src/lib/openjp2/j2k.c +++ b/src/lib/openjp2/j2k.c @@ -9947,6 +9947,7 @@ OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, OPJ_UINT32 l_nb_tiles; OPJ_UINT32 l_max_tile_size = 0, l_current_tile_size; OPJ_BYTE * l_current_data = 00; + OPJ_BOOL l_reuse_data = OPJ_FALSE; opj_tcd_t* p_tcd = 00; /* preconditions */ @@ -9957,6 +9958,17 @@ OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, p_tcd = p_j2k->m_tcd; l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + if (l_nb_tiles == 1) { + l_reuse_data = OPJ_TRUE; +#ifdef __SSE__ + for (j=0;jm_tcd->image->numcomps;++j) { + opj_image_comp_t * l_img_comp = p_tcd->image->comps + j; + if (((size_t)l_img_comp->data & 0xFU) != 0U) { /* tile data shall be aligned on 16 bytes */ + l_reuse_data = OPJ_FALSE; + } + } +#endif + } for (i=0;im_tcd->image->numcomps;++j) { opj_tcd_tilecomp_t* l_tilec = p_tcd->tcd_image->tiles->comps + j; - if (l_nb_tiles == 1) { + if (l_reuse_data) { opj_image_comp_t * l_img_comp = p_tcd->image->comps + j; l_tilec->data = l_img_comp->data; l_tilec->ownsData = OPJ_FALSE; @@ -9984,7 +9996,7 @@ OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, } } l_current_tile_size = opj_tcd_get_encoded_tile_size(p_j2k->m_tcd); - if (l_nb_tiles > 1) { + if (!l_reuse_data) { if (l_current_tile_size > l_max_tile_size) { OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_current_tile_size); if (! l_new_current_data) { diff --git a/src/lib/openjp2/opj_malloc.c b/src/lib/openjp2/opj_malloc.c index 30d89258..2636f076 100644 --- a/src/lib/openjp2/opj_malloc.c +++ b/src/lib/openjp2/opj_malloc.c @@ -79,8 +79,7 @@ static inline void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t si * allocated array (eg. _msize on Windows, malloc_size on MacOS, * malloc_usable_size on systems with glibc) */ a_ptr = opj_aligned_alloc_n(alignment, size); - /* memory may overlap, do not use memcpy */ - memmove(a_ptr, r_ptr, size); + memcpy(a_ptr, r_ptr, size); free( r_ptr ); return a_ptr; /* _MSC_VER */ diff --git a/src/lib/openjp2/tcd.c b/src/lib/openjp2/tcd.c index a5a5d272..5e20a7a0 100644 --- a/src/lib/openjp2/tcd.c +++ b/src/lib/openjp2/tcd.c @@ -635,18 +635,15 @@ 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_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_aligned_free(l_tilec->data); - l_tilec->data = NULL; + /* We don't need to keep old data */ + opj_aligned_free(l_tilec->data); + l_tilec->data = (OPJ_INT32 *) opj_aligned_malloc(l_tilec->data_size_needed); + if (! l_tilec->data ) { l_tilec->data_size = 0; l_tilec->data_size_needed = 0; l_tilec->ownsData = OPJ_FALSE; return OPJ_FALSE; } - l_tilec->data = new_data; /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/ l_tilec->data_size = l_tilec->data_size_needed; l_tilec->ownsData = OPJ_TRUE; @@ -1521,7 +1518,7 @@ static void opj_tcd_free_tile(opj_tcd_t *p_tcd) } if (l_tile_comp->ownsData && l_tile_comp->data) { - opj_free(l_tile_comp->data); + opj_aligned_free(l_tile_comp->data); l_tile_comp->data = 00; l_tile_comp->ownsData = 0; l_tile_comp->data_size = 0; From b3a15954f6bad45b8f2e8ae8f1d97fe40f5d5dc8 Mon Sep 17 00:00:00 2001 From: mayeut Date: Sat, 17 Oct 2015 02:16:17 +0200 Subject: [PATCH 15/18] Add missing checks. Fix crash on failed allocation. --- src/lib/openjp2/opj_malloc.c | 49 +++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/lib/openjp2/opj_malloc.c b/src/lib/openjp2/opj_malloc.c index 2636f076..66ce0316 100644 --- a/src/lib/openjp2/opj_malloc.c +++ b/src/lib/openjp2/opj_malloc.c @@ -5,6 +5,7 @@ * are granted under this license. * * Copyright (c) 2015, Mathieu Malaterre + * Copyright (c) 2015, Matthieu Darbois * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -38,57 +39,65 @@ static inline void *opj_aligned_alloc_n(size_t alignment, size_t size) { + void* ptr; + + /* alignment shall be power of 2 */ + assert( (alignment != 0U) && ((alignment & (alignment - 1U)) == 0U)); + #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 * superseded by aligned_alloc, it is more portable to older POSIX systems * that do not support ISO C11. */ - void* ptr; if (posix_memalign (&ptr, alignment, size)) { ptr = NULL; } - return ptr; /* older linux */ #elif defined(HAVE_MEMALIGN) - assert( size & (alignment - 1u) == 0 ); - return memalign( alignment, size ); + ptr = memalign( alignment, size ); /* _MSC_VER */ #elif defined(HAVE__ALIGNED_MALLOC) - return _aligned_malloc( alignment, size ); + ptr = _aligned_malloc( alignment, size ); #else /* TODO: _mm_malloc(x,y) */ #error missing aligned alloc function #endif + return ptr; } static inline void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t size) { - void *a_ptr; + void *r_ptr; + + /* alignment shall be power of 2 */ + assert( (alignment != 0U) && ((alignment & (alignment - 1U)) == 0U)); + /* 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 ); - assert( alignment > 0 ); - /* fast path */ + r_ptr = realloc( ptr, size ); /* fast path */ /* we simply use `size_t` to cast, since we are only interest in binary AND * operator */ - if( ((size_t)r_ptr & (alignment - 1u)) == 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) */ - a_ptr = opj_aligned_alloc_n(alignment, size); - memcpy(a_ptr, r_ptr, size); - free( r_ptr ); - return a_ptr; + if( ((size_t)r_ptr & (alignment - 1U)) != 0U ) { + /* 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); + if (a_ptr != NULL) { + memcpy(a_ptr, r_ptr, size); + } + free( r_ptr ); + r_ptr = a_ptr; + } /* _MSC_VER */ #elif defined(HAVE__ALIGNED_MALLOC) - return _aligned_realloc( ptr, size, alignment ); + r_ptr = _aligned_realloc( ptr, size, alignment ); #else /* TODO: _mm_malloc(x,y) */ #error missing aligned realloc function #endif + return r_ptr; } void * opj_malloc(size_t size) { From 8034ffde8b108957aeff801c30b1a4c31e147e55 Mon Sep 17 00:00:00 2001 From: mayeut Date: Sat, 17 Oct 2015 02:55:09 +0200 Subject: [PATCH 16/18] Fix inconsistent behavior of malloc(0) Update #635 Update #625 --- src/lib/openjp2/dwt.c | 8 ++++--- src/lib/openjp2/opj_malloc.c | 44 +++++++++++++++++++++++++----------- src/lib/openjp2/tcd.c | 4 ++-- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/lib/openjp2/dwt.c b/src/lib/openjp2/dwt.c index 4ad99ed9..41dca2fe 100644 --- a/src/lib/openjp2/dwt.c +++ b/src/lib/openjp2/dwt.c @@ -567,9 +567,11 @@ static OPJ_BOOL opj_dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 - tr->y0); /* height of the resolution level computed */ OPJ_UINT32 w = (OPJ_UINT32)(tilec->x1 - tilec->x0); - - h.mem = (OPJ_INT32*) - opj_aligned_malloc(opj_dwt_max_resolution(tr, numres) * sizeof(OPJ_INT32)); + + if (numres == 1U) { + return OPJ_TRUE; + } + h.mem = (OPJ_INT32*)opj_aligned_malloc(opj_dwt_max_resolution(tr, numres) * sizeof(OPJ_INT32)); if (! h.mem){ /* FIXME event manager error callback */ return OPJ_FALSE; diff --git a/src/lib/openjp2/opj_malloc.c b/src/lib/openjp2/opj_malloc.c index 66ce0316..beb887bc 100644 --- a/src/lib/openjp2/opj_malloc.c +++ b/src/lib/openjp2/opj_malloc.c @@ -44,6 +44,10 @@ 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)); + if (size == 0U) { /* prevent implementation defined behavior of realloc */ + return NULL; + } + #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 @@ -65,17 +69,21 @@ static inline void *opj_aligned_alloc_n(size_t alignment, size_t size) #endif return ptr; } -static inline void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t size) +static inline void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t new_size) { void *r_ptr; /* alignment shall be power of 2 */ assert( (alignment != 0U) && ((alignment & (alignment - 1U)) == 0U)); + if (new_size == 0U) { /* prevent implementation defined behavior of realloc */ + return NULL; + } + /* no portable aligned realloc */ #if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN) /* glibc doc states one can mixed aligned malloc with realloc */ - r_ptr = realloc( ptr, size ); /* fast path */ + r_ptr = realloc( ptr, new_size ); /* fast path */ /* we simply use `size_t` to cast, since we are only interest in binary AND * operator */ if( ((size_t)r_ptr & (alignment - 1U)) != 0U ) { @@ -83,16 +91,16 @@ static inline void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t si * 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); + void *a_ptr = opj_aligned_alloc_n(alignment, new_size); if (a_ptr != NULL) { - memcpy(a_ptr, r_ptr, size); + memcpy(a_ptr, r_ptr, new_size); } free( r_ptr ); r_ptr = a_ptr; } /* _MSC_VER */ #elif defined(HAVE__ALIGNED_MALLOC) - r_ptr = _aligned_realloc( ptr, size, alignment ); + r_ptr = _aligned_realloc( ptr, new_size, alignment ); #else /* TODO: _mm_malloc(x,y) */ #error missing aligned realloc function @@ -101,20 +109,27 @@ static inline void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t si } void * opj_malloc(size_t size) { + if (size == 0U) { /* prevent implementation defined behavior of realloc */ + return NULL; + } return malloc(size); } -void * opj_calloc(size_t numOfElements, size_t sizeOfElements) +void * opj_calloc(size_t num, size_t size) { - return calloc(numOfElements, sizeOfElements); + if (size == 0U) { /* prevent implementation defined behavior of realloc */ + return NULL; + } + /* according to C89 standard, num == 0 shall return a valid pointer */ + return calloc(num, size); } void *opj_aligned_malloc(size_t size) { - return opj_aligned_alloc_n(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); + return opj_aligned_realloc_n(ptr, 16U, size); } void opj_aligned_free(void* ptr) @@ -126,11 +141,14 @@ void opj_aligned_free(void* ptr) #endif } -void * opj_realloc(void * m, size_t s) +void * opj_realloc(void *ptr, size_t new_size) { - return realloc(m,s); + if (new_size == 0U) { /* prevent implementation defined behavior of realloc */ + return NULL; + } + return realloc(ptr, new_size); } -void opj_free(void * m) +void opj_free(void *ptr) { - free(m); + free(ptr); } diff --git a/src/lib/openjp2/tcd.c b/src/lib/openjp2/tcd.c index 5e20a7a0..6eeb211e 100644 --- a/src/lib/openjp2/tcd.c +++ b/src/lib/openjp2/tcd.c @@ -871,7 +871,7 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * fraction; l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits - 1; /* WHY -1 ? */ - if (! l_band->precincts) { + if (!l_band->precincts && (l_nb_precincts > 0U)) { l_band->precincts = (opj_tcd_precinct_t *) opj_malloc( /*3 * */ l_nb_precinct_size); if (! l_band->precincts) { return OPJ_FALSE; @@ -930,7 +930,7 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */ l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block; - if (! l_current_precinct->cblks.blocks) { + if (!l_current_precinct->cblks.blocks && (l_nb_code_blocks > 0U)) { l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size); if (! l_current_precinct->cblks.blocks ) { return OPJ_FALSE; From f9d47e28b92f0e1ee3182a3a7375b886f3fad2fd Mon Sep 17 00:00:00 2001 From: mayeut Date: Sun, 18 Oct 2015 02:23:01 +0200 Subject: [PATCH 17/18] Fix _aligned_malloc usage --- src/lib/openjp2/opj_malloc.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/lib/openjp2/opj_malloc.c b/src/lib/openjp2/opj_malloc.c index beb887bc..9c1a6cc8 100644 --- a/src/lib/openjp2/opj_malloc.c +++ b/src/lib/openjp2/opj_malloc.c @@ -30,14 +30,13 @@ * POSSIBILITY OF SUCH DAMAGE. */ #define OPJ_SKIP_POISON -#include "opj_malloc.h" -#include "opj_config_private.h" +#include "opj_includes.h" #include #include #include #include -static inline void *opj_aligned_alloc_n(size_t alignment, size_t size) +static INLINE void *opj_aligned_alloc_n(size_t alignment, size_t size) { void* ptr; @@ -62,14 +61,14 @@ static inline void *opj_aligned_alloc_n(size_t alignment, size_t size) ptr = memalign( alignment, size ); /* _MSC_VER */ #elif defined(HAVE__ALIGNED_MALLOC) - ptr = _aligned_malloc( alignment, size ); + ptr = _aligned_malloc(size, alignment); #else /* TODO: _mm_malloc(x,y) */ #error missing aligned alloc function #endif return ptr; } -static inline void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t new_size) +static INLINE void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t new_size) { void *r_ptr; From e1122a8f5081712147a5b36a7b0c9dd81f11c98d Mon Sep 17 00:00:00 2001 From: mayeut Date: Sun, 18 Oct 2015 02:52:33 +0200 Subject: [PATCH 18/18] Cleanup includes --- src/lib/openjp2/opj_malloc.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/lib/openjp2/opj_malloc.c b/src/lib/openjp2/opj_malloc.c index 9c1a6cc8..3bbf80d8 100644 --- a/src/lib/openjp2/opj_malloc.c +++ b/src/lib/openjp2/opj_malloc.c @@ -31,10 +31,6 @@ */ #define OPJ_SKIP_POISON #include "opj_includes.h" -#include -#include -#include -#include static INLINE void *opj_aligned_alloc_n(size_t alignment, size_t size) {