6

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

Edmund
  • 10,533
  • 3
  • 39
  • 57

5 Answers5

6

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

John Safranek
  • 743
  • 5
  • 10
6

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
  • 1,104
  • 2
  • 11
  • 25
  • what does ` old_malloc_hook = __malloc_hook;` mean? I am confused. We set `__malloc_hook` to `old_malloc_hook` before, why we save `__malloc_hook` to `old_malloc_hook` again? My question is here http://stackoverflow.com/questions/11356958/how-to-use-malloc-hook/11357013#comment14960880_11357013 – Fei Xue Jul 06 '12 at 09:50
  • David Schwartz mentioned in his answer to http://stackoverflow.com/questions/11356958/how-to-use-malloc-hook that saving the original __malloc_hook is important so that it can be restored just before the original malloc() is called. That's the line just below the comment /* Restore all old hooks */. I'm guessing that in this specific case, it's unnecessary, since the original malloc hook is null, but to be safe it should be done. It must be save in all three of the "my_" functions. In my_malloc and my_free, it must be done just before calling the system's malloc and free functions. – Randy Stegbauer Jul 06 '12 at 14:27
1

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

1

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 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.

Community
  • 1
  • 1
Andrew Stein
  • 12,880
  • 5
  • 35
  • 43
-3
#undef malloc
#define malloc my_malloc

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

Graeme Perrow
  • 56,086
  • 21
  • 82
  • 121
Alex Gartrell
  • 2,514
  • 5
  • 22
  • 23
  • 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? –  Nov 06 '08 at 18:09
  • 1
    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 Nov 06 '08 at 18:13
  • #undefine is not a valid C preprocesser directive, it's #undef – philant Nov 06 '08 at 19:51
  • 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 Dec 13 '09 at 07:10