1 /* memory allocation routines with error checking. 2 Copyright 1989, 90, 91, 92, 93, 94 Free Software Foundation, Inc. 3 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. 9 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. 14 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. */ 19 20 /* 21 22 @deftypefn Replacement void* xmalloc (size_t) 23 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. 29 30 @end deftypefn 31 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. 35 36 @end deftypefn 37 38 @deftypefn Replacement void* xcalloc (size_t @var{nelem}, size_t @var{elsize}) 39 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 42 cannot be found. 43 44 @end deftypefn 45 46 @deftypefn Replacement void xmalloc_set_program_name (const char *@var{name}) 47 48 You 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 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. 58 59 @end deftypefn 60 61 */ 62 63 #ifdef HAVE_CONFIG_H 64 #include "config.h" 65 #endif 66 #include "ansidecl.h" 67 #include "libiberty.h" 68 69 #include <stdio.h> 70 71 #include <stddef.h> 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. */ 78 # if HAVE_STDLIB_H && HAVE_UNISTD_H && HAVE_DECL_MALLOC \ 79 && HAVE_DECL_REALLOC && HAVE_DECL_CALLOC && HAVE_DECL_SBRK 80 # include <stdlib.h> 81 # include <unistd.h> 82 # else 83 # ifdef __cplusplus 84 extern "C" { 85 # endif /* __cplusplus */ 86 void *malloc (size_t); 87 void *realloc (void *, size_t); 88 void *calloc (size_t, size_t); 89 void *sbrk (ptrdiff_t); 90 # ifdef __cplusplus 91 } 92 # endif /* __cplusplus */ 93 # endif /* HAVE_STDLIB_H ... */ 94 #endif /* VMS */ 95 96 /* The program name if set. */ 97 static const char *name = ""; 98 99 void 100 xmalloc_set_program_name (const char *s) 101 { 102 name = s; 103 } 104 105 void 106 xmalloc_failed (size_t size) 107 { 108 fprintf (stderr, 109 "\n%s%sout of memory allocating %lu bytes\n", 110 name, *name ? ": " : "", 111 (unsigned long) size); 112 xexit (1); 113 } 114 115 PTR 116 xmalloc (size_t size) 117 { 118 PTR newmem; 119 120 if (size == 0) 121 size = 1; 122 newmem = malloc (size); 123 if (!newmem) 124 xmalloc_failed (size); 125 126 return (newmem); 127 } 128 129 PTR 130 xcalloc (size_t nelem, size_t elsize) 131 { 132 PTR newmem; 133 134 if (nelem == 0 || elsize == 0) 135 nelem = elsize = 1; 136 137 newmem = calloc (nelem, elsize); 138 if (!newmem) 139 xmalloc_failed (nelem * elsize); 140 141 return (newmem); 142 } 143 144 PTR 145 xrealloc (PTR oldmem, size_t size) 146 { 147 PTR newmem; 148 149 if (size == 0) 150 size = 1; 151 if (!oldmem) 152 newmem = malloc (size); 153 else 154 newmem = realloc (oldmem, size); 155 if (!newmem) 156 xmalloc_failed (size); 157 158 return (newmem); 159 } 160