OpenJPEG  1.5.0
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         /* Linux x86_64 and OSX always align allocations to 16 bytes */
00087         #elif !defined(__amd64__) && !defined(__APPLE__)        
00088                 #define HAVE_MEMALIGN
00089                 #include <malloc.h>                     
00090         #endif
00091 #endif
00092 
00093 #define opj_aligned_malloc(size) malloc(size)
00094 #define opj_aligned_free(m) free(m)
00095 
00096 #ifdef HAVE_MM_MALLOC
00097         #undef opj_aligned_malloc
00098         #define opj_aligned_malloc(size) _mm_malloc(size, 16)
00099         #undef opj_aligned_free
00100         #define opj_aligned_free(m) _mm_free(m)
00101 #endif
00102 
00103 #ifdef HAVE_MEMALIGN
00104         extern void* memalign(size_t, size_t);
00105         #undef opj_aligned_malloc
00106         #define opj_aligned_malloc(size) memalign(16, (size))
00107         #undef opj_aligned_free
00108         #define opj_aligned_free(m) free(m)
00109 #endif
00110 
00111 #ifdef HAVE_POSIX_MEMALIGN
00112         #undef opj_aligned_malloc
00113         extern int posix_memalign(void**, size_t, size_t);
00114 
00115         static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){
00116                 void* mem = NULL;
00117                 posix_memalign(&mem, 16, size);
00118                 return mem;
00119         }
00120         #undef opj_aligned_free
00121         #define opj_aligned_free(m) free(m)
00122 #endif
00123 
00124 #ifdef ALLOC_PERF_OPT
00125         #undef opj_aligned_malloc
00126         #define opj_aligned_malloc(size) opj_malloc(size)
00127         #undef opj_aligned_free
00128         #define opj_aligned_free(m) opj_free(m)
00129 #endif
00130 
00137 #ifdef ALLOC_PERF_OPT
00138 void * OPJ_CALLCONV opj_realloc(void * m, size_t s);
00139 #else
00140 #define opj_realloc(m, s) realloc(m, s)
00141 #endif
00142 
00147 #ifdef ALLOC_PERF_OPT
00148 void OPJ_CALLCONV opj_free(void * m);
00149 #else
00150 #define opj_free(m) free(m)
00151 #endif
00152 
00153 #ifdef __GNUC__
00154 #pragma GCC poison malloc calloc realloc free
00155 #endif
00156 
00157 /* ----------------------------------------------------------------------- */
00161 
00162 #endif /* __OPJ_MALLOC_H */
00163