1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
28 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
29 unsigned char audiobuffer
[(MEMORYSIZE
*1024-256)*1024];
30 unsigned char *audiobufend
= audiobuffer
+ sizeof(audiobuffer
);
32 /* defined in linker script */
33 extern unsigned char audiobuffer
[];
36 unsigned char *audiobuf
;
38 #ifdef BUFFER_ALLOC_DEBUG
39 static unsigned char *audiobuf_orig_start
;
41 struct buffer_start_marker
46 #define BUF_MAGIC 0xDEADD0D0
48 struct buffer_end_marker
53 #endif /* BUFFER_ALLOC_DEBUG */
55 void buffer_init(void)
58 audiobuf
= (void *)(((unsigned long)audiobuffer
+ 3) & ~3);
59 #ifdef BUFFER_ALLOC_DEBUG
60 audiobuf_orig_start
= audiobuf
;
61 #endif /* BUFFER_ALLOC_DEBUG */
64 /* protect concurrent access */
68 * Give the entire buffer, return the size in size.
69 * The caller needs to make sure audiobuf is not otherwise used
71 * Note that this does not modify the buffer position (buffer_release_buffer()
72 * does), so call this if you want to aquire temporary memory
74 #define _ALIGN (sizeof(char*))
75 void *buffer_get_buffer(size_t *size
)
78 panicf("concurrent audiobuf access");
80 audiobuf
= ALIGN_UP(audiobuf
, sizeof(intptr_t));
81 *size
= (audiobufend
- audiobuf
);
82 DEBUGF("%s(): Returning %8p\n", __func__
, audiobuf
);
87 * Release the buffer gotten with buffer_get_buffer
89 * size should have the amount of bytes (from the front) that caller keeps for
90 * its own, 0 if the entire buffer is to be released
92 * safe to be called with size=0 even if the buffer wasn't claimed before
94 void buffer_release_buffer(size_t size
)
96 DEBUGF("%s(): Claiming %zu bytes\n", __func__
, size
);
98 /* ensure alignment */
99 audiobuf
= ALIGN_UP(audiobuf
, sizeof(intptr_t));
104 * Query how much free space the buffer has */
105 size_t buffer_available(void)
107 return audiobufend
- audiobuf
;
110 void *buffer_alloc(size_t size
)
112 if (lock
) /* it's not save to call this here */
113 panicf("buffer_alloc(): exclusive buffer owner");
114 void *retval
= audiobuf
;
115 #ifdef BUFFER_ALLOC_DEBUG
116 struct buffer_start_marker
*start
;
117 struct buffer_end_marker
*end
;
118 #endif /* BUFFER_ALLOC_DEBUG */
121 size
= (size
+ 3) & ~3;
123 #ifdef BUFFER_ALLOC_DEBUG
124 retval
+=sizeof(struct buffer_start_marker
);
127 end
=(struct buffer_end_marker
*)(audiobuf
- sizeof(struct buffer_end_marker
));
128 if(end
->magic
== BUF_MAGIC
)
132 start
=(struct buffer_start_marker
*)audiobuf
;
133 start
->magic
= BUF_MAGIC
;
134 start
->buffer_size
= size
;
135 end
=(struct buffer_end_marker
*)(audiobuf
+sizeof(struct buffer_start_marker
)+size
);
136 end
->magic
= BUF_MAGIC
;
139 audiobuf
= ((unsigned char *)end
) + sizeof(struct buffer_end_marker
);
142 logf("Alloc %x %d",(unsigned int)retval
,size
);
143 #else /* !BUFFER_ALLOC_DEBUG */
145 #endif /* BUFFER_ALLOC_DEBUG */
147 if (audiobuf
> audiobufend
) {
148 panicf("OOM: %d bytes", (int) size
);
154 #ifdef BUFFER_ALLOC_DEBUG
155 void buffer_alloc_check(char *name
)
157 unsigned char *buf_ptr
= audiobuf_orig_start
;
158 struct buffer_start_marker
*start
;
159 struct buffer_end_marker
*end
;
162 while(buf_ptr
< audiobuf
)
164 start
=(struct buffer_start_marker
*)buf_ptr
;
165 if(start
->magic
!= BUF_MAGIC
)
167 panicf("%s corrupted buffer %x start", name
,(unsigned int)buf_ptr
+sizeof(struct buffer_start_marker
));
169 end
=(struct buffer_end_marker
*)(buf_ptr
+sizeof(struct buffer_start_marker
)+start
->buffer_size
);
170 if(end
->magic
!= BUF_MAGIC
)
172 panicf("%s corrupted %x end", name
,(unsigned int)buf_ptr
+sizeof(struct buffer_start_marker
));
176 buf_ptr
=((unsigned char *)end
)+sizeof(struct buffer_end_marker
);
179 #endif /* BUFFER_ALLOC_DEBUG */