1 .\" Copyright 2002 Walter Harms (walter.harms@informatik.uni-oldenburg.de)
3 .\" SPDX-License-Identifier: GPL-1.0-or-later
5 .\" Heavily based on glibc documentation
6 .\" Polished, added docs, removed glibc doc bug, 2002-07-20, aeb
8 .TH __malloc_hook 3 (date) "Linux man-pages (unreleased)"
10 __malloc_hook, __malloc_initialize_hook,
11 __memalign_hook, __free_hook, __realloc_hook,
12 __after_morecore_hook \- malloc debugging variables (DEPRECATED)
15 .RI ( libc ", " \-lc )
18 .B "#include <malloc.h>"
20 .BI "void *(*volatile __malloc_hook)(size_t " size ", const void *" caller );
22 .BI "void *(*volatile __realloc_hook)(void *" ptr ", size_t " size ,
23 .BI " const void *" caller );
25 .BI "void *(*volatile __memalign_hook)(size_t " alignment ", size_t " size ,
26 .BI " const void *" caller );
28 .BI "void (*volatile __free_hook)(void *" ptr ", const void *" caller );
30 .B "void (*__malloc_initialize_hook)(void);"
32 .B "void (*volatile __after_morecore_hook)(void);"
35 The GNU C library lets you modify the behavior of
40 by specifying appropriate hook functions.
41 You can use these hooks
42 to help you debug programs that use dynamic memory allocation,
46 .B __malloc_initialize_hook
47 points at a function that is called once when the malloc implementation
49 This is a weak variable, so it can be overridden in
50 the application with a definition like the following:
54 void (*__malloc_initialize_hook)(void) = my_init_hook;
60 can do the initialization of all hooks.
62 The four functions pointed to by
67 have a prototype like the functions
72 respectively, except that they have a final argument
74 that gives the address of the caller of
79 .B __after_morecore_hook
80 points at a function that is called each time after
82 was asked for more memory.
86 The use of these hook functions is not safe in multithreaded programs,
87 and they are now deprecated.
88 From glibc 2.24 onwards, the
89 .B __malloc_initialize_hook
90 variable has been removed from the API,
91 and from glibc 2.34 onwards, all
92 the hook variables have been removed from the API.
93 .\" https://bugzilla.redhat.com/show_bug.cgi?id=450187
94 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=9957
95 Programmers should instead preempt calls to the relevant functions
96 by defining and exporting
103 Here is a short example of how to use these variables.
109 /* Prototypes for our hooks */
110 static void my_init_hook(void);
111 static void *my_malloc_hook(size_t, const void *);
113 /* Variables to save original hooks */
114 static void *(*old_malloc_hook)(size_t, const void *);
116 /* Override initializing hook from the C library */
117 void (*__malloc_initialize_hook)(void) = my_init_hook;
122 old_malloc_hook = __malloc_hook;
123 __malloc_hook = my_malloc_hook;
127 my_malloc_hook(size_t size, const void *caller)
131 /* Restore all old hooks */
132 __malloc_hook = old_malloc_hook;
134 /* Call recursively */
135 result = malloc(size);
137 /* Save underlying hooks */
138 old_malloc_hook = __malloc_hook;
140 /* printf() might call malloc(), so protect it too */
141 printf("malloc(%zu) called from %p returns %p\en",
142 size, caller, result);
144 /* Restore our own hooks */
145 __malloc_hook = my_malloc_hook;