change license verbiage to Xiph's
[platform/upstream/flac.git] / src / libFLAC / memory.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2001,2002,2003  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 #include "private/memory.h"
33 #include "FLAC/assert.h"
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
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         *aligned_address = (void*)(((unsigned)x + 31) & -32);
49 #else
50         x = malloc(bytes);
51         *aligned_address = x;
52 #endif
53         return x;
54 }
55
56 FLAC__bool FLAC__memory_alloc_aligned_int32_array(unsigned elements, FLAC__int32 **unaligned_pointer, FLAC__int32 **aligned_pointer)
57 {
58         FLAC__int32 *pa, *pu; /* aligned pointer, unaligned pointer */
59
60         FLAC__ASSERT(elements > 0);
61         FLAC__ASSERT(0 != unaligned_pointer);
62         FLAC__ASSERT(0 != aligned_pointer);
63         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
64
65         pu = (FLAC__int32*)FLAC__memory_alloc_aligned(sizeof(FLAC__int32) * elements, (void**)&pa);
66         if(0 == pu) {
67                 return false;
68         }
69         else {
70                 if(*unaligned_pointer != 0)
71                         free(*unaligned_pointer);
72                 *unaligned_pointer = pu;
73                 *aligned_pointer = pa;
74                 return true;
75         }
76 }
77
78 FLAC__bool FLAC__memory_alloc_aligned_uint32_array(unsigned elements, FLAC__uint32 **unaligned_pointer, FLAC__uint32 **aligned_pointer)
79 {
80         FLAC__uint32 *pa, *pu; /* aligned pointer, unaligned pointer */
81
82         FLAC__ASSERT(elements > 0);
83         FLAC__ASSERT(0 != unaligned_pointer);
84         FLAC__ASSERT(0 != aligned_pointer);
85         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
86
87         pu = (FLAC__uint32*)FLAC__memory_alloc_aligned(sizeof(FLAC__uint32) * elements, (void**)&pa);
88         if(0 == pu) {
89                 return false;
90         }
91         else {
92                 if(*unaligned_pointer != 0)
93                         free(*unaligned_pointer);
94                 *unaligned_pointer = pu;
95                 *aligned_pointer = pa;
96                 return true;
97         }
98 }
99
100 FLAC__bool FLAC__memory_alloc_aligned_uint64_array(unsigned elements, FLAC__uint64 **unaligned_pointer, FLAC__uint64 **aligned_pointer)
101 {
102         FLAC__uint64 *pa, *pu; /* aligned pointer, unaligned pointer */
103
104         FLAC__ASSERT(elements > 0);
105         FLAC__ASSERT(0 != unaligned_pointer);
106         FLAC__ASSERT(0 != aligned_pointer);
107         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
108
109         pu = (FLAC__uint64*)FLAC__memory_alloc_aligned(sizeof(FLAC__uint64) * elements, (void**)&pa);
110         if(0 == pu) {
111                 return false;
112         }
113         else {
114                 if(*unaligned_pointer != 0)
115                         free(*unaligned_pointer);
116                 *unaligned_pointer = pu;
117                 *aligned_pointer = pa;
118                 return true;
119         }
120 }
121
122 FLAC__bool FLAC__memory_alloc_aligned_unsigned_array(unsigned elements, unsigned **unaligned_pointer, unsigned **aligned_pointer)
123 {
124         unsigned *pa, *pu; /* aligned pointer, unaligned pointer */
125
126         FLAC__ASSERT(elements > 0);
127         FLAC__ASSERT(0 != unaligned_pointer);
128         FLAC__ASSERT(0 != aligned_pointer);
129         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
130
131         pu = (unsigned*)FLAC__memory_alloc_aligned(sizeof(unsigned) * elements, (void**)&pa);
132         if(0 == pu) {
133                 return false;
134         }
135         else {
136                 if(*unaligned_pointer != 0)
137                         free(*unaligned_pointer);
138                 *unaligned_pointer = pu;
139                 *aligned_pointer = pa;
140                 return true;
141         }
142 }
143
144 FLAC__bool FLAC__memory_alloc_aligned_real_array(unsigned elements, FLAC__real **unaligned_pointer, FLAC__real **aligned_pointer)
145 {
146         FLAC__real *pa, *pu; /* aligned pointer, unaligned pointer */
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 = (FLAC__real*)FLAC__memory_alloc_aligned(sizeof(FLAC__real) * elements, (void**)&pa);
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 = pa;
162                 return true;
163         }
164 }