fix red.
[kugel-rb/myfork.git] / apps / plugins / lua / rockmalloc.c
blob14a43396ea595c6840e835ac09393351521506f0
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 Dan Everton (safetydan)
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "plugin.h"
23 #include "rockmalloc.h"
24 #include "malloc.c"
26 void *rocklua_morecore(int size)
28 static char *sbrk_top = NULL;
29 static ssize_t total_size = 0;
30 void *ptr = 0;
32 if (size > 0) {
33 size = size + 4 - (size % 4);
35 if (sbrk_top == NULL) {
36 sbrk_top = rb->plugin_get_buffer((size_t *) &total_size);
39 if (sbrk_top == NULL || size + 4 > abs(total_size)) {
40 /* Try the audio buffer */
41 sbrk_top = rb->plugin_get_audio_buffer((size_t *) &total_size);
43 if(sbrk_top == NULL || size + 4 > abs(total_size)) {
44 printf("malloc failed for %d!\n", size);
45 return (void *) MFAIL;
49 ptr = sbrk_top + 4;
50 *((unsigned int *) sbrk_top) = size;
52 sbrk_top += size + 4;
53 total_size -= size + 4;
55 return ptr;
56 } else if (size < 0) {
57 /* we don't currently support shrink behavior */
58 return (void *) MFAIL;
59 } else {
60 return sbrk_top;