* Makefile.in: Change distribution targets to use bzip2 instead of
[binutils.git] / libiberty / xmalloc.c
blob1083790789d566df2aa34e1c64e576533ae49bf8
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. */
20 #include "ansidecl.h"
21 #include "libiberty.h"
23 #include <stdio.h>
25 #ifdef __STDC__
26 #include <stddef.h>
27 #else
28 #define size_t unsigned long
29 #define ptrdiff_t long
30 #endif
32 #if VMS
33 #include <stdlib.h>
34 #include <unixlib.h>
35 #else
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));
41 #endif
43 /* The program name if set. */
44 static const char *name = "";
46 #if !defined (__CYGWIN__) && defined (__CYGWIN32__)
47 #define __CYGWIN__ 1
48 #endif
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__ */
56 void
57 xmalloc_set_program_name (s)
58 const char *s;
60 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__ */
68 PTR
69 xmalloc (size)
70 size_t size;
72 PTR newmem;
74 if (size == 0)
75 size = 1;
76 newmem = malloc (size);
77 if (!newmem)
79 #if ! defined (_WIN32) || defined (__CYGWIN__) || defined (__UWIN__)
80 extern char **environ;
81 size_t allocated;
83 if (first_break != NULL)
84 allocated = (char *) sbrk (0) - first_break;
85 else
86 allocated = (char *) sbrk (0) - (char *) &environ;
87 fprintf (stderr,
88 "\n%s%sCan not allocate %lu bytes after allocating %lu bytes\n",
89 name, *name ? ": " : "",
90 (unsigned long) size, (unsigned long) allocated);
91 #else
92 fprintf (stderr,
93 "\n%s%sCan not allocate %lu bytes\n",
94 name, *name ? ": " : "",
95 (unsigned long) size);
96 #endif /* ! _WIN32 || __CYGWIN __ || __UWIN__ */
97 xexit (1);
99 return (newmem);
103 xcalloc (nelem, elsize)
104 size_t nelem, elsize;
106 PTR newmem;
108 if (nelem == 0 || elsize == 0)
109 nelem = elsize = 1;
111 newmem = calloc (nelem, elsize);
112 if (!newmem)
114 #if ! defined (_WIN32) || defined (__CYGWIN__)
115 extern char **environ;
116 size_t allocated;
118 if (first_break != NULL)
119 allocated = (char *) sbrk (0) - first_break;
120 else
121 allocated = (char *) sbrk (0) - (char *) &environ;
122 fprintf (stderr,
123 "\n%s%sCan not allocate %lu bytes after allocating %lu bytes\n",
124 name, *name ? ": " : "",
125 (unsigned long) (nelem * elsize), (unsigned long) allocated);
126 #else
127 fprintf (stderr,
128 "\n%s%sCan not allocate %lu bytes\n",
129 name, *name ? ": " : "",
130 (unsigned long) (nelem * elsize));
131 #endif /* ! _WIN32 || __CYGWIN __ */
132 xexit (1);
134 return (newmem);
138 xrealloc (oldmem, size)
139 PTR oldmem;
140 size_t size;
142 PTR newmem;
144 if (size == 0)
145 size = 1;
146 if (!oldmem)
147 newmem = malloc (size);
148 else
149 newmem = realloc (oldmem, size);
150 if (!newmem)
152 #ifndef __MINGW32__
153 extern char **environ;
154 size_t allocated;
156 if (first_break != NULL)
157 allocated = (char *) sbrk (0) - first_break;
158 else
159 allocated = (char *) sbrk (0) - (char *) &environ;
160 fprintf (stderr,
161 "\n%s%sCan not reallocate %lu bytes after allocating %lu bytes\n",
162 name, *name ? ": " : "",
163 (unsigned long) size, (unsigned long) allocated);
164 #else
165 fprintf (stderr,
166 "\n%s%sCan not reallocate %lu bytes\n",
167 name, *name ? ": " : "",
168 (unsigned long) size);
169 #endif /* __MINGW32__ */
170 xexit (1);
172 return (newmem);