Reload the current playlist after reboot even if it has ended. (FS#11644)
[kugel-rb.git] / firmware / buffer.c
blobc317cec9249c8d4bc015addb453bc3718452b136
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 "buffer.h"
23 #include "panic.h"
24 #include "logf.h"
26 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
27 unsigned char audiobuffer[(MEMORYSIZE*1024-256)*1024];
28 unsigned char *audiobufend = audiobuffer + sizeof(audiobuffer);
29 #else
30 /* defined in linker script */
31 extern unsigned char audiobuffer[];
32 #endif
34 unsigned char *audiobuf;
36 #ifdef BUFFER_ALLOC_DEBUG
37 static unsigned char *audiobuf_orig_start;
39 struct buffer_start_marker
41 unsigned int magic;
42 size_t buffer_size;
44 #define BUF_MAGIC 0xDEADD0D0
46 struct buffer_end_marker
48 unsigned int magic;
49 int last;
51 #endif /* BUFFER_ALLOC_DEBUG */
53 void buffer_init(void)
55 /* 32-bit aligned */
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;
65 #ifdef BUFFER_ALLOC_DEBUG
66 struct buffer_start_marker *start;
67 struct buffer_end_marker *end;
68 #endif /* BUFFER_ALLOC_DEBUG */
70 /* 32-bit aligned */
71 size = (size + 3) & ~3;
73 /* Other code touches audiobuf. Make sure it stays aligned */
74 audiobuf = (void *)(((unsigned long)audiobuf + 3) & ~3);
76 retval = audiobuf;
78 #ifdef BUFFER_ALLOC_DEBUG
79 retval +=sizeof(struct buffer_start_marker);
80 if(size>0)
82 end=(struct buffer_end_marker*)(audiobuf - sizeof(struct buffer_end_marker));
83 if(end->magic == BUF_MAGIC)
85 end->last=0;
87 start=(struct buffer_start_marker*)audiobuf;
88 start->magic = BUF_MAGIC;
89 start->buffer_size = size;
90 end=(struct buffer_end_marker*)(audiobuf+sizeof(struct buffer_start_marker)+size);
91 end->magic = BUF_MAGIC;
92 end->last = 1;
94 audiobuf = ((unsigned char *)end) + sizeof(struct buffer_end_marker);
97 logf("Alloc %x %d",(unsigned int)retval,size);
98 #else /* !BUFFER_ALLOC_DEBUG */
99 audiobuf += size;
100 #endif /* BUFFER_ALLOC_DEBUG */
102 if (audiobuf > audiobufend) {
103 panicf("OOM: %d bytes", (int) size);
106 return retval;
109 #ifdef BUFFER_ALLOC_DEBUG
110 void buffer_alloc_check(char *name)
112 unsigned char *buf_ptr = audiobuf_orig_start;
113 struct buffer_start_marker *start;
114 struct buffer_end_marker *end;
117 while(buf_ptr < audiobuf)
119 start=(struct buffer_start_marker*)buf_ptr;
120 if(start->magic != BUF_MAGIC)
122 panicf("%s corrupted buffer %x start", name,(unsigned int)buf_ptr+sizeof(struct buffer_start_marker));
124 end=(struct buffer_end_marker*)(buf_ptr+sizeof(struct buffer_start_marker)+start->buffer_size);
125 if(end->magic != BUF_MAGIC)
127 panicf("%s corrupted %x end", name,(unsigned int)buf_ptr+sizeof(struct buffer_start_marker));
129 if(end->last)
130 break;
131 buf_ptr=((unsigned char *)end)+sizeof(struct buffer_end_marker);
134 #endif /* BUFFER_ALLOC_DEBUG */