2012-03-18 18:24:12 +02:00
|
|
|
/*
|
|
|
|
* util.h - Utility functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 by 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 <stdlib.h>
|
2012-05-20 17:18:18 +03:00
|
|
|
#include <string.h>
|
2012-03-18 18:24:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
#define alloc_size(s) \
|
|
|
|
({ void *alloc_size_tmp = malloc(s); \
|
|
|
|
if (!alloc_size_tmp) \
|
|
|
|
abort(); \
|
|
|
|
alloc_size_tmp; })
|
|
|
|
|
|
|
|
#define alloc_type(t) ((t *) alloc_size(sizeof(t)))
|
|
|
|
|
|
|
|
|
2012-05-20 17:18:18 +03:00
|
|
|
static inline char *stralloc(const char *s)
|
|
|
|
{
|
|
|
|
char *t;
|
|
|
|
|
|
|
|
t = strdup(s);
|
|
|
|
if (!t)
|
|
|
|
abort();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline char *stralloc_n(const char *s, int n)
|
|
|
|
{
|
|
|
|
char *t;
|
|
|
|
|
|
|
|
t = alloc_size(n+1);
|
|
|
|
if (!t)
|
|
|
|
abort();
|
|
|
|
memcpy(t, s, n);
|
|
|
|
t[n] = 0;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-18 18:24:12 +02:00
|
|
|
const char *unique(const char *s);
|
2012-05-23 22:44:44 +03:00
|
|
|
const char *unique_n(const char *s, int n);
|
2012-03-18 18:24:12 +02:00
|
|
|
|
|
|
|
#endif /* !UTIL_H */
|