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 *(*__malloc_hook)(size_t size, const void *caller);
13
14       void *(*__realloc_hook)(void *ptr, size_t size, const void *caller);
15
16       void *(*__memalign_hook)(size_t alignment, size_t size,
17                                const void *caller);
18
19       void (*__free_hook)(void *ptr, const void *caller);
20
21       void (*__malloc_initialize_hook)(void);
22
23       void (*__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
29       allocation, 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           void (*__malloc_initialize_hook)(void) = my_init_hook;
36       Now the function my_init_hook() can do the initialization of all hooks.
37
38       The four functions pointed to by __malloc_hook, __realloc_hook, __mema‐
39       lign_hook,  __free_hook  have a prototype like the functions malloc(3),
40       realloc(3), memalign(3), free(3), respectively, except that they have a
41       final  argument  caller  that  gives  the address of the caller of mal‐
42       loc(3), etc.
43
44       The variable __after_morecore_hook points at a function that is  called
45       each time after sbrk(2) was asked for more memory.
46
47       Hook  variables  are  not thread-safe so they are deprecated now.  Pro‐
48       grammers should instead preempt callst to  the  relevant  functions  by
49       defining and exporting functions like "malloc" and "free".
50
51
52

CONFORMING TO

54       These functions are GNU extensions.
55

EXAMPLE

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

SEE ALSO

104       mallinfo(3), malloc(3), mcheck(3), mtrace(3)
105

COLOPHON

107       This  page  is  part of release 3.25 of the Linux man-pages project.  A
108       description of the project, and information about reporting  bugs,  can
109       be found at http://www.kernel.org/doc/man-pages/.
110
111
112
113GNU                               2002-07-20                    MALLOC_HOOK(3)
Impressum