Root/
| 1 | /* |
| 2 | * util.h - Common utility functions |
| 3 | * |
| 4 | * Written 2006, 2009, 2010 by Werner Almesberger |
| 5 | * Copyright 2006, 2009, 2010 Werner Almesberger |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #ifndef UTIL_H |
| 15 | #define UTIL_H |
| 16 | |
| 17 | #include <stdarg.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | |
| 21 | |
| 22 | #define alloc_size(s) \ |
| 23 | ({ void *alloc_size_tmp = malloc(s); \ |
| 24 | if (!alloc_size_tmp) \ |
| 25 | abort(); \ |
| 26 | alloc_size_tmp; }) |
| 27 | |
| 28 | #define alloc_type(t) ((t *) alloc_size(sizeof(t))) |
| 29 | |
| 30 | #define zalloc_size(s) \ |
| 31 | ({ void *zalloc_size_tmp = alloc_size(s); \ |
| 32 | memset(zalloc_size_tmp, 0, (s)); \ |
| 33 | zalloc_size_tmp; }) |
| 34 | |
| 35 | #define zalloc_type(t) \ |
| 36 | ({ t *zalloc_type_tmp = alloc_type(t); \ |
| 37 | memset(zalloc_type_tmp, 0, sizeof(t)); \ |
| 38 | zalloc_type_tmp; }) |
| 39 | |
| 40 | #define stralloc(s) \ |
| 41 | ({ char *stralloc_tmp = strdup(s); \ |
| 42 | if (!stralloc_tmp) \ |
| 43 | abort(); \ |
| 44 | stralloc_tmp; }) |
| 45 | |
| 46 | #define strnalloc(s, n) \ |
| 47 | ({ char *strnalloc_tmp = alloc_size((n)+1); \ |
| 48 | if (!strnalloc_tmp) \ |
| 49 | abort(); \ |
| 50 | strncpy(strnalloc_tmp, (s), (n)); \ |
| 51 | strnalloc_tmp[n] = 0; \ |
| 52 | strnalloc_tmp; }) |
| 53 | |
| 54 | #define SWAP(a, b) \ |
| 55 | ({ typeof(a) SWAP_tmp = (a); \ |
| 56 | (a) = (b); \ |
| 57 | (b) = SWAP_tmp; }) |
| 58 | |
| 59 | |
| 60 | char *stralloc_vprintf(const char *fmt, va_list ap); |
| 61 | char *stralloc_printf(const char *fmt, ...) |
| 62 | __attribute__((format(printf, 1, 2))); |
| 63 | |
| 64 | int is_id_char(char c, int first); |
| 65 | int is_id(const char *s); |
| 66 | |
| 67 | const char *unique(const char *s); |
| 68 | void unique_cleanup(void); |
| 69 | |
| 70 | #endif /* !UTIL_H */ |
| 71 |
Branches:
master
