Date:2013-01-31 06:50:01 (6 years 7 months ago)
Author:Werner Almesberger
Commit:06c5f6b0fa8cc493d93cfb87f8cbc62a877c9bc7
Message: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.
Files: ubb-la/gui.c (3 diffs)

Change Details

ubb-la/gui.c
1010 * (at your option) any later version.
1111 */
1212
13#ifndef _GNU_SOURCE
14#define _GNU_SOURCE /* for vasprintf */
15#endif
16
1713#include <stdarg.h>
1814#include <stdint.h>
1915#include <stdlib.h>
16#include <alloca.h>
2017
2118#include "SDL.h"
2219#include "SDL_gfxPrimitives.h"
...... 
124121    va_list ap;
125122    char *tmp, *s;
126123    uint8_t *p;
127    int ix, iy;
124    int n, ix, iy;
128125
129126    va_start(ap, fmt);
130    if (vasprintf(&tmp, fmt, ap) < 0) {
131        perror("vasprintf");
132        exit(1);
133    }
127    n = vsnprintf(NULL, 0, fmt, ap);
128    va_end(ap);
129    tmp = alloca(n+1);
130    va_start(ap, fmt);
131    vsnprintf(tmp, n+1, fmt, ap);
134132    va_end(ap);
135133
136134    for (s = tmp; *s; s++) {
...... 
143141        }
144142        x += 8;
145143    }
146    free(tmp);
147144}
148145
149146

Archive Download the corresponding diff file

Branches:
master



interactive