1
0
mirror of git://projects.qi-hardware.com/antorcha.git synced 2024-11-22 18:56:15 +02:00
antorcha/tools/libant/util.h
2012-11-01 18:13:03 -03:00

54 lines
950 B
C

/*
* tools/libant/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 */