1
0
mirror of git://projects.qi-hardware.com/ben-blinkenlights.git synced 2024-11-05 10:18:26 +02:00

ubb-la/gui.c (textf): use vsnprintf+valloca instead of vasprintf

This way, we don't malloc/free all the time, which could lead to more
memory fragmentation than alloca at roughly the same stack location.
This commit is contained in:
Werner Almesberger 2013-01-31 02:50:01 -03:00
parent 52c3dcc16a
commit 06c5f6b0fa

View File

@ -10,13 +10,10 @@
* (at your option) any later version.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE /* for vasprintf */
#endif
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <alloca.h>
#include "SDL.h"
#include "SDL_gfxPrimitives.h"
@ -124,13 +121,14 @@ static void textf(int x, int y, uint32_t color, const char *fmt, ...)
va_list ap;
char *tmp, *s;
uint8_t *p;
int ix, iy;
int n, ix, iy;
va_start(ap, fmt);
if (vasprintf(&tmp, fmt, ap) < 0) {
perror("vasprintf");
exit(1);
}
n = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
tmp = alloca(n+1);
va_start(ap, fmt);
vsnprintf(tmp, n+1, fmt, ap);
va_end(ap);
for (s = tmp; *s; s++) {
@ -143,7 +141,6 @@ static void textf(int x, int y, uint32_t color, const char *fmt, ...)
}
x += 8;
}
free(tmp);
}