2 * Copyright (c) 2005, Herv� Drolon, FreeImage Team
3 * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
4 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 #ifndef __OPJ_MALLOC_H
29 #define __OPJ_MALLOC_H
32 @brief Internal functions
34 The functions in opj_malloc.h are internal utilities used for memory management.
37 #include "opj_includes.h"
38 /** @defgroup MISC MISC - Miscellaneous internal functions */
41 /** @name Exported functions */
43 /* ----------------------------------------------------------------------- */
46 Allocate an uninitialized memory block
47 @param size Bytes to allocate
48 @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
50 #define opj_malloc(size) malloc(size)
51 #define my_opj_malloc(size) malloc(size)
54 Allocate a memory block with elements initialized to 0
55 @param num Blocks to allocate
56 @param size Bytes per block to allocate
57 @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
59 #define opj_calloc(num, size) calloc(num, size)
62 Allocate memory aligned to a 16 byte boundry
63 @param size Bytes to allocate
64 @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
66 /* FIXME: These should be set with cmake tests, but we're currently not requiring use of cmake */
68 /* Someone should tell the mingw people that their malloc.h ought to provide _mm_malloc() */
70 #include <mm_malloc.h>
71 #define HAVE_MM_MALLOC
72 #else /* MSVC, Intel C++ */
75 #define HAVE_MM_MALLOC
81 /* Linux x86_64 and OSX always align allocations to 16 bytes */
82 #elif !defined(__amd64__) && !defined(__APPLE__)
83 /* FIXME: Yes, this is a big assumption */
84 #define HAVE_POSIX_MEMALIGN
88 #define opj_aligned_malloc(size) malloc(size)
89 #define opj_aligned_free(m) free(m)
92 #undef opj_aligned_malloc
93 #define opj_aligned_malloc(size) _mm_malloc(size, 16)
94 #undef opj_aligned_free
95 #define opj_aligned_free(m) _mm_free(m)
99 extern void* memalign(size_t, size_t);
100 #undef opj_aligned_malloc
101 #define opj_aligned_malloc(size) memalign(16, (size))
102 #undef opj_aligned_free
103 #define opj_aligned_free(m) free(m)
106 #ifdef HAVE_POSIX_MEMALIGN
107 #undef opj_aligned_malloc
108 extern int posix_memalign(void**, size_t, size_t);
110 static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){
112 posix_memalign(&mem, 16, size);
115 #undef opj_aligned_free
116 #define opj_aligned_free(m) free(m)
120 Reallocate memory blocks.
121 @param memblock Pointer to previously allocated memory block
122 @param size New size in bytes
123 @return Returns a void pointer to the reallocated (and possibly moved) memory block
125 #define opj_realloc(m, s) realloc(m, s)
126 #define my_opj_realloc(m,s) realloc(m,s)
130 Deallocates or frees a memory block.
131 @param memblock Previously allocated memory block to be freed
133 #define opj_free(m) free(m)
136 #pragma GCC poison malloc calloc realloc free
139 /* ----------------------------------------------------------------------- */
144 #endif /* __OPJ_MALLOC_H */