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