MPEGPlayer playlist should as well support all viewer-handled file extensions...indeed.
[kugel-rb.git] / lib / skin_parser / skin_buffer.c
blob0f72f768ddf0c22af0911fb8b9cbaf853fd8793a
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"
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 ****************************************************************************/
46 #ifdef ROCKBOX
47 #include "config.h"
49 #ifdef APPLICATION
50 # define USE_HOST_MALLOC
51 #else
52 # define USE_ROCKBOX_ALLOC
53 #endif
55 #endif
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;
61 #endif
63 #ifdef USE_HOST_MALLOC
65 struct malloc_object {
66 struct malloc_object *next;
67 char buf[0];
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;
75 while (obj)
77 this = obj;
78 obj = this->next;
79 free(this);
81 malloced_head = NULL;
82 malloced_tail = NULL;
85 #endif
87 void skin_buffer_init(char* buffer, size_t size)
89 #ifdef USE_ROCKBOX_ALLOC
90 buffer_start = buffer_front = buffer;
91 buf_size = size;
92 #elif defined(USE_HOST_MALLOC)
93 (void)buffer; (void)size;
94 skin_free_malloced();
95 #endif
98 /* Allocate size bytes from the buffer */
99 void* skin_buffer_alloc(size_t size)
101 void *retval = NULL;
102 #ifdef USE_ROCKBOX_ALLOC
103 /* 32-bit aligned */
104 size = (size + 3) & ~3;
105 if (size > skin_buffer_freespace())
106 return NULL;
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);
112 retval = &obj->buf;
113 obj->next = NULL;
114 if (malloced_tail == NULL)
115 malloced_head = malloced_tail = obj;
116 else
117 malloced_tail->next = obj;
118 malloced_tail = obj;
120 #else
121 retval = malloc(size);
122 #endif
123 return retval;
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;
149 #endif