2016-08-02 17:00:08 +03:00
|
|
|
/*
|
2016-08-18 03:37:15 +03:00
|
|
|
* misc/util.h - Common utility functions
|
2016-08-02 17:00:08 +03:00
|
|
|
*
|
|
|
|
* Written 2016 by Werner Almesberger
|
|
|
|
* Copyright 2016 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-08-18 03:37:15 +03:00
|
|
|
#ifndef MISC_UTIL_H
|
|
|
|
#define MISC_UTIL_H
|
2016-08-02 17:00:08 +03:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define alloc_size(s) \
|
|
|
|
({ void *alloc_size_tmp = malloc(s); \
|
|
|
|
if (!alloc_size_tmp) { \
|
|
|
|
perror("malloc"); \
|
|
|
|
exit(1); \
|
|
|
|
} \
|
|
|
|
alloc_size_tmp; })
|
|
|
|
|
|
|
|
#define alloc_type(t) ((t *) alloc_size(sizeof(t)))
|
2016-08-11 02:28:31 +03:00
|
|
|
#define alloc_type_n(t, n) ((t *) alloc_size(sizeof(t) * (n)))
|
2016-08-02 17:00:08 +03:00
|
|
|
|
|
|
|
#define stralloc(s) \
|
|
|
|
({ char *stralloc_tmp = strdup(s); \
|
|
|
|
if (!stralloc_tmp) { \
|
|
|
|
perror("strdup"); \
|
|
|
|
exit(1); \
|
|
|
|
} \
|
|
|
|
stralloc_tmp; })
|
|
|
|
|
|
|
|
|
|
|
|
#define ARRAY_ELEMENTS(a) (sizeof(a) / sizeof(a[0]))
|
|
|
|
#define ARRAY_END(a) ((a) + ARRAY_ELEMENTS(a))
|
|
|
|
|
|
|
|
|
|
|
|
#define swap(a, b) \
|
|
|
|
({ typeof(a) _tmp = (a); a = (b); b = _tmp; })
|
|
|
|
|
2016-08-18 23:12:42 +03:00
|
|
|
#define sign1(x) ((x) < 0 ? -1 : 1)
|
2016-08-02 17:00:08 +03:00
|
|
|
|
|
|
|
#define unsupported(s) \
|
|
|
|
fprintf(stderr, __FILE__ ":%d: unsupported: " s "\n", __LINE__)
|
|
|
|
|
2016-08-18 03:37:15 +03:00
|
|
|
#endif /* !MISC_UTIL_H */
|