summaryrefslogtreecommitdiff
path: root/lib/skin_parser/skin_buffer.c
diff options
context:
space:
mode:
authorJonathan Gordon <rockbox@jdgordon.info>2010-11-04 10:15:33 +0000
committerJonathan Gordon <rockbox@jdgordon.info>2010-11-04 10:15:33 +0000
commit97857324c32f929c396814ba268e39a865a6fa42 (patch)
treeffb911b3fac2e01f95956e540807583b92e65134 /lib/skin_parser/skin_buffer.c
parent72964f2571cce19524c45254d3b71639dd07def3 (diff)
downloadrockbox-97857324c32f929c396814ba268e39a865a6fa42.tar.gz
rockbox-97857324c32f929c396814ba268e39a865a6fa42.zip
RaaA: Use the host's malloc() for the skin engine.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@28480 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'lib/skin_parser/skin_buffer.c')
-rw-r--r--lib/skin_parser/skin_buffer.c74
1 files changed, 70 insertions, 4 deletions
diff --git a/lib/skin_parser/skin_buffer.c b/lib/skin_parser/skin_buffer.c
index ecfe04b167..608b03434d 100644
--- a/lib/skin_parser/skin_buffer.c
+++ b/lib/skin_parser/skin_buffer.c
@@ -8,7 +8,7 @@
8 * $Id: skin_buffer.c 25962 2010-05-12 09:31:40Z jdgordon $ 8 * $Id: skin_buffer.c 25962 2010-05-12 09:31:40Z jdgordon $
9 * 9 *
10 * Copyright (C) 2002 by Linus Nielsen Feltzing 10 * Copyright (C) 2002 by Linus Nielsen Feltzing
11 * Copyright (C) 2009 Jonathan Gordon 11 * Copyright (C) 2010 Jonathan Gordon
12 * 12 *
13 * This program is free software; you can redistribute it and/or 13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License 14 * modify it under the terms of the GNU General Public License
@@ -26,17 +26,72 @@
26 26
27#include "skin_buffer.h" 27#include "skin_buffer.h"
28 28
29/****************************************************************************
30 *
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
41 *
42 *
43 ****************************************************************************/
44
45
29#ifdef ROCKBOX 46#ifdef ROCKBOX
47#include "config.h"
48
49# if (CONFIG_PLATFORM&PLATFORM_HOSTED)
50# define USE_HOST_MALLOC
51# else
52# define USE_ROCKBOX_ALLOC
53# endif
54
55#endif
56
57#ifdef USE_ROCKBOX_ALLOC
30static size_t buf_size; 58static size_t buf_size;
31static unsigned char *buffer_start = NULL; 59static unsigned char *buffer_start = NULL;
32static unsigned char *buffer_front = NULL; 60static unsigned char *buffer_front = NULL;
33#endif 61#endif
34 62
63#ifdef USE_HOST_MALLOC
64
65struct malloc_object {
66 void* object;
67 struct malloc_object *next;
68};
69struct malloc_object *first = NULL, *last = NULL;
70
71void skin_free_malloced(void)
72{
73 struct malloc_object *obj = first, *this;
74 while (obj)
75 {
76 this = obj;
77 obj = this->next;
78 free(this->object);
79 free(this);
80 }
81 first = NULL;
82 last = NULL;
83}
84
85#endif
86
35void skin_buffer_init(char* buffer, size_t size) 87void skin_buffer_init(char* buffer, size_t size)
36{ 88{
37#if defined(ROCKBOX) 89#ifdef USE_ROCKBOX_ALLOC
38 buffer_start = buffer_front = buffer; 90 buffer_start = buffer_front = buffer;
39 buf_size = size; 91 buf_size = size;
92#elif defined(USE_HOST_MALLOC)
93 (void)buffer; (void)size;
94 skin_free_malloced();
40#endif 95#endif
41} 96}
42 97
@@ -44,13 +99,24 @@ void skin_buffer_init(char* buffer, size_t size)
44void* skin_buffer_alloc(size_t size) 99void* skin_buffer_alloc(size_t size)
45{ 100{
46 void *retval = NULL; 101 void *retval = NULL;
47#ifdef ROCKBOX 102#ifdef USE_ROCKBOX_ALLOC
48 /* 32-bit aligned */ 103 /* 32-bit aligned */
49 size = (size + 3) & ~3; 104 size = (size + 3) & ~3;
50 if (size > skin_buffer_freespace()) 105 if (size > skin_buffer_freespace())
51 return NULL; 106 return NULL;
52 retval = buffer_front; 107 retval = buffer_front;
53 buffer_front += size; 108 buffer_front += size;
109#elif defined(USE_HOST_MALLOC)
110 struct malloc_object *obj = malloc(sizeof (struct malloc_object));
111 if (!obj)
112 return NULL;
113 obj->object = malloc(size);
114 obj->next = NULL;
115 if (last == NULL)
116 first = last = obj;
117 else
118 last->next = obj;
119 retval = obj->object;
54#else 120#else
55 retval = malloc(size); 121 retval = malloc(size);
56#endif 122#endif
@@ -58,7 +124,7 @@ void* skin_buffer_alloc(size_t size)
58} 124}
59 125
60 126
61#ifdef ROCKBOX 127#ifdef USE_ROCKBOX_ALLOC
62/* get the number of bytes currently being used */ 128/* get the number of bytes currently being used */
63size_t skin_buffer_usage(void) 129size_t skin_buffer_usage(void)
64{ 130{