OpenJPEG 2.1.0

opj_malloc.h

Go to the documentation of this file.
00001 /*
00002  * The copyright in this software is being made available under the 2-clauses 
00003  * BSD License, included below. This software may be subject to other third 
00004  * party and contributor rights, including patent rights, and no such rights
00005  * are granted under this license.
00006  *
00007  * Copyright (c) 2005, Herve Drolon, FreeImage Team
00008  * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
00009  * All rights reserved.
00010  *
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  * 1. Redistributions of source code must retain the above copyright
00015  *    notice, this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright
00017  *    notice, this list of conditions and the following disclaimer in the
00018  *    documentation and/or other materials provided with the distribution.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
00021  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00024  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030  * POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 #ifndef __OPJ_MALLOC_H
00033 #define __OPJ_MALLOC_H
00034 
00043 
00046 /* ----------------------------------------------------------------------- */
00047 
00053 #ifdef ALLOC_PERF_OPT
00054 void * OPJ_CALLCONV opj_malloc(size_t size);
00055 #else
00056 /* prevent assertion on overflow for MSVC */
00057 #ifdef _MSC_VER
00058 #define opj_malloc(size) ((size_t)(size) >= (size_t)-0x100 ? NULL : malloc(size))
00059 #else
00060 #define opj_malloc(size) malloc(size)
00061 #endif
00062 #endif
00063 
00070 #ifdef ALLOC_PERF_OPT
00071 void * OPJ_CALLCONV opj_calloc(size_t _NumOfElements, size_t _SizeOfElements);
00072 #else
00073 /* prevent assertion on overflow for MSVC */
00074 #ifdef _MSC_VER
00075 #define opj_calloc(num, size) ((size_t)(num) != 0 && (size_t)(num) >= (size_t)-0x100 / (size_t)(size) ? NULL : calloc(num, size))
00076 #else
00077 #define opj_calloc(num, size) calloc(num, size)
00078 #endif
00079 #endif
00080 
00086 /* FIXME: These should be set with cmake tests, but we're currently not requiring use of cmake */
00087 #ifdef _WIN32
00088         /* Someone should tell the mingw people that their malloc.h ought to provide _mm_malloc() */
00089         #ifdef __GNUC__
00090                 #include <mm_malloc.h>
00091                 #define HAVE_MM_MALLOC
00092         #else /* MSVC, Intel C++ */
00093                 #include <malloc.h>
00094                 #ifdef _mm_malloc
00095                         #define HAVE_MM_MALLOC
00096                 #endif
00097         #endif
00098 #else /* Not _WIN32 */
00099         #if defined(__sun)
00100                 #define HAVE_MEMALIGN
00101   #elif defined(__FreeBSD__)
00102     #define HAVE_POSIX_MEMALIGN
00103         /* Linux x86_64 and OSX always align allocations to 16 bytes */
00104         #elif !defined(__amd64__) && !defined(__APPLE__) && !defined(_AIX)
00105                 #define HAVE_MEMALIGN
00106                 #include <malloc.h>                     
00107         #endif
00108 #endif
00109 
00110 #define opj_aligned_malloc(size) malloc(size)
00111 #define opj_aligned_free(m) free(m)
00112 
00113 #ifdef HAVE_MM_MALLOC
00114         #undef opj_aligned_malloc
00115         #define opj_aligned_malloc(size) _mm_malloc(size, 16)
00116         #undef opj_aligned_free
00117         #define opj_aligned_free(m) _mm_free(m)
00118 #endif
00119 
00120 #ifdef HAVE_MEMALIGN
00121         extern void* memalign(size_t, size_t);
00122         #undef opj_aligned_malloc
00123         #define opj_aligned_malloc(size) memalign(16, (size))
00124         #undef opj_aligned_free
00125         #define opj_aligned_free(m) free(m)
00126 #endif
00127 
00128 #ifdef HAVE_POSIX_MEMALIGN
00129         #undef opj_aligned_malloc
00130         extern int posix_memalign(void**, size_t, size_t);
00131 
00132         static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){
00133                 void* mem = NULL;
00134                 posix_memalign(&mem, 16, size);
00135                 return mem;
00136         }
00137         #undef opj_aligned_free
00138         #define opj_aligned_free(m) free(m)
00139 #endif
00140 
00141 #ifdef ALLOC_PERF_OPT
00142         #undef opj_aligned_malloc
00143         #define opj_aligned_malloc(size) opj_malloc(size)
00144         #undef opj_aligned_free
00145         #define opj_aligned_free(m) opj_free(m)
00146 #endif
00147 
00154 #ifdef ALLOC_PERF_OPT
00155 void * OPJ_CALLCONV opj_realloc(void * m, size_t s);
00156 #else
00157 /* prevent assertion on overflow for MSVC */
00158 #ifdef _MSC_VER
00159 #define opj_realloc(m, s) ((size_t)(s) >= (size_t)-0x100 ? NULL : realloc(m, s))
00160 #else
00161 #define opj_realloc(m, s) realloc(m, s)
00162 #endif
00163 #endif
00164 
00169 #ifdef ALLOC_PERF_OPT
00170 void OPJ_CALLCONV opj_free(void * m);
00171 #else
00172 #define opj_free(m) free(m)
00173 #endif
00174 
00175 #ifdef __GNUC__
00176 #pragma GCC poison malloc calloc realloc free
00177 #endif
00178 
00179 /* ----------------------------------------------------------------------- */
00183 
00184 #endif /* __OPJ_MALLOC_H */
00185