Root/tools/libant/util.h

Source at commit 0e9b093d2ee3f8195e9848f5a931c2068d37e59f created 11 years 1 day ago.
By Werner Almesberger, tornado/cpu/cpu.brd: improve 3V3 routing
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
22static 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
35static 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

Archive Download this file

Branches:
master
tornado-v1



interactive