Update support for Solaris.
[tinc] / src / 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) = NULL;
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 (size_t n)
74 {
75   void *p;
76
77   p = malloc (n);
78   if (p == NULL)
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 (size_t n)
87 {
88   void *p;
89
90   p = malloc (n);
91   if (p == NULL)
92     xalloc_fail ((int)n);
93   memset (p, '\0', n);
94   return p;
95 }
96
97 /* Change the size of an allocated block of memory P to N bytes,
98    with error checking.
99    If P is NULL, run xmalloc.  */
100
101 void *
102 xrealloc (void *p, size_t n)
103 {
104   p = realloc (p, n);
105   if (p == NULL)
106     xalloc_fail (n);
107   return p;
108 }
109
110 /* Duplicate a string */
111
112 char *xstrdup(const char *s)
113 {
114   char *p;
115   
116   p = strdup(s);
117   if(!p)
118     xalloc_fail ((int)strlen(s));
119   return p;
120 }
121
122 #ifdef NOT_USED
123
124 /* Allocate memory for N elements of S bytes, with error checking.  */
125
126 void *
127 xcalloc (n, s)
128      size_t n, s;
129 {
130   void *p;
131
132   p = calloc (n, s);
133   if (p == NULL)
134     xalloc_fail ();
135   return p;
136 }
137
138 #endif /* NOT_USED */
139
140 int xasprintf(char **strp, const char *fmt, ...) {
141         int result;
142         va_list ap;
143         va_start(ap, fmt);
144         result = xvasprintf(strp, fmt, ap);
145         va_end(ap);
146         return result;
147 }
148
149 int xvasprintf(char **strp, const char *fmt, va_list ap) {
150 #ifdef HAVE_MINGW
151         char buf[1024];
152         int result = vsnprintf(buf, sizeof buf, fmt, ap);
153         if(result < 0)
154                 exit(xalloc_exit_failure);
155         *strp = xstrdup(buf);
156 #else
157         int result = vasprintf(strp, fmt, ap);
158         if(result < 0) {
159                 fprintf(stderr, "vasprintf() failed: %s\n", strerror(errno));
160                 exit(xalloc_exit_failure);
161         }
162 #endif
163         return result;
164 }