]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - libiberty/xmalloc.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / libiberty / xmalloc.c
CommitLineData
252b5132 1/* memory allocation routines with error checking.
d87bef3a 2 Copyright (C) 1989-2023 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"
35a88fa5 68#include "environ.h"
252b5132
RH
69
70#include <stdio.h>
71
252b5132 72#include <stddef.h>
252b5132
RH
73
74#if VMS
75#include <stdlib.h>
76#include <unixlib.h>
77#else
78/* For systems with larger pointers than ints, these must be declared. */
abf6a75b
DD
79# if HAVE_STDLIB_H && HAVE_UNISTD_H && HAVE_DECL_MALLOC \
80 && HAVE_DECL_REALLOC && HAVE_DECL_CALLOC && HAVE_DECL_SBRK
81# include <stdlib.h>
82# include <unistd.h>
83# else
84# ifdef __cplusplus
85extern "C" {
86# endif /* __cplusplus */
87void *malloc (size_t);
88void *realloc (void *, size_t);
89void *calloc (size_t, size_t);
ce2d3708 90#ifdef HAVE_SBRK
abf6a75b 91void *sbrk (ptrdiff_t);
ce2d3708 92#endif
abf6a75b
DD
93# ifdef __cplusplus
94}
95# endif /* __cplusplus */
96# endif /* HAVE_STDLIB_H ... */
97#endif /* VMS */
252b5132
RH
98
99/* The program name if set. */
100static const char *name = "";
101
c1687039 102#ifdef HAVE_SBRK
252b5132
RH
103/* The initial sbrk, set when the program name is set. Not used for win32
104 ports other than cygwin32. */
105static char *first_break = NULL;
c1687039 106#endif /* HAVE_SBRK */
252b5132
RH
107
108void
49b1fae4 109xmalloc_set_program_name (const char *s)
252b5132
RH
110{
111 name = s;
c1687039
ILT
112#ifdef HAVE_SBRK
113 /* Win32 ports other than cygwin32 don't have brk() */
252b5132
RH
114 if (first_break == NULL)
115 first_break = (char *) sbrk (0);
c1687039 116#endif /* HAVE_SBRK */
252b5132
RH
117}
118
2ea7befd 119void
49b1fae4 120xmalloc_failed (size_t size)
2ea7befd
DD
121{
122#ifdef HAVE_SBRK
2ea7befd
DD
123 size_t allocated;
124
125 if (first_break != NULL)
126 allocated = (char *) sbrk (0) - first_break;
127 else
128 allocated = (char *) sbrk (0) - (char *) &environ;
129 fprintf (stderr,
50d4562d 130 "\n%s%sout of memory allocating %lu bytes after a total of %lu bytes\n",
2ea7befd
DD
131 name, *name ? ": " : "",
132 (unsigned long) size, (unsigned long) allocated);
133#else /* HAVE_SBRK */
134 fprintf (stderr,
6e9980f5 135 "\n%s%sout of memory allocating %lu bytes\n",
2ea7befd
DD
136 name, *name ? ": " : "",
137 (unsigned long) size);
138#endif /* HAVE_SBRK */
139 xexit (1);
140}
141
31b15688 142void *
49b1fae4 143xmalloc (size_t size)
252b5132 144{
31b15688 145 void *newmem;
252b5132
RH
146
147 if (size == 0)
148 size = 1;
149 newmem = malloc (size);
150 if (!newmem)
2ea7befd
DD
151 xmalloc_failed (size);
152
252b5132
RH
153 return (newmem);
154}
155
31b15688 156void *
49b1fae4 157xcalloc (size_t nelem, size_t elsize)
252b5132 158{
31b15688 159 void *newmem;
252b5132
RH
160
161 if (nelem == 0 || elsize == 0)
162 nelem = elsize = 1;
163
164 newmem = calloc (nelem, elsize);
165 if (!newmem)
2ea7befd
DD
166 xmalloc_failed (nelem * elsize);
167
252b5132
RH
168 return (newmem);
169}
170
31b15688
AM
171void *
172xrealloc (void *oldmem, size_t size)
252b5132 173{
31b15688 174 void *newmem;
252b5132
RH
175
176 if (size == 0)
177 size = 1;
178 if (!oldmem)
179 newmem = malloc (size);
180 else
181 newmem = realloc (oldmem, size);
182 if (!newmem)
2ea7befd
DD
183 xmalloc_failed (size);
184
252b5132
RH
185 return (newmem);
186}