Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | /* memory allocation routines with error checking. |
2 | Copyright 1989, 90, 91, 92, 93, 94 Free Software Foundation, Inc. | |
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 | ||
20 | #include "ansidecl.h" | |
21 | #include "libiberty.h" | |
22 | ||
23 | #include <stdio.h> | |
24 | ||
25 | #ifdef __STDC__ | |
26 | #include <stddef.h> | |
27 | #else | |
28 | #define size_t unsigned long | |
29 | #define ptrdiff_t long | |
30 | #endif | |
31 | ||
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 | |
42 | ||
43 | /* The program name if set. */ | |
44 | static const char *name = ""; | |
45 | ||
46 | #if !defined (__CYGWIN__) && defined (__CYGWIN32__) | |
47 | #define __CYGWIN__ 1 | |
48 | #endif | |
49 | ||
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__ */ | |
55 | ||
56 | void | |
57 | xmalloc_set_program_name (s) | |
58 | const char *s; | |
59 | { | |
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__ */ | |
66 | } | |
67 | ||
68 | PTR | |
69 | xmalloc (size) | |
70 | size_t size; | |
71 | { | |
72 | PTR newmem; | |
73 | ||
74 | if (size == 0) | |
75 | size = 1; | |
76 | newmem = malloc (size); | |
77 | if (!newmem) | |
78 | { | |
79 | #if ! defined (_WIN32) || defined (__CYGWIN__) || defined (__UWIN__) | |
80 | extern char **environ; | |
81 | size_t allocated; | |
82 | ||
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); | |
98 | } | |
99 | return (newmem); | |
100 | } | |
101 | ||
102 | PTR | |
103 | xcalloc (nelem, elsize) | |
104 | size_t nelem, elsize; | |
105 | { | |
106 | PTR newmem; | |
107 | ||
108 | if (nelem == 0 || elsize == 0) | |
109 | nelem = elsize = 1; | |
110 | ||
111 | newmem = calloc (nelem, elsize); | |
112 | if (!newmem) | |
113 | { | |
114 | #if ! defined (_WIN32) || defined (__CYGWIN__) | |
115 | extern char **environ; | |
116 | size_t allocated; | |
117 | ||
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); | |
133 | } | |
134 | return (newmem); | |
135 | } | |
136 | ||
137 | PTR | |
138 | xrealloc (oldmem, size) | |
139 | PTR oldmem; | |
140 | size_t size; | |
141 | { | |
142 | PTR newmem; | |
143 | ||
144 | if (size == 0) | |
145 | size = 1; | |
146 | if (!oldmem) | |
147 | newmem = malloc (size); | |
148 | else | |
149 | newmem = realloc (oldmem, size); | |
150 | if (!newmem) | |
151 | { | |
152 | #ifndef __MINGW32__ | |
153 | extern char **environ; | |
154 | size_t allocated; | |
155 | ||
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); | |
171 | } | |
172 | return (newmem); | |
173 | } |