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"
28 #include "skin_parser.h"
30 /****************************************************************************
32 * This code handles buffer allocation for the entire skin system.
33 * This needs to work in 3 different situations:
34 * 1) as a stand alone library. ROCKBOX isnt defined, alloc using malloc()
35 * and free the skin elements only (no callbacks doing more allocation)
36 * 2) ROCKBOX builds for normal targets, alloc from a single big buffer
37 * which origionally came from the audio buffer, likely to run out of
38 * room with large themes. No need to free anything, just restore to
39 * the start of our buffer
40 * 3) ROCKBOX "application/hosted" builds, alloc using the hosts malloc().
41 * We need to keep track of all allocations so they can be free()'d easily
44 ****************************************************************************/
51 # define USE_HOST_MALLOC
53 # define USE_ROCKBOX_ALLOC
58 #ifdef USE_ROCKBOX_ALLOC
59 static size_t buf_size
;
60 static unsigned char *buffer_start
= NULL
;
61 static unsigned char *buffer_front
= NULL
;
64 #ifdef USE_HOST_MALLOC
66 struct malloc_object
{
67 struct malloc_object
*next
;
70 static struct malloc_object
*malloced_head
= NULL
, *malloced_tail
= NULL
;
72 static void skin_free_malloced(void)
74 struct malloc_object
*obj
= malloced_head
;
75 struct malloc_object
*this;
88 void skin_buffer_init(char* buffer
, size_t size
)
90 #ifdef USE_ROCKBOX_ALLOC
91 buffer_start
= buffer_front
= buffer
;
93 #elif defined(USE_HOST_MALLOC)
94 (void)buffer
; (void)size
;
99 /* Allocate size bytes from the buffer */
100 void* skin_buffer_alloc(size_t size
)
103 #ifdef USE_ROCKBOX_ALLOC
105 size
= (size
+ 3) & ~3;
106 if (size
> skin_buffer_freespace())
108 skin_error(MEMORY_LIMIT_EXCEEDED
, NULL
);
111 retval
= buffer_front
;
112 buffer_front
+= size
;
113 #elif defined(USE_HOST_MALLOC)
114 size_t malloc_size
= sizeof(struct malloc_object
) + size
;
115 struct malloc_object
*obj
= malloc(malloc_size
);
118 if (malloced_tail
== NULL
)
119 malloced_head
= malloced_tail
= obj
;
121 malloced_tail
->next
= obj
;
125 retval
= malloc(size
);
131 #ifdef USE_ROCKBOX_ALLOC
132 /* get the number of bytes currently being used */
133 size_t skin_buffer_usage(void)
135 return buffer_front
- buffer_start
;
137 size_t skin_buffer_freespace(void)
139 return buf_size
- skin_buffer_usage();
142 static unsigned char *saved_buffer_pos
= NULL
;
143 void skin_buffer_save_position(void)
145 saved_buffer_pos
= buffer_front
;
148 void skin_buffer_restore_position(void)
150 if (saved_buffer_pos
)
151 buffer_front
= saved_buffer_pos
;