1
0
mirror of git://projects.qi-hardware.com/antorcha.git synced 2024-11-25 20:21:32 +02:00

tools/libtxt/: introduce utility functions alloc_size and alloc_type

This commit is contained in:
Werner Almesberger 2012-07-01 20:43:04 -03:00
parent d33449e128
commit 21e4fb267e
3 changed files with 24 additions and 13 deletions

View File

@ -16,6 +16,7 @@
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include "util.h"
#include "libtxt.h" #include "libtxt.h"
@ -116,9 +117,7 @@ static char *alloc_string_n(const char *s, size_t len)
{ {
char *t; char *t;
t = malloc(len+1); t = alloc_size(len+1);
if (!t)
abort();
memcpy(t, s, len); memcpy(t, s, len);
t[len] = 0; t[len] = 0;
return t; return t;
@ -129,9 +128,7 @@ static void add_string(struct edit ***last, const char *start, size_t len)
{ {
struct edit *e; struct edit *e;
e = malloc(sizeof(struct edit)); e = alloc_type(struct edit);
if (!e)
abort();
e->type = edit_string; e->type = edit_string;
e->u.s = alloc_string_n(start, len); e->u.s = alloc_string_n(start, len);
**last = e; **last = e;
@ -187,9 +184,7 @@ struct edit *text2edit(const char *s)
if (*s == '\n' && !have_text) if (*s == '\n' && !have_text)
continue; continue;
e = malloc(sizeof(struct edit)); e = alloc_type(struct edit);
if (!e)
abort();
e->type = edit_nl; /* pick something without data */ e->type = edit_nl; /* pick something without data */
e->next = NULL; e->next = NULL;
*last = e; *last = e;

View File

@ -112,9 +112,7 @@ struct image *load_image(const char *name, const char **error)
return NULL; return NULL;
} }
img = malloc(sizeof(struct image)); img = alloc_type(struct image);
if (!img)
abort();
err = read_xbm_file(file, img); err = read_xbm_file(file, img);
if (err) { if (err) {
if (error) if (error)

View File

@ -19,7 +19,25 @@
#include <string.h> #include <string.h>
static const char *alloc_sprintf(const char *fmt, ...) static inline void *alloc_size(size_t size)
{
void *tmp = malloc(size);
if (!tmp)
abort();
return tmp;
}
#define alloc_type(t) ((t *) alloc_size(sizeof(t)))
/*
* @@@ __attribute__((used)) is an ugly wait to get rid of the "unused
* function" warning. (The "unused" attribute doesn't do the trick.)
*/
static const char * __attribute__((used)) alloc_sprintf(const char *fmt, ...)
{ {
va_list ap; va_list ap;
char *tmp, *res; char *tmp, *res;