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 ****************************************************************************/
26 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
27 unsigned char audiobuffer
[(MEMORYSIZE
*1024-256)*1024];
28 unsigned char *audiobufend
= audiobuffer
+ sizeof(audiobuffer
);
30 /* defined in linker script */
31 extern unsigned char audiobuffer
[];
34 unsigned char *audiobuf
;
36 #ifdef BUFFER_ALLOC_DEBUG
37 static unsigned char *audiobuf_orig_start
;
39 struct buffer_start_marker
44 #define BUF_MAGIC 0xDEADD0D0
46 struct buffer_end_marker
51 #endif /* BUFFER_ALLOC_DEBUG */
53 void buffer_init(void)
56 audiobuf
= (void *)(((unsigned long)audiobuffer
+ 3) & ~3);
57 #ifdef BUFFER_ALLOC_DEBUG
58 audiobuf_orig_start
= audiobuf
;
59 #endif /* BUFFER_ALLOC_DEBUG */
62 void *buffer_alloc(size_t size
)
64 void *retval
= audiobuf
;
65 #ifdef BUFFER_ALLOC_DEBUG
66 struct buffer_start_marker
*start
;
67 struct buffer_end_marker
*end
;
68 #endif /* BUFFER_ALLOC_DEBUG */
71 size
= (size
+ 3) & ~3;
73 #ifdef BUFFER_ALLOC_DEBUG
74 retval
+=sizeof(struct buffer_start_marker
);
77 end
=(struct buffer_end_marker
*)(audiobuf
- sizeof(struct buffer_end_marker
));
78 if(end
->magic
== BUF_MAGIC
)
82 start
=(struct buffer_start_marker
*)audiobuf
;
83 start
->magic
= BUF_MAGIC
;
84 start
->buffer_size
= size
;
85 end
=(struct buffer_end_marker
*)(audiobuf
+sizeof(struct buffer_start_marker
)+size
);
86 end
->magic
= BUF_MAGIC
;
89 audiobuf
= ((unsigned char *)end
) + sizeof(struct buffer_end_marker
);
92 logf("Alloc %x %d",(unsigned int)retval
,size
);
93 #else /* !BUFFER_ALLOC_DEBUG */
95 #endif /* BUFFER_ALLOC_DEBUG */
97 if (audiobuf
> audiobufend
) {
98 panicf("OOM: %d bytes", (int) size
);
104 #ifdef BUFFER_ALLOC_DEBUG
105 void buffer_alloc_check(char *name
)
107 unsigned char *buf_ptr
= audiobuf_orig_start
;
108 struct buffer_start_marker
*start
;
109 struct buffer_end_marker
*end
;
112 while(buf_ptr
< audiobuf
)
114 start
=(struct buffer_start_marker
*)buf_ptr
;
115 if(start
->magic
!= BUF_MAGIC
)
117 panicf("%s corrupted buffer %x start", name
,(unsigned int)buf_ptr
+sizeof(struct buffer_start_marker
));
119 end
=(struct buffer_end_marker
*)(buf_ptr
+sizeof(struct buffer_start_marker
)+start
->buffer_size
);
120 if(end
->magic
!= BUF_MAGIC
)
122 panicf("%s corrupted %x end", name
,(unsigned int)buf_ptr
+sizeof(struct buffer_start_marker
));
126 buf_ptr
=((unsigned char *)end
)+sizeof(struct buffer_end_marker
);
129 #endif /* BUFFER_ALLOC_DEBUG */