Android: Partly revert r29569 and only call the new getJavaEnvironment() when needed.
[maemo-rb.git] / lib / skin_parser / skin_buffer.c
blob94f2e3ba7df8f6f9223ae9aaaece93877a057098
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"
49 #include "skin_debug.h"
51 #ifdef APPLICATION
52 # define USE_HOST_MALLOC
53 #else
54 # define USE_ROCKBOX_ALLOC
55 #endif
57 #endif
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;
63 #endif
65 #ifdef USE_HOST_MALLOC
67 struct malloc_object {
68 struct malloc_object *next;
69 char buf[0];
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;
77 while (obj)
79 this = obj;
80 obj = this->next;
81 free(this);
83 malloced_head = NULL;
84 malloced_tail = NULL;
87 #endif
89 void skin_buffer_init(char* buffer, size_t size)
91 #ifdef USE_ROCKBOX_ALLOC
92 buffer_start = buffer_front = buffer;
93 buf_size = size;
94 #elif defined(USE_HOST_MALLOC)
95 (void)buffer; (void)size;
96 skin_free_malloced();
97 #endif
100 /* Allocate size bytes from the buffer */
101 void* skin_buffer_alloc(size_t size)
103 void *retval = NULL;
104 #ifdef USE_ROCKBOX_ALLOC
105 /* 32-bit aligned */
106 size = (size + 3) & ~3;
107 if (size > skin_buffer_freespace())
109 skin_error(MEMORY_LIMIT_EXCEEDED, NULL);
110 return 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);
117 retval = &obj->buf;
118 obj->next = NULL;
119 if (malloced_tail == NULL)
120 malloced_head = malloced_tail = obj;
121 else
122 malloced_tail->next = obj;
123 malloced_tail = obj;
125 #else
126 retval = malloc(size);
127 #endif
128 return retval;
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;
154 #endif