source: trunk/src/binutils/libiberty/xmalloc.c@ 480

Last change on this file since 480 was 10, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 3.1 KB
Line 
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#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif
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
49#ifdef HAVE_SBRK
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;
53#endif /* HAVE_SBRK */
54
55void
56xmalloc_set_program_name (s)
57 const char *s;
58{
59 name = s;
60#ifdef HAVE_SBRK
61 /* Win32 ports other than cygwin32 don't have brk() */
62 if (first_break == NULL)
63 first_break = (char *) sbrk (0);
64#endif /* HAVE_SBRK */
65}
66
67void
68xmalloc_failed (size)
69 size_t size;
70{
71#ifdef HAVE_SBRK
72 extern char **environ;
73 size_t allocated;
74
75 if (first_break != NULL)
76 allocated = (char *) sbrk (0) - first_break;
77 else
78 allocated = (char *) sbrk (0) - (char *) &environ;
79 fprintf (stderr,
80 "\n%s%sCannot allocate %lu bytes after allocating %lu bytes\n",
81 name, *name ? ": " : "",
82 (unsigned long) size, (unsigned long) allocated);
83#else /* HAVE_SBRK */
84 fprintf (stderr,
85 "\n%s%sCannot allocate %lu bytes\n",
86 name, *name ? ": " : "",
87 (unsigned long) size);
88#endif /* HAVE_SBRK */
89 xexit (1);
90}
91
92PTR
93xmalloc (size)
94 size_t size;
95{
96 PTR newmem;
97
98 if (size == 0)
99 size = 1;
100 newmem = malloc (size);
101 if (!newmem)
102 xmalloc_failed (size);
103
104 return (newmem);
105}
106
107PTR
108xcalloc (nelem, elsize)
109 size_t nelem, elsize;
110{
111 PTR newmem;
112
113 if (nelem == 0 || elsize == 0)
114 nelem = elsize = 1;
115
116 newmem = calloc (nelem, elsize);
117 if (!newmem)
118 xmalloc_failed (nelem * elsize);
119
120 return (newmem);
121}
122
123PTR
124xrealloc (oldmem, size)
125 PTR oldmem;
126 size_t size;
127{
128 PTR newmem;
129
130 if (size == 0)
131 size = 1;
132 if (!oldmem)
133 newmem = malloc (size);
134 else
135 newmem = realloc (oldmem, size);
136 if (!newmem)
137 xmalloc_failed (size);
138
139 return (newmem);
140}
Note: See TracBrowser for help on using the repository browser.