GSoC/Buflib: Enable compaction in buflib.
[kugel-rb.git] / firmware / buffer.c
blobe0496fe149ceaa1b22d2fb27bf68f1c052480770
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) && !defined(BOOTLOADER)
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) && !defined(BOOTLOADER) && !defined(SIMULATOR)
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 volatile 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 void *buffer_get_buffer(size_t *size)
97 if (lock)
98 panicf("concurrent audiobuf access");
99 lock = 1;
100 audiobuf = ALIGN_UP(audiobuf, sizeof(intptr_t));
101 *size = (audiobufend - audiobuf);
102 return audiobuf;
106 * Release the buffer gotten with buffer_get_buffer
108 * size should have the amount of bytes (from the front) that caller keeps for
109 * its own, 0 if the entire buffer is to be released
111 * safe to be called with size=0 even if the buffer wasn't claimed before
113 void buffer_release_buffer(size_t size)
115 audiobuf += size;
116 /* ensure alignment */
117 audiobuf = ALIGN_UP(audiobuf, sizeof(intptr_t));
118 lock = 0;
122 * Query how much free space the buffer has */
123 size_t buffer_available(void)
125 return audiobufend - audiobuf;
128 void *buffer_alloc(size_t size)
130 if (lock) /* it's not save to call this here */
131 panicf("buffer_alloc(): exclusive buffer owner");
132 void *retval;
133 #ifdef BUFFER_ALLOC_DEBUG
134 struct buffer_start_marker *start;
135 struct buffer_end_marker *end;
136 #endif /* BUFFER_ALLOC_DEBUG */
138 /* 32-bit aligned */
139 size = (size + 3) & ~3;
141 /* Other code touches audiobuf. Make sure it stays aligned */
142 audiobuf = (void *)(((unsigned long)audiobuf + 3) & ~3);
144 retval = audiobuf;
146 #ifdef BUFFER_ALLOC_DEBUG
147 retval +=sizeof(struct buffer_start_marker);
148 if(size>0)
150 end=(struct buffer_end_marker*)(audiobuf - sizeof(struct buffer_end_marker));
151 if(end->magic == BUF_MAGIC)
153 end->last=0;
155 start=(struct buffer_start_marker*)audiobuf;
156 start->magic = BUF_MAGIC;
157 start->buffer_size = size;
158 end=(struct buffer_end_marker*)(audiobuf+sizeof(struct buffer_start_marker)+size);
159 end->magic = BUF_MAGIC;
160 end->last = 1;
162 audiobuf = ((unsigned char *)end) + sizeof(struct buffer_end_marker);
165 logf("Alloc %x %d",(unsigned int)retval,size);
166 #else /* !BUFFER_ALLOC_DEBUG */
167 audiobuf += size;
168 #endif /* BUFFER_ALLOC_DEBUG */
170 if (audiobuf > audiobufend) {
171 panicf("OOM: %d bytes", (int) size);
174 return retval;
177 #ifdef BUFFER_ALLOC_DEBUG
178 void buffer_alloc_check(char *name)
180 unsigned char *buf_ptr = audiobuf_orig_start;
181 struct buffer_start_marker *start;
182 struct buffer_end_marker *end;
185 while(buf_ptr < audiobuf)
187 start=(struct buffer_start_marker*)buf_ptr;
188 if(start->magic != BUF_MAGIC)
190 panicf("%s corrupted buffer %x start", name,(unsigned int)buf_ptr+sizeof(struct buffer_start_marker));
192 end=(struct buffer_end_marker*)(buf_ptr+sizeof(struct buffer_start_marker)+start->buffer_size);
193 if(end->magic != BUF_MAGIC)
195 panicf("%s corrupted %x end", name,(unsigned int)buf_ptr+sizeof(struct buffer_start_marker));
197 if(end->last)
198 break;
199 buf_ptr=((unsigned char *)end)+sizeof(struct buffer_end_marker);
202 #endif /* BUFFER_ALLOC_DEBUG */