[comp.lang.c] Need help monitoring malloc

rtidd@ccels3.mitre.org (Randy Tidd) (01/18/91)

In the application i'm writing, i'm running into some serious
performance problems I think are related to wasted memory. I'm trying
to track down bugs along the lines of:

int i;
char *s;

for(i=0; i<10; i++)
  s = (char *) malloc(1024);
free(s);

What I was thinking is making a front end to malloc() and free(), so
all calls to malloc() would actually call my routine and my routine
would in turn call the malloc() that lives in libc.a. I tried to play
games with the loader and separate compilation but couldn't get it to
work. I *don't* want to write my own malloc() routines, besides that
would result in different performance problems and the addition of
more variables.

I am using SunOS 4.1 and both the native cc and gcc-1.38. I noticed a
couple semi-useful routines in SunOS, such as mallocmap() and
malloc_debug(), but I can't seem to tailor these to what I need.

Does anyone have any suggestions? Please reply through e-mail.

Thanks!

Randy Tidd
rtidd@mwunix.mitre.org
"Never mind what's been selling, it's what you're buying."

res@cbnews.att.com (Robert E. Stampfli) (01/21/91)

>What I was thinking is making a front end to malloc() and free(), so
>all calls to malloc() would actually call my routine and my routine
>would in turn call the malloc() that lives in libc.a.

Have you tried compiling your program with "-Dmalloc=Mymalloc -Dfree=Myfree"
on the compile line?  Obviously, you leave this off the step that compiles
your liaison code -- the actual instance of Mymalloc() and Myfree().
-- 
Rob Stampfli		614-860-4268 (work)	614-864-9377 (home)
kd8wk@n8jyv.oh (ham)	stampfli@att.com	osu-cis!kd8wk!res

drh@duke.cs.duke.edu (D. Richard Hipp) (01/21/91)

In article <1991Jan20.185009.18694@cbnews.att.com> res@cbnews.att.com (Robert E. Stampfli) writes:
>>What I was thinking is making a front end to malloc() and free(), so
>>all calls to malloc() would actually call my routine and my routine
>>would in turn call the malloc() that lives in libc.a.
>
>Have you tried compiling your program with "-Dmalloc=Mymalloc -Dfree=Myfree"
>on the compile line?  Obviously, you leave this off the step that compiles
>your liaison code -- the actual instance of Mymalloc() and Myfree().

An alternative would be to include the following lines at the beginning
of the file containing your liaison code:

#ifdef malloc
#undef malloc
#endif

#ifdef free
#undef free
#endif

This way, the "-Dmalloc=MyMalloc" defines could be specified with the
CFLAGS macro in your makefile.