Blame lib/xmalloc.c

Packit 33f14e
/* xmalloc.c -- malloc with out of memory checking
Packit 33f14e
Packit 33f14e
   Copyright (C) 1990-2000, 2002-2006, 2008-2017 Free Software Foundation, Inc.
Packit 33f14e
Packit 33f14e
   This program is free software: you can redistribute it and/or modify
Packit 33f14e
   it under the terms of the GNU General Public License as published by
Packit 33f14e
   the Free Software Foundation; either version 3 of the License, or
Packit 33f14e
   (at your option) any later version.
Packit 33f14e
Packit 33f14e
   This program is distributed in the hope that it will be useful,
Packit 33f14e
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 33f14e
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 33f14e
   GNU General Public License for more details.
Packit 33f14e
Packit 33f14e
   You should have received a copy of the GNU General Public License
Packit 33f14e
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 33f14e
Packit 33f14e
#include <config.h>
Packit 33f14e
Packit 33f14e
#define XALLOC_INLINE _GL_EXTERN_INLINE
Packit 33f14e
Packit 33f14e
#include "xalloc.h"
Packit 33f14e
Packit 33f14e
#include <stdlib.h>
Packit 33f14e
#include <string.h>
Packit 33f14e
Packit 33f14e
/* 1 if calloc is known to be compatible with GNU calloc.  This
Packit 33f14e
   matters if we are not also using the calloc module, which defines
Packit 33f14e
   HAVE_CALLOC_GNU and supports the GNU API even on non-GNU platforms.  */
Packit 33f14e
#if defined HAVE_CALLOC_GNU || (defined __GLIBC__ && !defined __UCLIBC__)
Packit 33f14e
enum { HAVE_GNU_CALLOC = 1 };
Packit 33f14e
#else
Packit 33f14e
enum { HAVE_GNU_CALLOC = 0 };
Packit 33f14e
#endif
Packit 33f14e
Packit 33f14e
/* Allocate N bytes of memory dynamically, with error checking.  */
Packit 33f14e
Packit 33f14e
void *
Packit 33f14e
xmalloc (size_t n)
Packit 33f14e
{
Packit 33f14e
  void *p = malloc (n);
Packit 33f14e
  if (!p && n != 0)
Packit 33f14e
    xalloc_die ();
Packit 33f14e
  return p;
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
/* Change the size of an allocated block of memory P to N bytes,
Packit 33f14e
   with error checking.  */
Packit 33f14e
Packit 33f14e
void *
Packit 33f14e
xrealloc (void *p, size_t n)
Packit 33f14e
{
Packit 33f14e
  if (!n && p)
Packit 33f14e
    {
Packit 33f14e
      /* The GNU and C99 realloc behaviors disagree here.  Act like
Packit 33f14e
         GNU, even if the underlying realloc is C99.  */
Packit 33f14e
      free (p);
Packit 33f14e
      return NULL;
Packit 33f14e
    }
Packit 33f14e
Packit 33f14e
  p = realloc (p, n);
Packit 33f14e
  if (!p && n)
Packit 33f14e
    xalloc_die ();
Packit 33f14e
  return p;
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
/* If P is null, allocate a block of at least *PN bytes; otherwise,
Packit 33f14e
   reallocate P so that it contains more than *PN bytes.  *PN must be
Packit 33f14e
   nonzero unless P is null.  Set *PN to the new block's size, and
Packit 33f14e
   return the pointer to the new block.  *PN is never set to zero, and
Packit 33f14e
   the returned pointer is never null.  */
Packit 33f14e
Packit 33f14e
void *
Packit 33f14e
x2realloc (void *p, size_t *pn)
Packit 33f14e
{
Packit 33f14e
  return x2nrealloc (p, pn, 1);
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
/* Allocate S bytes of zeroed memory dynamically, with error checking.
Packit 33f14e
   There's no need for xnzalloc (N, S), since it would be equivalent
Packit 33f14e
   to xcalloc (N, S).  */
Packit 33f14e
Packit 33f14e
void *
Packit 33f14e
xzalloc (size_t s)
Packit 33f14e
{
Packit 33f14e
  return memset (xmalloc (s), 0, s);
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
/* Allocate zeroed memory for N elements of S bytes, with error
Packit 33f14e
   checking.  S must be nonzero.  */
Packit 33f14e
Packit 33f14e
void *
Packit 33f14e
xcalloc (size_t n, size_t s)
Packit 33f14e
{
Packit 33f14e
  void *p;
Packit 33f14e
  /* Test for overflow, since objects with size greater than
Packit 33f14e
     PTRDIFF_MAX cause pointer subtraction to go awry.  Omit size-zero
Packit 33f14e
     tests if HAVE_GNU_CALLOC, since GNU calloc never returns NULL if
Packit 33f14e
     successful.  */
Packit 33f14e
  if (xalloc_oversized (n, s)
Packit 33f14e
      || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0)))
Packit 33f14e
    xalloc_die ();
Packit 33f14e
  return p;
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
/* Clone an object P of size S, with error checking.  There's no need
Packit 33f14e
   for xnmemdup (P, N, S), since xmemdup (P, N * S) works without any
Packit 33f14e
   need for an arithmetic overflow check.  */
Packit 33f14e
Packit 33f14e
void *
Packit 33f14e
xmemdup (void const *p, size_t s)
Packit 33f14e
{
Packit 33f14e
  return memcpy (xmalloc (s), p, s);
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
/* Clone STRING.  */
Packit 33f14e
Packit 33f14e
char *
Packit 33f14e
xstrdup (char const *string)
Packit 33f14e
{
Packit 33f14e
  return xmemdup (string, strlen (string) + 1);
Packit 33f14e
}