Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/lib/skin_parser/skin_buffer.c b/lib/skin_parser/skin_buffer.c
- index 3c0870e..59c6a02 100644
- --- a/lib/skin_parser/skin_buffer.c
- +++ b/lib/skin_parser/skin_buffer.c
- @@ -62,10 +62,14 @@ static unsigned char *buffer_front = NULL;
- #ifdef USE_HOST_MALLOC
- +#define MALLOC_BLOCK_SIZE (4*1024)
- struct malloc_object {
- - void* object;
- + size_t remaining;
- + char *start;
- struct malloc_object *next;
- + char buf[];
- };
- +#define MALLOC_FREE_SPACE (MALLOC_BLOCK_SIZE - sizeof(struct malloc_object) - 32)
- static struct malloc_object *malloced_head = NULL, *malloced_tail = NULL;
- static void skin_free_malloced(void)
- @@ -76,7 +80,6 @@ static void skin_free_malloced(void)
- {
- this = obj;
- obj = this->next;
- - free(this->object);
- free(this);
- }
- malloced_head = NULL;
- @@ -100,25 +103,37 @@ void skin_buffer_init(char* buffer, size_t size)
- void* skin_buffer_alloc(size_t size)
- {
- void *retval = NULL;
- + size = (size + 3) & ~3;
- #ifdef USE_ROCKBOX_ALLOC
- /* 32-bit aligned */
- - size = (size + 3) & ~3;
- if (size > skin_buffer_freespace())
- return NULL;
- retval = buffer_front;
- buffer_front += size;
- #elif defined(USE_HOST_MALLOC)
- - struct malloc_object *obj = malloc(sizeof (struct malloc_object));
- - if (!obj)
- - return NULL;
- - obj->object = malloc(size);
- - obj->next = NULL;
- - if (malloced_tail == NULL)
- - malloced_head = malloced_tail = obj;
- + if (malloced_tail && size <= malloced_tail->remaining)
- + {
- + retval = malloced_tail->start;
- + malloced_tail->start += size;
- + malloced_tail->remaining -= size;
- + }
- else
- - malloced_tail->next = obj;
- - malloced_tail = obj;
- - retval = obj->object;
- + {
- + size_t malloc_size = MALLOC_BLOCK_SIZE;
- + struct malloc_object *obj;
- + if (size > MALLOC_FREE_SPACE)
- + malloc_size = size + sizeof(struct malloc_object);
- + obj = malloc(malloc_size);
- + retval = obj->buf;
- + obj->start = obj->buf + size;
- + obj->remaining = malloc_size - sizeof(struct malloc_object);
- + obj->next = NULL;
- + if (malloced_tail == NULL)
- + malloced_head = malloced_tail = obj;
- + else
- + malloced_tail->next = obj;
- + malloced_tail = obj;
- + }
- #else
- retval = malloc(size);
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement