Blame lib/realloc.c

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