From cce9ceaffd4cb9d2bc55c92798a38dfa000d4f48 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Tue, 12 Dec 2017 16:57:20 -0800 Subject: [PATCH] Try and allocate inner loop mem on stack Check the available stack space, and if large enough use a stack variable for the inner loop. If not, fall back to heap allocation. --- src/AudioGeneratorMP3.cpp | 33 +++++++++++++++++++++++++++++++++ src/libmad/config.h | 9 +++++++++ src/libmad/layer3.c | 15 ++++++++++++--- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/AudioGeneratorMP3.cpp b/src/AudioGeneratorMP3.cpp index 52e291b..2229948 100644 --- a/src/AudioGeneratorMP3.cpp +++ b/src/AudioGeneratorMP3.cpp @@ -232,3 +232,36 @@ bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output) return true; } +// The following are helper routines for use in libmad to check stack/heap free +// and to determine if there's enough stack space to allocate some blocks there +// instead of precious heap. + +#undef stack +extern "C" { + #include + extern cont_t g_cont; + + void stack(const char *s, const char *t, int i) { + (void) t; + (void) i; + register uint32_t *sp asm("a1"); + int freestack = 4 * (sp - g_cont.stack); + int freeheap = ESP.getFreeHeap(); + if ((freestack < 512) || (freeheap < 5120)) { + static int laststack, lastheap; + if (laststack!=freestack|| lastheap !=freeheap) + Serial.printf("%s: FREESTACK=%d, FREEHEAP=%d\n", s, /*t, i,*/ freestack, /*cont_get_free_stack(&g_cont),*/ freeheap); Serial.flush(); + if (freestack < 256) {Serial.printf("out of stack!\n"); while (1); } + if (freeheap < 1024) {Serial.printf("out of heap!\n"); while (1); } + laststack=freestack;lastheap=freeheap; + } + } + + int stackfree() + { + register uint32_t *sp asm("a1"); + int freestack = 4 * (sp - g_cont.stack); + return freestack; + } +} + diff --git a/src/libmad/config.h b/src/libmad/config.h index 2e7465f..0cb10b4 100644 --- a/src/libmad/config.h +++ b/src/libmad/config.h @@ -7,6 +7,15 @@ // Uncomment to show heap and stack space on entry #define stack(a,b,c) +// Helper function to see if we can allocate one chunk on the stack +# ifdef __cplusplus +extern "C" { +# endif +extern int stackfree(); +# ifdef __cplusplus +}; +# endif + /* Define to enable experimental code. */ /* #undef EXPERIMENTAL */ diff --git a/src/libmad/layer3.c b/src/libmad/layer3.c index 37ba707..c071723 100644 --- a/src/libmad/layer3.c +++ b/src/libmad/layer3.c @@ -1321,8 +1321,16 @@ enum mad_error III_reorder(mad_fixed_t xr[576], struct channel const *channel, { unsigned int sb, l, f, w, sbw[3], sw[3]; mad_fixed_t *tmp; // [32][3][6] - tmp = (mad_fixed_t*)malloc(sizeof(mad_fixed_t)*32*3*6); - if (!tmp) return MAD_ERROR_NOMEM; + // See if we can allocate this buffer on the stack and save heap + char onstack = 0; + if (stackfree() > (int)(100 + sizeof(mad_fixed_t)*32*3*6)) { + onstack = 1; + tmp = alloca(sizeof(mad_fixed_t)*32*3*6); + } else { + tmp = (mad_fixed_t*)malloc(sizeof(mad_fixed_t)*32*3*6); + if (!tmp) return MAD_ERROR_NOMEM; + } + stack(__FUNCTION__, __FILE__, __LINE__); /* this is probably wrong for 8000 Hz mixed blocks */ @@ -1360,7 +1368,8 @@ enum mad_error III_reorder(mad_fixed_t xr[576], struct channel const *channel, memcpy(&xr[18 * sb], &tmp[sb * 3 * 6], (576 - 18 * sb) * sizeof(mad_fixed_t)); - free(tmp); + if (!onstack) free(tmp); + // If it's on the stack, it'll go away on the return return MAD_ERROR_NONE; }