public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: How to tell compiler to use my_malloc inplace of normal 'malloc'
@ 2001-08-23  7:45 Mynampati, Venkata S.
  2001-08-23  8:38 ` John Love-Jensen
  0 siblings, 1 reply; 7+ messages in thread
From: Mynampati, Venkata S. @ 2001-08-23  7:45 UTC (permalink / raw)
  To: 'Ryan T. Sammartino'; +Cc: 'gcc-help@gcc.gnu.org'

> 
> 
> 
> #define malloc my_malloc
 
   If i use this, then where ever i use malloc, it gets
   replaced by my_malloc.
   what i want is this:
    my_malloc()
    {
		/* do my own stuff */
		malloc();
     } 
  and i want my_malloc to be substituted in place of all malloc
  calls.
  I am thinking that this could be done without changing all the
  files, except for Makefiles.

  Can i do this or is my understanding is wrong.

> or, on the command-line, gcc ... "-Dmalloc=my_malloc"
> 
> 
> 
> * Mynampati, Venkata S. <MynamVS@nsc-msg01.network.com> 
> [22/08/2001 14:43]:
> > Hi,
> > My question is a bit confusing, but it pertains to what i 
> read sometime
> > back, which is like this:
> > I read that while compiling i can tell the compiler to 
> replace all mallocs
> > with my wrapper function called 'my_malloc', in which, i do 
> something
> > and call malloc. I remember reading that i can use this 
> facility to keep
> > track of whose using malloc and how many number of times.
> > i.e where ever 'malloc' is used, 'my_malloc' gets substituted.
> > 
> > Could you helping me how to achieve this using gcc? 
> > [or may be i dreamt up this one]
> > 
> > Best Regards
> > Venkat
> > 
> > 
> > Worry about chances you miss when you don't even try.
> >   
> > 
> 
> -- 
> Ryan T. Sammartino
> http://members.home.net/ryants/
> To communicate is the beginning of understanding.
> 		-- AT&T

 Thanks and regards,
Venkat 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to tell compiler to use my_malloc inplace of normal 'malloc'
  2001-08-23  7:45 How to tell compiler to use my_malloc inplace of normal 'malloc' Mynampati, Venkata S.
@ 2001-08-23  8:38 ` John Love-Jensen
  0 siblings, 0 replies; 7+ messages in thread
From: John Love-Jensen @ 2001-08-23  8:38 UTC (permalink / raw)
  To: gcc-help

>what i want is this:
>    my_malloc()
>    {
>        /* do my own stuff */
>        malloc();
>     }

Oh, that's different.  But not too different.

You can still define your own malloc, and within your malloc you can dlopen
and dladdr the libc.so malloc function and have a function pointer
explicitly access the standard C malloc routine.

(That's using a shared object library (.so) as a DSO "dynamic shared object"
instead of the more common SSO "static shared object".  Something I've done
with Apache modules.)

Another technique I've seen used is extracting the malloc routine from the
libc.a, intentionally munging the binary object (say into malloc -->
MaLlOc), and linking in the swizzled malloc.o

// my malloc
void* malloc(size_t size)
{
     /* do my own stuff */
     return MaLlOc(size); /* the real libc malloc */
}

Not for the faint of heart.  You may also want to proxy/harness realloc,
calloc, valloc, alloca, memalign, and free.  Depending on the side-effects
of "do my own stuff", such as maintaining correct state if you are tracking
allocations/deallocations for memory leaks.

Sincerely,
--Eljay


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to tell compiler to use my_malloc inplace of normal 'malloc'
  2001-08-22 14:42 Mynampati, Venkata S.
  2001-08-22 15:12 ` Ryan T. Sammartino
@ 2001-08-23 23:39 ` Alexandre Oliva
  1 sibling, 0 replies; 7+ messages in thread
From: Alexandre Oliva @ 2001-08-23 23:39 UTC (permalink / raw)
  To: Mynampati, Venkata S.; +Cc: 'gcc-help@gcc.gnu.org'

On Aug 22, 2001, "Mynampati, Venkata S." <MynamVS@nsc-msg01.network.com> wrote:

> I read that while compiling i can tell the compiler to replace all mallocs
> with my wrapper function called 'my_malloc', in which, i do something
> and call malloc.

You can do this if you're using the GNU linker: it accepts
-wrap=malloc, so you define _wrap_malloc and call _real_malloc() in it
whenever you mean to call the actual malloc.  Of course, you only use
this at link time.

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                  aoliva@{cygnus.com, redhat.com}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist    *Please* write to mailing lists, not to me

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to tell compiler to use my_malloc inplace of normal 'malloc'
  2001-08-23  8:04 Mirko Vogel
@ 2001-08-23  8:26 ` John Love-Jensen
  0 siblings, 0 replies; 7+ messages in thread
From: John Love-Jensen @ 2001-08-23  8:26 UTC (permalink / raw)
  To: gcc-help

Just define your malloc function as "malloc", with the exact same C
signature.  Your .o will preferential link in lieu of the libc.so malloc or
lib.a malloc.

extern "C" void* malloc(size_t size);

// My malloc.
void* malloc(size_t size)
{
    // ...my own heap management routines using sbrk and such...
}

--Eljay

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to tell compiler to use my_malloc inplace of normal 'malloc'
@ 2001-08-23  8:04 Mirko Vogel
  2001-08-23  8:26 ` John Love-Jensen
  0 siblings, 1 reply; 7+ messages in thread
From: Mirko Vogel @ 2001-08-23  8:04 UTC (permalink / raw)
  To: gcc-help

Hello,

why don't you compile the files you want to use my_alloc
with -Dmalloc=my_malloc and the rest without:

gcc -c file1.c file2.c -Dmalloc=my_malloc
gcc -c my_alloc.c
gcc file1.o file2.o my_alloc.o

You said, you just want to replace the malloc-*calls*, so I assume, the
string "alloc" appears otherwise, too. Perhaps -Dmalloc(=my_malloc( will
work?

But thats a really ugly solution...

Mirko Vogel



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to tell compiler to use my_malloc inplace of normal 'malloc'
  2001-08-22 14:42 Mynampati, Venkata S.
@ 2001-08-22 15:12 ` Ryan T. Sammartino
  2001-08-23 23:39 ` Alexandre Oliva
  1 sibling, 0 replies; 7+ messages in thread
From: Ryan T. Sammartino @ 2001-08-22 15:12 UTC (permalink / raw)
  To: Mynampati, Venkata S.; +Cc: 'gcc-help@gcc.gnu.org'

#define malloc my_malloc


or, on the command-line, gcc ... "-Dmalloc=my_malloc"



* Mynampati, Venkata S. <MynamVS@nsc-msg01.network.com> [22/08/2001 14:43]:
> Hi,
> My question is a bit confusing, but it pertains to what i read sometime
> back, which is like this:
> I read that while compiling i can tell the compiler to replace all mallocs
> with my wrapper function called 'my_malloc', in which, i do something
> and call malloc. I remember reading that i can use this facility to keep
> track of whose using malloc and how many number of times.
> i.e where ever 'malloc' is used, 'my_malloc' gets substituted.
> 
> Could you helping me how to achieve this using gcc? 
> [or may be i dreamt up this one]
> 
> Best Regards
> Venkat
> 
> 
> Worry about chances you miss when you don't even try.
>   
> 

-- 
Ryan T. Sammartino
http://members.home.net/ryants/
To communicate is the beginning of understanding.
		-- AT&T

^ permalink raw reply	[flat|nested] 7+ messages in thread

* How to tell compiler to use my_malloc inplace of normal 'malloc'
@ 2001-08-22 14:42 Mynampati, Venkata S.
  2001-08-22 15:12 ` Ryan T. Sammartino
  2001-08-23 23:39 ` Alexandre Oliva
  0 siblings, 2 replies; 7+ messages in thread
From: Mynampati, Venkata S. @ 2001-08-22 14:42 UTC (permalink / raw)
  To: 'gcc-help@gcc.gnu.org'

Hi,
My question is a bit confusing, but it pertains to what i read sometime
back, which is like this:
I read that while compiling i can tell the compiler to replace all mallocs
with my wrapper function called 'my_malloc', in which, i do something
and call malloc. I remember reading that i can use this facility to keep
track of whose using malloc and how many number of times.
i.e where ever 'malloc' is used, 'my_malloc' gets substituted.

Could you helping me how to achieve this using gcc? 
[or may be i dreamt up this one]

Best Regards
Venkat


Worry about chances you miss when you don't even try.
  

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2001-08-23 23:39 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-23  7:45 How to tell compiler to use my_malloc inplace of normal 'malloc' Mynampati, Venkata S.
2001-08-23  8:38 ` John Love-Jensen
  -- strict thread matches above, loose matches on Subject: below --
2001-08-23  8:04 Mirko Vogel
2001-08-23  8:26 ` John Love-Jensen
2001-08-22 14:42 Mynampati, Venkata S.
2001-08-22 15:12 ` Ryan T. Sammartino
2001-08-23 23:39 ` Alexandre Oliva

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).