2 * Copyright (c) 2005, Herve Drolon, FreeImage Team
3 * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
27 #ifndef __OPJ_MALLOC_H
28 #define __OPJ_MALLOC_H
31 @brief Internal functions
33 The functions in opj_malloc.h are internal utilities used for memory management.
36 /** @defgroup MISC MISC - Miscellaneous internal functions */
39 /** @name Exported functions */
41 /* ----------------------------------------------------------------------- */
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
49 void * OPJ_CALLCONV opj_malloc(size_t size);
51 /* prevent assertion on overflow for MSVC */
52 #define opj_malloc(size) ((size_t)(size) >= (size_t)-0x100 ? NULL : malloc(size))
56 Allocate a memory block with elements initialized to 0
57 @param num Blocks to allocate
58 @param size Bytes per block to allocate
59 @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
62 void * OPJ_CALLCONV opj_calloc(size_t _NumOfElements, size_t _SizeOfElements);
64 /* prevent assertion on overflow for MSVC */
65 #define opj_calloc(num, size) ((size_t)(num) != 0 && (size_t)(num) >= (size_t)-0x100 / (size_t)(size) ? NULL : calloc(num, size))
69 Allocate memory aligned to a 16 byte boundry
70 @param size Bytes to allocate
71 @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
73 /* FIXME: These should be set with cmake tests, but we're currently not requiring use of cmake */
75 /* Someone should tell the mingw people that their malloc.h ought to provide _mm_malloc() */
77 #include <mm_malloc.h>
78 #define HAVE_MM_MALLOC
79 #else /* MSVC, Intel C++ */
82 #define HAVE_MM_MALLOC
85 #else /* Not _WIN32 */
88 #elif defined(__FreeBSD__)
89 #define HAVE_POSIX_MEMALIGN
90 /* Linux x86_64 and OSX always align allocations to 16 bytes */
91 #elif !defined(__amd64__) && !defined(__APPLE__) && !defined(_AIX)
97 #define opj_aligned_malloc(size) malloc(size)
98 #define opj_aligned_free(m) free(m)
100 #ifdef HAVE_MM_MALLOC
101 #undef opj_aligned_malloc
102 #define opj_aligned_malloc(size) _mm_malloc(size, 16)
103 #undef opj_aligned_free
104 #define opj_aligned_free(m) _mm_free(m)
108 extern void* memalign(size_t, size_t);
109 #undef opj_aligned_malloc
110 #define opj_aligned_malloc(size) memalign(16, (size))
111 #undef opj_aligned_free
112 #define opj_aligned_free(m) free(m)
115 #ifdef HAVE_POSIX_MEMALIGN
116 #undef opj_aligned_malloc
117 extern int posix_memalign(void**, size_t, size_t);
119 static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){
121 posix_memalign(&mem, 16, size);
124 #undef opj_aligned_free
125 #define opj_aligned_free(m) free(m)
128 #ifdef ALLOC_PERF_OPT
129 #undef opj_aligned_malloc
130 #define opj_aligned_malloc(size) opj_malloc(size)
131 #undef opj_aligned_free
132 #define opj_aligned_free(m) opj_free(m)
136 Reallocate memory blocks.
137 @param m Pointer to previously allocated memory block
138 @param s New size in bytes
139 @return Returns a void pointer to the reallocated (and possibly moved) memory block
141 #ifdef ALLOC_PERF_OPT
142 void * OPJ_CALLCONV opj_realloc(void * m, size_t s);
144 /* prevent assertion on overflow for MSVC */
145 #define opj_realloc(m, s) ((size_t)(s) >= (size_t)-0x100 ? NULL : realloc(m, s))
149 Deallocates or frees a memory block.
150 @param m Previously allocated memory block to be freed
152 #ifdef ALLOC_PERF_OPT
153 void OPJ_CALLCONV opj_free(void * m);
155 #define opj_free(m) free(m)
159 #pragma GCC poison malloc calloc realloc free
162 /* ----------------------------------------------------------------------- */
167 #endif /* __OPJ_MALLOC_H */