Blame gnulib/lib/xmalloc.c

Packit 15a5a8
/* xmalloc.c -- malloc with out of memory checking
Packit 15a5a8
Packit 15a5a8
   Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
Packit 15a5a8
   1999, 2000, 2002, 2003, 2004, 2005, 2006 Free Software Foundation,
Packit 15a5a8
   Inc.
Packit 15a5a8
Packit 15a5a8
   This program is free software; you can redistribute it and/or modify
Packit 15a5a8
   it under the terms of the GNU General Public License as published by
Packit 15a5a8
   the Free Software Foundation; either version 2, or (at your option)
Packit 15a5a8
   any later version.
Packit 15a5a8
Packit 15a5a8
   This program is distributed in the hope that it will be useful,
Packit 15a5a8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 15a5a8
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 15a5a8
   GNU General Public License for more details.
Packit 15a5a8
Packit 15a5a8
   You should have received a copy of the GNU General Public License
Packit 15a5a8
   along with this program; if not, write to the Free Software Foundation,
Packit 15a5a8
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
Packit 15a5a8
Packit 15a5a8
#include <config.h>
Packit 15a5a8
Packit 15a5a8
#if ! HAVE_INLINE
Packit 15a5a8
# define static_inline
Packit 15a5a8
#endif
Packit 15a5a8
#include "xalloc.h"
Packit 15a5a8
#undef static_inline
Packit 15a5a8
Packit 15a5a8
#include <stdlib.h>
Packit 15a5a8
#include <string.h>
Packit 15a5a8
Packit 15a5a8
#ifndef SIZE_MAX
Packit 15a5a8
# define SIZE_MAX ((size_t) -1)
Packit 15a5a8
#endif
Packit 15a5a8
Packit 15a5a8
/* 1 if calloc is known to be compatible with GNU calloc.  This
Packit 15a5a8
   matters if we are not also using the calloc module, which defines
Packit 15a5a8
   HAVE_CALLOC and supports the GNU API even on non-GNU platforms.  */
Packit 15a5a8
#if defined HAVE_CALLOC || defined __GLIBC__
Packit 15a5a8
enum { HAVE_GNU_CALLOC = 1 };
Packit 15a5a8
#else
Packit 15a5a8
enum { HAVE_GNU_CALLOC = 0 };
Packit 15a5a8
#endif
Packit 15a5a8
Packit 15a5a8
/* Allocate N bytes of memory dynamically, with error checking.  */
Packit 15a5a8
Packit 15a5a8
void *
Packit 15a5a8
xmalloc (size_t n)
Packit 15a5a8
{
Packit 15a5a8
  void *p = malloc (n);
Packit 15a5a8
  if (!p && n != 0)
Packit 15a5a8
    xalloc_die ();
Packit 15a5a8
  return p;
Packit 15a5a8
}
Packit 15a5a8
Packit 15a5a8
/* Change the size of an allocated block of memory P to N bytes,
Packit 15a5a8
   with error checking.  */
Packit 15a5a8
Packit 15a5a8
void *
Packit 15a5a8
xrealloc (void *p, size_t n)
Packit 15a5a8
{
Packit 15a5a8
  p = realloc (p, n);
Packit 15a5a8
  if (!p && n != 0)
Packit 15a5a8
    xalloc_die ();
Packit 15a5a8
  return p;
Packit 15a5a8
}
Packit 15a5a8
Packit 15a5a8
/* If P is null, allocate a block of at least *PN bytes; otherwise,
Packit 15a5a8
   reallocate P so that it contains more than *PN bytes.  *PN must be
Packit 15a5a8
   nonzero unless P is null.  Set *PN to the new block's size, and
Packit 15a5a8
   return the pointer to the new block.  *PN is never set to zero, and
Packit 15a5a8
   the returned pointer is never null.  */
Packit 15a5a8
Packit 15a5a8
void *
Packit 15a5a8
x2realloc (void *p, size_t *pn)
Packit 15a5a8
{
Packit 15a5a8
  return x2nrealloc (p, pn, 1);
Packit 15a5a8
}
Packit 15a5a8
Packit 15a5a8
/* Allocate S bytes of zeroed memory dynamically, with error checking.
Packit 15a5a8
   There's no need for xnzalloc (N, S), since it would be equivalent
Packit 15a5a8
   to xcalloc (N, S).  */
Packit 15a5a8
Packit 15a5a8
void *
Packit 15a5a8
xzalloc (size_t s)
Packit 15a5a8
{
Packit 15a5a8
  return memset (xmalloc (s), 0, s);
Packit 15a5a8
}
Packit 15a5a8
Packit 15a5a8
/* Allocate zeroed memory for N elements of S bytes, with error
Packit 15a5a8
   checking.  S must be nonzero.  */
Packit 15a5a8
Packit 15a5a8
void *
Packit 15a5a8
xcalloc (size_t n, size_t s)
Packit 15a5a8
{
Packit 15a5a8
  void *p;
Packit 15a5a8
  /* Test for overflow, since some calloc implementations don't have
Packit 15a5a8
     proper overflow checks.  But omit overflow and size-zero tests if
Packit 15a5a8
     HAVE_GNU_CALLOC, since GNU calloc catches overflow and never
Packit 15a5a8
     returns NULL if successful.  */
Packit 15a5a8
  if ((! HAVE_GNU_CALLOC && xalloc_oversized (n, s))
Packit 15a5a8
      || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0)))
Packit 15a5a8
    xalloc_die ();
Packit 15a5a8
  return p;
Packit 15a5a8
}
Packit 15a5a8
Packit 15a5a8
/* Clone an object P of size S, with error checking.  There's no need
Packit 15a5a8
   for xnmemdup (P, N, S), since xmemdup (P, N * S) works without any
Packit 15a5a8
   need for an arithmetic overflow check.  */
Packit 15a5a8
Packit 15a5a8
void *
Packit 15a5a8
xmemdup (void const *p, size_t s)
Packit 15a5a8
{
Packit 15a5a8
  return memcpy (xmalloc (s), p, s);
Packit 15a5a8
}
Packit 15a5a8
Packit 15a5a8
/* Clone STRING.  */
Packit 15a5a8
Packit 15a5a8
char *
Packit 15a5a8
xstrdup (char const *string)
Packit 15a5a8
{
Packit 15a5a8
  return xmemdup (string, strlen (string) + 1);
Packit 15a5a8
}