Few more fixes, notably make audiobufend static.
[kugel-rb.git] / firmware / buffer.c
blob8ce920609e700e38dbcc298910fe0486d31707d3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include <stdint.h>
23 #include "system.h"
24 #include "buffer.h"
25 #include "panic.h"
26 #include "logf.h"
28 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
29 #else
30 #endif
32 /* defined in linker script */
33 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
34 #if defined(IPOD_VIDEO)
35 extern unsigned char *audiobufend_lds[];
36 unsigned char *audiobufend;
37 #else /* !IPOD_VIDEO */
38 extern unsigned char audiobufend[];
39 #endif
40 /* defined in linker script */
41 extern unsigned char audiobuffer[];
42 #else /* PLATFORM_HOSTED */
43 unsigned char audiobuffer[(MEMORYSIZE*1024-256)*1024];
44 unsigned char *audiobufend = audiobuffer + sizeof(audiobuffer);
45 extern unsigned char *audiobufend;
46 #endif
48 static unsigned char *audiobuf;
50 #ifdef BUFFER_ALLOC_DEBUG
51 static unsigned char *audiobuf_orig_start;
53 struct buffer_start_marker
55 unsigned int magic;
56 size_t buffer_size;
58 #define BUF_MAGIC 0xDEADD0D0
60 struct buffer_end_marker
62 unsigned int magic;
63 int last;
65 #endif /* BUFFER_ALLOC_DEBUG */
67 void buffer_init(void)
69 /* 32-bit aligned */
70 audiobuf = (void *)(((unsigned long)audiobuffer + 3) & ~3);
72 #if defined(IPOD_VIDEO)
73 audiobufend=(unsigned char *)audiobufend_lds;
74 if(MEMORYSIZE==64 && probed_ramsize!=64)
76 audiobufend -= (32<<20);
78 #endif
80 #ifdef BUFFER_ALLOC_DEBUG
81 audiobuf_orig_start = audiobuf;
82 #endif /* BUFFER_ALLOC_DEBUG */
85 /* protect concurrent access */
86 static int lock;
89 * Give the entire buffer, return the size in size.
90 * The caller needs to make sure audiobuf is not otherwise used
92 * Note that this does not modify the buffer position (buffer_release_buffer()
93 * does), so call this if you want to aquire temporary memory
94 **/
95 #define _ALIGN (sizeof(char*))
96 void *buffer_get_buffer(size_t *size)
98 if (lock)
99 panicf("concurrent audiobuf access");
100 lock = 1;
101 audiobuf = ALIGN_UP(audiobuf, sizeof(intptr_t));
102 *size = (audiobufend - audiobuf);
103 DEBUGF("%s(): Returning %8p\n", __func__, audiobuf);
104 return audiobuf;
108 * Release the buffer gotten with buffer_get_buffer
110 * size should have the amount of bytes (from the front) that caller keeps for
111 * its own, 0 if the entire buffer is to be released
113 * safe to be called with size=0 even if the buffer wasn't claimed before
115 void buffer_release_buffer(size_t size)
117 DEBUGF("%s(): Claiming %zu bytes\n", __func__, size);
118 audiobuf += size;
119 /* ensure alignment */
120 audiobuf = ALIGN_UP(audiobuf, sizeof(intptr_t));
121 lock = 0;
125 * Query how much free space the buffer has */
126 size_t buffer_available(void)
128 return audiobufend - audiobuf;
131 void *buffer_alloc(size_t size)
133 if (lock) /* it's not save to call this here */
134 panicf("buffer_alloc(): exclusive buffer owner");
135 void *retval = audiobuf;
136 #ifdef BUFFER_ALLOC_DEBUG
137 struct buffer_start_marker *start;
138 struct buffer_end_marker *end;
139 #endif /* BUFFER_ALLOC_DEBUG */
141 /* 32-bit aligned */
142 size = (size + 3) & ~3;
144 #ifdef BUFFER_ALLOC_DEBUG
145 retval +=sizeof(struct buffer_start_marker);
146 if(size>0)
148 end=(struct buffer_end_marker*)(audiobuf - sizeof(struct buffer_end_marker));
149 if(end->magic == BUF_MAGIC)
151 end->last=0;
153 start=(struct buffer_start_marker*)audiobuf;
154 start->magic = BUF_MAGIC;
155 start->buffer_size = size;
156 end=(struct buffer_end_marker*)(audiobuf+sizeof(struct buffer_start_marker)+size);
157 end->magic = BUF_MAGIC;
158 end->last = 1;
160 audiobuf = ((unsigned char *)end) + sizeof(struct buffer_end_marker);
163 logf("Alloc %x %d",(unsigned int)retval,size);
164 #else /* !BUFFER_ALLOC_DEBUG */
165 audiobuf += size;
166 #endif /* BUFFER_ALLOC_DEBUG */
168 if (audiobuf > audiobufend) {
169 panicf("OOM: %d bytes", (int) size);
172 return retval;
175 #ifdef BUFFER_ALLOC_DEBUG
176 void buffer_alloc_check(char *name)
178 unsigned char *buf_ptr = audiobuf_orig_start;
179 struct buffer_start_marker *start;
180 struct buffer_end_marker *end;
183 while(buf_ptr < audiobuf)
185 start=(struct buffer_start_marker*)buf_ptr;
186 if(start->magic != BUF_MAGIC)
188 panicf("%s corrupted buffer %x start", name,(unsigned int)buf_ptr+sizeof(struct buffer_start_marker));
190 end=(struct buffer_end_marker*)(buf_ptr+sizeof(struct buffer_start_marker)+start->buffer_size);
191 if(end->magic != BUF_MAGIC)
193 panicf("%s corrupted %x end", name,(unsigned int)buf_ptr+sizeof(struct buffer_start_marker));
195 if(end->last)
196 break;
197 buf_ptr=((unsigned char *)end)+sizeof(struct buffer_end_marker);
200 #endif /* BUFFER_ALLOC_DEBUG */