Home | History | Annotate | Download | only in libopenjpeg20
      1 /*
      2  * The copyright in this software is being made available under the 2-clauses
      3  * BSD License, included below. This software may be subject to other third
      4  * party and contributor rights, including patent rights, and no such rights
      5  * are granted under this license.
      6  *
      7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
      8  * Copyright (c) 2007, Callum Lerwick <seg (at) haxxed.com>
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
     21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 #ifndef __OPJ_MALLOC_H
     33 #define __OPJ_MALLOC_H
     34 /**
     35 @file opj_malloc.h
     36 @brief Internal functions
     37 
     38 The functions in opj_malloc.h are internal utilities used for memory management.
     39 */
     40 
     41 /** @defgroup MISC MISC - Miscellaneous internal functions */
     42 /*@{*/
     43 
     44 /** @name Exported functions */
     45 /*@{*/
     46 /* ----------------------------------------------------------------------- */
     47 
     48 /**
     49 Allocate an uninitialized memory block
     50 @param size Bytes to allocate
     51 @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
     52 */
     53 #define _FOXIT_MEM_MANAGER_
     54 #ifdef _FOXIT_MEM_MANAGER_
     55 void* opj_malloc(size_t size);
     56 void* opj_calloc(size_t _NumOfElements, size_t _SizeOfElements);
     57 void* opj_realloc(void * m, size_t s);
     58 void opj_free(void * m);
     59 
     60 #define opj_aligned_malloc(size) opj_malloc(size)
     61 #define opj_aligned_free(m) opj_free(m)
     62 #else
     63 #ifdef ALLOC_PERF_OPT
     64 void * OPJ_CALLCONV opj_malloc(size_t size);
     65 #else
     66 /* prevent assertion on overflow for MSVC */
     67 #ifdef _MSC_VER
     68 #define opj_malloc(size) ((size_t)(size) >= (size_t)-0x100 ? NULL : malloc(size))
     69 #else
     70 #define opj_malloc(size) malloc(size)
     71 #endif
     72 #endif
     73 
     74 /**
     75 Allocate a memory block with elements initialized to 0
     76 @param num Blocks to allocate
     77 @param size Bytes per block to allocate
     78 @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
     79 */
     80 #ifdef ALLOC_PERF_OPT
     81 void * OPJ_CALLCONV opj_calloc(size_t _NumOfElements, size_t _SizeOfElements);
     82 #else
     83 /* prevent assertion on overflow for MSVC */
     84 #ifdef _MSC_VER
     85 #define opj_calloc(num, size) ((size_t)(num) != 0 && (size_t)(num) >= (size_t)-0x100 / (size_t)(size) ? NULL : calloc(num, size))
     86 #else
     87 #define opj_calloc(num, size) calloc(num, size)
     88 #endif
     89 #endif
     90 
     91 /**
     92 Allocate memory aligned to a 16 byte boundry
     93 @param size Bytes to allocate
     94 @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
     95 */
     96 /* FIXME: These should be set with cmake tests, but we're currently not requiring use of cmake */
     97 #ifdef _WIN32
     98 	/* Someone should tell the mingw people that their malloc.h ought to provide _mm_malloc() */
     99 	#ifdef __GNUC__
    100 		#include <mm_malloc.h>
    101 		#define HAVE_MM_MALLOC
    102 	#else /* MSVC, Intel C++ */
    103 		#include <malloc.h>
    104 		#ifdef _mm_malloc
    105 			#define HAVE_MM_MALLOC
    106 		#endif
    107 	#endif
    108 #else /* Not _WIN32 */
    109 	#if defined(__sun)
    110 		#define HAVE_MEMALIGN
    111   #elif defined(__FreeBSD__)
    112     #define HAVE_POSIX_MEMALIGN
    113 	/* Linux x86_64 and OSX always align allocations to 16 bytes */
    114 	#elif !defined(__amd64__) && !defined(__APPLE__) && !defined(_AIX)
    115 		#define HAVE_MEMALIGN
    116 		#include <malloc.h>
    117 	#endif
    118 #endif
    119 
    120 #define opj_aligned_malloc(size) malloc(size)
    121 #define opj_aligned_free(m) free(m)
    122 
    123 #ifdef HAVE_MM_MALLOC
    124 	#undef opj_aligned_malloc
    125 	#define opj_aligned_malloc(size) _mm_malloc(size, 16)
    126 	#undef opj_aligned_free
    127 	#define opj_aligned_free(m) _mm_free(m)
    128 #endif
    129 
    130 #ifdef HAVE_MEMALIGN
    131 	extern void* memalign(size_t, size_t);
    132 	#undef opj_aligned_malloc
    133 	#define opj_aligned_malloc(size) memalign(16, (size))
    134 	#undef opj_aligned_free
    135 	#define opj_aligned_free(m) free(m)
    136 #endif
    137 
    138 #ifdef HAVE_POSIX_MEMALIGN
    139 	#undef opj_aligned_malloc
    140 	extern int posix_memalign(void**, size_t, size_t);
    141 
    142 	static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){
    143 		void* mem = NULL;
    144 		posix_memalign(&mem, 16, size);
    145 		return mem;
    146 	}
    147 	#undef opj_aligned_free
    148 	#define opj_aligned_free(m) free(m)
    149 #endif
    150 
    151 #ifdef ALLOC_PERF_OPT
    152 	#undef opj_aligned_malloc
    153 	#define opj_aligned_malloc(size) opj_malloc(size)
    154 	#undef opj_aligned_free
    155 	#define opj_aligned_free(m) opj_free(m)
    156 #endif
    157 
    158 /**
    159 Reallocate memory blocks.
    160 @param m Pointer to previously allocated memory block
    161 @param s New size in bytes
    162 @return Returns a void pointer to the reallocated (and possibly moved) memory block
    163 */
    164 #ifdef ALLOC_PERF_OPT
    165 void * OPJ_CALLCONV opj_realloc(void * m, size_t s);
    166 #else
    167 /* prevent assertion on overflow for MSVC */
    168 #ifdef _MSC_VER
    169 #define opj_realloc(m, s) ((size_t)(s) >= (size_t)-0x100 ? NULL : realloc(m, s))
    170 #else
    171 #define opj_realloc(m, s) realloc(m, s)
    172 #endif
    173 #endif
    174 
    175 /**
    176 Deallocates or frees a memory block.
    177 @param m Previously allocated memory block to be freed
    178 */
    179 #ifdef ALLOC_PERF_OPT
    180 void OPJ_CALLCONV opj_free(void * m);
    181 #else
    182 #define opj_free(m) free(m)
    183 #endif
    184 
    185 #ifdef __GNUC__
    186 #pragma GCC poison malloc calloc realloc free
    187 #endif
    188 #endif
    189 /* ----------------------------------------------------------------------- */
    190 /*@}*/
    191 
    192 /*@}*/
    193 
    194 #endif /* __OPJ_MALLOC_H */
    195