Blame gl/realloc.c

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