#!/bin/bash
#
# $Id: memleak.sh,v 1.2 2002/07/26 01:39:45 darkmoon Exp $
#
# Script used to trace memleaks - with conjuction of 'xmem.c' debug mode
# I know it's not ideal one, but better than nothing :)
#
# Author: Michal Safranek
#

if [ $# -lt 1 ]; then
	echo "Usage: memleak.sh logfile [outfile]"
	exit 1
fi
if [ $# -gt 1 ]; then
	TMPFILE=$2
else
	TMPFILE=`mktemp /tmp/memleak.XXXXXX`
fi
S1='s!<D>xmalloc:Allocating [0-9]* bytes \.\.\. !+!'
S2='s!<D>xrealloc:Reallocating \(.*\) to [0-9]* bytes \.\.\. !-\1N+!'
S3='s!<D>xfree:Freeing memory at \(.*\) \.\.\.!-\1!'
sed "$S1;$S2;$S3" $1 | tr 'N' '\n' > $TMPFILE
ALL=`grep + $TMPFILE|wc -l`
FRE=`grep - $TMPFILE|wc -l`
if [ $ALL != $FRE ]; then
	echo "Memleak detected! >> allocated: $ALL freed: $FRE"
else
	echo "Memleak NOT detected :)"
fi
if [ ! $# -gt 1 ]; then
	rm -f $TMPFILE
fi

