OpenJPEG  1.5.1
opj_malloc.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005, Herve Drolon, FreeImage Team
00003  * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
00016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00025  * POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 #ifndef __OPJ_MALLOC_H
00028 #define __OPJ_MALLOC_H
00029 
00038 
00041 /* ----------------------------------------------------------------------- */
00042 
00048 #ifdef ALLOC_PERF_OPT
00049 void * OPJ_CALLCONV opj_malloc(size_t size);
00050 #else
00051 #define opj_malloc(size) malloc(size)
00052 #endif
00053 
00060 #ifdef ALLOC_PERF_OPT
00061 void * OPJ_CALLCONV opj_calloc(size_t _NumOfElements, size_t _SizeOfElements);
00062 #else
00063 #define opj_calloc(num, size) calloc(num, size)
00064 #endif
00065 
00071 /* FIXME: These should be set with cmake tests, but we're currently not requiring use of cmake */
00072 #ifdef _WIN32
00073         /* Someone should tell the mingw people that their malloc.h ought to provide _mm_malloc() */
00074         #ifdef __GNUC__
00075                 #include <mm_malloc.h>
00076                 #define HAVE_MM_MALLOC
00077         #else /* MSVC, Intel C++ */
00078                 #include <malloc.h>
00079                 #ifdef _mm_malloc
00080                         #define HAVE_MM_MALLOC
00081                 #endif
00082         #endif
00083 #else /* Not _WIN32 */
00084         #if defined(__sun)
00085                 #define HAVE_MEMALIGN
00086   #elif defined(__FreeBSD__)
00087     #define HAVE_POSIX_MEMALIGN
00088         /* Linux x86_64 and OSX always align allocations to 16 bytes */
00089         #elif !defined(__amd64__) && !defined(__APPLE__) && !defined(_AIX)
00090                 #define HAVE_MEMALIGN
00091                 #include <malloc.h>                     
00092         #endif
00093 #endif
00094 
00095 #define opj_aligned_malloc(size) malloc(size)
00096 #define opj_aligned_free(m) free(m)
00097 
00098 #ifdef HAVE_MM_MALLOC
00099         #undef opj_aligned_malloc
00100         #define opj_aligned_malloc(size) _mm_malloc(size, 16)
00101         #undef opj_aligned_free
00102         #define opj_aligned_free(m) _mm_free(m)
00103 #endif
00104 
00105 #ifdef HAVE_MEMALIGN
00106         extern void* memalign(size_t, size_t);
00107         #undef opj_aligned_malloc
00108         #define opj_aligned_malloc(size) memalign(16, (size))
00109         #undef opj_aligned_free
00110         #define opj_aligned_free(m) free(m)
00111 #endif
00112 
00113 #ifdef HAVE_POSIX_MEMALIGN
00114         #undef opj_aligned_malloc
00115         extern int posix_memalign(void**, size_t, size_t);
00116 
00117         static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){
00118                 void* mem = NULL;
00119                 posix_memalign(&mem, 16, size);
00120                 return mem;
00121         }
00122         #undef opj_aligned_free
00123         #define opj_aligned_free(m) free(m)
00124 #endif
00125 
00126 #ifdef ALLOC_PERF_OPT
00127         #undef opj_aligned_malloc
00128         #define opj_aligned_malloc(size) opj_malloc(size)
00129         #undef opj_aligned_free
00130         #define opj_aligned_free(m) opj_free(m)
00131 #endif
00132 
00139 #ifdef ALLOC_PERF_OPT
00140 void * OPJ_CALLCONV opj_realloc(void * m, size_t s);
00141 #else
00142 #define opj_realloc(m, s) realloc(m, s)
00143 #endif
00144 
00149 #ifdef ALLOC_PERF_OPT
00150 void OPJ_CALLCONV opj_free(void * m);
00151 #else
00152 #define opj_free(m) free(m)
00153 #endif
00154 
00155 #ifdef __GNUC__
00156 #pragma GCC poison malloc calloc realloc free
00157 #endif
00158 
00159 /* ----------------------------------------------------------------------- */
00163 
00164 #endif /* __OPJ_MALLOC_H */
00165