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