Add some better debug info for when checkwps erros out because of full skin buffer
[kugel-rb.git] / lib / skin_parser / skin_buffer.c
blob8d94c1acdb33dd4f588310ff632e650d5971558d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
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 ****************************************************************************/
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
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 ****************************************************************************/
47 #ifdef ROCKBOX
48 #include "config.h"
50 #ifdef APPLICATION
51 # define USE_HOST_MALLOC
52 #else
53 # define USE_ROCKBOX_ALLOC
54 #endif
56 #endif
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;
62 #endif
64 #ifdef USE_HOST_MALLOC
66 struct malloc_object {
67 struct malloc_object *next;
68 char buf[0];
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;
76 while (obj)
78 this = obj;
79 obj = this->next;
80 free(this);
82 malloced_head = NULL;
83 malloced_tail = NULL;
86 #endif
88 void skin_buffer_init(char* buffer, size_t size)
90 #ifdef USE_ROCKBOX_ALLOC
91 buffer_start = buffer_front = buffer;
92 buf_size = size;
93 #elif defined(USE_HOST_MALLOC)
94 (void)buffer; (void)size;
95 skin_free_malloced();
96 #endif
99 /* Allocate size bytes from the buffer */
100 void* skin_buffer_alloc(size_t size)
102 void *retval = NULL;
103 #ifdef USE_ROCKBOX_ALLOC
104 /* 32-bit aligned */
105 size = (size + 3) & ~3;
106 if (size > skin_buffer_freespace())
108 skin_error(MEMORY_LIMIT_EXCEEDED, NULL);
109 return 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);
116 retval = &obj->buf;
117 obj->next = NULL;
118 if (malloced_tail == NULL)
119 malloced_head = malloced_tail = obj;
120 else
121 malloced_tail->next = obj;
122 malloced_tail = obj;
124 #else
125 retval = malloc(size);
126 #endif
127 return retval;
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;
153 #endif