From 637c7414a91c33e2627f99fee7b4c9cbf04abdb4 Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Fri, 13 Jan 2017 18:45:04 -0500 Subject: puzzles: enable fallback to audiobuf when smalloc() fails - upon a failed smalloc(), the audio buffer will be used for further allocations - should fix things on low-memory targets (c100 and c200v2), but breaks playback - playback should still be intact on other targets Change-Id: Ic239f1316efadc957050afacf5c614dbbca3f805 --- apps/plugins/puzzles/malloc.c | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) (limited to 'apps/plugins/puzzles/malloc.c') diff --git a/apps/plugins/puzzles/malloc.c b/apps/plugins/puzzles/malloc.c index d9943c6b40..47a7137459 100644 --- a/apps/plugins/puzzles/malloc.c +++ b/apps/plugins/puzzles/malloc.c @@ -14,12 +14,40 @@ int allocs = 0; int frees = 0; +bool audiobuf_available = +#ifndef COMBINED + true; +#else + false; +#endif + +static bool grab_audiobuf(void) +{ + if(!audiobuf_available) + return false; + + if(rb->audio_status()) + rb->audio_stop(); + + size_t sz, junk; + void *audiobuf = rb->plugin_get_audio_buffer(&sz); + extern char *giant_buffer; + + add_new_area(audiobuf, sz, giant_buffer); + audiobuf_available = false; + return true; +} + void *smalloc(size_t size) { void *p; p = malloc(size); LOGF("allocs: %d", ++allocs); if (!p) - fatal("out of memory"); + { + if(grab_audiobuf()) + return smalloc(size); + fatal("out of memory"); + } return p; } @@ -30,7 +58,7 @@ void sfree(void *p) { if (p) { ++frees; LOGF("frees: %d, total outstanding: %d", frees, allocs - frees); - free(p); + free(p); } } @@ -46,7 +74,11 @@ void *srealloc(void *p, size_t size) { q = malloc(size); } if (!q) - fatal("out of memory"); + { + if(grab_audiobuf()) + return srealloc(p, size); + fatal("out of memory"); + } return q; } -- cgit v1.2.3