This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: How to tell compiler to use my_malloc inplace of normal 'malloc'
>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