Blame src/gl/realloc.c

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