Document the behavior of "-n."
[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 "dropin.h"
38 #include "xalloc.h"
39
40 #ifndef EXIT_FAILURE
41 # define EXIT_FAILURE 1
42 #endif
43
44 /* Prototypes for functions defined here.  */
45 #if defined (__STDC__) && __STDC__
46 void *xmalloc (size_t n);
47 void *xcalloc (size_t n, size_t s);
48 void *xrealloc (void *p, size_t n);
49 #endif
50
51 /* Exit value when the requested amount of memory is not available.
52    The caller may set it to some other value.  */
53 int xalloc_exit_failure = EXIT_FAILURE;
54
55 /* FIXME: describe */
56 char *const xalloc_msg_memory_exhausted = "Memory exhausted";
57
58 /* FIXME: describe */
59 void (*xalloc_fail_func) (int) = 0;
60
61 static void
62 xalloc_fail (int size)
63 {
64   if (xalloc_fail_func)
65     (*xalloc_fail_func) (size);
66   fprintf(stderr, "%s\n", xalloc_msg_memory_exhausted);
67   exit(xalloc_exit_failure);
68 }
69
70 /* Allocate N bytes of memory dynamically, with error checking.  */
71
72 void *
73 xmalloc (n)
74      size_t n;
75 {
76   void *p;
77
78   p = malloc (n);
79   if (p == 0)
80     xalloc_fail ((int)n);
81   return p;
82 }
83
84 /* Allocate N bytes of memory dynamically, and set it all to zero. */
85
86 void *
87 xmalloc_and_zero (n)
88      size_t n;
89 {
90   void *p;
91
92   p = malloc (n);
93   if (p == 0)
94     xalloc_fail ((int)n);
95   memset (p, '\0', n);
96   return p;
97 }
98
99 /* Change the size of an allocated block of memory P to N bytes,
100    with error checking.
101    If P is NULL, run xmalloc.  */
102
103 void *
104 xrealloc (p, n)
105      void *p;
106      size_t n;
107 {
108   p = realloc (p, n);
109   if (p == 0)
110     xalloc_fail (n);
111   return p;
112 }
113
114 /* Duplicate a string */
115
116 char *xstrdup(const char *s)
117 {
118   char *p;
119   
120   p = strdup(s);
121   if(!p)
122     xalloc_fail ((int)strlen(s));
123   return p;
124 }
125
126 #ifdef NOT_USED
127
128 /* Allocate memory for N elements of S bytes, with error checking.  */
129
130 void *
131 xcalloc (n, s)
132      size_t n, s;
133 {
134   void *p;
135
136   p = calloc (n, s);
137   if (p == 0)
138     xalloc_fail ();
139   return p;
140 }
141
142 #endif /* NOT_USED */
143
144 int xasprintf(char **strp, const char *fmt, ...) {
145         int result;
146         va_list ap;
147         va_start(ap, fmt);
148         result = xvasprintf(strp, fmt, ap);
149         va_end(ap);
150         return result;
151 }
152
153 int xvasprintf(char **strp, const char *fmt, va_list ap) {
154         int result = vasprintf(strp, fmt, ap);
155         if(result < 0) {
156                 fprintf(stderr, "vasprintf() failed: %s\n", strerror(errno));
157                 exit(xalloc_exit_failure);
158         }
159         return result;
160 }