1__malloc_hook(3) Library Functions Manual __malloc_hook(3)
2
3
4
6 __malloc_hook, __malloc_initialize_hook, __memalign_hook, __free_hook,
7 __realloc_hook, __after_morecore_hook - malloc debugging variables
8 (DEPRECATED)
9
11 Standard C library (libc, -lc)
12
14 #include <malloc.h>
15
16 void *(*volatile __malloc_hook)(size_t size, const void *caller);
17
18 void *(*volatile __realloc_hook)(void *ptr, size_t size,
19 const void *caller);
20
21 void *(*volatile __memalign_hook)(size_t alignment, size_t size,
22 const void *caller);
23
24 void (*volatile __free_hook)(void *ptr, const void *caller);
25
26 void (*__malloc_initialize_hook)(void);
27
28 void (*volatile __after_morecore_hook)(void);
29
31 The GNU C library lets you modify the behavior of malloc(3), real‐
32 loc(3), and free(3) by specifying appropriate hook functions. You can
33 use these hooks to help you debug programs that use dynamic memory al‐
34 location, for example.
35
36 The variable __malloc_initialize_hook points at a function that is
37 called once when the malloc implementation is initialized. This is a
38 weak variable, so it can be overridden in the application with a defi‐
39 nition like the following:
40
41 void (*__malloc_initialize_hook)(void) = my_init_hook;
42
43 Now the function my_init_hook() can do the initialization of all hooks.
44
45 The four functions pointed to by __malloc_hook, __realloc_hook, __mema‐
46 lign_hook, __free_hook have a prototype like the functions malloc(3),
47 realloc(3), memalign(3), free(3), respectively, except that they have a
48 final argument caller that gives the address of the caller of mal‐
49 loc(3), etc.
50
51 The variable __after_morecore_hook points at a function that is called
52 each time after sbrk(2) was asked for more memory.
53
55 GNU.
56
58 The use of these hook functions is not safe in multithreaded programs,
59 and they are now deprecated. From glibc 2.24 onwards, the __mal‐
60 loc_initialize_hook variable has been removed from the API, and from
61 glibc 2.34 onwards, all the hook variables have been removed from the
62 API. Programmers should instead preempt calls to the relevant func‐
63 tions by defining and exporting malloc(), free(), realloc(), and cal‐
64 loc().
65
67 Here is a short example of how to use these variables.
68
69 #include <stdio.h>
70 #include <malloc.h>
71
72 /* Prototypes for our hooks */
73 static void my_init_hook(void);
74 static void *my_malloc_hook(size_t, const void *);
75
76 /* Variables to save original hooks */
77 static void *(*old_malloc_hook)(size_t, const void *);
78
79 /* Override initializing hook from the C library */
80 void (*__malloc_initialize_hook)(void) = my_init_hook;
81
82 static void
83 my_init_hook(void)
84 {
85 old_malloc_hook = __malloc_hook;
86 __malloc_hook = my_malloc_hook;
87 }
88
89 static void *
90 my_malloc_hook(size_t size, const void *caller)
91 {
92 void *result;
93
94 /* Restore all old hooks */
95 __malloc_hook = old_malloc_hook;
96
97 /* Call recursively */
98 result = malloc(size);
99
100 /* Save underlying hooks */
101 old_malloc_hook = __malloc_hook;
102
103 /* printf() might call malloc(), so protect it too */
104 printf("malloc(%zu) called from %p returns %p\n",
105 size, caller, result);
106
107 /* Restore our own hooks */
108 __malloc_hook = my_malloc_hook;
109
110 return result;
111 }
112
114 mallinfo(3), malloc(3), mcheck(3), mtrace(3)
115
116
117
118Linux man-pages 6.05 2023-05-03 __malloc_hook(3)