]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/xmalloc.c
Update copyright years.
[thirdparty/gcc.git] / libiberty / xmalloc.c
CommitLineData
6599da04 1/* memory allocation routines with error checking.
85ec4feb 2 Copyright (C) 1989-2018 Free Software Foundation, Inc.
6599da04
JM
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
ee58dffd
NC
17not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18Boston, MA 02110-1301, USA. */
6599da04 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"
efd40fc4 68#include "environ.h"
6599da04
JM
69
70#include <stdio.h>
71
6599da04 72#include <stddef.h>
6599da04
JM
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. */
d7cf8390
GDR
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);
90void *sbrk (ptrdiff_t);
91# ifdef __cplusplus
92}
93# endif /* __cplusplus */
94# endif /* HAVE_STDLIB_H ... */
95#endif /* VMS */
6599da04
JM
96
97/* The program name if set. */
98static const char *name = "";
99
acbbd80a 100#ifdef HAVE_SBRK
77aff459
MK
101/* The initial sbrk, set when the program name is set. Not used for win32
102 ports other than cygwin32. */
6599da04 103static char *first_break = NULL;
acbbd80a 104#endif /* HAVE_SBRK */
6599da04
JM
105
106void
7a17ef5e 107xmalloc_set_program_name (const char *s)
6599da04
JM
108{
109 name = s;
acbbd80a 110#ifdef HAVE_SBRK
77aff459 111 /* Win32 ports other than cygwin32 don't have brk() */
6599da04
JM
112 if (first_break == NULL)
113 first_break = (char *) sbrk (0);
acbbd80a 114#endif /* HAVE_SBRK */
6599da04
JM
115}
116
d1209685 117void
7a17ef5e 118xmalloc_failed (size_t size)
d1209685
ZW
119{
120#ifdef HAVE_SBRK
d1209685
ZW
121 size_t allocated;
122
123 if (first_break != NULL)
124 allocated = (char *) sbrk (0) - first_break;
125 else
126 allocated = (char *) sbrk (0) - (char *) &environ;
127 fprintf (stderr,
af18e951 128 "\n%s%sout of memory allocating %lu bytes after a total of %lu bytes\n",
d1209685
ZW
129 name, *name ? ": " : "",
130 (unsigned long) size, (unsigned long) allocated);
131#else /* HAVE_SBRK */
132 fprintf (stderr,
d8d7c3c3 133 "\n%s%sout of memory allocating %lu bytes\n",
d1209685
ZW
134 name, *name ? ": " : "",
135 (unsigned long) size);
136#endif /* HAVE_SBRK */
137 xexit (1);
138}
139
6599da04 140PTR
7a17ef5e 141xmalloc (size_t size)
6599da04
JM
142{
143 PTR newmem;
144
145 if (size == 0)
146 size = 1;
147 newmem = malloc (size);
148 if (!newmem)
d1209685
ZW
149 xmalloc_failed (size);
150
6599da04
JM
151 return (newmem);
152}
153
a9acf741 154PTR
7a17ef5e 155xcalloc (size_t nelem, size_t elsize)
a9acf741
KG
156{
157 PTR newmem;
158
159 if (nelem == 0 || elsize == 0)
160 nelem = elsize = 1;
161
162 newmem = calloc (nelem, elsize);
163 if (!newmem)
d1209685
ZW
164 xmalloc_failed (nelem * elsize);
165
a9acf741
KG
166 return (newmem);
167}
168
6599da04 169PTR
7a17ef5e 170xrealloc (PTR oldmem, size_t size)
6599da04
JM
171{
172 PTR newmem;
173
174 if (size == 0)
175 size = 1;
176 if (!oldmem)
177 newmem = malloc (size);
178 else
179 newmem = realloc (oldmem, size);
180 if (!newmem)
d1209685
ZW
181 xmalloc_failed (size);
182
6599da04
JM
183 return (newmem);
184}