Blame gl/realloc.c

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