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