blob: 94f2e3ba7df8f6f9223ae9aaaece93877a057098 [file] [log] [blame]
Jonathan Gordondb658d92010-06-17 08:36:28 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: skin_buffer.c 25962 2010-05-12 09:31:40Z jdgordon $
9 *
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
Jonathan Gordon97857322010-11-04 10:15:33 +000011 * Copyright (C) 2010 Jonathan Gordon
Jonathan Gordondb658d92010-06-17 08:36:28 +000012 *
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.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22
23#include <stdio.h>
24#include <string.h>
25#include <stdlib.h>
26
Jonathan Gordon2d31d772010-07-29 12:37:48 +000027#include "skin_buffer.h"
Jonathan Gordon21e89e82011-01-27 11:27:53 +000028#include "skin_parser.h"
Jonathan Gordon2d31d772010-07-29 12:37:48 +000029
Jonathan Gordon97857322010-11-04 10:15:33 +000030/****************************************************************************
31 *
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
42 *
43 *
44 ****************************************************************************/
45
46
Jonathan Gordondb658d92010-06-17 08:36:28 +000047#ifdef ROCKBOX
Jonathan Gordon97857322010-11-04 10:15:33 +000048#include "config.h"
Jonathan Gordoncb56c462011-01-27 11:33:33 +000049#include "skin_debug.h"
Jonathan Gordon97857322010-11-04 10:15:33 +000050
Thomas Martitz5411e142010-11-05 19:12:23 +000051#ifdef APPLICATION
Jonathan Gordon97857322010-11-04 10:15:33 +000052# define USE_HOST_MALLOC
Thomas Martitz5411e142010-11-05 19:12:23 +000053#else
Jonathan Gordon97857322010-11-04 10:15:33 +000054# define USE_ROCKBOX_ALLOC
Thomas Martitz5411e142010-11-05 19:12:23 +000055#endif
Jonathan Gordon97857322010-11-04 10:15:33 +000056
57#endif
58
59#ifdef USE_ROCKBOX_ALLOC
Jonathan Gordon2d31d772010-07-29 12:37:48 +000060static size_t buf_size;
61static unsigned char *buffer_start = NULL;
62static unsigned char *buffer_front = NULL;
Jonathan Gordondb658d92010-06-17 08:36:28 +000063#endif
64
Jonathan Gordon97857322010-11-04 10:15:33 +000065#ifdef USE_HOST_MALLOC
66
67struct malloc_object {
Jonathan Gordon97857322010-11-04 10:15:33 +000068 struct malloc_object *next;
Jonathan Gordonc98e5252010-11-06 14:18:33 +000069 char buf[0];
Jonathan Gordon97857322010-11-04 10:15:33 +000070};
Alexander Levinde56a222010-11-05 22:10:42 +000071static struct malloc_object *malloced_head = NULL, *malloced_tail = NULL;
Jonathan Gordon97857322010-11-04 10:15:33 +000072
Jonathan Gordonc8f143f2010-11-04 22:20:31 +000073static void skin_free_malloced(void)
Jonathan Gordon97857322010-11-04 10:15:33 +000074{
Alexander Levinde56a222010-11-05 22:10:42 +000075 struct malloc_object *obj = malloced_head;
76 struct malloc_object *this;
Jonathan Gordon97857322010-11-04 10:15:33 +000077 while (obj)
78 {
79 this = obj;
80 obj = this->next;
Jonathan Gordon97857322010-11-04 10:15:33 +000081 free(this);
82 }
Alexander Levinde56a222010-11-05 22:10:42 +000083 malloced_head = NULL;
84 malloced_tail = NULL;
Jonathan Gordon97857322010-11-04 10:15:33 +000085}
86
87#endif
88
Jonathan Gordon2d31d772010-07-29 12:37:48 +000089void skin_buffer_init(char* buffer, size_t size)
Jonathan Gordondb658d92010-06-17 08:36:28 +000090{
Jonathan Gordon97857322010-11-04 10:15:33 +000091#ifdef USE_ROCKBOX_ALLOC
Jonathan Gordon2d31d772010-07-29 12:37:48 +000092 buffer_start = buffer_front = buffer;
93 buf_size = size;
Jonathan Gordon97857322010-11-04 10:15:33 +000094#elif defined(USE_HOST_MALLOC)
95 (void)buffer; (void)size;
Alexander Levinde56a222010-11-05 22:10:42 +000096 skin_free_malloced();
Jonathan Gordondb658d92010-06-17 08:36:28 +000097#endif
98}
99
100/* Allocate size bytes from the buffer */
101void* skin_buffer_alloc(size_t size)
102{
103 void *retval = NULL;
Jonathan Gordon97857322010-11-04 10:15:33 +0000104#ifdef USE_ROCKBOX_ALLOC
Jonathan Gordondcc0d7d2010-09-26 12:56:02 +0000105 /* 32-bit aligned */
106 size = (size + 3) & ~3;
Jonathan Gordon2d31d772010-07-29 12:37:48 +0000107 if (size > skin_buffer_freespace())
Jonathan Gordon21e89e82011-01-27 11:27:53 +0000108 {
109 skin_error(MEMORY_LIMIT_EXCEEDED, NULL);
Jonathan Gordon2d31d772010-07-29 12:37:48 +0000110 return NULL;
Jonathan Gordon21e89e82011-01-27 11:27:53 +0000111 }
Jonathan Gordondb658d92010-06-17 08:36:28 +0000112 retval = buffer_front;
113 buffer_front += size;
Jonathan Gordon97857322010-11-04 10:15:33 +0000114#elif defined(USE_HOST_MALLOC)
Jonathan Gordonc98e5252010-11-06 14:18:33 +0000115 size_t malloc_size = sizeof(struct malloc_object) + size;
116 struct malloc_object *obj = malloc(malloc_size);
117 retval = &obj->buf;
Jonathan Gordon97857322010-11-04 10:15:33 +0000118 obj->next = NULL;
Alexander Levinde56a222010-11-05 22:10:42 +0000119 if (malloced_tail == NULL)
120 malloced_head = malloced_tail = obj;
Jonathan Gordon97857322010-11-04 10:15:33 +0000121 else
Alexander Levinde56a222010-11-05 22:10:42 +0000122 malloced_tail->next = obj;
123 malloced_tail = obj;
Jonathan Gordonc98e5252010-11-06 14:18:33 +0000124
Jonathan Gordondb658d92010-06-17 08:36:28 +0000125#else
126 retval = malloc(size);
127#endif
128 return retval;
129}
Jonathan Gordon95b0cef2010-06-17 13:54:09 +0000130
131
Jonathan Gordon97857322010-11-04 10:15:33 +0000132#ifdef USE_ROCKBOX_ALLOC
Jonathan Gordon95b0cef2010-06-17 13:54:09 +0000133/* get the number of bytes currently being used */
134size_t skin_buffer_usage(void)
135{
Jonathan Gordon2d31d772010-07-29 12:37:48 +0000136 return buffer_front - buffer_start;
Jonathan Gordon95b0cef2010-06-17 13:54:09 +0000137}
138size_t skin_buffer_freespace(void)
139{
Jonathan Gordon2d31d772010-07-29 12:37:48 +0000140 return buf_size - skin_buffer_usage();
141}
142
143static unsigned char *saved_buffer_pos = NULL;
144void skin_buffer_save_position(void)
145{
146 saved_buffer_pos = buffer_front;
147}
148
149void skin_buffer_restore_position(void)
150{
151 if (saved_buffer_pos)
152 buffer_front = saved_buffer_pos;
Jonathan Gordon95b0cef2010-06-17 13:54:09 +0000153}
154#endif