OpenJPEG  1.5.1
opj_malloc.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005, Herve Drolon, FreeImage Team
3  * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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.
26  */
27 #ifndef __OPJ_MALLOC_H
28 #define __OPJ_MALLOC_H
29 
38 
41 /* ----------------------------------------------------------------------- */
42 
48 #ifdef ALLOC_PERF_OPT
49 void * OPJ_CALLCONV opj_malloc(size_t size);
50 #else
51 #define opj_malloc(size) calloc(1, size)
52 #endif
53 
60 #ifdef ALLOC_PERF_OPT
61 void * OPJ_CALLCONV opj_calloc(size_t _NumOfElements, size_t _SizeOfElements);
62 #else
63 #define opj_calloc(num, size) calloc(num, size)
64 #endif
65 
71 /* FIXME: These should be set with cmake tests, but we're currently not requiring use of cmake */
72 #ifdef _WIN32
73  /* Someone should tell the mingw people that their malloc.h ought to provide _mm_malloc() */
74  #ifdef __GNUC__
75  #include <mm_malloc.h>
76  #define HAVE_MM_MALLOC
77  #else /* MSVC, Intel C++ */
78  #include <malloc.h>
79  #ifdef _mm_malloc
80  #define HAVE_MM_MALLOC
81  #endif
82  #endif
83 #else /* Not _WIN32 */
84  #if defined(__sun)
85  #define HAVE_MEMALIGN
86  #elif defined(__FreeBSD__)
87  #define HAVE_POSIX_MEMALIGN
88  /* Linux x86_64 and OSX always align allocations to 16 bytes */
89  #elif !defined(__amd64__) && !defined(__APPLE__) && !defined(_AIX)
90  #define HAVE_MEMALIGN
91  #include <malloc.h>
92  #endif
93 #endif
94 
95 #define opj_aligned_malloc(size) malloc(size)
96 #define opj_aligned_free(m) free(m)
97 
98 #ifdef HAVE_MM_MALLOC
99  #undef opj_aligned_malloc
100  #define opj_aligned_malloc(size) _mm_malloc(size, 16)
101  #undef opj_aligned_free
102  #define opj_aligned_free(m) _mm_free(m)
103 #endif
104 
105 #ifdef HAVE_MEMALIGN
106  extern void* memalign(size_t, size_t);
107  #undef opj_aligned_malloc
108  #define opj_aligned_malloc(size) memalign(16, (size))
109  #undef opj_aligned_free
110  #define opj_aligned_free(m) free(m)
111 #endif
112 
113 #ifdef HAVE_POSIX_MEMALIGN
114  #undef opj_aligned_malloc
115  extern int posix_memalign(void**, size_t, size_t);
116 
117  static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){
118  void* mem = NULL;
119  posix_memalign(&mem, 16, size);
120  return mem;
121  }
122  #undef opj_aligned_free
123  #define opj_aligned_free(m) free(m)
124 #endif
125 
126 #ifdef ALLOC_PERF_OPT
127  #undef opj_aligned_malloc
128  #define opj_aligned_malloc(size) opj_malloc(size)
129  #undef opj_aligned_free
130  #define opj_aligned_free(m) opj_free(m)
131 #endif
132 
139 #ifdef ALLOC_PERF_OPT
140 void * OPJ_CALLCONV opj_realloc(void * m, size_t s);
141 #else
142 #define opj_realloc(m, s) realloc(m, s)
143 #endif
144 
149 #ifdef ALLOC_PERF_OPT
150 void OPJ_CALLCONV opj_free(void * m);
151 #else
152 #define opj_free(m) free(m)
153 #endif
154 
155 #ifdef __GNUC__
156 #pragma GCC poison malloc calloc realloc free
157 #endif
158 
159 /* ----------------------------------------------------------------------- */
163 
164 #endif /* __OPJ_MALLOC_H */
165 
void * memalign(size_t, size_t)
#define opj_calloc(num, size)
Allocate a memory block with elements initialized to 0.
Definition: opj_malloc.h:63
#define opj_realloc(m, s)
Reallocate memory blocks.
Definition: opj_malloc.h:142
#define opj_aligned_malloc(size)
Definition: opj_malloc.h:108
#define __attribute__(x)
Definition: opj_includes.h:59
#define OPJ_CALLCONV
Definition: openjpeg.h:45
#define INLINE
Definition: opj_includes.h:75
#define opj_malloc(size)
Allocate an uninitialized memory block.
Definition: opj_malloc.h:51
#define opj_free(m)
Deallocates or frees a memory block.
Definition: opj_malloc.h:152