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