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
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.  Programmers should instead preempt  calls
55       to  the  relevant functions by defining and exporting functions such as
56       "malloc" and "free".
57

EXAMPLE

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

SEE ALSO

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

COLOPHON

109       This page is part of release 3.53 of the Linux  man-pages  project.   A
110       description  of  the project, and information about reporting bugs, can
111       be found at http://www.kernel.org/doc/man-pages/.
112
113
114
115GNU                               2010-10-13                    MALLOC_HOOK(3)
Impressum