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