man-pages/man3/malloc_hook.3

135 lines
3.3 KiB
Groff
Raw Normal View History

2004-11-03 13:51:07 +00:00
.\" Copyright 2002 Walter Harms (walter.harms@informatik.uni-oldenburg.de)
.\" Distributed under GPL
.\" Heavily based on glibc documentation
.\" Polished, added docs, removed glibc doc bug, 2002-07-20, aeb
2008-07-09 04:54:57 +00:00
.\"
.\" FIXME
.\" According to a Fedora downstream patch, malloc hooks are deprecated
.\" https://bugzilla.redhat.com/show_bug.cgi?id=450187
2009-02-20 11:19:53 +00:00
.\" Integrate this upstream?
2004-11-03 13:51:07 +00:00
.TH MALLOC_HOOK 3 2002-07-20 "GNU" "Linux Programmer's Manual"
.SH NAME
__malloc_hook, __malloc_initialize_hook,
__memalign_hook, __free_hook, __realloc_hook,
2004-11-03 13:51:07 +00:00
__after_morecore_hook \- malloc debugging variables
.SH SYNOPSIS
2007-12-23 08:20:39 +00:00
.nf
2007-09-20 16:26:31 +00:00
.B "#include <malloc.h>"
2004-11-03 13:51:07 +00:00
.sp
2007-12-23 08:20:39 +00:00
.BI "void *(*__malloc_hook)(size_t " size ", const void *" caller );
2004-11-03 13:51:07 +00:00
.sp
2007-12-23 08:20:39 +00:00
.BI "void *(*__realloc_hook)(void *" ptr ", size_t " size \
", const void *" caller );
2004-11-03 13:51:07 +00:00
.sp
2007-12-23 08:20:39 +00:00
.BI "void *(*__memalign_hook)(size_t " alignment ", size_t " size ,
.BI " const void *" caller );
2004-11-03 13:51:07 +00:00
.sp
2007-12-23 08:20:39 +00:00
.BI "void (*__free_hook)(void *" ptr ", const void *" caller );
2004-11-03 13:51:07 +00:00
.sp
2007-09-20 16:26:31 +00:00
.B "void (*__malloc_initialize_hook)(void);"
2004-11-03 13:51:07 +00:00
.sp
2007-09-20 16:26:31 +00:00
.B "void (*__after_morecore_hook)(void);"
2007-12-23 08:20:39 +00:00
.fi
2004-11-03 13:51:07 +00:00
.SH DESCRIPTION
The GNU C library lets you modify the behavior of
.BR malloc (3),
.BR realloc (3),
2004-11-03 13:51:07 +00:00
and
.BR free (3)
by specifying appropriate hook functions.
You can use these hooks
2004-11-03 13:51:07 +00:00
to help you debug programs that use dynamic memory allocation,
for example.
.LP
The variable
.B __malloc_initialize_hook
points at a function that is called once when the malloc implementation
is initialized.
This is a weak variable, so it can be overridden in
2004-11-03 13:51:07 +00:00
the application with a definition like the following:
.nf
2004-11-03 13:51:07 +00:00
void (*__malloc_initialize_hook)(void) = my_init_hook;
2004-11-03 13:51:07 +00:00
.fi
Now the function
.IR my_init_hook ()
can do the initialization of all hooks.
.LP
The four functions pointed to by
.BR __malloc_hook ,
.BR __realloc_hook ,
.BR __memalign_hook ,
2007-09-20 16:26:31 +00:00
.B __free_hook
2004-11-03 13:51:07 +00:00
have a prototype like the functions
.BR malloc (3),
.BR realloc (3),
.BR memalign (3),
.BR free (3),
2004-11-03 13:51:07 +00:00
respectively, except that they have a final argument
.I caller
that gives the address of the caller of
.BR malloc (3),
2004-11-03 13:51:07 +00:00
etc.
.LP
The variable
.B __after_morecore_hook
points at a function that is called each time after
.BR sbrk (2)
2005-06-22 07:19:03 +00:00
was asked for more memory.
.SH "CONFORMING TO"
These functions are GNU extensions.
2004-11-03 13:51:07 +00:00
.SH "EXAMPLE"
2005-06-22 07:19:03 +00:00
Here is a short example of how to use these variables.
2004-11-03 13:51:07 +00:00
.sp
.nf
#include <stdio.h>
#include <malloc.h>
2004-11-03 13:51:07 +00:00
/* Prototypes for our hooks. */
static void my_init_hook(void);
static void *my_malloc_hook(size_t, const void *);
/* Variables to save original hooks. */
static void *(*old_malloc_hook)(size_t, const void *);
/* Override initializing hook from the C library. */
2004-11-03 13:51:07 +00:00
void (*__malloc_initialize_hook) (void) = my_init_hook;
static void
my_init_hook(void)
2007-04-05 12:36:57 +00:00
{
2004-11-03 13:51:07 +00:00
old_malloc_hook = __malloc_hook;
__malloc_hook = my_malloc_hook;
}
static void *
my_malloc_hook(size_t size, const void *caller)
2007-04-05 12:36:57 +00:00
{
2004-11-03 13:51:07 +00:00
void *result;
/* Restore all old hooks */
__malloc_hook = old_malloc_hook;
/* Call recursively */
2007-04-05 12:36:57 +00:00
result = malloc(size);
2004-11-03 13:51:07 +00:00
/* Save underlying hooks */
old_malloc_hook = __malloc_hook;
/* printf() might call malloc(), so protect it too. */
2007-04-05 12:36:57 +00:00
printf("malloc(%u) called from %p returns %p\\n",
(unsigned int) size, caller, result);
2004-11-03 13:51:07 +00:00
/* Restore our own hooks */
__malloc_hook = my_malloc_hook;
return result;
}
.fi
.SH "SEE ALSO"
.BR mallinfo (3),
.BR malloc (3),
.BR mcheck (3),
.BR mtrace (3)