diff options
Diffstat (limited to 'lib/skin_parser/skin_buffer.c')
-rw-r--r-- | lib/skin_parser/skin_buffer.c | 73 |
1 files changed, 10 insertions, 63 deletions
diff --git a/lib/skin_parser/skin_buffer.c b/lib/skin_parser/skin_buffer.c index 5a9d4464b8..d18122ef20 100644 --- a/lib/skin_parser/skin_buffer.c +++ b/lib/skin_parser/skin_buffer.c | |||
@@ -47,54 +47,26 @@ | |||
47 | #ifdef ROCKBOX | 47 | #ifdef ROCKBOX |
48 | #include "config.h" | 48 | #include "config.h" |
49 | #include "skin_debug.h" | 49 | #include "skin_debug.h" |
50 | |||
51 | #ifdef APPLICATION | ||
52 | # define USE_HOST_MALLOC | ||
53 | #else | ||
54 | # define USE_ROCKBOX_ALLOC | ||
55 | #endif | ||
56 | |||
57 | #endif | ||
58 | |||
59 | #ifdef USE_ROCKBOX_ALLOC | ||
60 | static size_t buf_size; | 50 | static size_t buf_size; |
61 | static unsigned char *buffer_start = NULL; | 51 | static unsigned char *buffer_start = NULL; |
62 | static unsigned char *buffer_front = NULL; | 52 | static unsigned char *buffer_front = NULL; |
63 | #endif | ||
64 | |||
65 | #ifdef USE_HOST_MALLOC | ||
66 | |||
67 | struct malloc_object { | ||
68 | struct malloc_object *next; | ||
69 | char buf[0]; | ||
70 | }; | ||
71 | static struct malloc_object *malloced_head = NULL, *malloced_tail = NULL; | ||
72 | 53 | ||
73 | static void skin_free_malloced(void) | 54 | #ifndef __PCTOOL__ |
55 | long skin_buffer_to_offset(void *pointer) | ||
74 | { | 56 | { |
75 | struct malloc_object *obj = malloced_head; | 57 | return pointer == NULL ? -1 : (void*)pointer - (void*)buffer_start; |
76 | struct malloc_object *this; | ||
77 | while (obj) | ||
78 | { | ||
79 | this = obj; | ||
80 | obj = this->next; | ||
81 | free(this); | ||
82 | } | ||
83 | malloced_head = NULL; | ||
84 | malloced_tail = NULL; | ||
85 | } | 58 | } |
86 | 59 | ||
60 | void* skin_buffer_from_offset(long offset) | ||
61 | { | ||
62 | return offset < 0 ? NULL : buffer_start + offset; | ||
63 | } | ||
87 | #endif | 64 | #endif |
88 | 65 | ||
89 | void skin_buffer_init(char* buffer, size_t size) | 66 | void skin_buffer_init(char* buffer, size_t size) |
90 | { | 67 | { |
91 | #ifdef USE_ROCKBOX_ALLOC | ||
92 | buffer_start = buffer_front = buffer; | 68 | buffer_start = buffer_front = buffer; |
93 | buf_size = size; | 69 | buf_size = size; |
94 | #elif defined(USE_HOST_MALLOC) | ||
95 | (void)buffer; (void)size; | ||
96 | skin_free_malloced(); | ||
97 | #endif | ||
98 | } | 70 | } |
99 | 71 | ||
100 | /* Allocate size bytes from the buffer */ | 72 | /* Allocate size bytes from the buffer */ |
@@ -108,8 +80,6 @@ void* skin_buffer_alloc(size_t size) | |||
108 | { | 80 | { |
109 | void *retval = NULL; | 81 | void *retval = NULL; |
110 | #endif | 82 | #endif |
111 | |||
112 | #ifdef USE_ROCKBOX_ALLOC | ||
113 | /* 32-bit aligned */ | 83 | /* 32-bit aligned */ |
114 | size = (size + 3) & ~3; | 84 | size = (size + 3) & ~3; |
115 | if (size > skin_buffer_freespace()) | 85 | if (size > skin_buffer_freespace()) |
@@ -119,25 +89,9 @@ void* skin_buffer_alloc(size_t size) | |||
119 | } | 89 | } |
120 | retval = buffer_front; | 90 | retval = buffer_front; |
121 | buffer_front += size; | 91 | buffer_front += size; |
122 | #elif defined(USE_HOST_MALLOC) | ||
123 | size_t malloc_size = sizeof(struct malloc_object) + size; | ||
124 | struct malloc_object *obj = malloc(malloc_size); | ||
125 | retval = &obj->buf; | ||
126 | obj->next = NULL; | ||
127 | if (malloced_tail == NULL) | ||
128 | malloced_head = malloced_tail = obj; | ||
129 | else | ||
130 | malloced_tail->next = obj; | ||
131 | malloced_tail = obj; | ||
132 | |||
133 | #else | ||
134 | retval = malloc(size); | ||
135 | #endif | ||
136 | return retval; | 92 | return retval; |
137 | } | 93 | } |
138 | 94 | ||
139 | |||
140 | #ifdef USE_ROCKBOX_ALLOC | ||
141 | /* get the number of bytes currently being used */ | 95 | /* get the number of bytes currently being used */ |
142 | size_t skin_buffer_usage(void) | 96 | size_t skin_buffer_usage(void) |
143 | { | 97 | { |
@@ -147,16 +101,9 @@ size_t skin_buffer_freespace(void) | |||
147 | { | 101 | { |
148 | return buf_size - skin_buffer_usage(); | 102 | return buf_size - skin_buffer_usage(); |
149 | } | 103 | } |
150 | 104 | #else | |
151 | static unsigned char *saved_buffer_pos = NULL; | 105 | void* skin_buffer_alloc(size_t size) |
152 | void skin_buffer_save_position(void) | ||
153 | { | ||
154 | saved_buffer_pos = buffer_front; | ||
155 | } | ||
156 | |||
157 | void skin_buffer_restore_position(void) | ||
158 | { | 106 | { |
159 | if (saved_buffer_pos) | 107 | return malloc(size); |
160 | buffer_front = saved_buffer_pos; | ||
161 | } | 108 | } |
162 | #endif | 109 | #endif |