[comp.sys.amiga.tech] Memory Tracing Source

jojo@astroatc.UUCP (Jon Wesener) (09/22/88)

	Since a number of people thought it was relevant, here's the
source to the memory tracing routines I've been using.  If you
have any problems with it let me know.  This was done under Manx 3.6
but should be easily ported to Lattice.  

end of speech,
--j

# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# Wrapped by astroatc!jojo on Wed Sep 21 13:38:51 CDT 1988
# Contents:  README mem.h mem.c
 
echo x - README
sed 's/^@//' > "README" <<'@//E*O*F README//'
	These routines will help debug memory allocation and freeing
in your programs.  These routines will complain if you try and
free memory you didn't allocate and later will complain about
memory not freed when your program finishes.  The complaint will
tell the file, function and line # of the offensive memory call.

	To use these routines, include mem.h in all of your source
files.  Then compile mem.c and link your program with mem.o.
Use AllocMem() and FreeMem() as normal but add the routine call to
freeall() before your program exits in order to get a list of
memory that wasn't free'd up.

	I used this under Manx 3.6 with the +L option, but it should
work with 16 bit ints as well.

	To turn on the memory trace, define MEMTRACE at compile time
with the -D option, (-DMEMTRACE), and make sure that all the files
get compiled with this option.  Do NOT include mem.h in mem.c as
it will break things big time.

Happy hacking,
--j
@//E*O*F README//
chmod u=rw,g=rw,o=r README
 
echo x - mem.h
sed 's/^@//' > "mem.h" <<'@//E*O*F mem.h//'
/*
 * mem.h -- this file contains macros to take over AllocMem() and
 *	FreeMem();
 * (c)1988 Jojo Wesener
 * NOTE: do not include this file in mem.c
 */

#ifdef MEMTRACE

#define AllocMem(amt,type)	alloctrack(amt,type,__FILE__,__FUNC__,__LINE__)
#define FreeMem(addr,amt)	freetrack(addr,amt,__FILE__,__FUNC__,__LINE__)

extern char * alloctrack();

#endif MEMTRACE
@//E*O*F mem.h//
chmod u=rw,g=rw,o=r mem.h
 
echo x - mem.c
sed 's/^@//' > "mem.c" <<'@//E*O*F mem.c//'
/*
 * mem.c - This file contains the routines which trace memory allocations
 * (c)1988 Jojo Wesener
 */

#include <stdio.h>
/*
 * use FreeMem() and AllocMem() as usual and then call freeall()
 * when your program exits.
 */

#ifdef MEMTRACE

struct membkt {
	char * addr;
	long amt;
	long type;
	char * func, * file;
	long line;
	struct membkt * next;
};

typedef struct membkt membkt_t;

membkt_t * Mem_list = NULL;

char * malloc();

membkt_t *
get_bkt()
{
	return (membkt_t *)malloc(sizeof(membkt_t));
}

put_bkt(bkt)
	membkt_t *bkt;
{
	free( (char *)bkt );
}

membkt_t *
find_bkt(addr)
	char * addr;
{
	membkt_t * bkt, *pre;

	bkt = Mem_list;

	for( ; bkt != NULL ; bkt = bkt->next )
		if( bkt->addr == addr )
			break;

	if( bkt == NULL )
		return NULL;

	/* remove it from the list */
	if( bkt == Mem_list ) {
		Mem_list = Mem_list->next;
		return bkt;
	}

	for( pre = Mem_list ; pre->next != bkt ; pre = pre->next );

	pre->next = bkt->next;

	return bkt;
}

store_bkt( bkt )
	membkt_t *bkt;
{
	bkt->next = Mem_list;
	Mem_list = bkt;
}

char *
alloctrack(amt,type,file,func,line)
	long amt;
	long type;
	char * file, * func;
	long line;
{
	char * mem;
	membkt_t * bkt;

	mem = (char *)AllocMem(amt,type);

	if( mem  == NULL )
		return mem;

	if( (bkt = get_bkt()) == NULL ) {
		printf("alloctrack: unable to alloc bucket\n");
		exit( 1 );
	}

	bkt->addr = mem;
	bkt->amt = amt;
	bkt->type = type;
	bkt->file = file;
	bkt->func = func;
	bkt->line = line;

	store_bkt( bkt );

	return mem;
}

freetrack(addr,amt,file,func,line)
	char * addr;
	long amt;
	char * file, * func, * line;
{
	membkt_t * bkt;

	if( (bkt = find_bkt( addr )) == NULL ) {
		printf("freetrack: error %s, %s, %d, 0x%x -- 0x%x\n",file,func,line,
			addr,amt);
		return;
	}

	(void)FreeMem(addr,amt);

	put_bkt(bkt);
}

freeall()
{
	membkt_t *bkt;

	printf("freeall:\n");
	for( bkt = Mem_list; bkt != NULL ; bkt = bkt->next ) {
		(void)FreeMem(bkt->addr, bkt->amt);
		printf("\t%s, %s, %d, 0x%x --0x%x --0x%x\n",bkt->file,bkt->func,
			bkt->line, bkt->addr, bkt->amt, bkt->type);
		put_bkt( bkt );
	}
	printf("end freeall\n");
}
#endif MEMTRACE

/* Lint Output
mem.c:
*/
@//E*O*F mem.c//
chmod u=rw,g=rw,o=r mem.c
 
exit 0
-- 
... {seismo | harvard } ! {uwvax | cs.wisc.edu} ! astroatc!jojo
    "They weren't just any women...  They were women fresh from Hell, with
	wisps of devil-smoke curling behind their ears and unholy vapors hiding 
	in their tight leather dresses, looking for Mr. Right."