1 /* memory allocation routines with error checking.
2 Copyright 1989, 90, 91, 92, 93, 94, 1999 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 = "";
46 #if !defined (__CYGWIN__) && defined (__CYGWIN32__)
50 /* On Unix systems we use sbrk to determine how much memory has been
53 #if (! defined (_WIN32) && ! defined (__INTERIX)) || defined (__CYGWIN__) || defined (__UWIN__)
58 /* The initial sbrk, set when the program name is set. Not used for win32
59 ports other than cygwin32. */
60 static char *first_break = NULL;
64 xmalloc_set_program_name (s)
69 if (first_break == NULL)
70 first_break = (char *) sbrk (0);
82 newmem = malloc (size);
86 extern char **environ;
89 if (first_break != NULL)
90 allocated = (char *) sbrk (0) - first_break;
92 allocated = (char *) sbrk (0) - (char *) &environ;
94 "\n%s%sCan not allocate %lu bytes after allocating %lu bytes\n",
95 name, *name ? ": " : "",
96 (unsigned long) size, (unsigned long) allocated);
99 "\n%s%sCan not allocate %lu bytes\n",
100 name, *name ? ": " : "",
101 (unsigned long) size);
102 #endif /* ! USE_SBRK */
109 xcalloc (nelem, elsize)
110 size_t nelem, elsize;
114 if (nelem == 0 || elsize == 0)
117 newmem = calloc (nelem, elsize);
121 extern char **environ;
124 if (first_break != NULL)
125 allocated = (char *) sbrk (0) - first_break;
127 allocated = (char *) sbrk (0) - (char *) &environ;
129 "\n%s%sCan not allocate %lu bytes after allocating %lu bytes\n",
130 name, *name ? ": " : "",
131 (unsigned long) (nelem * elsize), (unsigned long) allocated);
134 "\n%s%sCan not allocate %lu bytes\n",
135 name, *name ? ": " : "",
136 (unsigned long) (nelem * elsize));
137 #endif /* ! USE_SBRK */
144 xrealloc (oldmem, size)
153 newmem = malloc (size);
155 newmem = realloc (oldmem, size);
159 extern char **environ;
162 if (first_break != NULL)
163 allocated = (char *) sbrk (0) - first_break;
165 allocated = (char *) sbrk (0) - (char *) &environ;
167 "\n%s%sCan not reallocate %lu bytes after allocating %lu bytes\n",
168 name, *name ? ": " : "",
169 (unsigned long) size, (unsigned long) allocated);
172 "\n%s%sCan not reallocate %lu bytes\n",
173 name, *name ? ": " : "",
174 (unsigned long) size);
175 #endif /* ! USE_SBRK */