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 = "";
46 #if !defined (__CYGWIN__) && defined (__CYGWIN32__)
50 #if ! defined (_WIN32) || defined (__CYGWIN__) || defined (__UWIN__)
51 /* The initial sbrk, set when the program name is set. Not used for win32
52 ports other than cygwin32. */
53 static char *first_break = NULL;
54 #endif /* ! _WIN32 || __CYGWIN __ || __UWIN__ */
57 xmalloc_set_program_name (s)
61 #if ! defined (_WIN32) || defined (__CYGWIN__) || defined (__UWIN__)
62 /* Win32 ports other than cygwin32 don't have brk() */
63 if (first_break == NULL)
64 first_break = (char *) sbrk (0);
65 #endif /* ! _WIN32 || __CYGWIN __ || __UWIN__ */
76 newmem = malloc (size);
79 #if ! defined (_WIN32) || defined (__CYGWIN__) || defined (__UWIN__)
80 extern char **environ;
83 if (first_break != NULL)
84 allocated = (char *) sbrk (0) - first_break;
86 allocated = (char *) sbrk (0) - (char *) &environ;
88 "\n%s%sCan not allocate %lu bytes after allocating %lu bytes\n",
89 name, *name ? ": " : "",
90 (unsigned long) size, (unsigned long) allocated);
93 "\n%s%sCan not allocate %lu bytes\n",
94 name, *name ? ": " : "",
95 (unsigned long) size);
96 #endif /* ! _WIN32 || __CYGWIN __ || __UWIN__ */
103 xcalloc (nelem, elsize)
104 size_t nelem, elsize;
108 if (nelem == 0 || elsize == 0)
111 newmem = calloc (nelem, elsize);
114 #if ! defined (_WIN32) || defined (__CYGWIN__)
115 extern char **environ;
118 if (first_break != NULL)
119 allocated = (char *) sbrk (0) - first_break;
121 allocated = (char *) sbrk (0) - (char *) &environ;
123 "\n%s%sCan not allocate %lu bytes after allocating %lu bytes\n",
124 name, *name ? ": " : "",
125 (unsigned long) (nelem * elsize), (unsigned long) allocated);
128 "\n%s%sCan not allocate %lu bytes\n",
129 name, *name ? ": " : "",
130 (unsigned long) (nelem * elsize));
131 #endif /* ! _WIN32 || __CYGWIN __ */
138 xrealloc (oldmem, size)
147 newmem = malloc (size);
149 newmem = realloc (oldmem, size);
153 extern char **environ;
156 if (first_break != NULL)
157 allocated = (char *) sbrk (0) - first_break;
159 allocated = (char *) sbrk (0) - (char *) &environ;
161 "\n%s%sCan not reallocate %lu bytes after allocating %lu bytes\n",
162 name, *name ? ": " : "",
163 (unsigned long) size, (unsigned long) allocated);
166 "\n%s%sCan not reallocate %lu bytes\n",
167 name, *name ? ": " : "",
168 (unsigned long) size);
169 #endif /* __MINGW32__ */