1f117b6a5c85c4f18abf6e138a5171ac7ccb8892
[openjpeg.git] / libopenjpeg / opj_malloc.h
1 /*\r
2  * Copyright (c) 2005, Herv� 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 #define opj_malloc(size) malloc(size)\r
49 \r
50 /**\r
51 Allocate a memory block with elements initialized to 0\r
52 @param num Blocks to allocate\r
53 @param size Bytes per block to allocate\r
54 @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available\r
55 */\r
56 #define opj_calloc(num, size) calloc(num, size)\r
57 \r
58 /**\r
59 Allocate memory aligned to a 16 byte boundry\r
60 @param size Bytes to allocate\r
61 @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available\r
62 */\r
63 /* FIXME: These should be set with cmake tests, but we're currently not requiring use of cmake */\r
64 #ifdef WIN32\r
65         /* Someone should tell the mingw people that their malloc.h ought to provide _mm_malloc() */\r
66         #ifdef __GNUC__\r
67                 #include <mm_malloc.h>\r
68                 #define HAVE_MM_MALLOC\r
69         #else /* MSVC, Intel C++ */\r
70                 #include <malloc.h>\r
71                 #ifdef _mm_malloc\r
72                         #define HAVE_MM_MALLOC\r
73                 #endif\r
74         #endif\r
75 #else /* Not WIN32 */\r
76         #if defined(__sun)\r
77                         #define HAVE_MEMALIGN\r
78                 #elif defined(__GNUC__)\r
79                         #ifndef __APPLE__\r
80                                 #define HAVE_MEMALIGN\r
81                                 #include <malloc.h>\r
82                         #endif\r
83                 /* Linux x86_64 and OSX always align allocations to 16 bytes */\r
84                 #elif !defined(__amd64__) && !defined(__APPLE__)        \r
85                         /* FIXME: Yes, this is a big assumption */\r
86                         #define HAVE_POSIX_MEMALIGN\r
87         #endif\r
88 #endif\r
89 \r
90 \r
91 \r
92 #define opj_aligned_malloc(size) malloc(size)\r
93 #define opj_aligned_free(m) free(m)\r
94 \r
95 #ifdef HAVE_MM_MALLOC\r
96         #undef opj_aligned_malloc\r
97         #define opj_aligned_malloc(size) _mm_malloc(size, 16)\r
98         #undef opj_aligned_free\r
99         #define opj_aligned_free(m) _mm_free(m)\r
100 #endif\r
101 \r
102 #ifdef HAVE_MEMALIGN\r
103         extern void* memalign(size_t, size_t);\r
104         #undef opj_aligned_malloc\r
105         #define opj_aligned_malloc(size) memalign(16, (size))\r
106         #undef opj_aligned_free\r
107         #define opj_aligned_free(m) free(m)\r
108 #endif\r
109 \r
110 #ifdef HAVE_POSIX_MEMALIGN\r
111         #undef opj_aligned_malloc\r
112         extern int posix_memalign(void**, size_t, size_t);\r
113 \r
114         static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){\r
115                 void* mem = NULL;\r
116                 posix_memalign(&mem, 16, size);\r
117                 return mem;\r
118         }\r
119         #undef opj_aligned_free\r
120         #define opj_aligned_free(m) free(m)\r
121 #endif\r
122 \r
123 /**\r
124 Reallocate memory blocks.\r
125 @param memblock Pointer to previously allocated memory block\r
126 @param size New size in bytes\r
127 @return Returns a void pointer to the reallocated (and possibly moved) memory block\r
128 */\r
129 #define opj_realloc(m, s) realloc(m, s)\r
130 \r
131 /**\r
132 Deallocates or frees a memory block.\r
133 @param memblock Previously allocated memory block to be freed\r
134 */\r
135 #define opj_free(m) free(m)\r
136 \r
137 #ifdef __GNUC__\r
138 #pragma GCC poison malloc calloc realloc free\r
139 #endif\r
140 \r
141 /* ----------------------------------------------------------------------- */\r
142 /*@}*/\r
143 \r
144 /*@}*/\r
145 \r
146 #endif /* __OPJ_MALLOC_H */\r
147 \r