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