Root/
| Source at commit 30c683effd7ab362d1517950700c96cfa09e45e8 created 11 years 17 days ago. By Werner Almesberger, tornado/fw/sim/p: pass arguments to "alg"; label graphs; better separate "up" | |
|---|---|
| 1 | /* |
| 2 | * tools/libant/util.h - Utility functions |
| 3 | * |
| 4 | * Written 2012 by Werner Almesberger |
| 5 | * Copyright 2012 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 | #ifndef UTIL_H |
| 14 | #define UTIL_H |
| 15 | |
| 16 | #include <stdarg.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 | |
| 21 | |
| 22 | static inline void *alloc_size(size_t size) |
| 23 | { |
| 24 | void *tmp = malloc(size); |
| 25 | |
| 26 | if (!tmp) |
| 27 | abort(); |
| 28 | return tmp; |
| 29 | } |
| 30 | |
| 31 | |
| 32 | #define alloc_type(t) ((t *) alloc_size(sizeof(t))) |
| 33 | |
| 34 | |
| 35 | static const char *alloc_sprintf(const char *fmt, ...) |
| 36 | { |
| 37 | va_list ap; |
| 38 | char *tmp, *res; |
| 39 | int n; |
| 40 | |
| 41 | va_start(ap, fmt); |
| 42 | n = vasprintf(&tmp, fmt, ap); |
| 43 | va_end(ap); |
| 44 | if (n < 0) |
| 45 | abort(); |
| 46 | res = malloc(n+1); |
| 47 | if (!res) |
| 48 | abort(); |
| 49 | memcpy(res, tmp, n+1); |
| 50 | return res; |
| 51 | } |
| 52 | |
| 53 | #endif /* !UTIL_H */ |
| 54 | |
Branches:
master
tornado-v1
