src/linux/device.c: Fix segfault when running without `--net'.
[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
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
26 #if STDC_HEADERS
27 # include <stdlib.h>
28 #else
29 void *calloc ();
30 void *malloc ();
31 void *realloc ();
32 void free ();
33 #endif
34
35 #include "gettext.h"
36 #include "xalloc.h"
37
38 #ifndef EXIT_FAILURE
39 # define EXIT_FAILURE 1
40 #endif
41
42 /* Prototypes for functions defined here.  */
43 #if defined (__STDC__) && __STDC__
44 void *xmalloc (size_t n);
45 void *xcalloc (size_t n, size_t s);
46 void *xrealloc (void *p, size_t n);
47 #endif
48
49 /* Exit value when the requested amount of memory is not available.
50    The caller may set it to some other value.  */
51 int xalloc_exit_failure = EXIT_FAILURE;
52
53 /* FIXME: describe */
54 char *const xalloc_msg_memory_exhausted = N_("Memory exhausted");
55
56 /* FIXME: describe */
57 void (*xalloc_fail_func) (int) = 0;
58
59 static void
60 xalloc_fail (int size)
61 {
62   if (xalloc_fail_func)
63     (*xalloc_fail_func) (size);
64   fprintf(stderr, "%s\n", xalloc_msg_memory_exhausted);
65   exit(xalloc_exit_failure);
66 }
67
68 /* Allocate N bytes of memory dynamically, with error checking.  */
69
70 void *
71 xmalloc (n)
72      size_t n;
73 {
74   void *p;
75
76   p = malloc (n);
77   if (p == 0)
78     xalloc_fail ((int)n);
79   return p;
80 }
81
82 /* Allocate N bytes of memory dynamically, and set it all to zero. */
83
84 void *
85 xmalloc_and_zero (n)
86      size_t n;
87 {
88   void *p;
89
90   p = malloc (n);
91   if (p == 0)
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 (p, n)
103      void *p;
104      size_t n;
105 {
106   p = realloc (p, n);
107   if (p == 0)
108     xalloc_fail (n);
109   return p;
110 }
111
112 /* Duplicate a string */
113
114 char *xstrdup(const char *s)
115 {
116   char *p;
117   
118   p = strdup(s);
119   if(!p)
120     xalloc_fail ((int)strlen(s));
121   return p;
122 }
123
124 #ifdef NOT_USED
125
126 /* Allocate memory for N elements of S bytes, with error checking.  */
127
128 void *
129 xcalloc (n, s)
130      size_t n, s;
131 {
132   void *p;
133
134   p = calloc (n, s);
135   if (p == 0)
136     xalloc_fail ();
137   return p;
138 }
139
140 #endif /* NOT_USED */