2N/A/* realloc() function that is glibc compatible.
2N/A
2N/A Copyright (C) 1997, 2003-2004, 2006-2007, 2009-2010 Free Software
2N/A Foundation, Inc.
2N/A
2N/A This program is free software: you can redistribute it and/or modify
2N/A it under the terms of the GNU General Public License as published by
2N/A the Free Software Foundation; either version 3 of the License, or
2N/A (at your option) any later version.
2N/A
2N/A This program is distributed in the hope that it will be useful,
2N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A GNU General Public License for more details.
2N/A
2N/A You should have received a copy of the GNU General Public License
2N/A along with this program. If not, see <http://www.gnu.org/licenses/>. */
2N/A
2N/A/* written by Jim Meyering and Bruno Haible */
2N/A
2N/A#include <config.h>
2N/A
2N/A/* Only the AC_FUNC_REALLOC macro defines 'realloc' already in config.h. */
2N/A#ifdef realloc
2N/A# define NEED_REALLOC_GNU 1
2N/A/* Whereas the gnulib module 'realloc-gnu' defines HAVE_REALLOC_GNU. */
2N/A#elif GNULIB_REALLOC_GNU && !HAVE_REALLOC_GNU
2N/A# define NEED_REALLOC_GNU 1
2N/A#endif
2N/A
2N/A/* Infer the properties of the system's malloc function.
2N/A The gnulib module 'malloc-gnu' defines HAVE_MALLOC_GNU. */
2N/A#if GNULIB_MALLOC_GNU && HAVE_MALLOC_GNU
2N/A# define SYSTEM_MALLOC_GLIBC_COMPATIBLE 1
2N/A#endif
2N/A
2N/A/* Below we want to call the system's malloc and realloc.
2N/A Undefine the symbols here so that including <stdlib.h> provides a
2N/A declaration of malloc(), not of rpl_malloc(), and likewise for realloc. */
2N/A#undef malloc
2N/A#undef realloc
2N/A
2N/A/* Specification. */
2N/A#include <stdlib.h>
2N/A
2N/A#include <errno.h>
2N/A
2N/A/* Below we want to call the system's malloc and realloc.
2N/A Undefine the symbols, if they were defined by gnulib's <stdlib.h>
2N/A replacement. */
2N/A#undef malloc
2N/A#undef realloc
2N/A
2N/A/* Change the size of an allocated block of memory P to N bytes,
2N/A with error checking. If N is zero, change it to 1. If P is NULL,
2N/A use malloc. */
2N/A
2N/Avoid *
2N/Arpl_realloc (void *p, size_t n)
2N/A{
2N/A void *result;
2N/A
2N/A#if NEED_REALLOC_GNU
2N/A if (n == 0)
2N/A {
2N/A n = 1;
2N/A
2N/A /* In theory realloc might fail, so don't rely on it to free. */
2N/A free (p);
2N/A p = NULL;
2N/A }
2N/A#endif
2N/A
2N/A if (p == NULL)
2N/A {
2N/A#if GNULIB_REALLOC_GNU && !NEED_REALLOC_GNU && !SYSTEM_MALLOC_GLIBC_COMPATIBLE
2N/A if (n == 0)
2N/A n = 1;
2N/A#endif
2N/A result = malloc (n);
2N/A }
2N/A else
2N/A result = realloc (p, n);
2N/A
2N/A#if !HAVE_REALLOC_POSIX
2N/A if (result == NULL)
2N/A errno = ENOMEM;
2N/A#endif
2N/A
2N/A return result;
2N/A}