Import upstream version 1.28
[debian/tar] / gnu / realloc.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* realloc() function that is glibc compatible.
4
5    Copyright (C) 1997, 2003-2004, 2006-2007, 2009-2014 Free Software
6    Foundation, Inc.
7
8    This program is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 /* written by Jim Meyering and Bruno Haible */
22
23 #define _GL_USE_STDLIB_ALLOC 1
24 #include <config.h>
25
26 /* Only the AC_FUNC_REALLOC macro defines 'realloc' already in config.h.  */
27 #ifdef realloc
28 # define NEED_REALLOC_GNU 1
29 /* Whereas the gnulib module 'realloc-gnu' defines HAVE_REALLOC_GNU.  */
30 #elif GNULIB_REALLOC_GNU && !HAVE_REALLOC_GNU
31 # define NEED_REALLOC_GNU 1
32 #endif
33
34 /* Infer the properties of the system's malloc function.
35    The gnulib module 'malloc-gnu' defines HAVE_MALLOC_GNU.  */
36 #if GNULIB_MALLOC_GNU && HAVE_MALLOC_GNU
37 # define SYSTEM_MALLOC_GLIBC_COMPATIBLE 1
38 #endif
39
40 #include <stdlib.h>
41
42 #include <errno.h>
43
44 /* Change the size of an allocated block of memory P to N bytes,
45    with error checking.  If N is zero, change it to 1.  If P is NULL,
46    use malloc.  */
47
48 void *
49 rpl_realloc (void *p, size_t n)
50 {
51   void *result;
52
53 #if NEED_REALLOC_GNU
54   if (n == 0)
55     {
56       n = 1;
57
58       /* In theory realloc might fail, so don't rely on it to free.  */
59       free (p);
60       p = NULL;
61     }
62 #endif
63
64   if (p == NULL)
65     {
66 #if GNULIB_REALLOC_GNU && !NEED_REALLOC_GNU && !SYSTEM_MALLOC_GLIBC_COMPATIBLE
67       if (n == 0)
68         n = 1;
69 #endif
70       result = malloc (n);
71     }
72   else
73     result = realloc (p, n);
74
75 #if !HAVE_REALLOC_POSIX
76   if (result == NULL)
77     errno = ENOMEM;
78 #endif
79
80   return result;
81 }