tinc-gui: Reformat codebase according to PEP8
[tinc] / src / xalloc.h
1 /*
2    xalloc.h -- malloc and related fuctions with out of memory checking
3    Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97 Free Software Foundation, Inc.
4    Copyright (C) 2011-2013 Guus Sliepen <guus@tinc-vpn.org>
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, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License along
17    with this program; if not, write to the Free Software Foundation, Inc., Foundation,
18    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.  */
19
20 #ifndef __TINC_XALLOC_H__
21 #define __TINC_XALLOC_H__
22
23 static inline void *xmalloc(size_t n) __attribute__ ((__malloc__));
24 static inline void *xmalloc(size_t n) {
25         void *p = malloc(n);
26         if(!p)
27                 abort();
28         return p;
29 }
30
31 static inline void *xzalloc(size_t n) __attribute__ ((__malloc__));
32 static inline void *xzalloc(size_t n) {
33         void *p = calloc(1, n);
34         if(!p)
35                 abort();
36         return p;
37 }
38
39 static inline void *xrealloc(void *p, size_t n) {
40         p = realloc(p, n);
41         if(!p)
42                 abort();
43         return p;
44 }
45
46 static inline char *xstrdup(const char *s) __attribute__ ((__malloc__));
47 static inline char *xstrdup(const char *s) {
48         char *p = strdup(s);
49         if(!p)
50                 abort();
51         return p;
52 }
53
54 static inline int xvasprintf(char **strp, const char *fmt, va_list ap) {
55 #ifdef HAVE_MINGW
56         char buf[1024];
57         int result = vsnprintf(buf, sizeof buf, fmt, ap);
58         if(result < 0)
59                 abort();
60         *strp = xstrdup(buf);
61 #else
62         int result = vasprintf(strp, fmt, ap);
63         if(result < 0)
64                 abort();
65 #endif
66         return result;
67 }
68
69 static inline int xasprintf(char **strp, const char *fmt, ...) __attribute__ ((__format__(printf, 2, 3)));
70 static inline int xasprintf(char **strp, const char *fmt, ...) {
71         va_list ap;
72         va_start(ap, fmt);
73         int result = xvasprintf(strp, fmt, ap);
74         va_end(ap);
75         return result;
76 }
77
78 #endif