]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - libiberty/xmalloc.c
* s390-tdep.c (s390_frame_prev_register): Change type of last
[thirdparty/binutils-gdb.git] / libiberty / xmalloc.c
CommitLineData
252b5132 1/* memory allocation routines with error checking.
c1687039 2 Copyright 1989, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
252b5132
RH
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
979c05d3
NC
17not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18Boston, MA 02110-1301, USA. */
252b5132 19
39423523
DD
20/*
21
22@deftypefn Replacement void* xmalloc (size_t)
23
24Allocate memory without fail. If @code{malloc} fails, this will print
fa9f0e33
DD
25a message to @code{stderr} (using the name set by
26@code{xmalloc_set_program_name},
39423523
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
99b58139 32@deftypefn Replacement void* xrealloc (void *@var{ptr}, size_t @var{size})
39423523
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
99b58139 38@deftypefn Replacement void* xcalloc (size_t @var{nelem}, size_t @var{elsize})
39423523
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
eb383413
L
63#ifdef HAVE_CONFIG_H
64#include "config.h"
65#endif
252b5132
RH
66#include "ansidecl.h"
67#include "libiberty.h"
68
69#include <stdio.h>
70
252b5132 71#include <stddef.h>
252b5132
RH
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. */
49b1fae4
DD
78PTR malloc (size_t);
79PTR realloc (PTR, size_t);
80PTR calloc (size_t, size_t);
81PTR sbrk (ptrdiff_t);
252b5132
RH
82#endif
83
84/* The program name if set. */
85static const char *name = "";
86
c1687039 87#ifdef HAVE_SBRK
252b5132
RH
88/* The initial sbrk, set when the program name is set. Not used for win32
89 ports other than cygwin32. */
90static char *first_break = NULL;
c1687039 91#endif /* HAVE_SBRK */
252b5132
RH
92
93void
49b1fae4 94xmalloc_set_program_name (const char *s)
252b5132
RH
95{
96 name = s;
c1687039
ILT
97#ifdef HAVE_SBRK
98 /* Win32 ports other than cygwin32 don't have brk() */
252b5132
RH
99 if (first_break == NULL)
100 first_break = (char *) sbrk (0);
c1687039 101#endif /* HAVE_SBRK */
252b5132
RH
102}
103
2ea7befd 104void
49b1fae4 105xmalloc_failed (size_t size)
2ea7befd
DD
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,
50d4562d 116 "\n%s%sout of memory allocating %lu bytes after a total of %lu bytes\n",
2ea7befd
DD
117 name, *name ? ": " : "",
118 (unsigned long) size, (unsigned long) allocated);
119#else /* HAVE_SBRK */
120 fprintf (stderr,
6e9980f5 121 "\n%s%sout of memory allocating %lu bytes\n",
2ea7befd
DD
122 name, *name ? ": " : "",
123 (unsigned long) size);
124#endif /* HAVE_SBRK */
125 xexit (1);
126}
127
252b5132 128PTR
49b1fae4 129xmalloc (size_t size)
252b5132
RH
130{
131 PTR newmem;
132
133 if (size == 0)
134 size = 1;
135 newmem = malloc (size);
136 if (!newmem)
2ea7befd
DD
137 xmalloc_failed (size);
138
252b5132
RH
139 return (newmem);
140}
141
142PTR
49b1fae4 143xcalloc (size_t nelem, size_t elsize)
252b5132
RH
144{
145 PTR newmem;
146
147 if (nelem == 0 || elsize == 0)
148 nelem = elsize = 1;
149
150 newmem = calloc (nelem, elsize);
151 if (!newmem)
2ea7befd
DD
152 xmalloc_failed (nelem * elsize);
153
252b5132
RH
154 return (newmem);
155}
156
157PTR
49b1fae4 158xrealloc (PTR oldmem, size_t size)
252b5132
RH
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)
2ea7befd
DD
169 xmalloc_failed (size);
170
252b5132
RH
171 return (newmem);
172}