1 /* xrealloc.c: realloc with error checking.
2 
3    Copyright 1992, 1993, 2008, 2010, 2013 Karl Berry.
4    Copyright 2005 Olaf Weber.
5 
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10 
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15 
16    You should have received a copy of the GNU Lesser General Public License
17    along with this library; if not, see <http://www.gnu.org/licenses/>.  */
18 
19 #include <kpathsea/config.h>
20 
21 void *
xrealloc(void * old_ptr,size_t size)22 xrealloc (void *old_ptr, size_t size)
23 {
24     void *new_mem;
25 
26     if (old_ptr == NULL) {
27         new_mem = xmalloc(size);
28     } else {
29         new_mem = (void *)realloc(old_ptr, size ? size : 1);
30         if (new_mem == NULL) {
31             /* We used to print OLD_PTR here using %x, and casting its
32                value to unsigned, but that lost on the Alpha, where
33                pointers and unsigned had different sizes.  Since the info
34                is of little or no value anyway, just don't print it.  */
35             fprintf(stderr,
36                     "fatal: memory exhausted (realloc of %lu bytes).\n",
37                     (unsigned long)size);
38             exit(EXIT_FAILURE);
39         }
40     }
41 
42     return new_mem;
43 }
44