1 /* memory allocation routines with error checking.
2 Copyright (C) 1989-2023 Free Software Foundation, Inc.
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.
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.
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. */
22 @deftypefn Replacement void* xmalloc (size_t)
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.
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.
38 @deftypefn Replacement void* xcalloc (size_t @var{nelem}, size_t @var{elsize})
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
46 @deftypefn Replacement void xmalloc_set_program_name (const char *@var{name})
48 You can use this to set the name of the program used by
49 @code{xmalloc_failed} when printing a failure message.
53 @deftypefn Replacement void xmalloc_failed (size_t)
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.
67 #include "libiberty.h"
78 /* For systems with larger pointers than ints, these must be declared. */
79 # if HAVE_STDLIB_H && HAVE_UNISTD_H && HAVE_DECL_MALLOC \
80 && HAVE_DECL_REALLOC && HAVE_DECL_CALLOC && HAVE_DECL_SBRK
86 # endif /* __cplusplus */
87 void *malloc (size_t);
88 void *realloc (void *, size_t);
89 void *calloc (size_t, size_t);
91 void *sbrk (ptrdiff_t);
95 # endif /* __cplusplus */
96 # endif /* HAVE_STDLIB_H ... */
99 /* The program name if set. */
100 static const char *name
= "";
103 /* The initial sbrk, set when the program name is set. Not used for win32
104 ports other than cygwin32. */
105 static char *first_break
= NULL
;
106 #endif /* HAVE_SBRK */
109 xmalloc_set_program_name (const char *s
)
113 /* Win32 ports other than cygwin32 don't have brk() */
114 if (first_break
== NULL
)
115 first_break
= (char *) sbrk (0);
116 #endif /* HAVE_SBRK */
120 xmalloc_failed (size_t size
)
125 if (first_break
!= NULL
)
126 allocated
= (char *) sbrk (0) - first_break
;
128 allocated
= (char *) sbrk (0) - (char *) &environ
;
130 "\n%s%sout of memory allocating %lu bytes after a total of %lu bytes\n",
131 name
, *name
? ": " : "",
132 (unsigned long) size
, (unsigned long) allocated
);
133 #else /* HAVE_SBRK */
135 "\n%s%sout of memory allocating %lu bytes\n",
136 name
, *name
? ": " : "",
137 (unsigned long) size
);
138 #endif /* HAVE_SBRK */
143 xmalloc (size_t size
)
149 newmem
= malloc (size
);
151 xmalloc_failed (size
);
157 xcalloc (size_t nelem
, size_t elsize
)
161 if (nelem
== 0 || elsize
== 0)
164 newmem
= calloc (nelem
, elsize
);
166 xmalloc_failed (nelem
* elsize
);
172 xrealloc (void *oldmem
, size_t size
)
179 newmem
= malloc (size
);
181 newmem
= realloc (oldmem
, size
);
183 xmalloc_failed (size
);