Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Codec library |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 2 | * Copyright (C) 2001-2009 Josh Coalson |
Elliott Hughes | ae0e7bc | 2018-01-12 14:46:04 -0800 | [diff] [blame] | 3 | * Copyright (C) 2011-2016 Xiph.Org Foundation |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * - Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * |
| 12 | * - 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 | * - Neither the name of the Xiph.org Foundation nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 33 | #ifdef HAVE_CONFIG_H |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 34 | # include <config.h> |
| 35 | #endif |
| 36 | |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 37 | #ifdef HAVE_STDINT_H |
| 38 | #include <stdint.h> |
| 39 | #endif |
| 40 | |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 41 | #include "private/memory.h" |
| 42 | #include "FLAC/assert.h" |
| 43 | #include "share/alloc.h" |
| 44 | |
| 45 | void *FLAC__memory_alloc_aligned(size_t bytes, void **aligned_address) |
| 46 | { |
| 47 | void *x; |
| 48 | |
| 49 | FLAC__ASSERT(0 != aligned_address); |
| 50 | |
| 51 | #ifdef FLAC__ALIGN_MALLOC_DATA |
| 52 | /* align on 32-byte (256-bit) boundary */ |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 53 | x = safe_malloc_add_2op_(bytes, /*+*/31L); |
| 54 | *aligned_address = (void*)(((uintptr_t)x + 31L) & -32L); |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 55 | #else |
| 56 | x = safe_malloc_(bytes); |
| 57 | *aligned_address = x; |
| 58 | #endif |
| 59 | return x; |
| 60 | } |
| 61 | |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 62 | FLAC__bool FLAC__memory_alloc_aligned_int32_array(size_t elements, FLAC__int32 **unaligned_pointer, FLAC__int32 **aligned_pointer) |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 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 | |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 75 | if(elements > SIZE_MAX / sizeof(*pu)) /* overflow check */ |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 76 | return false; |
| 77 | |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 78 | pu = FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv); |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 79 | if(0 == pu) { |
| 80 | return false; |
| 81 | } |
| 82 | else { |
| 83 | if(*unaligned_pointer != 0) |
| 84 | free(*unaligned_pointer); |
| 85 | *unaligned_pointer = pu; |
| 86 | *aligned_pointer = u.pa; |
| 87 | return true; |
| 88 | } |
| 89 | } |
| 90 | |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 91 | FLAC__bool FLAC__memory_alloc_aligned_uint32_array(size_t elements, FLAC__uint32 **unaligned_pointer, FLAC__uint32 **aligned_pointer) |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 92 | { |
| 93 | FLAC__uint32 *pu; /* unaligned pointer */ |
| 94 | union { /* union needed to comply with C99 pointer aliasing rules */ |
| 95 | FLAC__uint32 *pa; /* aligned pointer */ |
| 96 | void *pv; /* aligned pointer alias */ |
| 97 | } u; |
| 98 | |
| 99 | FLAC__ASSERT(elements > 0); |
| 100 | FLAC__ASSERT(0 != unaligned_pointer); |
| 101 | FLAC__ASSERT(0 != aligned_pointer); |
| 102 | FLAC__ASSERT(unaligned_pointer != aligned_pointer); |
| 103 | |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 104 | if(elements > SIZE_MAX / sizeof(*pu)) /* overflow check */ |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 105 | return false; |
| 106 | |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 107 | pu = FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv); |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 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 | |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 120 | FLAC__bool FLAC__memory_alloc_aligned_uint64_array(size_t elements, FLAC__uint64 **unaligned_pointer, FLAC__uint64 **aligned_pointer) |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 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 | |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 133 | if(elements > SIZE_MAX / sizeof(*pu)) /* overflow check */ |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 134 | return false; |
| 135 | |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 136 | pu = FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv); |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 137 | if(0 == pu) { |
| 138 | return false; |
| 139 | } |
| 140 | else { |
| 141 | if(*unaligned_pointer != 0) |
| 142 | free(*unaligned_pointer); |
| 143 | *unaligned_pointer = pu; |
| 144 | *aligned_pointer = u.pa; |
| 145 | return true; |
| 146 | } |
| 147 | } |
| 148 | |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 149 | FLAC__bool FLAC__memory_alloc_aligned_unsigned_array(size_t elements, unsigned **unaligned_pointer, unsigned **aligned_pointer) |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 150 | { |
| 151 | unsigned *pu; /* unaligned pointer */ |
| 152 | union { /* union needed to comply with C99 pointer aliasing rules */ |
| 153 | unsigned *pa; /* aligned pointer */ |
| 154 | void *pv; /* aligned pointer alias */ |
| 155 | } u; |
| 156 | |
| 157 | FLAC__ASSERT(elements > 0); |
| 158 | FLAC__ASSERT(0 != unaligned_pointer); |
| 159 | FLAC__ASSERT(0 != aligned_pointer); |
| 160 | FLAC__ASSERT(unaligned_pointer != aligned_pointer); |
| 161 | |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 162 | if(elements > SIZE_MAX / sizeof(*pu)) /* overflow check */ |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 163 | return false; |
| 164 | |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 165 | pu = FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv); |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 166 | if(0 == pu) { |
| 167 | return false; |
| 168 | } |
| 169 | else { |
| 170 | if(*unaligned_pointer != 0) |
| 171 | free(*unaligned_pointer); |
| 172 | *unaligned_pointer = pu; |
| 173 | *aligned_pointer = u.pa; |
| 174 | return true; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 179 | |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 180 | FLAC__bool FLAC__memory_alloc_aligned_real_array(size_t elements, FLAC__real **unaligned_pointer, FLAC__real **aligned_pointer) |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 181 | { |
| 182 | FLAC__real *pu; /* unaligned pointer */ |
| 183 | union { /* union needed to comply with C99 pointer aliasing rules */ |
| 184 | FLAC__real *pa; /* aligned pointer */ |
| 185 | void *pv; /* aligned pointer alias */ |
| 186 | } u; |
| 187 | |
| 188 | FLAC__ASSERT(elements > 0); |
| 189 | FLAC__ASSERT(0 != unaligned_pointer); |
| 190 | FLAC__ASSERT(0 != aligned_pointer); |
| 191 | FLAC__ASSERT(unaligned_pointer != aligned_pointer); |
| 192 | |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 193 | if(elements > SIZE_MAX / sizeof(*pu)) /* overflow check */ |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 194 | return false; |
| 195 | |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 196 | pu = FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv); |
Kenny Root | c746637 | 2009-11-08 12:46:32 -0600 | [diff] [blame] | 197 | if(0 == pu) { |
| 198 | return false; |
| 199 | } |
| 200 | else { |
| 201 | if(*unaligned_pointer != 0) |
| 202 | free(*unaligned_pointer); |
| 203 | *unaligned_pointer = pu; |
| 204 | *aligned_pointer = u.pa; |
| 205 | return true; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | #endif |
Robert Shih | 31e4f31 | 2015-01-05 17:35:54 -0800 | [diff] [blame] | 210 | |
| 211 | void *safe_malloc_mul_2op_p(size_t size1, size_t size2) |
| 212 | { |
| 213 | if(!size1 || !size2) |
| 214 | return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */ |
| 215 | if(size1 > SIZE_MAX / size2) |
| 216 | return 0; |
| 217 | return malloc(size1*size2); |
| 218 | } |