* libbfd.c (bfd_put_8): Make it of type `void'.
[deliverable/binutils-gdb.git] / libiberty / xmalloc.c
CommitLineData
252b5132
RH
1/* memory allocation routines with error checking.
2 Copyright 1989, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
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
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. */
37PTR malloc PARAMS ((size_t));
38PTR realloc PARAMS ((PTR, size_t));
39PTR calloc PARAMS ((size_t, size_t));
40PTR sbrk PARAMS ((ptrdiff_t));
41#endif
42
43/* The program name if set. */
44static 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. */
53static char *first_break = NULL;
54#endif /* ! _WIN32 || __CYGWIN __ || __UWIN__ */
55
56void
57xmalloc_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
68PTR
69xmalloc (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
102PTR
103xcalloc (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
137PTR
138xrealloc (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}
This page took 0.035293 seconds and 4 git commands to generate.