1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
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 ****************************************************************************/
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
;
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
;
66 void skin_buffer_init(char* buffer
, size_t size
)
68 buffer_start
= buffer_front
= buffer
;
72 /* Allocate size bytes from the buffer */
73 #ifdef DEBUG_SKIN_ALLOCATIONS
74 void* skin_buffer_alloc_ex(size_t size
, char* debug
)
77 printf("%d %s\n", size
, debug
);
79 void* skin_buffer_alloc(size_t size
)
84 size
= (size
+ 3) & ~3;
85 if (size
> skin_buffer_freespace())
87 skin_error(MEMORY_LIMIT_EXCEEDED
, NULL
);
90 retval
= buffer_front
;
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();
105 void* skin_buffer_alloc(size_t size
)