From d1322b71595336740eb5e18e5deed056ddb71c7a Mon Sep 17 00:00:00 2001 From: Thomas Martitz Date: Sun, 14 Aug 2011 15:13:00 +0000 Subject: GSoC/Buflib: Replace all direct accesses to audiobuf with buffer API functions. Namely, introduce buffer_get_buffer() and buffer_release_buffer(). buffer_get_buffer() aquires all available and grabs a lock, attempting to call buffer_alloc() or buffer_get_buffer() while this lock is locked will cause a panicf() (doesn't actually happen, but is for debugging purpose). buffer_release_buffer() unlocks that lock and can additionally increment the audiobuf buffer to make an allocation. Pass 0 to only unlock if buffer was used temporarily only. buffer_available() is a replacement function to query audiobuflen, i.e. what's left in the buffer. Buffer init is moved up in the init chain and handles ipodvideo64mb internally. Further changes happened to mp3data.c and talk.c as to not call the above API functions, but get the buffer from callers. The caller is the audio system which has the buffer lock while mp3data.c and talk mess with the buffer. mpeg.c now implements some buffer related functions of playback.h, especially audio_get_buffer(), allowing to reduce #ifdef hell a tiny bit. audiobuf and audiobufend are local to buffer.c now. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30308 a1c6a512-1295-4272-9138-f99709370657 --- firmware/buffer.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 3 deletions(-) (limited to 'firmware/buffer.c') diff --git a/firmware/buffer.c b/firmware/buffer.c index c317cec924..2168087bd9 100644 --- a/firmware/buffer.c +++ b/firmware/buffer.c @@ -19,19 +19,33 @@ * ****************************************************************************/ #include +#include +#include "system.h" #include "buffer.h" #include "panic.h" #include "logf.h" #if (CONFIG_PLATFORM & PLATFORM_HOSTED) -unsigned char audiobuffer[(MEMORYSIZE*1024-256)*1024]; -unsigned char *audiobufend = audiobuffer + sizeof(audiobuffer); #else +#endif + +/* defined in linker script */ +#if (CONFIG_PLATFORM & PLATFORM_NATIVE) +#if defined(IPOD_VIDEO) +extern unsigned char *audiobufend_lds[]; +unsigned char *audiobufend; +#else /* !IPOD_VIDEO */ +extern unsigned char audiobufend[]; +#endif /* defined in linker script */ extern unsigned char audiobuffer[]; +#else /* PLATFORM_HOSTED */ +unsigned char audiobuffer[(MEMORYSIZE*1024-256)*1024]; +unsigned char *audiobufend = audiobuffer + sizeof(audiobuffer); +extern unsigned char *audiobufend; #endif -unsigned char *audiobuf; +static unsigned char *audiobuf; #ifdef BUFFER_ALLOC_DEBUG static unsigned char *audiobuf_orig_start; @@ -54,13 +68,68 @@ void buffer_init(void) { /* 32-bit aligned */ audiobuf = (void *)(((unsigned long)audiobuffer + 3) & ~3); + +#if defined(IPOD_VIDEO) + audiobufend=(unsigned char *)audiobufend_lds; + if(MEMORYSIZE==64 && probed_ramsize!=64) + { + audiobufend -= (32<<20); + } +#endif + #ifdef BUFFER_ALLOC_DEBUG audiobuf_orig_start = audiobuf; #endif /* BUFFER_ALLOC_DEBUG */ } +/* protect concurrent access */ +static volatile int lock; + +/* + * Give the entire buffer, return the size in size. + * The caller needs to make sure audiobuf is not otherwise used + * + * Note that this does not modify the buffer position (buffer_release_buffer() + * does), so call this if you want to aquire temporary memory + **/ +#define _ALIGN (sizeof(char*)) +void *buffer_get_buffer(size_t *size) +{ + if (lock) + panicf("concurrent audiobuf access"); + lock = 1; + audiobuf = ALIGN_UP(audiobuf, sizeof(intptr_t)); + *size = (audiobufend - audiobuf); + return audiobuf; +} + +/* + * Release the buffer gotten with buffer_get_buffer + * + * size should have the amount of bytes (from the front) that caller keeps for + * its own, 0 if the entire buffer is to be released + * + * safe to be called with size=0 even if the buffer wasn't claimed before + **/ +void buffer_release_buffer(size_t size) +{ + audiobuf += size; + /* ensure alignment */ + audiobuf = ALIGN_UP(audiobuf, sizeof(intptr_t)); + lock = 0; +} + +/* + * Query how much free space the buffer has */ +size_t buffer_available(void) +{ + return audiobufend - audiobuf; +} + void *buffer_alloc(size_t size) { + if (lock) /* it's not save to call this here */ + panicf("buffer_alloc(): exclusive buffer owner"); void *retval; #ifdef BUFFER_ALLOC_DEBUG struct buffer_start_marker *start; -- cgit v1.2.3