1MALLOC_HOOK(3)             Linux Programmer's Manual            MALLOC_HOOK(3)
2
3
4

NAME

6       __malloc_hook,  __malloc_initialize_hook, __memalign_hook, __free_hook,
7       __realloc_hook, __after_morecore_hook - malloc debugging variables
8

SYNOPSIS

10       #include <malloc.h>
11
12       void *(*volatile __malloc_hook)(size_t size, const void *caller);
13
14       void *(*volatile __realloc_hook)(void *ptr, size_t size, const void *caller);
15
16       void *(*volatile __memalign_hook)(size_t alignment, size_t size,
17                                const void *caller);
18
19       void (*volatile __free_hook)(void *ptr, const void *caller);
20
21       void (*__malloc_initialize_hook)(void);
22
23       void (*volatile __after_morecore_hook)(void);
24

DESCRIPTION

26       The GNU C library lets you modify  the  behavior  of  malloc(3),  real‐
27       loc(3),  and free(3) by specifying appropriate hook functions.  You can
28       use these hooks to help you debug programs that use dynamic memory  al‐
29       location, for example.
30
31       The  variable  __malloc_initialize_hook  points  at  a function that is
32       called once when the malloc implementation is initialized.  This  is  a
33       weak  variable, so it can be overridden in the application with a defi‐
34       nition like the following:
35
36           void (*__malloc_initialize_hook)(void) = my_init_hook;
37
38       Now the function my_init_hook() can do the initialization of all hooks.
39
40       The four functions pointed to by __malloc_hook, __realloc_hook, __mema‐
41       lign_hook,  __free_hook  have a prototype like the functions malloc(3),
42       realloc(3), memalign(3), free(3), respectively, except that they have a
43       final  argument  caller  that  gives  the address of the caller of mal‐
44       loc(3), etc.
45
46       The variable __after_morecore_hook points at a function that is  called
47       each time after sbrk(2) was asked for more memory.
48

CONFORMING TO

50       These functions are GNU extensions.
51

NOTES

53       The  use of these hook functions is not safe in multithreaded programs,
54       and they are now deprecated.   From  glibc  2.24  onwards,  the  __mal‐
55       loc_initialize_hook  variable  has been removed from the API.  Program‐
56       mers should instead preempt calls to the relevant functions by defining
57       and exporting functions such as "malloc" and "free".
58

EXAMPLES

60       Here is a short example of how to use these variables.
61
62       #include <stdio.h>
63       #include <malloc.h>
64
65       /* Prototypes for our hooks */
66       static void my_init_hook(void);
67       static void *my_malloc_hook(size_t, const void *);
68
69       /* Variables to save original hooks */
70       static void *(*old_malloc_hook)(size_t, const void *);
71
72       /* Override initializing hook from the C library */
73       void (*__malloc_initialize_hook)(void) = my_init_hook;
74
75       static void
76       my_init_hook(void)
77       {
78           old_malloc_hook = __malloc_hook;
79           __malloc_hook = my_malloc_hook;
80       }
81
82       static void *
83       my_malloc_hook(size_t size, const void *caller)
84       {
85           void *result;
86
87           /* Restore all old hooks */
88           __malloc_hook = old_malloc_hook;
89
90           /* Call recursively */
91           result = malloc(size);
92
93           /* Save underlying hooks */
94           old_malloc_hook = __malloc_hook;
95
96           /* printf() might call malloc(), so protect it too */
97           printf("malloc(%zu) called from %p returns %p\n",
98                   size, caller, result);
99
100           /* Restore our own hooks */
101           __malloc_hook = my_malloc_hook;
102
103           return result;
104       }
105

SEE ALSO

107       mallinfo(3), malloc(3), mcheck(3), mtrace(3)
108

COLOPHON

110       This  page  is  part of release 5.12 of the Linux man-pages project.  A
111       description of the project, information about reporting bugs,  and  the
112       latest     version     of     this    page,    can    be    found    at
113       https://www.kernel.org/doc/man-pages/.
114
115
116
117GNU                               2021-03-22                    MALLOC_HOOK(3)
Impressum