1
0
mirror of git://projects.qi-hardware.com/antorcha.git synced 2024-11-22 07:24:06 +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 <sys/types.h>
#include "util.h"
#include "libtxt.h"
@ -116,9 +117,7 @@ static char *alloc_string_n(const char *s, size_t len)
{
char *t;
t = malloc(len+1);
if (!t)
abort();
t = alloc_size(len+1);
memcpy(t, s, len);
t[len] = 0;
return t;
@ -129,9 +128,7 @@ static void add_string(struct edit ***last, const char *start, size_t len)
{
struct edit *e;
e = malloc(sizeof(struct edit));
if (!e)
abort();
e = alloc_type(struct edit);
e->type = edit_string;
e->u.s = alloc_string_n(start, len);
**last = e;
@ -187,9 +184,7 @@ struct edit *text2edit(const char *s)
if (*s == '\n' && !have_text)
continue;
e = malloc(sizeof(struct edit));
if (!e)
abort();
e = alloc_type(struct edit);
e->type = edit_nl; /* pick something without data */
e->next = NULL;
*last = e;

View File

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

View File

@ -19,7 +19,25 @@
#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;
char *tmp, *res;