Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* memory allocation routines with error checking. |
c1687039 | 2 | Copyright 1989, 90, 91, 92, 93, 94 Free Software Foundation, Inc. |
252b5132 RH |
3 | |
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. | |
9 | ||
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. | |
14 | ||
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. */ | |
19 | ||
eb383413 L |
20 | #ifdef HAVE_CONFIG_H |
21 | #include "config.h" | |
22 | #endif | |
252b5132 RH |
23 | #include "ansidecl.h" |
24 | #include "libiberty.h" | |
25 | ||
26 | #include <stdio.h> | |
27 | ||
28 | #ifdef __STDC__ | |
29 | #include <stddef.h> | |
30 | #else | |
31 | #define size_t unsigned long | |
32 | #define ptrdiff_t long | |
33 | #endif | |
34 | ||
35 | #if VMS | |
36 | #include <stdlib.h> | |
37 | #include <unixlib.h> | |
38 | #else | |
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)); | |
44 | #endif | |
45 | ||
46 | /* The program name if set. */ | |
47 | static const char *name = ""; | |
48 | ||
c1687039 | 49 | #ifdef HAVE_SBRK |
252b5132 RH |
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; | |
c1687039 | 53 | #endif /* HAVE_SBRK */ |
252b5132 RH |
54 | |
55 | void | |
56 | xmalloc_set_program_name (s) | |
57 | const char *s; | |
58 | { | |
59 | name = s; | |
c1687039 ILT |
60 | #ifdef HAVE_SBRK |
61 | /* Win32 ports other than cygwin32 don't have brk() */ | |
252b5132 RH |
62 | if (first_break == NULL) |
63 | first_break = (char *) sbrk (0); | |
c1687039 | 64 | #endif /* HAVE_SBRK */ |
252b5132 RH |
65 | } |
66 | ||
67 | PTR | |
68 | xmalloc (size) | |
69 | size_t size; | |
70 | { | |
71 | PTR newmem; | |
72 | ||
73 | if (size == 0) | |
74 | size = 1; | |
75 | newmem = malloc (size); | |
76 | if (!newmem) | |
77 | { | |
c1687039 | 78 | #ifdef HAVE_SBRK |
252b5132 RH |
79 | extern char **environ; |
80 | size_t allocated; | |
81 | ||
82 | if (first_break != NULL) | |
83 | allocated = (char *) sbrk (0) - first_break; | |
84 | else | |
85 | allocated = (char *) sbrk (0) - (char *) &environ; | |
86 | fprintf (stderr, | |
0c0a36a4 | 87 | "\n%s%sCannot allocate %lu bytes after allocating %lu bytes\n", |
252b5132 RH |
88 | name, *name ? ": " : "", |
89 | (unsigned long) size, (unsigned long) allocated); | |
c1687039 | 90 | #else /* HAVE_SBRK */ |
252b5132 | 91 | fprintf (stderr, |
0c0a36a4 | 92 | "\n%s%sCannot allocate %lu bytes\n", |
252b5132 RH |
93 | name, *name ? ": " : "", |
94 | (unsigned long) size); | |
c1687039 | 95 | #endif /* HAVE_SBRK */ |
252b5132 RH |
96 | xexit (1); |
97 | } | |
98 | return (newmem); | |
99 | } | |
100 | ||
101 | PTR | |
102 | xcalloc (nelem, elsize) | |
103 | size_t nelem, elsize; | |
104 | { | |
105 | PTR newmem; | |
106 | ||
107 | if (nelem == 0 || elsize == 0) | |
108 | nelem = elsize = 1; | |
109 | ||
110 | newmem = calloc (nelem, elsize); | |
111 | if (!newmem) | |
112 | { | |
c1687039 | 113 | #ifdef HAVE_SBRK |
252b5132 RH |
114 | extern char **environ; |
115 | size_t allocated; | |
116 | ||
117 | if (first_break != NULL) | |
118 | allocated = (char *) sbrk (0) - first_break; | |
119 | else | |
120 | allocated = (char *) sbrk (0) - (char *) &environ; | |
121 | fprintf (stderr, | |
0c0a36a4 | 122 | "\n%s%sCannot allocate %lu bytes after allocating %lu bytes\n", |
252b5132 RH |
123 | name, *name ? ": " : "", |
124 | (unsigned long) (nelem * elsize), (unsigned long) allocated); | |
c1687039 | 125 | #else /* HAVE_SBRK */ |
252b5132 | 126 | fprintf (stderr, |
0c0a36a4 | 127 | "\n%s%sCannot allocate %lu bytes\n", |
252b5132 RH |
128 | name, *name ? ": " : "", |
129 | (unsigned long) (nelem * elsize)); | |
c1687039 | 130 | #endif /* HAVE_SBRK */ |
252b5132 RH |
131 | xexit (1); |
132 | } | |
133 | return (newmem); | |
134 | } | |
135 | ||
136 | PTR | |
137 | xrealloc (oldmem, size) | |
138 | PTR oldmem; | |
139 | size_t size; | |
140 | { | |
141 | PTR newmem; | |
142 | ||
143 | if (size == 0) | |
144 | size = 1; | |
145 | if (!oldmem) | |
146 | newmem = malloc (size); | |
147 | else | |
148 | newmem = realloc (oldmem, size); | |
149 | if (!newmem) | |
150 | { | |
c1687039 | 151 | #ifdef HAVE_SBRK |
252b5132 RH |
152 | extern char **environ; |
153 | size_t allocated; | |
154 | ||
155 | if (first_break != NULL) | |
156 | allocated = (char *) sbrk (0) - first_break; | |
157 | else | |
158 | allocated = (char *) sbrk (0) - (char *) &environ; | |
159 | fprintf (stderr, | |
0c0a36a4 | 160 | "\n%s%sCannot reallocate %lu bytes after allocating %lu bytes\n", |
252b5132 RH |
161 | name, *name ? ": " : "", |
162 | (unsigned long) size, (unsigned long) allocated); | |
c1687039 | 163 | #else /* HAVE_SBRK */ |
252b5132 | 164 | fprintf (stderr, |
0c0a36a4 | 165 | "\n%s%sCannot reallocate %lu bytes\n", |
252b5132 RH |
166 | name, *name ? ": " : "", |
167 | (unsigned long) size); | |
c1687039 | 168 | #endif /* HAVE_SBRK */ |
252b5132 RH |
169 | xexit (1); |
170 | } | |
171 | return (newmem); | |
172 | } |