OSDN Git Service

Update the address and phone number of the FSF organization.
[pf3gnuchains/gcc-fork.git] / libiberty / xmalloc.c
1 /* memory allocation routines with error checking.
2    Copyright 1989, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
3    
4 This file is part of the libiberty library.
5 Libiberty is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 Libiberty is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with libiberty; see the file COPYING.LIB.  If
17 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18 Boston, MA 02110-1301, USA.  */
19
20 /*
21
22 @deftypefn Replacement void* xmalloc (size_t)
23
24 Allocate memory without fail.  If @code{malloc} fails, this will print
25 a message to @code{stderr} (using the name set by
26 @code{xmalloc_set_program_name},
27 if any) and then call @code{xexit}.  Note that it is therefore safe for
28 a program to contain @code{#define malloc xmalloc} in its source.
29
30 @end deftypefn
31
32 @deftypefn Replacement void* xrealloc (void *@var{ptr}, size_t @var{size})
33 Reallocate memory without fail.  This routine functions like @code{realloc},
34 but will behave the same as @code{xmalloc} if memory cannot be found.
35
36 @end deftypefn
37
38 @deftypefn Replacement void* xcalloc (size_t @var{nelem}, size_t @var{elsize})
39
40 Allocate memory without fail, and set it to zero.  This routine functions
41 like @code{calloc}, but will behave the same as @code{xmalloc} if memory
42 cannot be found.
43
44 @end deftypefn
45
46 @deftypefn Replacement void xmalloc_set_program_name (const char *@var{name})
47
48 You can use this to set the name of the program used by
49 @code{xmalloc_failed} when printing a failure message.
50
51 @end deftypefn
52
53 @deftypefn Replacement void xmalloc_failed (size_t)
54
55 This function is not meant to be called by client code, and is listed
56 here for completeness only.  If any of the allocation routines fail, this
57 function will be called to print an error message and terminate execution.
58
59 @end deftypefn
60
61 */
62
63 #ifdef HAVE_CONFIG_H
64 #include "config.h"
65 #endif
66 #include "ansidecl.h"
67 #include "libiberty.h"
68
69 #include <stdio.h>
70
71 #include <stddef.h>
72
73 #if VMS
74 #include <stdlib.h>
75 #include <unixlib.h>
76 #else
77 /* For systems with larger pointers than ints, these must be declared.  */
78 PTR malloc (size_t);
79 PTR realloc (PTR, size_t);
80 PTR calloc (size_t, size_t);
81 PTR sbrk (ptrdiff_t);
82 #endif
83
84 /* The program name if set.  */
85 static const char *name = "";
86
87 #ifdef HAVE_SBRK
88 /* The initial sbrk, set when the program name is set. Not used for win32
89    ports other than cygwin32.  */
90 static char *first_break = NULL;
91 #endif /* HAVE_SBRK */
92
93 void
94 xmalloc_set_program_name (const char *s)
95 {
96   name = s;
97 #ifdef HAVE_SBRK
98   /* Win32 ports other than cygwin32 don't have brk() */
99   if (first_break == NULL)
100     first_break = (char *) sbrk (0);
101 #endif /* HAVE_SBRK */
102 }
103
104 void
105 xmalloc_failed (size_t size)
106 {
107 #ifdef HAVE_SBRK
108   extern char **environ;
109   size_t allocated;
110
111   if (first_break != NULL)
112     allocated = (char *) sbrk (0) - first_break;
113   else
114     allocated = (char *) sbrk (0) - (char *) &environ;
115   fprintf (stderr,
116            "\n%s%sout of memory allocating %lu bytes after a total of %lu bytes\n",
117            name, *name ? ": " : "",
118            (unsigned long) size, (unsigned long) allocated);
119 #else /* HAVE_SBRK */
120   fprintf (stderr,
121            "\n%s%sout of memory allocating %lu bytes\n",
122            name, *name ? ": " : "",
123            (unsigned long) size);
124 #endif /* HAVE_SBRK */
125   xexit (1);
126 }  
127
128 PTR
129 xmalloc (size_t size)
130 {
131   PTR newmem;
132
133   if (size == 0)
134     size = 1;
135   newmem = malloc (size);
136   if (!newmem)
137     xmalloc_failed (size);
138
139   return (newmem);
140 }
141
142 PTR
143 xcalloc (size_t nelem, size_t elsize)
144 {
145   PTR newmem;
146
147   if (nelem == 0 || elsize == 0)
148     nelem = elsize = 1;
149
150   newmem = calloc (nelem, elsize);
151   if (!newmem)
152     xmalloc_failed (nelem * elsize);
153
154   return (newmem);
155 }
156
157 PTR
158 xrealloc (PTR oldmem, size_t size)
159 {
160   PTR newmem;
161
162   if (size == 0)
163     size = 1;
164   if (!oldmem)
165     newmem = malloc (size);
166   else
167     newmem = realloc (oldmem, size);
168   if (!newmem)
169     xmalloc_failed (size);
170
171   return (newmem);
172 }