xref: /openbsd-src/gnu/lib/libiberty/src/xmalloc.c (revision 31b08222e46a5c824d06574863b8f73c72aea677)
100bf4279Sespie /* memory allocation routines with error checking.
200bf4279Sespie    Copyright 1989, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
300bf4279Sespie 
400bf4279Sespie This file is part of the libiberty library.
500bf4279Sespie Libiberty is free software; you can redistribute it and/or
600bf4279Sespie modify it under the terms of the GNU Library General Public
700bf4279Sespie License as published by the Free Software Foundation; either
800bf4279Sespie version 2 of the License, or (at your option) any later version.
900bf4279Sespie 
1000bf4279Sespie Libiberty is distributed in the hope that it will be useful,
1100bf4279Sespie but WITHOUT ANY WARRANTY; without even the implied warranty of
1200bf4279Sespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1300bf4279Sespie Library General Public License for more details.
1400bf4279Sespie 
1500bf4279Sespie You should have received a copy of the GNU Library General Public
1600bf4279Sespie License along with libiberty; see the file COPYING.LIB.  If
17*150b7e42Smiod not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18*150b7e42Smiod Boston, MA 02110-1301, USA.  */
1900bf4279Sespie 
2037c53322Sespie /*
2137c53322Sespie 
2237c53322Sespie @deftypefn Replacement void* xmalloc (size_t)
2337c53322Sespie 
2437c53322Sespie Allocate memory without fail.  If @code{malloc} fails, this will print
2537c53322Sespie a message to @code{stderr} (using the name set by
2637c53322Sespie @code{xmalloc_set_program_name},
2737c53322Sespie if any) and then call @code{xexit}.  Note that it is therefore safe for
2837c53322Sespie a program to contain @code{#define malloc xmalloc} in its source.
2937c53322Sespie 
3037c53322Sespie @end deftypefn
3137c53322Sespie 
3237c53322Sespie @deftypefn Replacement void* xrealloc (void *@var{ptr}, size_t @var{size})
3337c53322Sespie Reallocate memory without fail.  This routine functions like @code{realloc},
3437c53322Sespie but will behave the same as @code{xmalloc} if memory cannot be found.
3537c53322Sespie 
3637c53322Sespie @end deftypefn
3737c53322Sespie 
3837c53322Sespie @deftypefn Replacement void* xcalloc (size_t @var{nelem}, size_t @var{elsize})
3937c53322Sespie 
4037c53322Sespie Allocate memory without fail, and set it to zero.  This routine functions
4137c53322Sespie like @code{calloc}, but will behave the same as @code{xmalloc} if memory
4237c53322Sespie cannot be found.
4337c53322Sespie 
4437c53322Sespie @end deftypefn
4537c53322Sespie 
4637c53322Sespie @deftypefn Replacement void xmalloc_set_program_name (const char *@var{name})
4737c53322Sespie 
4837c53322Sespie You can use this to set the name of the program used by
4937c53322Sespie @code{xmalloc_failed} when printing a failure message.
5037c53322Sespie 
5137c53322Sespie @end deftypefn
5237c53322Sespie 
5337c53322Sespie @deftypefn Replacement void xmalloc_failed (size_t)
5437c53322Sespie 
5537c53322Sespie This function is not meant to be called by client code, and is listed
5637c53322Sespie here for completeness only.  If any of the allocation routines fail, this
5737c53322Sespie function will be called to print an error message and terminate execution.
5837c53322Sespie 
5937c53322Sespie @end deftypefn
6037c53322Sespie 
6137c53322Sespie */
6237c53322Sespie 
6337c53322Sespie #ifdef HAVE_CONFIG_H
6437c53322Sespie #include "config.h"
6537c53322Sespie #endif
6600bf4279Sespie #include "ansidecl.h"
6700bf4279Sespie #include "libiberty.h"
6800bf4279Sespie 
6900bf4279Sespie #include <stdio.h>
7000bf4279Sespie 
7100bf4279Sespie #include <stddef.h>
7200bf4279Sespie 
7300bf4279Sespie #if VMS
7400bf4279Sespie #include <stdlib.h>
7500bf4279Sespie #include <unixlib.h>
7600bf4279Sespie #else
7700bf4279Sespie /* For systems with larger pointers than ints, these must be declared.  */
78*150b7e42Smiod #  if HAVE_STDLIB_H && HAVE_UNISTD_H && HAVE_DECL_MALLOC \
79*150b7e42Smiod       && HAVE_DECL_REALLOC && HAVE_DECL_CALLOC && HAVE_DECL_SBRK
80*150b7e42Smiod #    include <stdlib.h>
81*150b7e42Smiod #    include <unistd.h>
82*150b7e42Smiod #  else
83*150b7e42Smiod #    ifdef __cplusplus
84*150b7e42Smiod extern "C" {
85*150b7e42Smiod #    endif /* __cplusplus */
86*150b7e42Smiod void *malloc (size_t);
87*150b7e42Smiod void *realloc (void *, size_t);
88*150b7e42Smiod void *calloc (size_t, size_t);
89*150b7e42Smiod void *sbrk (ptrdiff_t);
90*150b7e42Smiod #    ifdef __cplusplus
91*150b7e42Smiod }
92*150b7e42Smiod #    endif /* __cplusplus */
93*150b7e42Smiod #  endif /* HAVE_STDLIB_H ...  */
94*150b7e42Smiod #endif /* VMS */
9500bf4279Sespie 
9600bf4279Sespie /* The program name if set.  */
9700bf4279Sespie static const char *name = "";
9800bf4279Sespie 
9900bf4279Sespie void
xmalloc_set_program_name(const char * s)100*150b7e42Smiod xmalloc_set_program_name (const char *s)
10100bf4279Sespie {
10200bf4279Sespie   name = s;
10300bf4279Sespie }
10400bf4279Sespie 
10537c53322Sespie void
xmalloc_failed(size_t size)106*150b7e42Smiod xmalloc_failed (size_t size)
10700bf4279Sespie {
10800bf4279Sespie   fprintf (stderr,
10937c53322Sespie 	   "\n%s%sout of memory allocating %lu bytes\n",
11000bf4279Sespie 	   name, *name ? ": " : "",
11100bf4279Sespie 	   (unsigned long) size);
11200bf4279Sespie   xexit (1);
11300bf4279Sespie }
11437c53322Sespie 
11537c53322Sespie PTR
xmalloc(size_t size)116*150b7e42Smiod xmalloc (size_t size)
11737c53322Sespie {
11837c53322Sespie   PTR newmem;
11937c53322Sespie 
12037c53322Sespie   if (size == 0)
12137c53322Sespie     size = 1;
12237c53322Sespie   newmem = malloc (size);
12337c53322Sespie   if (!newmem)
12437c53322Sespie     xmalloc_failed (size);
12537c53322Sespie 
12600bf4279Sespie   return (newmem);
12700bf4279Sespie }
12800bf4279Sespie 
12900bf4279Sespie PTR
xcalloc(size_t nelem,size_t elsize)130*150b7e42Smiod xcalloc (size_t nelem, size_t elsize)
13100bf4279Sespie {
13200bf4279Sespie   PTR newmem;
13300bf4279Sespie 
13400bf4279Sespie   if (nelem == 0 || elsize == 0)
13500bf4279Sespie     nelem = elsize = 1;
13600bf4279Sespie 
13700bf4279Sespie   newmem = calloc (nelem, elsize);
13800bf4279Sespie   if (!newmem)
13937c53322Sespie     xmalloc_failed (nelem * elsize);
14000bf4279Sespie 
14100bf4279Sespie   return (newmem);
14200bf4279Sespie }
14300bf4279Sespie 
14400bf4279Sespie PTR
xrealloc(PTR oldmem,size_t size)145*150b7e42Smiod xrealloc (PTR oldmem, size_t size)
14600bf4279Sespie {
14700bf4279Sespie   PTR newmem;
14800bf4279Sespie 
14900bf4279Sespie   if (size == 0)
15000bf4279Sespie     size = 1;
15100bf4279Sespie   if (!oldmem)
15200bf4279Sespie     newmem = malloc (size);
15300bf4279Sespie   else
15400bf4279Sespie     newmem = realloc (oldmem, size);
15500bf4279Sespie   if (!newmem)
15637c53322Sespie     xmalloc_failed (size);
15700bf4279Sespie 
15800bf4279Sespie   return (newmem);
15900bf4279Sespie }
160