Go to the first, previous, next, last section, table of contents.


Storage Allocation Hooks

The GNU C library lets you modify the behavior of malloc, realloc, and free by specifying appropriate hook functions. You can use these hooks to help you debug programs that use dynamic storage allocation, for example.

The hook variables are declared in `malloc.h'.

Variable: __malloc_hook
The value of this variable is a pointer to function that malloc uses whenever it is called. You should define this function to look like malloc; that is, like:

void *function (size_t size, void *caller)

The value of caller is the return address found on the stack when the malloc function was called. This value allows to trace the memory consumption of the program.

Variable: __realloc_hook
The value of this variable is a pointer to function that realloc uses whenever it is called. You should define this function to look like realloc; that is, like:

void *function (void *ptr, size_t size, void *caller)

The value of caller is the return address found on the stack when the realloc function was called. This value allows to trace the memory consumption of the program.

Variable: __free_hook
The value of this variable is a pointer to function that free uses whenever it is called. You should define this function to look like free; that is, like:

void function (void *ptr, void *caller)

The value of caller is the return address found on the stack when the free function was called. This value allows to trace the memory consumption of the program.

Variable: __memalign_hook
The value of this variable is a pointer to function that memalign uses whenever it is called. You should define this function to look like memalign; that is, like:

void *function (size_t size, size_t alignment)

You must make sure that the function you install as a hook for one of these functions does not call that function recursively without restoring the old value of the hook first! Otherwise, your program will get stuck in an infinite recursion. Before calling the function recursively, one should make sure to restore all the hooks to their previous value. When coming back from the recursive call, all the hooks should be resaved since a hook might modify itself.

Here is an example showing how to use __malloc_hook and __free_hook properly. It installs a function that prints out information every time malloc or free is called. We just assume here that realloc and memalign are not used in our program.

/* Global variables used to hold underlaying hook values.  */
static void *(*old_malloc_hook) (size_t);
static void (*old_free_hook) (void*);

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

static void *
my_malloc_hook (size_t size)
{
  void *result;
  /* Restore all old hooks */
  __malloc_hook = old_malloc_hook;
  __free_hook = old_free_hook;
  /* Call recursively */
  result = malloc (size);
  /* Save underlaying 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)
{
  /* Restore all old hooks */
  __malloc_hook = old_malloc_hook;
  __free_hook = old_free_hook;
  /* Call recursively */
  free (ptr);
  /* Save underlaying 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 ()
{
  ...
  old_malloc_hook = __malloc_hook;
  old_free_hook = __free_hook;
  __malloc_hook = my_malloc_hook;
  __free_hook = my_free_hook;
  ...
}

The mcheck function (see section Heap Consistency Checking) works by installing such hooks.


Go to the first, previous, next, last section, table of contents.