/* xalloc.c - memory allocation with error checking.
   Copyright (C) 2000, 2001, 2002, 2003 Cris van Pelt
   Copyright (C) 2003, 2004 Peter Palfrader

   Distributed under the terms of the Artistic License.
*/

#include <stdio.h>
#include <string.h>

#include <config.h>
#include <deborphan.h>

#ifdef USE_XALLOC		/* xalloc can be disabled in config.h */

#include <xalloc.h>
#include <stdlib.h>
#include <errno.h>

void *
xrealloc(void *ptr, size_t size)
{
    void *t;
    if (!(t = realloc(ptr, size)))
	error(EXIT_FAILURE, errno, "xrealloc: %p", t);
    return t;
}

void *
xcalloc(size_t nmemb, size_t size)
{
    void *t;

    if (!(t = calloc(nmemb, size)))
	error(EXIT_FAILURE, errno, "xcalloc: %p", t);

    return t;
}

void *
xmalloc(size_t size)
{
    void *t;

    if (!(t = malloc(size)))
	error(EXIT_FAILURE, errno, "xmalloc: %p", t);

    return t;
}

char *
xstrdup(const char *s)
{
    char *t;

    if ((t = strdup(s)) == NULL)
	error(EXIT_FAILURE, errno, "xstrdup: %p", (void *)t);

    return t;
}

#endif /* USE_XALLOC */
