1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2001,2002 Josh Coalson
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.
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.
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.
20 #include "private/memory.h"
21 #include "FLAC/assert.h"
27 void *FLAC__memory_alloc_aligned(size_t bytes, void **aligned_address)
31 FLAC__ASSERT(0 != aligned_address);
33 #ifdef FLAC__ALIGN_MALLOC_DATA
34 /* align on 32-byte (256-bit) boundary */
36 *aligned_address = (void*)(((unsigned)x + 31) & -32);
44 FLAC__bool FLAC__memory_alloc_aligned_int32_array(unsigned elements, FLAC__int32 **unaligned_pointer, FLAC__int32 **aligned_pointer)
46 FLAC__int32 *pa, *pu; /* aligned pointer, unaligned pointer */
48 FLAC__ASSERT(elements > 0);
49 FLAC__ASSERT(0 != unaligned_pointer);
50 FLAC__ASSERT(0 != aligned_pointer);
51 FLAC__ASSERT(unaligned_pointer != aligned_pointer);
53 pu = (FLAC__int32*)FLAC__memory_alloc_aligned(sizeof(FLAC__int32) * elements, (void**)&pa);
58 if(*unaligned_pointer != 0)
59 free(*unaligned_pointer);
60 *unaligned_pointer = pu;
61 *aligned_pointer = pa;
66 FLAC__bool FLAC__memory_alloc_aligned_uint32_array(unsigned elements, FLAC__uint32 **unaligned_pointer, FLAC__uint32 **aligned_pointer)
68 FLAC__uint32 *pa, *pu; /* aligned pointer, unaligned pointer */
70 FLAC__ASSERT(elements > 0);
71 FLAC__ASSERT(0 != unaligned_pointer);
72 FLAC__ASSERT(0 != aligned_pointer);
73 FLAC__ASSERT(unaligned_pointer != aligned_pointer);
75 pu = (FLAC__uint32*)FLAC__memory_alloc_aligned(sizeof(FLAC__uint32) * elements, (void**)&pa);
80 if(*unaligned_pointer != 0)
81 free(*unaligned_pointer);
82 *unaligned_pointer = pu;
83 *aligned_pointer = pa;
88 FLAC__bool FLAC__memory_alloc_aligned_uint64_array(unsigned elements, FLAC__uint64 **unaligned_pointer, FLAC__uint64 **aligned_pointer)
90 FLAC__uint64 *pa, *pu; /* aligned pointer, unaligned pointer */
92 FLAC__ASSERT(elements > 0);
93 FLAC__ASSERT(0 != unaligned_pointer);
94 FLAC__ASSERT(0 != aligned_pointer);
95 FLAC__ASSERT(unaligned_pointer != aligned_pointer);
97 pu = (FLAC__uint64*)FLAC__memory_alloc_aligned(sizeof(FLAC__uint64) * elements, (void**)&pa);
102 if(*unaligned_pointer != 0)
103 free(*unaligned_pointer);
104 *unaligned_pointer = pu;
105 *aligned_pointer = pa;
110 FLAC__bool FLAC__memory_alloc_aligned_unsigned_array(unsigned elements, unsigned **unaligned_pointer, unsigned **aligned_pointer)
112 unsigned *pa, *pu; /* aligned pointer, unaligned pointer */
114 FLAC__ASSERT(elements > 0);
115 FLAC__ASSERT(0 != unaligned_pointer);
116 FLAC__ASSERT(0 != aligned_pointer);
117 FLAC__ASSERT(unaligned_pointer != aligned_pointer);
119 pu = (unsigned*)FLAC__memory_alloc_aligned(sizeof(unsigned) * elements, (void**)&pa);
124 if(*unaligned_pointer != 0)
125 free(*unaligned_pointer);
126 *unaligned_pointer = pu;
127 *aligned_pointer = pa;
132 FLAC__bool FLAC__memory_alloc_aligned_real_array(unsigned elements, FLAC__real **unaligned_pointer, FLAC__real **aligned_pointer)
134 FLAC__real *pa, *pu; /* aligned pointer, unaligned pointer */
136 FLAC__ASSERT(elements > 0);
137 FLAC__ASSERT(0 != unaligned_pointer);
138 FLAC__ASSERT(0 != aligned_pointer);
139 FLAC__ASSERT(unaligned_pointer != aligned_pointer);
141 pu = (FLAC__real*)FLAC__memory_alloc_aligned(sizeof(FLAC__real) * elements, (void**)&pa);
146 if(*unaligned_pointer != 0)
147 free(*unaligned_pointer);
148 *unaligned_pointer = pu;
149 *aligned_pointer = pa;