1 /* memory allocation routines with error checking.
2 Copyright 1989, 90, 91, 92, 93, 94 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
21 #include "libiberty.h"
28 #define size_t unsigned long
29 #define ptrdiff_t long
36 /* For systems with larger pointers than ints, these must be declared. */
37 PTR malloc PARAMS ((size_t));
38 PTR realloc PARAMS ((PTR, size_t));
39 PTR calloc PARAMS ((size_t, size_t));
40 PTR sbrk PARAMS ((ptrdiff_t));
43 /* The program name if set. */
44 static const char *name = "";
47 /* The initial sbrk, set when the program name is set. Not used for win32
48 ports other than cygwin32. */
49 static char *first_break = NULL;
50 #endif /* HAVE_SBRK */
53 xmalloc_set_program_name (s)
58 /* Win32 ports other than cygwin32 don't have brk() */
59 if (first_break == NULL)
60 first_break = (char *) sbrk (0);
61 #endif /* HAVE_SBRK */
72 newmem = malloc (size);
76 extern char **environ;
79 if (first_break != NULL)
80 allocated = (char *) sbrk (0) - first_break;
82 allocated = (char *) sbrk (0) - (char *) &environ;
84 "\n%s%sCan not allocate %lu bytes after allocating %lu bytes\n",
85 name, *name ? ": " : "",
86 (unsigned long) size, (unsigned long) allocated);
89 "\n%s%sCan not allocate %lu bytes\n",
90 name, *name ? ": " : "",
91 (unsigned long) size);
92 #endif /* HAVE_SBRK */
99 xcalloc (nelem, elsize)
100 size_t nelem, elsize;
104 if (nelem == 0 || elsize == 0)
107 newmem = calloc (nelem, elsize);
111 extern char **environ;
114 if (first_break != NULL)
115 allocated = (char *) sbrk (0) - first_break;
117 allocated = (char *) sbrk (0) - (char *) &environ;
119 "\n%s%sCan not allocate %lu bytes after allocating %lu bytes\n",
120 name, *name ? ": " : "",
121 (unsigned long) (nelem * elsize), (unsigned long) allocated);
122 #else /* HAVE_SBRK */
124 "\n%s%sCan not allocate %lu bytes\n",
125 name, *name ? ": " : "",
126 (unsigned long) (nelem * elsize));
127 #endif /* HAVE_SBRK */
134 xrealloc (oldmem, size)
143 newmem = malloc (size);
145 newmem = realloc (oldmem, size);
149 extern char **environ;
152 if (first_break != NULL)
153 allocated = (char *) sbrk (0) - first_break;
155 allocated = (char *) sbrk (0) - (char *) &environ;
157 "\n%s%sCan not reallocate %lu bytes after allocating %lu bytes\n",
158 name, *name ? ": " : "",
159 (unsigned long) size, (unsigned long) allocated);
160 #else /* HAVE_SBRK */
162 "\n%s%sCan not reallocate %lu bytes\n",
163 name, *name ? ": " : "",
164 (unsigned long) size);
165 #endif /* HAVE_SBRK */