/*
 *   Copyright (C) 1988-2006 Lucent Technologies Inc. and others.
 *   All Rights Reserved.
 */

/* memory allocation functions */

#include <stdio.h>
#if !BSD
#include <string.h>
#endif

extern	char	*argv0;	/* command name (must be set in main function) */

#if defined( __STDC__ ) || defined( __BORLANDC__ )

#ifdef DMALLOC
#include "dmalloc.h"
#else
#include <stdlib.h>
#endif

char	*stralloc(char *s);
char	*mymalloc(unsigned size);
char	*mycalloc(unsigned nelem, unsigned size);
char	*myrealloc(char *p, unsigned size);
static char	*alloctest(char *p);
char	*mycalloc(), *mymalloc(), *myrealloc(), *stralloc();
#else
char	*mycalloc(), *mymalloc(), *myrealloc(), *stralloc();
char	*alloctest();
char	*calloc(), *malloc(), *realloc(), *strcpy();
void	exit();
#endif

/* allocate a string */

char *
stralloc(s)
char	*s;
{
	return(strcpy(mymalloc((unsigned) strlen(s) + 1), s));
}

/* version of malloc that only returns if successful */

char *
mymalloc(size)
unsigned size;
{
	return(alloctest(malloc(size)));
}

/* version of calloc that only returns if successful */

char *
mycalloc(nelem, size)
unsigned nelem, size;
{
	return(alloctest(calloc(nelem, size)));
}

/* version of realloc that only returns if successful */

char *
myrealloc(p, size)
char	*p;
unsigned size;
{
	return(alloctest(realloc(p, size)));
}

/* check for memory allocation failure */

static	char *
alloctest(p)
char	*p;
{
	if (p == NULL) {
		(void) fprintf(stderr, "\n%s: out of storage\n", argv0);
		exit(1);
	}
	return(p);
}
