imxtools/sbtoelf: use new method to get default xor key
[maemo-rb.git] / lib / skin_parser / skin_buffer.c
blobd18122ef20f2e1ce5592ca643c5a074849d63cbe
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"
50 static size_t buf_size;
51 static unsigned char *buffer_start = NULL;
52 static unsigned char *buffer_front = NULL;
54 #ifndef __PCTOOL__
55 long skin_buffer_to_offset(void *pointer)
57 return pointer == NULL ? -1 : (void*)pointer - (void*)buffer_start;
60 void* skin_buffer_from_offset(long offset)
62 return offset < 0 ? NULL : buffer_start + offset;
64 #endif
66 void skin_buffer_init(char* buffer, size_t size)
68 buffer_start = buffer_front = buffer;
69 buf_size = size;
72 /* Allocate size bytes from the buffer */
73 #ifdef DEBUG_SKIN_ALLOCATIONS
74 void* skin_buffer_alloc_ex(size_t size, char* debug)
76 void *retval = NULL;
77 printf("%d %s\n", size, debug);
78 #else
79 void* skin_buffer_alloc(size_t size)
81 void *retval = NULL;
82 #endif
83 /* 32-bit aligned */
84 size = (size + 3) & ~3;
85 if (size > skin_buffer_freespace())
87 skin_error(MEMORY_LIMIT_EXCEEDED, NULL);
88 return NULL;
90 retval = buffer_front;
91 buffer_front += size;
92 return retval;
95 /* get the number of bytes currently being used */
96 size_t skin_buffer_usage(void)
98 return buffer_front - buffer_start;
100 size_t skin_buffer_freespace(void)
102 return buf_size - skin_buffer_usage();
104 #else
105 void* skin_buffer_alloc(size_t size)
107 return malloc(size);
109 #endif