/******************************************************************************

    MODULE
	malloc.c

    DESCRIPTION
	replacements for malloc/strdup/free
	do achieve some mungwall like behaviour

    HISTORY
	23-11-94 b_boll created
	$Log: malloc.c $

******************************************************************************/

/**************************************
	      Includes
**************************************/

#include <stdlib.h>
#include <string.h>

/**************************************
      Internal Defines & Structures
**************************************/

struct Header {
    int size;
    char *file;
    int line;
}; /* struct Header */
	  /*
findr ˜ ( )
	    */
#define HEADER sizeof (struct Header)
#define FOOTER 8

#define NEW	0xD0
#define DEAD	0xDE
#define END	0xED

/**************************************
	     Implementation
**************************************/



void *xmalloc (int size, char *file, int line)
{
    struct Header *rv;

    if ((rv = malloc(size + HEADER + FOOTER))) {
	rv->size = size;
	rv->file = file;
	rv->line = line;
	rv ++;
	{
	    unsigned char *x;
	    int i;
	    x = (void *)rv;

	    for (i = size;   i; --i, ++x)
		*x = NEW;

	    for (i = FOOTER; i; --i, ++x)
		*x = END;
	}
    } /* if */
    return rv;
} /* xmalloc */

char *xstrdup (char *str, char *file, int line)
{
    char *pstr;
    if ((pstr = xmalloc (strlen (str) + 1, file, line))) {
	strcpy (pstr, str);
    } /* if */
    return pstr;
} /* xstrdup */

void xfree (void *ptr, char *file, int line)
{
    if (ptr) {
	struct Header *rv;
	rv = ptr;
	--rv;
	{
	    unsigned char *x;
	    int   i;

	    x = ptr;
	    for (i = rv->size; i; --i, ++x)
		*x = DEAD;
	}
	free  (rv);
    } /* if */
} /* xfree */




/******************************************************************************
*****  END malloc.c
******************************************************************************/
