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 ****************************************************************************/
49 #include "skin_debug.h"
52 # define USE_HOST_MALLOC
54 # define USE_ROCKBOX_ALLOC
59 #ifdef USE_ROCKBOX_ALLOC
60 static size_t buf_size
;
61 static unsigned char *buffer_start
= NULL
;
62 static unsigned char *buffer_front
= NULL
;
65 #ifdef USE_HOST_MALLOC
67 struct malloc_object
{
68 struct malloc_object
*next
;
71 static struct malloc_object
*malloced_head
= NULL
, *malloced_tail
= NULL
;
73 static void skin_free_malloced(void)
75 struct malloc_object
*obj
= malloced_head
;
76 struct malloc_object
*this;
89 void skin_buffer_init(char* buffer
, size_t size
)
91 #ifdef USE_ROCKBOX_ALLOC
92 buffer_start
= buffer_front
= buffer
;
94 #elif defined(USE_HOST_MALLOC)
95 (void)buffer
; (void)size
;
100 /* Allocate size bytes from the buffer */
101 void* skin_buffer_alloc(size_t size
)
104 #ifdef USE_ROCKBOX_ALLOC
106 size
= (size
+ 3) & ~3;
107 if (size
> skin_buffer_freespace())
109 skin_error(MEMORY_LIMIT_EXCEEDED
, NULL
);
112 retval
= buffer_front
;
113 buffer_front
+= size
;
114 #elif defined(USE_HOST_MALLOC)
115 size_t malloc_size
= sizeof(struct malloc_object
) + size
;
116 struct malloc_object
*obj
= malloc(malloc_size
);
119 if (malloced_tail
== NULL
)
120 malloced_head
= malloced_tail
= obj
;
122 malloced_tail
->next
= obj
;
126 retval
= malloc(size
);
132 #ifdef USE_ROCKBOX_ALLOC
133 /* get the number of bytes currently being used */
134 size_t skin_buffer_usage(void)
136 return buffer_front
- buffer_start
;
138 size_t skin_buffer_freespace(void)
140 return buf_size
- skin_buffer_usage();
143 static unsigned char *saved_buffer_pos
= NULL
;
144 void skin_buffer_save_position(void)
146 saved_buffer_pos
= buffer_front
;
149 void skin_buffer_restore_position(void)
151 if (saved_buffer_pos
)
152 buffer_front
= saved_buffer_pos
;