1 .\" Copyright 2002 Walter Harms (walter.harms@informatik.uni-oldenburg.de)
3 .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
4 .\" Distributed under GPL
7 .\" Heavily based on glibc documentation
8 .\" Polished, added docs, removed glibc doc bug, 2002-07-20, aeb
10 .TH MALLOC_HOOK 3 2016-07-17 "GNU" "Linux Programmer's Manual"
12 __malloc_hook, __malloc_initialize_hook,
13 __memalign_hook, __free_hook, __realloc_hook,
14 __after_morecore_hook \- malloc debugging variables
17 .B "#include <malloc.h>"
19 .BI "void *(*__malloc_hook)(size_t " size ", const void *" caller );
21 .BI "void *(*__realloc_hook)(void *" ptr ", size_t " size \
22 ", const void *" caller );
24 .BI "void *(*__memalign_hook)(size_t " alignment ", size_t " size ,
25 .BI " const void *" caller );
27 .BI "void (*__free_hook)(void *" ptr ", const void *" caller );
29 .B "void (*__malloc_initialize_hook)(void);"
31 .B "void (*__after_morecore_hook)(void);"
34 The GNU C library lets you modify the behavior of
39 by specifying appropriate hook functions.
40 You can use these hooks
41 to help you debug programs that use dynamic memory allocation,
45 .B __malloc_initialize_hook
46 points at a function that is called once when the malloc implementation
48 This is a weak variable, so it can be overridden in
49 the application with a definition like the following:
52 void (*__malloc_initialize_hook)(void) = my_init_hook;
57 can do the initialization of all hooks.
59 The four functions pointed to by
64 have a prototype like the functions
69 respectively, except that they have a final argument
71 that gives the address of the caller of
76 .B __after_morecore_hook
77 points at a function that is called each time after
79 was asked for more memory.
81 These functions are GNU extensions.
83 The use of these hook functions is not safe in multithreaded programs,
84 and they are now deprecated.
85 From glibc 2.24 onwards, the
86 .B __malloc_initialize_hook
87 variable has been removed from the API.
88 .\" https://bugzilla.redhat.com/show_bug.cgi?id=450187
89 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=9957
90 Programmers should instead preempt calls to the relevant functions
91 by defining and exporting functions such as "malloc" and "free".
93 Here is a short example of how to use these variables.
99 /* Prototypes for our hooks. */
100 static void my_init_hook(void);
101 static void *my_malloc_hook(size_t, const void *);
103 /* Variables to save original hooks. */
104 static void *(*old_malloc_hook)(size_t, const void *);
106 /* Override initializing hook from the C library. */
107 void (*__malloc_initialize_hook) (void) = my_init_hook;
112 old_malloc_hook = __malloc_hook;
113 __malloc_hook = my_malloc_hook;
117 my_malloc_hook(size_t size, const void *caller)
121 /* Restore all old hooks */
122 __malloc_hook = old_malloc_hook;
124 /* Call recursively */
125 result = malloc(size);
127 /* Save underlying hooks */
128 old_malloc_hook = __malloc_hook;
130 /* printf() might call malloc(), so protect it too. */
131 printf("malloc(%u) called from %p returns %p\\n",
132 (unsigned int) size, caller, result);
134 /* Restore our own hooks */
135 __malloc_hook = my_malloc_hook;