update copyright date to include 2002
[flac.git] / src / libFLAC / memory.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2001,2002  Josh Coalson
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA  02111-1307, USA.
18  */
19
20 #include "private/memory.h"
21 #include "FLAC/assert.h"
22
23 void *FLAC__memory_alloc_aligned(size_t bytes, void **aligned_address)
24 {
25         void *x;
26
27         FLAC__ASSERT(0 != aligned_address);
28
29 #ifdef FLAC__ALIGN_MALLOC_DATA
30         /* align on 32-byte (256-bit) boundary */
31         x = malloc(bytes+31);
32         *aligned_address = (void*)(((unsigned)x + 31) & -32);
33 #else
34         x = malloc(bytes);
35         *aligned_address = x;
36 #endif
37         return x;
38 }
39
40 FLAC__bool FLAC__memory_alloc_aligned_int32_array(unsigned elements, FLAC__int32 **unaligned_pointer, FLAC__int32 **aligned_pointer)
41 {
42         FLAC__int32 *pa, *pu; /* aligned pointer, unaligned pointer */
43
44         FLAC__ASSERT(elements > 0);
45         FLAC__ASSERT(0 != unaligned_pointer);
46         FLAC__ASSERT(0 != aligned_pointer);
47         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
48
49         pu = (FLAC__int32*)FLAC__memory_alloc_aligned(sizeof(FLAC__int32) * elements, (void*)&pa);
50         if(0 == pu) {
51                 return false;
52         }
53         else {
54                 if(*unaligned_pointer != 0)
55                         free(*unaligned_pointer);
56                 *unaligned_pointer = pu;
57                 *aligned_pointer = pa;
58                 return true;
59         }
60 }
61
62 FLAC__bool FLAC__memory_alloc_aligned_uint32_array(unsigned elements, FLAC__uint32 **unaligned_pointer, FLAC__uint32 **aligned_pointer)
63 {
64         FLAC__uint32 *pa, *pu; /* aligned pointer, unaligned pointer */
65
66         FLAC__ASSERT(elements > 0);
67         FLAC__ASSERT(0 != unaligned_pointer);
68         FLAC__ASSERT(0 != aligned_pointer);
69         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
70
71         pu = (FLAC__uint32*)FLAC__memory_alloc_aligned(sizeof(FLAC__uint32) * elements, (void*)&pa);
72         if(0 == pu) {
73                 return false;
74         }
75         else {
76                 if(*unaligned_pointer != 0)
77                         free(*unaligned_pointer);
78                 *unaligned_pointer = pu;
79                 *aligned_pointer = pa;
80                 return true;
81         }
82 }
83
84 FLAC__bool FLAC__memory_alloc_aligned_uint64_array(unsigned elements, FLAC__uint64 **unaligned_pointer, FLAC__uint64 **aligned_pointer)
85 {
86         FLAC__uint64 *pa, *pu; /* aligned pointer, unaligned pointer */
87
88         FLAC__ASSERT(elements > 0);
89         FLAC__ASSERT(0 != unaligned_pointer);
90         FLAC__ASSERT(0 != aligned_pointer);
91         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
92
93         pu = (FLAC__uint64*)FLAC__memory_alloc_aligned(sizeof(FLAC__uint64) * elements, (void*)&pa);
94         if(0 == pu) {
95                 return false;
96         }
97         else {
98                 if(*unaligned_pointer != 0)
99                         free(*unaligned_pointer);
100                 *unaligned_pointer = pu;
101                 *aligned_pointer = pa;
102                 return true;
103         }
104 }
105
106 FLAC__bool FLAC__memory_alloc_aligned_unsigned_array(unsigned elements, unsigned **unaligned_pointer, unsigned **aligned_pointer)
107 {
108         unsigned *pa, *pu; /* aligned pointer, unaligned pointer */
109
110         FLAC__ASSERT(elements > 0);
111         FLAC__ASSERT(0 != unaligned_pointer);
112         FLAC__ASSERT(0 != aligned_pointer);
113         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
114
115         pu = (unsigned*)FLAC__memory_alloc_aligned(sizeof(unsigned) * elements, (void*)&pa);
116         if(0 == pu) {
117                 return false;
118         }
119         else {
120                 if(*unaligned_pointer != 0)
121                         free(*unaligned_pointer);
122                 *unaligned_pointer = pu;
123                 *aligned_pointer = pa;
124                 return true;
125         }
126 }
127
128 FLAC__bool FLAC__memory_alloc_aligned_real_array(unsigned elements, FLAC__real **unaligned_pointer, FLAC__real **aligned_pointer)
129 {
130         FLAC__real *pa, *pu; /* aligned pointer, unaligned pointer */
131
132         FLAC__ASSERT(elements > 0);
133         FLAC__ASSERT(0 != unaligned_pointer);
134         FLAC__ASSERT(0 != aligned_pointer);
135         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
136
137         pu = (FLAC__real*)FLAC__memory_alloc_aligned(sizeof(FLAC__real) * elements, (void*)&pa);
138         if(0 == pu) {
139                 return false;
140         }
141         else {
142                 if(*unaligned_pointer != 0)
143                         free(*unaligned_pointer);
144                 *unaligned_pointer = pu;
145                 *aligned_pointer = pa;
146                 return true;
147         }
148 }