397f7b0e19bfce88f8034024547278bb721b32ad
[oweals/tinc.git] / 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) {
24         void *p = malloc(n);
25         if(!p)
26                 abort();
27         return p;
28 }
29
30 static inline void *xzalloc(size_t n) {
31         void *p = calloc(1, n);
32         if(!p)
33                 abort();
34         return p;
35 }
36
37 static inline void *xrealloc(void *p, size_t n) {
38         p = realloc(p, n);
39         if(!p)
40                 abort();
41         return p;
42 }
43
44 static inline char *xstrdup(const char *s) {
45         char *p = strdup(s);
46         if(!p)
47                 abort();
48         return p;
49 }
50
51 static inline int xvasprintf(char **strp, const char *fmt, va_list ap) {
52         int result = vasprintf(strp, fmt, ap);
53         if(result < 0)
54                 abort();
55         return result;
56 }
57
58 static inline int xasprintf(char **strp, const char *fmt, ...) {
59         va_list ap;
60         va_start(ap, fmt);
61         int result = xvasprintf(strp, fmt, ap);
62         va_end(ap);
63         return result;
64 }
65
66 #endif