/
MudOS_0.9.19/bin/
MudOS_0.9.19/doc/concepts/
MudOS_0.9.19/doc/driver/
MudOS_0.9.19/doc/efuns/bitstrings/
MudOS_0.9.19/doc/efuns/buffers/
MudOS_0.9.19/doc/efuns/communication/
MudOS_0.9.19/doc/efuns/core/
MudOS_0.9.19/doc/efuns/mappings/
MudOS_0.9.19/doc/efuns/math/
MudOS_0.9.19/doc/efuns/security/
MudOS_0.9.19/doc/lpc/constructs/
MudOS_0.9.19/doc/lpc/types/
MudOS_0.9.19/doc/platforms/
MudOS_0.9.19/etc/
MudOS_0.9.19/mudlib/
MudOS_0.9.19/mudlib/lil/
MudOS_0.9.19/mudlib/lil/clone/
MudOS_0.9.19/mudlib/lil/command/
MudOS_0.9.19/mudlib/lil/data/
MudOS_0.9.19/mudlib/lil/etc/
MudOS_0.9.19/mudlib/lil/include/
MudOS_0.9.19/mudlib/lil/inherit/
MudOS_0.9.19/mudlib/lil/inherit/master/
MudOS_0.9.19/mudlib/lil/log/
MudOS_0.9.19/mudlib/lil/single/
MudOS_0.9.19/mudlib/lil/u/
MudOS_0.9.19/src/testsuite/
MudOS_0.9.19/src/testsuite/clone/
MudOS_0.9.19/src/testsuite/command/
MudOS_0.9.19/src/testsuite/data/
MudOS_0.9.19/src/testsuite/etc/
MudOS_0.9.19/src/testsuite/include/
MudOS_0.9.19/src/testsuite/inherit/
MudOS_0.9.19/src/testsuite/inherit/master/
MudOS_0.9.19/src/testsuite/log/
MudOS_0.9.19/src/testsuite/single/
MudOS_0.9.19/src/testsuite/single/efuns/
MudOS_0.9.19/src/testsuite/u/
/*
   wrapper functions for system malloc -- keep malloc stats.
   Truilkan@TMI - 92/04/17
*/

/* config.h gets INLINE */
#include "config.h"
#include "lint.h"
#include "interpret.h"
#ifdef NeXT
#include <stdlib.h>
#endif

#include "md.h"

extern unsigned int total_malloced;
extern unsigned int hiwater;

typedef struct stats_s {
	unsigned int free_calls, alloc_calls, realloc_calls;
} stats_t;

static stats_t stats;

void debugmalloc_init()
{
	stats.free_calls = 0;
	stats.alloc_calls = 0;
	stats.realloc_calls = 0;
	MDinit();
}

INLINE void *debugrealloc(ptr,size,tag,desc)
void *ptr;
int size;
int tag;
char *desc;
{
	void *tmp;

	stats.realloc_calls++;
	tmp = (node_t *)ptr - 1;
	if (MDfree(tmp)) {
		tmp = (void *)realloc(tmp,size + sizeof(node_t));
		MDmalloc(tmp, size, tag, desc);
		return (node_t *)tmp + 1;
	}
	return (void *)0;
}

INLINE void *debugmalloc(size, tag, desc)
int size;
int tag;
char *desc;
{
	void *tmp;

	stats.alloc_calls++;
	tmp = (void *)malloc(size + sizeof(node_t));
	MDmalloc(tmp, size, tag, desc);
	return (node_t *)tmp + 1;
}

INLINE void *debugcalloc(nitems,size,tag,desc)
int nitems;
int size;
int tag;
char *desc;
{
	void *tmp;

	stats.alloc_calls++;
	tmp = (void *)calloc(nitems * size + sizeof(node_t), 1);
	MDmalloc(tmp, nitems * size, tag, desc);
	return (node_t *)tmp + 1;
}

INLINE void debugfree(ptr)
void *ptr;
{
	void *tmp;

	stats.free_calls++;
	tmp = (node_t *)ptr - 1;
	if (MDfree(tmp)) {
		free(tmp);  /* only free if safe to do so */
	}
}

void dump_malloc_data()
{
	int net;

	net = stats.alloc_calls - stats.free_calls;
	add_message("using debug malloc:\n\n");
	add_message("total malloc'd:   %10lu\n", total_malloced);
	add_message("high water mark:  %10lu\n", hiwater);
	add_message("overhead:         %10lu\n",
		(TABLESIZE * sizeof(node_t *)) + (net * sizeof(node_t)));
	add_message("#alloc calls:     %10lu\n",stats.alloc_calls);
	add_message("#free calls:      %10lu\n",stats.free_calls);
	add_message("#alloc - #free:   %10lu\n", net);
	add_message("#realloc calls:   %10lu\n",stats.realloc_calls);
}