fix LD_LIBRARY_PATH-saving logic
[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         /* there's got to be a better way to do this right for all archs */
49         if(sizeof(void*) == sizeof(unsigned))
50                 *aligned_address = (void*)(((unsigned)x + 31) & -32);
51         else if(sizeof(void*) == sizeof(FLAC__uint64))
52                 *aligned_address = (void*)(((FLAC__uint64)x + 31) & (FLAC__uint64)(-((FLAC__int64)32)));
53         else
54                 return 0;
55 #else
56         x = malloc(bytes);
57         *aligned_address = x;
58 #endif
59         return x;
60 }
61
62 FLAC__bool FLAC__memory_alloc_aligned_int32_array(unsigned elements, FLAC__int32 **unaligned_pointer, FLAC__int32 **aligned_pointer)
63 {
64         FLAC__int32 *pu; /* unaligned pointer */
65         union { /* union needed to comply with C99 pointer aliasing rules */
66                 FLAC__int32 *pa; /* aligned pointer */
67                 void        *pv; /* aligned pointer alias */
68         } u;
69
70         FLAC__ASSERT(elements > 0);
71         FLAC__ASSERT(0 != unaligned_pointer);
72         FLAC__ASSERT(0 != aligned_pointer);
73         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
74
75         pu = (FLAC__int32*)FLAC__memory_alloc_aligned(sizeof(FLAC__int32) * elements, &u.pv);
76         if(0 == pu) {
77                 return false;
78         }
79         else {
80                 if(*unaligned_pointer != 0)
81                         free(*unaligned_pointer);
82                 *unaligned_pointer = pu;
83                 *aligned_pointer = u.pa;
84                 return true;
85         }
86 }
87
88 FLAC__bool FLAC__memory_alloc_aligned_uint32_array(unsigned elements, FLAC__uint32 **unaligned_pointer, FLAC__uint32 **aligned_pointer)
89 {
90         FLAC__uint32 *pu; /* unaligned pointer */
91         union { /* union needed to comply with C99 pointer aliasing rules */
92                 FLAC__uint32 *pa; /* aligned pointer */
93                 void         *pv; /* aligned pointer alias */
94         } u;
95
96         FLAC__ASSERT(elements > 0);
97         FLAC__ASSERT(0 != unaligned_pointer);
98         FLAC__ASSERT(0 != aligned_pointer);
99         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
100
101         pu = (FLAC__uint32*)FLAC__memory_alloc_aligned(sizeof(FLAC__uint32) * elements, &u.pv);
102         if(0 == pu) {
103                 return false;
104         }
105         else {
106                 if(*unaligned_pointer != 0)
107                         free(*unaligned_pointer);
108                 *unaligned_pointer = pu;
109                 *aligned_pointer = u.pa;
110                 return true;
111         }
112 }
113
114 FLAC__bool FLAC__memory_alloc_aligned_uint64_array(unsigned elements, FLAC__uint64 **unaligned_pointer, FLAC__uint64 **aligned_pointer)
115 {
116         FLAC__uint64 *pu; /* unaligned pointer */
117         union { /* union needed to comply with C99 pointer aliasing rules */
118                 FLAC__uint64 *pa; /* aligned pointer */
119                 void         *pv; /* aligned pointer alias */
120         } u;
121
122         FLAC__ASSERT(elements > 0);
123         FLAC__ASSERT(0 != unaligned_pointer);
124         FLAC__ASSERT(0 != aligned_pointer);
125         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
126
127         pu = (FLAC__uint64*)FLAC__memory_alloc_aligned(sizeof(FLAC__uint64) * elements, &u.pv);
128         if(0 == pu) {
129                 return false;
130         }
131         else {
132                 if(*unaligned_pointer != 0)
133                         free(*unaligned_pointer);
134                 *unaligned_pointer = pu;
135                 *aligned_pointer = u.pa;
136                 return true;
137         }
138 }
139
140 FLAC__bool FLAC__memory_alloc_aligned_unsigned_array(unsigned elements, unsigned **unaligned_pointer, unsigned **aligned_pointer)
141 {
142         unsigned *pu; /* unaligned pointer */
143         union { /* union needed to comply with C99 pointer aliasing rules */
144                 unsigned *pa; /* aligned pointer */
145                 void     *pv; /* aligned pointer alias */
146         } u;
147
148         FLAC__ASSERT(elements > 0);
149         FLAC__ASSERT(0 != unaligned_pointer);
150         FLAC__ASSERT(0 != aligned_pointer);
151         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
152
153         pu = (unsigned*)FLAC__memory_alloc_aligned(sizeof(unsigned) * elements, &u.pv);
154         if(0 == pu) {
155                 return false;
156         }
157         else {
158                 if(*unaligned_pointer != 0)
159                         free(*unaligned_pointer);
160                 *unaligned_pointer = pu;
161                 *aligned_pointer = u.pa;
162                 return true;
163         }
164 }
165
166 #ifndef FLAC__INTEGER_ONLY_LIBRARY
167
168 FLAC__bool FLAC__memory_alloc_aligned_real_array(unsigned elements, FLAC__real **unaligned_pointer, FLAC__real **aligned_pointer)
169 {
170         FLAC__real *pu; /* unaligned pointer */
171         union { /* union needed to comply with C99 pointer aliasing rules */
172                 FLAC__real *pa; /* aligned pointer */
173                 void       *pv; /* aligned pointer alias */
174         } u;
175
176         FLAC__ASSERT(elements > 0);
177         FLAC__ASSERT(0 != unaligned_pointer);
178         FLAC__ASSERT(0 != aligned_pointer);
179         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
180
181         pu = (FLAC__real*)FLAC__memory_alloc_aligned(sizeof(FLAC__real) * elements, &u.pv);
182         if(0 == pu) {
183                 return false;
184         }
185         else {
186                 if(*unaligned_pointer != 0)
187                         free(*unaligned_pointer);
188                 *unaligned_pointer = pu;
189                 *aligned_pointer = u.pa;
190                 return true;
191         }
192 }
193
194 #endif