Root/
| 1 | /* |
| 2 | * util.h - Utility functions |
| 3 | * |
| 4 | * Copyright 2012 by Werner Almesberger |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #ifndef UTIL_H |
| 13 | #define UTIL_H |
| 14 | |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | |
| 18 | |
| 19 | #define alloc_size(s) \ |
| 20 | ({ void *alloc_size_tmp = malloc(s); \ |
| 21 | if (!alloc_size_tmp) \ |
| 22 | abort(); \ |
| 23 | alloc_size_tmp; }) |
| 24 | |
| 25 | #define alloc_type(t) ((t *) alloc_size(sizeof(t))) |
| 26 | |
| 27 | |
| 28 | static inline char *stralloc(const char *s) |
| 29 | { |
| 30 | char *t; |
| 31 | |
| 32 | t = strdup(s); |
| 33 | if (!t) |
| 34 | abort(); |
| 35 | return t; |
| 36 | } |
| 37 | |
| 38 | |
| 39 | static inline char *stralloc_n(const char *s, int n) |
| 40 | { |
| 41 | char *t; |
| 42 | |
| 43 | t = alloc_size(n+1); |
| 44 | if (!t) |
| 45 | abort(); |
| 46 | memcpy(t, s, n); |
| 47 | t[n] = 0; |
| 48 | return t; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | const char *unique(const char *s); |
| 53 | const char *unique_n(const char *s, int n); |
| 54 | |
| 55 | #endif /* !UTIL_H */ |
| 56 |
Branches:
master
