blob: 32077740a13ffe8e0905a4f8ca771fd4678a60fb [file] [log] [blame]
Josh Coalsonde469cf2001-05-11 23:54:27 +00001/* libFLAC - Free Lossless Audio Codec library
Josh Coalsondea0f5a2009-01-07 07:31:28 +00002 * Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
Josh Coalsonde469cf2001-05-11 23:54:27 +00003 *
Josh Coalsonafd81072003-01-31 23:34:56 +00004 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
Josh Coalsonde469cf2001-05-11 23:54:27 +00007 *
Josh Coalsonafd81072003-01-31 23:34:56 +00008 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
Josh Coalsonde469cf2001-05-11 23:54:27 +000010 *
Josh Coalsonafd81072003-01-31 23:34:56 +000011 * - 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.
Josh Coalsonde469cf2001-05-11 23:54:27 +000030 */
31
Josh Coalsonb1ec7962006-05-24 04:41:36 +000032#if HAVE_CONFIG_H
33# include <config.h>
34#endif
35
Josh Coalsonde469cf2001-05-11 23:54:27 +000036#include "private/memory.h"
Josh Coalson1b689822001-05-31 20:11:02 +000037#include "FLAC/assert.h"
Josh Coalson0f008d22007-09-11 04:49:56 +000038#include "share/alloc.h"
Josh Coalsonde469cf2001-05-11 23:54:27 +000039
Josh Coalson02f39002001-05-13 05:15:47 +000040void *FLAC__memory_alloc_aligned(size_t bytes, void **aligned_address)
Josh Coalsonde469cf2001-05-11 23:54:27 +000041{
42 void *x;
43
Josh Coalson1b689822001-05-31 20:11:02 +000044 FLAC__ASSERT(0 != aligned_address);
Josh Coalsonde469cf2001-05-11 23:54:27 +000045
46#ifdef FLAC__ALIGN_MALLOC_DATA
47 /* align on 32-byte (256-bit) boundary */
Josh Coalson0f008d22007-09-11 04:49:56 +000048 x = safe_malloc_add_2op_(bytes, /*+*/31);
Josh Coalsoncebba2a2007-06-14 06:10:00 +000049#ifdef SIZEOF_VOIDP
50#if SIZEOF_VOIDP == 4
51 /* could do *aligned_address = x + ((unsigned) (32 - (((unsigned)x) & 31))) & 31; */
52 *aligned_address = (void*)(((unsigned)x + 31) & -32);
53#elif SIZEOF_VOIDP == 8
54 *aligned_address = (void*)(((FLAC__uint64)x + 31) & (FLAC__uint64)(-((FLAC__int64)32)));
55#else
56# error Unsupported sizeof(void*)
57#endif
58#else
Josh Coalsonab56ef12006-11-17 06:52:19 +000059 /* there's got to be a better way to do this right for all archs */
60 if(sizeof(void*) == sizeof(unsigned))
61 *aligned_address = (void*)(((unsigned)x + 31) & -32);
62 else if(sizeof(void*) == sizeof(FLAC__uint64))
63 *aligned_address = (void*)(((FLAC__uint64)x + 31) & (FLAC__uint64)(-((FLAC__int64)32)));
64 else
65 return 0;
Josh Coalsoncebba2a2007-06-14 06:10:00 +000066#endif
Josh Coalsonde469cf2001-05-11 23:54:27 +000067#else
Josh Coalson0f008d22007-09-11 04:49:56 +000068 x = safe_malloc_(bytes);
Josh Coalsonde469cf2001-05-11 23:54:27 +000069 *aligned_address = x;
70#endif
71 return x;
72}
Josh Coalson02f39002001-05-13 05:15:47 +000073
Erik de Castro Lopo587e1182012-02-17 17:52:12 +110074FLAC__bool FLAC__memory_alloc_aligned_int32_array(size_t elements, FLAC__int32 **unaligned_pointer, FLAC__int32 **aligned_pointer)
Josh Coalson02f39002001-05-13 05:15:47 +000075{
Josh Coalson5e8e7332004-10-07 00:22:03 +000076 FLAC__int32 *pu; /* unaligned pointer */
77 union { /* union needed to comply with C99 pointer aliasing rules */
78 FLAC__int32 *pa; /* aligned pointer */
79 void *pv; /* aligned pointer alias */
80 } u;
Josh Coalson02f39002001-05-13 05:15:47 +000081
Josh Coalson1b689822001-05-31 20:11:02 +000082 FLAC__ASSERT(elements > 0);
83 FLAC__ASSERT(0 != unaligned_pointer);
84 FLAC__ASSERT(0 != aligned_pointer);
85 FLAC__ASSERT(unaligned_pointer != aligned_pointer);
Josh Coalson02f39002001-05-13 05:15:47 +000086
Erik de Castro Lopo587e1182012-02-17 17:52:12 +110087 if(elements > SIZE_MAX / sizeof(*pu)) /* overflow check */
Josh Coalson0f008d22007-09-11 04:49:56 +000088 return false;
89
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +100090 pu = FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
Josh Coalson02f39002001-05-13 05:15:47 +000091 if(0 == pu) {
92 return false;
93 }
94 else {
95 if(*unaligned_pointer != 0)
96 free(*unaligned_pointer);
97 *unaligned_pointer = pu;
Josh Coalson5e8e7332004-10-07 00:22:03 +000098 *aligned_pointer = u.pa;
Josh Coalson02f39002001-05-13 05:15:47 +000099 return true;
100 }
101}
102
Erik de Castro Lopo587e1182012-02-17 17:52:12 +1100103FLAC__bool FLAC__memory_alloc_aligned_uint32_array(size_t elements, FLAC__uint32 **unaligned_pointer, FLAC__uint32 **aligned_pointer)
Josh Coalson02f39002001-05-13 05:15:47 +0000104{
Josh Coalson5e8e7332004-10-07 00:22:03 +0000105 FLAC__uint32 *pu; /* unaligned pointer */
106 union { /* union needed to comply with C99 pointer aliasing rules */
107 FLAC__uint32 *pa; /* aligned pointer */
108 void *pv; /* aligned pointer alias */
109 } u;
Josh Coalson02f39002001-05-13 05:15:47 +0000110
Josh Coalson1b689822001-05-31 20:11:02 +0000111 FLAC__ASSERT(elements > 0);
112 FLAC__ASSERT(0 != unaligned_pointer);
113 FLAC__ASSERT(0 != aligned_pointer);
114 FLAC__ASSERT(unaligned_pointer != aligned_pointer);
Josh Coalson02f39002001-05-13 05:15:47 +0000115
Erik de Castro Lopo587e1182012-02-17 17:52:12 +1100116 if(elements > SIZE_MAX / sizeof(*pu)) /* overflow check */
Josh Coalson0f008d22007-09-11 04:49:56 +0000117 return false;
118
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000119 pu = FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
Josh Coalson02f39002001-05-13 05:15:47 +0000120 if(0 == pu) {
121 return false;
122 }
123 else {
124 if(*unaligned_pointer != 0)
125 free(*unaligned_pointer);
126 *unaligned_pointer = pu;
Josh Coalson5e8e7332004-10-07 00:22:03 +0000127 *aligned_pointer = u.pa;
Josh Coalson02f39002001-05-13 05:15:47 +0000128 return true;
129 }
130}
131
Erik de Castro Lopo587e1182012-02-17 17:52:12 +1100132FLAC__bool FLAC__memory_alloc_aligned_uint64_array(size_t elements, FLAC__uint64 **unaligned_pointer, FLAC__uint64 **aligned_pointer)
Josh Coalsonbf9dd762001-07-16 18:04:52 +0000133{
Josh Coalson5e8e7332004-10-07 00:22:03 +0000134 FLAC__uint64 *pu; /* unaligned pointer */
135 union { /* union needed to comply with C99 pointer aliasing rules */
136 FLAC__uint64 *pa; /* aligned pointer */
137 void *pv; /* aligned pointer alias */
138 } u;
Josh Coalsonbf9dd762001-07-16 18:04:52 +0000139
140 FLAC__ASSERT(elements > 0);
141 FLAC__ASSERT(0 != unaligned_pointer);
142 FLAC__ASSERT(0 != aligned_pointer);
143 FLAC__ASSERT(unaligned_pointer != aligned_pointer);
144
Erik de Castro Lopo587e1182012-02-17 17:52:12 +1100145 if(elements > SIZE_MAX / sizeof(*pu)) /* overflow check */
Josh Coalson0f008d22007-09-11 04:49:56 +0000146 return false;
147
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000148 pu = FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
Josh Coalsonbf9dd762001-07-16 18:04:52 +0000149 if(0 == pu) {
150 return false;
151 }
152 else {
153 if(*unaligned_pointer != 0)
154 free(*unaligned_pointer);
155 *unaligned_pointer = pu;
Josh Coalson5e8e7332004-10-07 00:22:03 +0000156 *aligned_pointer = u.pa;
Josh Coalsonbf9dd762001-07-16 18:04:52 +0000157 return true;
158 }
159}
160
Erik de Castro Lopo587e1182012-02-17 17:52:12 +1100161FLAC__bool FLAC__memory_alloc_aligned_unsigned_array(size_t elements, unsigned **unaligned_pointer, unsigned **aligned_pointer)
Josh Coalson02f39002001-05-13 05:15:47 +0000162{
Josh Coalson5e8e7332004-10-07 00:22:03 +0000163 unsigned *pu; /* unaligned pointer */
164 union { /* union needed to comply with C99 pointer aliasing rules */
165 unsigned *pa; /* aligned pointer */
166 void *pv; /* aligned pointer alias */
167 } u;
Josh Coalson02f39002001-05-13 05:15:47 +0000168
Josh Coalson1b689822001-05-31 20:11:02 +0000169 FLAC__ASSERT(elements > 0);
170 FLAC__ASSERT(0 != unaligned_pointer);
171 FLAC__ASSERT(0 != aligned_pointer);
172 FLAC__ASSERT(unaligned_pointer != aligned_pointer);
Josh Coalson02f39002001-05-13 05:15:47 +0000173
Erik de Castro Lopo587e1182012-02-17 17:52:12 +1100174 if(elements > SIZE_MAX / sizeof(*pu)) /* overflow check */
Josh Coalson0f008d22007-09-11 04:49:56 +0000175 return false;
176
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000177 pu = FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
Josh Coalson02f39002001-05-13 05:15:47 +0000178 if(0 == pu) {
179 return false;
180 }
181 else {
182 if(*unaligned_pointer != 0)
183 free(*unaligned_pointer);
184 *unaligned_pointer = pu;
Josh Coalson5e8e7332004-10-07 00:22:03 +0000185 *aligned_pointer = u.pa;
Josh Coalson02f39002001-05-13 05:15:47 +0000186 return true;
187 }
188}
189
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000190#ifndef FLAC__INTEGER_ONLY_LIBRARY
191
Erik de Castro Lopo587e1182012-02-17 17:52:12 +1100192FLAC__bool FLAC__memory_alloc_aligned_real_array(size_t elements, FLAC__real **unaligned_pointer, FLAC__real **aligned_pointer)
Josh Coalson02f39002001-05-13 05:15:47 +0000193{
Josh Coalson5e8e7332004-10-07 00:22:03 +0000194 FLAC__real *pu; /* unaligned pointer */
195 union { /* union needed to comply with C99 pointer aliasing rules */
196 FLAC__real *pa; /* aligned pointer */
197 void *pv; /* aligned pointer alias */
198 } u;
Josh Coalson02f39002001-05-13 05:15:47 +0000199
Josh Coalson1b689822001-05-31 20:11:02 +0000200 FLAC__ASSERT(elements > 0);
201 FLAC__ASSERT(0 != unaligned_pointer);
202 FLAC__ASSERT(0 != aligned_pointer);
203 FLAC__ASSERT(unaligned_pointer != aligned_pointer);
Josh Coalson02f39002001-05-13 05:15:47 +0000204
Erik de Castro Lopo587e1182012-02-17 17:52:12 +1100205 if(elements > SIZE_MAX / sizeof(*pu)) /* overflow check */
Josh Coalson0f008d22007-09-11 04:49:56 +0000206 return false;
207
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000208 pu = FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
Josh Coalson02f39002001-05-13 05:15:47 +0000209 if(0 == pu) {
210 return false;
211 }
212 else {
213 if(*unaligned_pointer != 0)
214 free(*unaligned_pointer);
215 *unaligned_pointer = pu;
Josh Coalson5e8e7332004-10-07 00:22:03 +0000216 *aligned_pointer = u.pa;
Josh Coalson02f39002001-05-13 05:15:47 +0000217 return true;
218 }
219}
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000220
221#endif
Erik de Castro Lopob4890972012-02-26 20:00:36 +1100222
Erik de Castro Lopo8749dc22012-06-22 14:23:56 +1000223void *safe_malloc_mul_2op_p(size_t size1, size_t size2)
Erik de Castro Lopob4890972012-02-26 20:00:36 +1100224{
225 if(!size1 || !size2)
226 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
227 if(size1 > SIZE_MAX / size2)
228 return 0;
229 return malloc(size1*size2);
230}