1
0
mirror of git://projects.qi-hardware.com/antorcha.git synced 2024-11-01 15:37:31 +02:00
antorcha/tools/libtxt/util.h

54 lines
950 B
C
Raw Normal View History

/*
* tools/libtxt/util.h - Utility functions
*
* Written 2012 by Werner Almesberger
* Copyright 2012 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#ifndef UTIL_H
#define UTIL_H
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
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)))
static const char *alloc_sprintf(const char *fmt, ...)
{
va_list ap;
char *tmp, *res;
int n;
va_start(ap, fmt);
n = vasprintf(&tmp, fmt, ap);
va_end(ap);
if (n < 0)
abort();
res = malloc(n+1);
if (!res)
abort();
memcpy(res, tmp, n+1);
return res;
}
#endif /* !UTIL_H */