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.
This commit is contained in:
1 parent
f2550b0ab1
commit
cce9ceaffd
3 files changed
+54
-3
No files matched your search
@@ -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 <cont.h>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 */
|
||||
|
||||
|
||||
+12
-3
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user