1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 #include "rockmalloc.h"
26 void *rocklua_morecore(int size
)
28 static char *sbrk_top
= NULL
;
29 static ssize_t total_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
;
50 *((unsigned int *) sbrk_top
) = size
;
53 total_size
-= size
+ 4;
56 } else if (size
< 0) {
57 /* we don't currently support shrink behavior */
58 return (void *) MFAIL
;