The "GR-PEACH_Audio_Playback_7InchLCD_Sample" is a sample code that can provides high-resolution audio playback of FLAC format files. It also allows the user to audio-playback control functions such as play, pause, and stop by manipulating key switches.

Dependencies:   GR-PEACH_video R_BSP TLV320_RBSP USBHost_custom

Fork of GR-PEACH_Audio_Playback_Sample by Renesas

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers memory.c Source File

memory.c

00001 /* libFLAC - Free Lossless Audio Codec library
00002  * Copyright (C) 2001-2009  Josh Coalson
00003  * Copyright (C) 2011-2014  Xiph.Org Foundation
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  * notice, this list of conditions and the following disclaimer.
00011  *
00012  * - Redistributions in binary form must reproduce the above copyright
00013  * notice, this list of conditions and the following disclaimer in the
00014  * documentation and/or other materials provided with the distribution.
00015  *
00016  * - Neither the name of the Xiph.org Foundation nor the names of its
00017  * contributors may be used to endorse or promote products derived from
00018  * this software without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00023  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
00024  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #ifdef HAVE_CONFIG_H
00034 #  include <config.h>
00035 #endif
00036 
00037 #ifdef HAVE_STDINT_H
00038 #include <stdint.h>
00039 #endif
00040 
00041 #include "private/memory.h"
00042 #include "FLAC/assert.h"
00043 #include "share/alloc.h"
00044 
00045 void *FLAC__memory_alloc_aligned(size_t bytes, void **aligned_address)
00046 {
00047     void *x;
00048 
00049     FLAC__ASSERT(0 != aligned_address);
00050 
00051 #ifdef FLAC__ALIGN_MALLOC_DATA
00052     /* align on 32-byte (256-bit) boundary */
00053     x = safe_malloc_add_2op_(bytes, /*+*/31L);
00054     *aligned_address = (void*)(((uintptr_t)x + 31L) & -32L);
00055 #else
00056     x = safe_malloc_(bytes);
00057     *aligned_address = x;
00058 #endif
00059     return x;
00060 }
00061 
00062 FLAC__bool FLAC__memory_alloc_aligned_int32_array(size_t elements, FLAC__int32 **unaligned_pointer, FLAC__int32 **aligned_pointer)
00063 {
00064     FLAC__int32 *pu; /* unaligned pointer */
00065     union { /* union needed to comply with C99 pointer aliasing rules */
00066         FLAC__int32 *pa; /* aligned pointer */
00067         void        *pv; /* aligned pointer alias */
00068     } u;
00069 
00070     FLAC__ASSERT(elements > 0);
00071     FLAC__ASSERT(0 != unaligned_pointer);
00072     FLAC__ASSERT(0 != aligned_pointer);
00073     FLAC__ASSERT(unaligned_pointer != aligned_pointer);
00074 
00075     if(elements > SIZE_MAX / sizeof(*pu)) /* overflow check */
00076         return false;
00077 
00078     pu = FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
00079     if(0 == pu) {
00080         return false;
00081     }
00082     else {
00083         if(*unaligned_pointer != 0)
00084             free(*unaligned_pointer);
00085         *unaligned_pointer = pu;
00086         *aligned_pointer = u.pa;
00087         return true;
00088     }
00089 }
00090 
00091 FLAC__bool FLAC__memory_alloc_aligned_uint32_array(size_t elements, FLAC__uint32 **unaligned_pointer, FLAC__uint32 **aligned_pointer)
00092 {
00093     FLAC__uint32 *pu; /* unaligned pointer */
00094     union { /* union needed to comply with C99 pointer aliasing rules */
00095         FLAC__uint32 *pa; /* aligned pointer */
00096         void         *pv; /* aligned pointer alias */
00097     } u;
00098 
00099     FLAC__ASSERT(elements > 0);
00100     FLAC__ASSERT(0 != unaligned_pointer);
00101     FLAC__ASSERT(0 != aligned_pointer);
00102     FLAC__ASSERT(unaligned_pointer != aligned_pointer);
00103 
00104     if(elements > SIZE_MAX / sizeof(*pu)) /* overflow check */
00105         return false;
00106 
00107     pu = FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
00108     if(0 == pu) {
00109         return false;
00110     }
00111     else {
00112         if(*unaligned_pointer != 0)
00113             free(*unaligned_pointer);
00114         *unaligned_pointer = pu;
00115         *aligned_pointer = u.pa;
00116         return true;
00117     }
00118 }
00119 
00120 FLAC__bool FLAC__memory_alloc_aligned_uint64_array(size_t elements, FLAC__uint64 **unaligned_pointer, FLAC__uint64 **aligned_pointer)
00121 {
00122     FLAC__uint64 *pu; /* unaligned pointer */
00123     union { /* union needed to comply with C99 pointer aliasing rules */
00124         FLAC__uint64 *pa; /* aligned pointer */
00125         void         *pv; /* aligned pointer alias */
00126     } u;
00127 
00128     FLAC__ASSERT(elements > 0);
00129     FLAC__ASSERT(0 != unaligned_pointer);
00130     FLAC__ASSERT(0 != aligned_pointer);
00131     FLAC__ASSERT(unaligned_pointer != aligned_pointer);
00132 
00133     if(elements > SIZE_MAX / sizeof(*pu)) /* overflow check */
00134         return false;
00135 
00136     pu = FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
00137     if(0 == pu) {
00138         return false;
00139     }
00140     else {
00141         if(*unaligned_pointer != 0)
00142             free(*unaligned_pointer);
00143         *unaligned_pointer = pu;
00144         *aligned_pointer = u.pa;
00145         return true;
00146     }
00147 }
00148 
00149 FLAC__bool FLAC__memory_alloc_aligned_unsigned_array(size_t elements, unsigned **unaligned_pointer, unsigned **aligned_pointer)
00150 {
00151     unsigned *pu; /* unaligned pointer */
00152     union { /* union needed to comply with C99 pointer aliasing rules */
00153         unsigned *pa; /* aligned pointer */
00154         void     *pv; /* aligned pointer alias */
00155     } u;
00156 
00157     FLAC__ASSERT(elements > 0);
00158     FLAC__ASSERT(0 != unaligned_pointer);
00159     FLAC__ASSERT(0 != aligned_pointer);
00160     FLAC__ASSERT(unaligned_pointer != aligned_pointer);
00161 
00162     if(elements > SIZE_MAX / sizeof(*pu)) /* overflow check */
00163         return false;
00164 
00165     pu = FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
00166     if(0 == pu) {
00167         return false;
00168     }
00169     else {
00170         if(*unaligned_pointer != 0)
00171             free(*unaligned_pointer);
00172         *unaligned_pointer = pu;
00173         *aligned_pointer = u.pa;
00174         return true;
00175     }
00176 }
00177 
00178 #ifndef FLAC__INTEGER_ONLY_LIBRARY
00179 
00180 FLAC__bool FLAC__memory_alloc_aligned_real_array(size_t elements, FLAC__real **unaligned_pointer, FLAC__real **aligned_pointer)
00181 {
00182     FLAC__real *pu; /* unaligned pointer */
00183     union { /* union needed to comply with C99 pointer aliasing rules */
00184         FLAC__real *pa; /* aligned pointer */
00185         void       *pv; /* aligned pointer alias */
00186     } u;
00187 
00188     FLAC__ASSERT(elements > 0);
00189     FLAC__ASSERT(0 != unaligned_pointer);
00190     FLAC__ASSERT(0 != aligned_pointer);
00191     FLAC__ASSERT(unaligned_pointer != aligned_pointer);
00192 
00193     if(elements > SIZE_MAX / sizeof(*pu)) /* overflow check */
00194         return false;
00195 
00196     pu = FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
00197     if(0 == pu) {
00198         return false;
00199     }
00200     else {
00201         if(*unaligned_pointer != 0)
00202             free(*unaligned_pointer);
00203         *unaligned_pointer = pu;
00204         *aligned_pointer = u.pa;
00205         return true;
00206     }
00207 }
00208 
00209 #endif
00210 
00211 void *safe_malloc_mul_2op_p(size_t size1, size_t size2)
00212 {
00213     if(!size1 || !size2)
00214         return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
00215     if(size1 > SIZE_MAX / size2)
00216         return 0;
00217     return malloc(size1*size2);
00218 }