]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - libiberty/xmalloc.c
* xmalloc.c: Control all uses of SBRK with a single define,
[thirdparty/binutils-gdb.git] / libiberty / xmalloc.c
1 /* memory allocation routines with error checking.
2 Copyright 1989, 90, 91, 92, 93, 94, 1999 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 /* On Unix systems we use sbrk to determine how much memory has been
51 allocated. */
52 #undef USE_SBRK
53 #if (! defined (_WIN32) && ! defined (__INTERIX)) || defined (__CYGWIN__) || defined (__UWIN__)
54 #define USE_SBRK
55 #endif
56
57 #ifdef USE_SBRK
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;
61 #endif
62
63 void
64 xmalloc_set_program_name (s)
65 const char *s;
66 {
67 name = s;
68 #ifdef USE_SBRK
69 if (first_break == NULL)
70 first_break = (char *) sbrk (0);
71 #endif
72 }
73
74 PTR
75 xmalloc (size)
76 size_t size;
77 {
78 PTR newmem;
79
80 if (size == 0)
81 size = 1;
82 newmem = malloc (size);
83 if (!newmem)
84 {
85 #ifdef USE_SBRK
86 extern char **environ;
87 size_t allocated;
88
89 if (first_break != NULL)
90 allocated = (char *) sbrk (0) - first_break;
91 else
92 allocated = (char *) sbrk (0) - (char *) &environ;
93 fprintf (stderr,
94 "\n%s%sCan not allocate %lu bytes after allocating %lu bytes\n",
95 name, *name ? ": " : "",
96 (unsigned long) size, (unsigned long) allocated);
97 #else
98 fprintf (stderr,
99 "\n%s%sCan not allocate %lu bytes\n",
100 name, *name ? ": " : "",
101 (unsigned long) size);
102 #endif /* ! USE_SBRK */
103 xexit (1);
104 }
105 return (newmem);
106 }
107
108 PTR
109 xcalloc (nelem, elsize)
110 size_t nelem, elsize;
111 {
112 PTR newmem;
113
114 if (nelem == 0 || elsize == 0)
115 nelem = elsize = 1;
116
117 newmem = calloc (nelem, elsize);
118 if (!newmem)
119 {
120 #ifdef USE_SBRK
121 extern char **environ;
122 size_t allocated;
123
124 if (first_break != NULL)
125 allocated = (char *) sbrk (0) - first_break;
126 else
127 allocated = (char *) sbrk (0) - (char *) &environ;
128 fprintf (stderr,
129 "\n%s%sCan not allocate %lu bytes after allocating %lu bytes\n",
130 name, *name ? ": " : "",
131 (unsigned long) (nelem * elsize), (unsigned long) allocated);
132 #else
133 fprintf (stderr,
134 "\n%s%sCan not allocate %lu bytes\n",
135 name, *name ? ": " : "",
136 (unsigned long) (nelem * elsize));
137 #endif /* ! USE_SBRK */
138 xexit (1);
139 }
140 return (newmem);
141 }
142
143 PTR
144 xrealloc (oldmem, size)
145 PTR oldmem;
146 size_t size;
147 {
148 PTR newmem;
149
150 if (size == 0)
151 size = 1;
152 if (!oldmem)
153 newmem = malloc (size);
154 else
155 newmem = realloc (oldmem, size);
156 if (!newmem)
157 {
158 #ifdef USE_SBRK
159 extern char **environ;
160 size_t allocated;
161
162 if (first_break != NULL)
163 allocated = (char *) sbrk (0) - first_break;
164 else
165 allocated = (char *) sbrk (0) - (char *) &environ;
166 fprintf (stderr,
167 "\n%s%sCan not reallocate %lu bytes after allocating %lu bytes\n",
168 name, *name ? ": " : "",
169 (unsigned long) size, (unsigned long) allocated);
170 #else
171 fprintf (stderr,
172 "\n%s%sCan not reallocate %lu bytes\n",
173 name, *name ? ": " : "",
174 (unsigned long) size);
175 #endif /* ! USE_SBRK */
176 xexit (1);
177 }
178 return (newmem);
179 }