Added the default lossless parameter to opj_set_default_encoder_parameters in openjpeg.c
[openjpeg.git] / v2 / libopenjpeg / opj_malloc.h
1 /*
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>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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.
27  */
28 #ifndef __OPJ_MALLOC_H
29 #define __OPJ_MALLOC_H
30 /**
31 @file opj_malloc.h
32 @brief Internal functions
33
34 The functions in opj_malloc.h are internal utilities used for memory management.
35 */
36 #include "openjpeg.h"
37 #include "opj_includes.h"
38 /** @defgroup MISC MISC - Miscellaneous internal functions */
39 /*@{*/
40
41 /** @name Exported functions */
42 /*@{*/
43 /* ----------------------------------------------------------------------- */
44
45 /**
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
49 */
50 #define opj_malloc(size)                malloc(size)
51 #define my_opj_malloc(size)             malloc(size)
52
53 /**
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
58 */
59 #define opj_calloc(num, size) calloc(num, size)
60
61 /**
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
65 */
66 /* FIXME: These should be set with cmake tests, but we're currently not requiring use of cmake */
67 #ifdef WIN32
68         /* Someone should tell the mingw people that their malloc.h ought to provide _mm_malloc() */
69         #ifdef __GNUC__
70                 #include <mm_malloc.h>
71                 #define HAVE_MM_MALLOC
72         #else /* MSVC, Intel C++ */
73                 #include <malloc.h>
74                 #ifdef _mm_malloc
75                         #define HAVE_MM_MALLOC
76                 #endif
77         #endif
78 #else /* Not WIN32 */
79         #if defined(__sun)
80                 #define HAVE_MEMALIGN
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
85         #endif
86 #endif
87
88 #define opj_aligned_malloc(size) malloc(size)
89 #define opj_aligned_free(m) free(m)
90
91 #ifdef HAVE_MM_MALLOC
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)
96 #endif
97
98 #ifdef HAVE_MEMALIGN
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)
104 #endif
105
106 #ifdef HAVE_POSIX_MEMALIGN
107         #undef opj_aligned_malloc
108         extern int posix_memalign(void**, size_t, size_t);
109
110         static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){
111                 void* mem = NULL;
112                 posix_memalign(&mem, 16, size);
113                 return mem;
114         }
115         #undef opj_aligned_free
116         #define opj_aligned_free(m) free(m)
117 #endif
118
119 /**
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
124 */
125 #define opj_realloc(m, s)               realloc(m, s)
126 #define my_opj_realloc(m,s)             realloc(m,s)
127
128
129 /**
130 Deallocates or frees a memory block.
131 @param memblock Previously allocated memory block to be freed
132 */
133 #define opj_free(m) free(m)
134
135 #ifdef __GNUC__
136 #pragma GCC poison malloc calloc realloc free
137 #endif
138
139 /* ----------------------------------------------------------------------- */
140 /*@}*/
141
142 /*@}*/
143
144 #endif /* __OPJ_MALLOC_H */
145