]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/binutils/libiberty/xmalloc.c
Initial import of GNU binutils version 2.8.1. Believe it or not,
[FreeBSD/FreeBSD.git] / contrib / binutils / libiberty / xmalloc.c
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 sbrk PARAMS ((ptrdiff_t));
40 #endif
41
42 /* The program name if set.  */
43 static const char *name = "";
44
45 /* The initial sbrk, set when the program name is set.  */
46 static char *first_break = NULL;
47
48 void
49 xmalloc_set_program_name (s)
50      const char *s;
51 {
52   name = s;
53   if (first_break == NULL)
54     first_break = (char *) sbrk (0);
55 }
56
57 PTR
58 xmalloc (size)
59     size_t size;
60 {
61   PTR newmem;
62
63   if (size == 0)
64     size = 1;
65   newmem = malloc (size);
66   if (!newmem)
67     {
68       extern char **environ;
69       size_t allocated;
70
71       if (first_break != NULL)
72         allocated = (char *) sbrk (0) - first_break;
73       else
74         allocated = (char *) sbrk (0) - (char *) &environ;
75       fprintf (stderr,
76                "\n%s%sCan not allocate %lu bytes after allocating %lu bytes\n",
77                name, *name ? ": " : "",
78                (unsigned long) size, (unsigned long) allocated);
79       xexit (1);
80     }
81   return (newmem);
82 }
83
84 PTR
85 xrealloc (oldmem, size)
86     PTR oldmem;
87     size_t size;
88 {
89   PTR newmem;
90
91   if (size == 0)
92     size = 1;
93   if (!oldmem)
94     newmem = malloc (size);
95   else
96     newmem = realloc (oldmem, size);
97   if (!newmem)
98     {
99       extern char **environ;
100       size_t allocated;
101
102       if (first_break != NULL)
103         allocated = (char *) sbrk (0) - first_break;
104       else
105         allocated = (char *) sbrk (0) - (char *) &environ;
106       fprintf (stderr,
107                "\n%s%sCan not reallocate %lu bytes after allocating %lu bytes\n",
108                name, *name ? ": " : "",
109                (unsigned long) size, (unsigned long) allocated);
110       xexit (1);
111     }
112   return (newmem);
113 }