1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * $Id: skin_buffer.c 25962 2010-05-12 09:31:40Z jdgordon $
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
11 * Copyright (C) 2010 Jonathan Gordon
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
27 #include "skin_buffer.h"
29 /****************************************************************************
31 * This code handles buffer allocation for the entire skin system.
32 * This needs to work in 3 different situations:
33 * 1) as a stand alone library. ROCKBOX isnt defined, alloc using malloc()
34 * and free the skin elements only (no callbacks doing more allocation)
35 * 2) ROCKBOX builds for normal targets, alloc from a single big buffer
36 * which origionally came from the audio buffer, likely to run out of
37 * room with large themes. No need to free anything, just restore to
38 * the start of our buffer
39 * 3) ROCKBOX "application/hosted" builds, alloc using the hosts malloc().
40 * We need to keep track of all allocations so they can be free()'d easily
43 ****************************************************************************/
50 # define USE_HOST_MALLOC
52 # define USE_ROCKBOX_ALLOC
57 #ifdef USE_ROCKBOX_ALLOC
58 static size_t buf_size
;
59 static unsigned char *buffer_start
= NULL
;
60 static unsigned char *buffer_front
= NULL
;
63 #ifdef USE_HOST_MALLOC
65 struct malloc_object
{
66 struct malloc_object
*next
;
69 static struct malloc_object
*malloced_head
= NULL
, *malloced_tail
= NULL
;
71 static void skin_free_malloced(void)
73 struct malloc_object
*obj
= malloced_head
;
74 struct malloc_object
*this;
87 void skin_buffer_init(char* buffer
, size_t size
)
89 #ifdef USE_ROCKBOX_ALLOC
90 buffer_start
= buffer_front
= buffer
;
92 #elif defined(USE_HOST_MALLOC)
93 (void)buffer
; (void)size
;
98 /* Allocate size bytes from the buffer */
99 void* skin_buffer_alloc(size_t size
)
102 #ifdef USE_ROCKBOX_ALLOC
104 size
= (size
+ 3) & ~3;
105 if (size
> skin_buffer_freespace())
107 retval
= buffer_front
;
108 buffer_front
+= size
;
109 #elif defined(USE_HOST_MALLOC)
110 size_t malloc_size
= sizeof(struct malloc_object
) + size
;
111 struct malloc_object
*obj
= malloc(malloc_size
);
114 if (malloced_tail
== NULL
)
115 malloced_head
= malloced_tail
= obj
;
117 malloced_tail
->next
= obj
;
121 retval
= malloc(size
);
127 #ifdef USE_ROCKBOX_ALLOC
128 /* get the number of bytes currently being used */
129 size_t skin_buffer_usage(void)
131 return buffer_front
- buffer_start
;
133 size_t skin_buffer_freespace(void)
135 return buf_size
- skin_buffer_usage();
138 static unsigned char *saved_buffer_pos
= NULL
;
139 void skin_buffer_save_position(void)
141 saved_buffer_pos
= buffer_front
;
144 void skin_buffer_restore_position(void)
146 if (saved_buffer_pos
)
147 buffer_front
= saved_buffer_pos
;