add FLAC__metadata_simple_iterator_get_application_id()
[platform/upstream/flac.git] / src / libFLAC / memory.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2001,2002,2003,2004,2005,2006,2007  Josh Coalson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - 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  * - Neither the name of the Xiph.org Foundation nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #if HAVE_CONFIG_H
33 #  include <config.h>
34 #endif
35
36 #include "private/memory.h"
37 #include "FLAC/assert.h"
38
39 void *FLAC__memory_alloc_aligned(size_t bytes, void **aligned_address)
40 {
41         void *x;
42
43         FLAC__ASSERT(0 != aligned_address);
44
45 #ifdef FLAC__ALIGN_MALLOC_DATA
46         /* align on 32-byte (256-bit) boundary */
47         x = malloc(bytes+31);
48 #ifdef SIZEOF_VOIDP
49 #if SIZEOF_VOIDP == 4
50                 /* could do  *aligned_address = x + ((unsigned) (32 - (((unsigned)x) & 31))) & 31; */
51                 *aligned_address = (void*)(((unsigned)x + 31) & -32);
52 #elif SIZEOF_VOIDP == 8
53                 *aligned_address = (void*)(((FLAC__uint64)x + 31) & (FLAC__uint64)(-((FLAC__int64)32)));
54 #else
55 # error  Unsupported sizeof(void*)
56 #endif
57 #else
58         /* there's got to be a better way to do this right for all archs */
59         if(sizeof(void*) == sizeof(unsigned))
60                 *aligned_address = (void*)(((unsigned)x + 31) & -32);
61         else if(sizeof(void*) == sizeof(FLAC__uint64))
62                 *aligned_address = (void*)(((FLAC__uint64)x + 31) & (FLAC__uint64)(-((FLAC__int64)32)));
63         else
64                 return 0;
65 #endif
66 #else
67         x = malloc(bytes);
68         *aligned_address = x;
69 #endif
70         return x;
71 }
72
73 FLAC__bool FLAC__memory_alloc_aligned_int32_array(unsigned elements, FLAC__int32 **unaligned_pointer, FLAC__int32 **aligned_pointer)
74 {
75         FLAC__int32 *pu; /* unaligned pointer */
76         union { /* union needed to comply with C99 pointer aliasing rules */
77                 FLAC__int32 *pa; /* aligned pointer */
78                 void        *pv; /* aligned pointer alias */
79         } u;
80
81         FLAC__ASSERT(elements > 0);
82         FLAC__ASSERT(0 != unaligned_pointer);
83         FLAC__ASSERT(0 != aligned_pointer);
84         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
85
86         pu = (FLAC__int32*)FLAC__memory_alloc_aligned(sizeof(FLAC__int32) * elements, &u.pv);
87         if(0 == pu) {
88                 return false;
89         }
90         else {
91                 if(*unaligned_pointer != 0)
92                         free(*unaligned_pointer);
93                 *unaligned_pointer = pu;
94                 *aligned_pointer = u.pa;
95                 return true;
96         }
97 }
98
99 FLAC__bool FLAC__memory_alloc_aligned_uint32_array(unsigned elements, FLAC__uint32 **unaligned_pointer, FLAC__uint32 **aligned_pointer)
100 {
101         FLAC__uint32 *pu; /* unaligned pointer */
102         union { /* union needed to comply with C99 pointer aliasing rules */
103                 FLAC__uint32 *pa; /* aligned pointer */
104                 void         *pv; /* aligned pointer alias */
105         } u;
106
107         FLAC__ASSERT(elements > 0);
108         FLAC__ASSERT(0 != unaligned_pointer);
109         FLAC__ASSERT(0 != aligned_pointer);
110         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
111
112         pu = (FLAC__uint32*)FLAC__memory_alloc_aligned(sizeof(FLAC__uint32) * elements, &u.pv);
113         if(0 == pu) {
114                 return false;
115         }
116         else {
117                 if(*unaligned_pointer != 0)
118                         free(*unaligned_pointer);
119                 *unaligned_pointer = pu;
120                 *aligned_pointer = u.pa;
121                 return true;
122         }
123 }
124
125 FLAC__bool FLAC__memory_alloc_aligned_uint64_array(unsigned elements, FLAC__uint64 **unaligned_pointer, FLAC__uint64 **aligned_pointer)
126 {
127         FLAC__uint64 *pu; /* unaligned pointer */
128         union { /* union needed to comply with C99 pointer aliasing rules */
129                 FLAC__uint64 *pa; /* aligned pointer */
130                 void         *pv; /* aligned pointer alias */
131         } u;
132
133         FLAC__ASSERT(elements > 0);
134         FLAC__ASSERT(0 != unaligned_pointer);
135         FLAC__ASSERT(0 != aligned_pointer);
136         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
137
138         pu = (FLAC__uint64*)FLAC__memory_alloc_aligned(sizeof(FLAC__uint64) * elements, &u.pv);
139         if(0 == pu) {
140                 return false;
141         }
142         else {
143                 if(*unaligned_pointer != 0)
144                         free(*unaligned_pointer);
145                 *unaligned_pointer = pu;
146                 *aligned_pointer = u.pa;
147                 return true;
148         }
149 }
150
151 FLAC__bool FLAC__memory_alloc_aligned_unsigned_array(unsigned elements, unsigned **unaligned_pointer, unsigned **aligned_pointer)
152 {
153         unsigned *pu; /* unaligned pointer */
154         union { /* union needed to comply with C99 pointer aliasing rules */
155                 unsigned *pa; /* aligned pointer */
156                 void     *pv; /* aligned pointer alias */
157         } u;
158
159         FLAC__ASSERT(elements > 0);
160         FLAC__ASSERT(0 != unaligned_pointer);
161         FLAC__ASSERT(0 != aligned_pointer);
162         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
163
164         pu = (unsigned*)FLAC__memory_alloc_aligned(sizeof(unsigned) * elements, &u.pv);
165         if(0 == pu) {
166                 return false;
167         }
168         else {
169                 if(*unaligned_pointer != 0)
170                         free(*unaligned_pointer);
171                 *unaligned_pointer = pu;
172                 *aligned_pointer = u.pa;
173                 return true;
174         }
175 }
176
177 #ifndef FLAC__INTEGER_ONLY_LIBRARY
178
179 FLAC__bool FLAC__memory_alloc_aligned_real_array(unsigned elements, FLAC__real **unaligned_pointer, FLAC__real **aligned_pointer)
180 {
181         FLAC__real *pu; /* unaligned pointer */
182         union { /* union needed to comply with C99 pointer aliasing rules */
183                 FLAC__real *pa; /* aligned pointer */
184                 void       *pv; /* aligned pointer alias */
185         } u;
186
187         FLAC__ASSERT(elements > 0);
188         FLAC__ASSERT(0 != unaligned_pointer);
189         FLAC__ASSERT(0 != aligned_pointer);
190         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
191
192         pu = (FLAC__real*)FLAC__memory_alloc_aligned(sizeof(FLAC__real) * elements, &u.pv);
193         if(0 == pu) {
194                 return false;
195         }
196         else {
197                 if(*unaligned_pointer != 0)
198                         free(*unaligned_pointer);
199                 *unaligned_pointer = pu;
200                 *aligned_pointer = u.pa;
201                 return true;
202         }
203 }
204
205 #endif