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