Remove code duplication when checking ADD_EDGE/DEL_EDGE messages.
[tinc] / lib / xmalloc.c
1 /* xmalloc.c -- malloc with out of memory checking
2    Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License along
15    with this program; if not, write to the Free Software Foundation, Inc., Foundation,
16    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.  */
17
18 #if HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <sys/types.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdarg.h>
26 #include <errno.h>
27
28 #if STDC_HEADERS
29 # include <stdlib.h>
30 #else
31 void *calloc ();
32 void *malloc ();
33 void *realloc ();
34 void free ();
35 #endif
36
37 #include "xalloc.h"
38
39 #ifndef EXIT_FAILURE
40 # define EXIT_FAILURE 1
41 #endif
42
43 /* Prototypes for functions defined here.  */
44 #if defined (__STDC__) && __STDC__
45 void *xmalloc (size_t n);
46 void *xcalloc (size_t n, size_t s);
47 void *xrealloc (void *p, size_t n);
48 #endif
49
50 /* Exit value when the requested amount of memory is not available.
51    The caller may set it to some other value.  */
52 int xalloc_exit_failure = EXIT_FAILURE;
53
54 /* FIXME: describe */
55 char *const xalloc_msg_memory_exhausted = "Memory exhausted";
56
57 /* FIXME: describe */
58 void (*xalloc_fail_func) (int) = 0;
59
60 static void
61 xalloc_fail (int size)
62 {
63   if (xalloc_fail_func)
64     (*xalloc_fail_func) (size);
65   fprintf(stderr, "%s\n", xalloc_msg_memory_exhausted);
66   exit(xalloc_exit_failure);
67 }
68
69 /* Allocate N bytes of memory dynamically, with error checking.  */
70
71 void *
72 xmalloc (n)
73      size_t n;
74 {
75   void *p;
76
77   p = malloc (n);
78   if (p == 0)
79     xalloc_fail ((int)n);
80   return p;
81 }
82
83 /* Allocate N bytes of memory dynamically, and set it all to zero. */
84
85 void *
86 xmalloc_and_zero (n)
87      size_t n;
88 {
89   void *p;
90
91   p = malloc (n);
92   if (p == 0)
93     xalloc_fail ((int)n);
94   memset (p, '\0', n);
95   return p;
96 }
97
98 /* Change the size of an allocated block of memory P to N bytes,
99    with error checking.
100    If P is NULL, run xmalloc.  */
101
102 void *
103 xrealloc (p, n)
104      void *p;
105      size_t n;
106 {
107   p = realloc (p, n);
108   if (p == 0)
109     xalloc_fail (n);
110   return p;
111 }
112
113 /* Duplicate a string */
114
115 char *xstrdup(const char *s)
116 {
117   char *p;
118   
119   p = strdup(s);
120   if(!p)
121     xalloc_fail ((int)strlen(s));
122   return p;
123 }
124
125 #ifdef NOT_USED
126
127 /* Allocate memory for N elements of S bytes, with error checking.  */
128
129 void *
130 xcalloc (n, s)
131      size_t n, s;
132 {
133   void *p;
134
135   p = calloc (n, s);
136   if (p == 0)
137     xalloc_fail ();
138   return p;
139 }
140
141 #endif /* NOT_USED */
142
143 int xasprintf(char **strp, const char *fmt, ...) {
144         int result;
145         va_list ap;
146         va_start(ap, fmt);
147         result = xvasprintf(strp, fmt, ap);
148         va_end(ap);
149         return result;
150 }
151
152 int xvasprintf(char **strp, const char *fmt, va_list ap) {
153         int result = vasprintf(strp, fmt, ap);
154         if(result < 0) {
155                 fprintf(stderr, "vasprintf() failed: %s\n", strerror(errno));
156                 exit(xalloc_exit_failure);
157         }
158         return result;
159 }