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. */
24 #include "libiberty.h"
31 #define size_t unsigned long
32 #define ptrdiff_t long
39 /* For systems with larger pointers than ints, these must be declared. */
40 PTR malloc PARAMS ((size_t));
41 PTR realloc PARAMS ((PTR, size_t));
42 PTR calloc PARAMS ((size_t, size_t));
43 PTR sbrk PARAMS ((ptrdiff_t));
46 /* The program name if set. */
47 static const char *name = "";
50 /* The initial sbrk, set when the program name is set. Not used for win32
51 ports other than cygwin32. */
52 static char *first_break = NULL;
53 #endif /* HAVE_SBRK */
56 xmalloc_set_program_name (s)
61 /* Win32 ports other than cygwin32 don't have brk() */
62 if (first_break == NULL)
63 first_break = (char *) sbrk (0);
64 #endif /* HAVE_SBRK */
75 newmem = malloc (size);
79 extern char **environ;
82 if (first_break != NULL)
83 allocated = (char *) sbrk (0) - first_break;
85 allocated = (char *) sbrk (0) - (char *) &environ;
87 "\n%s%sCannot allocate %lu bytes after allocating %lu bytes\n",
88 name, *name ? ": " : "",
89 (unsigned long) size, (unsigned long) allocated);
92 "\n%s%sCannot allocate %lu bytes\n",
93 name, *name ? ": " : "",
94 (unsigned long) size);
95 #endif /* HAVE_SBRK */
102 xcalloc (nelem, elsize)
103 size_t nelem, elsize;
107 if (nelem == 0 || elsize == 0)
110 newmem = calloc (nelem, elsize);
114 extern char **environ;
117 if (first_break != NULL)
118 allocated = (char *) sbrk (0) - first_break;
120 allocated = (char *) sbrk (0) - (char *) &environ;
122 "\n%s%sCannot allocate %lu bytes after allocating %lu bytes\n",
123 name, *name ? ": " : "",
124 (unsigned long) (nelem * elsize), (unsigned long) allocated);
125 #else /* HAVE_SBRK */
127 "\n%s%sCannot allocate %lu bytes\n",
128 name, *name ? ": " : "",
129 (unsigned long) (nelem * elsize));
130 #endif /* HAVE_SBRK */
137 xrealloc (oldmem, size)
146 newmem = malloc (size);
148 newmem = realloc (oldmem, size);
152 extern char **environ;
155 if (first_break != NULL)
156 allocated = (char *) sbrk (0) - first_break;
158 allocated = (char *) sbrk (0) - (char *) &environ;
160 "\n%s%sCannot reallocate %lu bytes after allocating %lu bytes\n",
161 name, *name ? ": " : "",
162 (unsigned long) size, (unsigned long) allocated);
163 #else /* HAVE_SBRK */
165 "\n%s%sCannot reallocate %lu bytes\n",
166 name, *name ? ": " : "",
167 (unsigned long) size);
168 #endif /* HAVE_SBRK */