#include config.h
[platform/upstream/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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 void *FLAC__memory_alloc_aligned(size_t bytes, void **aligned_address)
28 {
29         void *x;
30
31         FLAC__ASSERT(0 != aligned_address);
32
33 #ifdef FLAC__ALIGN_MALLOC_DATA
34         /* align on 32-byte (256-bit) boundary */
35         x = malloc(bytes+31);
36         *aligned_address = (void*)(((unsigned)x + 31) & -32);
37 #else
38         x = malloc(bytes);
39         *aligned_address = x;
40 #endif
41         return x;
42 }
43
44 FLAC__bool FLAC__memory_alloc_aligned_int32_array(unsigned elements, FLAC__int32 **unaligned_pointer, FLAC__int32 **aligned_pointer)
45 {
46         FLAC__int32 *pa, *pu; /* aligned pointer, unaligned pointer */
47
48         FLAC__ASSERT(elements > 0);
49         FLAC__ASSERT(0 != unaligned_pointer);
50         FLAC__ASSERT(0 != aligned_pointer);
51         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
52
53         pu = (FLAC__int32*)FLAC__memory_alloc_aligned(sizeof(FLAC__int32) * elements, (void**)&pa);
54         if(0 == pu) {
55                 return false;
56         }
57         else {
58                 if(*unaligned_pointer != 0)
59                         free(*unaligned_pointer);
60                 *unaligned_pointer = pu;
61                 *aligned_pointer = pa;
62                 return true;
63         }
64 }
65
66 FLAC__bool FLAC__memory_alloc_aligned_uint32_array(unsigned elements, FLAC__uint32 **unaligned_pointer, FLAC__uint32 **aligned_pointer)
67 {
68         FLAC__uint32 *pa, *pu; /* aligned pointer, unaligned pointer */
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
75         pu = (FLAC__uint32*)FLAC__memory_alloc_aligned(sizeof(FLAC__uint32) * elements, (void**)&pa);
76         if(0 == pu) {
77                 return false;
78         }
79         else {
80                 if(*unaligned_pointer != 0)
81                         free(*unaligned_pointer);
82                 *unaligned_pointer = pu;
83                 *aligned_pointer = pa;
84                 return true;
85         }
86 }
87
88 FLAC__bool FLAC__memory_alloc_aligned_uint64_array(unsigned elements, FLAC__uint64 **unaligned_pointer, FLAC__uint64 **aligned_pointer)
89 {
90         FLAC__uint64 *pa, *pu; /* aligned pointer, unaligned pointer */
91
92         FLAC__ASSERT(elements > 0);
93         FLAC__ASSERT(0 != unaligned_pointer);
94         FLAC__ASSERT(0 != aligned_pointer);
95         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
96
97         pu = (FLAC__uint64*)FLAC__memory_alloc_aligned(sizeof(FLAC__uint64) * elements, (void**)&pa);
98         if(0 == pu) {
99                 return false;
100         }
101         else {
102                 if(*unaligned_pointer != 0)
103                         free(*unaligned_pointer);
104                 *unaligned_pointer = pu;
105                 *aligned_pointer = pa;
106                 return true;
107         }
108 }
109
110 FLAC__bool FLAC__memory_alloc_aligned_unsigned_array(unsigned elements, unsigned **unaligned_pointer, unsigned **aligned_pointer)
111 {
112         unsigned *pa, *pu; /* aligned pointer, unaligned pointer */
113
114         FLAC__ASSERT(elements > 0);
115         FLAC__ASSERT(0 != unaligned_pointer);
116         FLAC__ASSERT(0 != aligned_pointer);
117         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
118
119         pu = (unsigned*)FLAC__memory_alloc_aligned(sizeof(unsigned) * elements, (void**)&pa);
120         if(0 == pu) {
121                 return false;
122         }
123         else {
124                 if(*unaligned_pointer != 0)
125                         free(*unaligned_pointer);
126                 *unaligned_pointer = pu;
127                 *aligned_pointer = pa;
128                 return true;
129         }
130 }
131
132 FLAC__bool FLAC__memory_alloc_aligned_real_array(unsigned elements, FLAC__real **unaligned_pointer, FLAC__real **aligned_pointer)
133 {
134         FLAC__real *pa, *pu; /* aligned pointer, unaligned pointer */
135
136         FLAC__ASSERT(elements > 0);
137         FLAC__ASSERT(0 != unaligned_pointer);
138         FLAC__ASSERT(0 != aligned_pointer);
139         FLAC__ASSERT(unaligned_pointer != aligned_pointer);
140
141         pu = (FLAC__real*)FLAC__memory_alloc_aligned(sizeof(FLAC__real) * elements, (void**)&pa);
142         if(0 == pu) {
143                 return false;
144         }
145         else {
146                 if(*unaligned_pointer != 0)
147                         free(*unaligned_pointer);
148                 *unaligned_pointer = pu;
149                 *aligned_pointer = pa;
150                 return true;
151         }
152 }