Merge aosp-toolchain/gcc/gcc-4_9 changes.
[official-gcc.git] / gcc-4_9-mobile / libiberty / xmalloc.c
blob726ef64273ee169f8cff4d7ec72f0588fc68e33e
1 /* memory allocation routines with error checking.
2 Copyright 1989, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
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.
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.
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., 51 Franklin Street - Fifth Floor,
18 Boston, MA 02110-1301, USA. */
22 @deftypefn Replacement void* xmalloc (size_t)
24 Allocate memory without fail. If @code{malloc} fails, this will print
25 a message to @code{stderr} (using the name set by
26 @code{xmalloc_set_program_name},
27 if any) and then call @code{xexit}. Note that it is therefore safe for
28 a program to contain @code{#define malloc xmalloc} in its source.
30 @end deftypefn
32 @deftypefn Replacement void* xrealloc (void *@var{ptr}, size_t @var{size})
33 Reallocate memory without fail. This routine functions like @code{realloc},
34 but will behave the same as @code{xmalloc} if memory cannot be found.
36 @end deftypefn
38 @deftypefn Replacement void* xcalloc (size_t @var{nelem}, size_t @var{elsize})
40 Allocate memory without fail, and set it to zero. This routine functions
41 like @code{calloc}, but will behave the same as @code{xmalloc} if memory
42 cannot be found.
44 @end deftypefn
46 @deftypefn Replacement void xmalloc_set_program_name (const char *@var{name})
48 You can use this to set the name of the program used by
49 @code{xmalloc_failed} when printing a failure message.
51 @end deftypefn
53 @deftypefn Replacement void xmalloc_failed (size_t)
55 This function is not meant to be called by client code, and is listed
56 here for completeness only. If any of the allocation routines fail, this
57 function will be called to print an error message and terminate execution.
59 @end deftypefn
63 #ifdef HAVE_CONFIG_H
64 #include "config.h"
65 #endif
66 #include "ansidecl.h"
67 #include "libiberty.h"
69 #include <stdio.h>
71 #include <stddef.h>
73 #ifdef __APPLE__
74 #include <crt_externs.h>
75 #endif
77 #if VMS
78 #include <stdlib.h>
79 #include <unixlib.h>
80 #else
81 /* For systems with larger pointers than ints, these must be declared. */
82 # if HAVE_STDLIB_H && HAVE_UNISTD_H && HAVE_DECL_MALLOC \
83 && HAVE_DECL_REALLOC && HAVE_DECL_CALLOC && HAVE_DECL_SBRK
84 # include <stdlib.h>
85 # include <unistd.h>
86 # else
87 # ifdef __cplusplus
88 extern "C" {
89 # endif /* __cplusplus */
90 void *malloc (size_t);
91 void *realloc (void *, size_t);
92 void *calloc (size_t, size_t);
93 void *sbrk (ptrdiff_t);
94 # ifdef __cplusplus
96 # endif /* __cplusplus */
97 # endif /* HAVE_STDLIB_H ... */
98 #endif /* VMS */
100 /* The program name if set. */
101 static const char *name = "";
103 #ifdef HAVE_SBRK
104 /* The initial sbrk, set when the program name is set. Not used for win32
105 ports other than cygwin32. */
106 static char *first_break = NULL;
107 #endif /* HAVE_SBRK */
109 void
110 xmalloc_set_program_name (const char *s)
112 name = s;
113 #ifdef HAVE_SBRK
114 /* Win32 ports other than cygwin32 don't have brk() */
115 if (first_break == NULL)
116 first_break = (char *) sbrk (0);
117 #endif /* HAVE_SBRK */
120 void
121 xmalloc_failed (size_t size)
123 #ifdef HAVE_SBRK
124 #ifdef __APPLE__
125 #define environ (*_NSGetEnviron())
126 #else
127 extern char **environ;
128 #endif
129 size_t allocated;
131 if (first_break != NULL)
132 allocated = (char *) sbrk (0) - first_break;
133 else
134 allocated = (char *) sbrk (0) - (char *) &environ;
135 fprintf (stderr,
136 "\n%s%sout of memory allocating %lu bytes after a total of %lu bytes\n",
137 name, *name ? ": " : "",
138 (unsigned long) size, (unsigned long) allocated);
139 #else /* HAVE_SBRK */
140 fprintf (stderr,
141 "\n%s%sout of memory allocating %lu bytes\n",
142 name, *name ? ": " : "",
143 (unsigned long) size);
144 #endif /* HAVE_SBRK */
145 xexit (1);
149 xmalloc (size_t size)
151 PTR newmem;
153 if (size == 0)
154 size = 1;
155 newmem = malloc (size);
156 if (!newmem)
157 xmalloc_failed (size);
159 return (newmem);
163 xcalloc (size_t nelem, size_t elsize)
165 PTR newmem;
167 if (nelem == 0 || elsize == 0)
168 nelem = elsize = 1;
170 newmem = calloc (nelem, elsize);
171 if (!newmem)
172 xmalloc_failed (nelem * elsize);
174 return (newmem);
178 xrealloc (PTR oldmem, size_t size)
180 PTR newmem;
182 if (size == 0)
183 size = 1;
184 if (!oldmem)
185 newmem = malloc (size);
186 else
187 newmem = realloc (oldmem, size);
188 if (!newmem)
189 xmalloc_failed (size);
191 return (newmem);