This commit was generated by cvs2svn to track changes on a CVS vendor
[deliverable/binutils-gdb.git] / libiberty / xmalloc.c
CommitLineData
252b5132 1/* memory allocation routines with error checking.
c1687039 2 Copyright 1989, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
252b5132
RH
3
4This file is part of the libiberty library.
5Libiberty is free software; you can redistribute it and/or
6modify it under the terms of the GNU Library General Public
7License as published by the Free Software Foundation; either
8version 2 of the License, or (at your option) any later version.
9
10Libiberty is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Library General Public License for more details.
14
15You should have received a copy of the GNU Library General Public
16License along with libiberty; see the file COPYING.LIB. If
17not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18Boston, 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. */
40PTR malloc PARAMS ((size_t));
41PTR realloc PARAMS ((PTR, size_t));
42PTR calloc PARAMS ((size_t, size_t));
43PTR sbrk PARAMS ((ptrdiff_t));
44#endif
45
46/* The program name if set. */
47static 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. */
52static char *first_break = NULL;
c1687039 53#endif /* HAVE_SBRK */
252b5132
RH
54
55void
56xmalloc_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
67PTR
68xmalloc (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
101PTR
102xcalloc (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
136PTR
137xrealloc (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}
This page took 0.052877 seconds and 4 git commands to generate.