diff options
Diffstat (limited to 'apps/plugins/lua/rockmalloc.c')
-rw-r--r-- | apps/plugins/lua/rockmalloc.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/apps/plugins/lua/rockmalloc.c b/apps/plugins/lua/rockmalloc.c new file mode 100644 index 0000000000..14a43396ea --- /dev/null +++ b/apps/plugins/lua/rockmalloc.c | |||
@@ -0,0 +1,63 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2008 Dan Everton (safetydan) | ||
11 | * | ||
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. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "plugin.h" | ||
23 | #include "rockmalloc.h" | ||
24 | #include "malloc.c" | ||
25 | |||
26 | void *rocklua_morecore(int size) | ||
27 | { | ||
28 | static char *sbrk_top = NULL; | ||
29 | static ssize_t total_size = 0; | ||
30 | void *ptr = 0; | ||
31 | |||
32 | if (size > 0) { | ||
33 | size = size + 4 - (size % 4); | ||
34 | |||
35 | if (sbrk_top == NULL) { | ||
36 | sbrk_top = rb->plugin_get_buffer((size_t *) &total_size); | ||
37 | } | ||
38 | |||
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); | ||
42 | |||
43 | if(sbrk_top == NULL || size + 4 > abs(total_size)) { | ||
44 | printf("malloc failed for %d!\n", size); | ||
45 | return (void *) MFAIL; | ||
46 | } | ||
47 | } | ||
48 | |||
49 | ptr = sbrk_top + 4; | ||
50 | *((unsigned int *) sbrk_top) = size; | ||
51 | |||
52 | sbrk_top += size + 4; | ||
53 | total_size -= size + 4; | ||
54 | |||
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; | ||
61 | } | ||
62 | } | ||
63 | |||