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