tags:

views:

1764

answers:

7
+4  Q: 

Using Malloc Hooks

I am trying to use a malloc hook to create a custom function my_malloc(). In my main program when I call malloc() I want it to call my_malloc() can someone please give me an example on how to do this in C

A: 
#undef malloc
#define malloc my_malloc

Just put that at the top of any of the files you need to do it for.

Alex Gartrell
so if i create a include file my_malloc.c and include it into my main program and just do that it will call my_malloc instead of malloc?
This will only work for calls directly to malloc() in that file. Any malloc calls in functions in other files (or in a library that you don't control) will not be affected. If you do this, however, you should probably have a my_realloc as well.
Graeme Perrow
#undefine is not a valid C preprocesser directive, it's #undef
philippe
For good reason you could prefer #define malloc(s) my_malloc(s,...), such as #define malloc(s) my_malloc(s, __FILE__, __LINE__) and similar. Also using the macro with arguments allows one to call (malloc)(s) in those cases where one does desire the underlying symbol.
Heath Hunnicutt
+4  A: 

Did you look here? http://www.gnu.org/software/libtool/manual/libc/Hooks-for-Malloc.html

J. John
+1  A: 

If your function calls sbrk directly you can simply call it malloc and make sure it gets linked in before the library's malloc. This works on all Unix type OSes. For windows see http://stackoverflow.com/questions/120627/is-there-a-way-to-redefine-malloc-at-link-time-on-windows

If your function is a wrapper around the library's malloc, the #define suggestion by Alex will work.

Andrew Stein
+3  A: 

According to http://www.gnu.org/software/libtool/manual/libc/Hooks-for-Malloc.html, here's how to do this with the GCC libraries.

/* Prototypes for __malloc_hook, __free_hook */
 #include <malloc.h>

 /* Prototypes for our hooks.  */
 static void my_init_hook (void);
 static void *my_malloc_hook (size_t, const void *);
 static void my_free_hook (void*, const void *);

 /* Override initializing hook from the C library. */
 void (*__malloc_initialize_hook) (void) = my_init_hook;

 static void
 my_init_hook (void)
 {
   old_malloc_hook = __malloc_hook;
   old_free_hook = __free_hook;
   __malloc_hook = my_malloc_hook;
   __free_hook = my_free_hook;
 }

 static void *
 my_malloc_hook (size_t size, const void *caller)
 {
   void *result;
   /* Restore all old hooks */
   __malloc_hook = old_malloc_hook;
   __free_hook = old_free_hook;
   /* Call recursively */
   result = malloc (size);
   /* Save underlying hooks */
   old_malloc_hook = __malloc_hook;
   old_free_hook = __free_hook;
   /* printf might call malloc, so protect it too. */
   printf ("malloc (%u) returns %p\n", (unsigned int) size, result);
   /* Restore our own hooks */
   __malloc_hook = my_malloc_hook;
   __free_hook = my_free_hook;
   return result;
 }

 static void
 my_free_hook (void *ptr, const void *caller)
 {
   /* Restore all old hooks */
   __malloc_hook = old_malloc_hook;
   __free_hook = old_free_hook;
   /* Call recursively */
   free (ptr);
   /* Save underlying hooks */
   old_malloc_hook = __malloc_hook;
   old_free_hook = __free_hook;
   /* printf might call free, so protect it too. */
   printf ("freed pointer %p\n", ptr);
   /* Restore our own hooks */
   __malloc_hook = my_malloc_hook;
   __free_hook = my_free_hook;
 }

 main ()
 {
   ...
 }
Randy Stegbauer
A: 

This question looks identical to this: http://stackoverflow.com/questions/262439/create-a-wrapper-function-for-malloc-and-free-in-c

dmityugov
A: 

Very interesting thread, I need to define my own allocation, deallocation routines for a small project at work. This has to be in C++.

I hope my question is not out of topic here. Question: how to find out if my C++ compiler implements new and delete through malloc and free? Then it would be enough for me to intercept malloc and free. I just need to trace all allocation / deallocation operations in my program.

Thanks for any information. Giorgio

Create an entirely new question. You're not likely to get much help while piggybacking on this one.
Steven Schlansker
+1  A: 

Just note that my_malloc_hook() solution not really works in mutlithreaded program - see http://www.phpman.info/index.php/man/malloc%5Fhook/3.

Pesah Spector