]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright 2002 Walter Harms (walter.harms@informatik.uni-oldenburg.de) |
2 | .\" Distributed under GPL | |
3 | .\" Heavily based on glibc documentation | |
4 | .\" Polished, added docs, removed glibc doc bug, 2002-07-20, aeb | |
5 | .TH MALLOC_HOOK 3 2002-07-20 "GNU" "Linux Programmer's Manual" | |
6 | .SH NAME | |
7 | __malloc_hook, __malloc_initialize_hook, | |
8 | __memalign_hook, __free_hook, __realloc_hook, | |
9 | __after_morecore_hook \- malloc debugging variables | |
10 | .SH SYNOPSIS | |
11 | .sp | |
12 | .BR "#include <malloc.h>" | |
13 | .sp | |
14 | .BI "void *(*__malloc_hook)(size_t " size , | |
15 | .BI "const void *" caller ); | |
16 | .sp | |
17 | .BI "void *(*__realloc_hook)(void *" ptr , | |
18 | .BI "size_t " size , | |
19 | .BI "const void *" caller ); | |
20 | .sp | |
21 | .BI "void *(*__memalign_hook)(size_t " alignment , | |
22 | .BI "size_t " size , | |
23 | .BI "const void *" caller ); | |
24 | .sp | |
25 | .BI "void (*__free_hook)(void *" ptr , | |
26 | .BI "const void *" caller ); | |
27 | .sp | |
28 | .BI "void (*__malloc_initialize_hook)(void);" | |
29 | .sp | |
30 | .BI "void (*__after_morecore_hook)(void);" | |
31 | .SH DESCRIPTION | |
32 | The GNU C library lets you modify the behavior of | |
31e9a9ec MK |
33 | .BR malloc (), |
34 | .BR realloc (), | |
fea681da | 35 | and |
31e9a9ec | 36 | .BR free () |
fea681da MK |
37 | by specifying appropriate hook functions. You can use these hooks |
38 | to help you debug programs that use dynamic memory allocation, | |
39 | for example. | |
40 | .LP | |
41 | The variable | |
42 | .B __malloc_initialize_hook | |
43 | points at a function that is called once when the malloc implementation | |
44 | is initialized. This is a weak variable, so it can be overridden in | |
45 | the application with a definition like the following: | |
46 | .br | |
47 | .nf | |
48 | void (*__malloc_initialize_hook)(void) = my_init_hook; | |
49 | .fi | |
50 | .br | |
51 | Now the function | |
52 | .IR my_init_hook () | |
53 | can do the initialization of all hooks. | |
54 | .LP | |
55 | The four functions pointed to by | |
56 | .BR __malloc_hook , | |
57 | .BR __realloc_hook , | |
58 | .BR __memalign_hook , | |
59 | .BR __free_hook | |
60 | have a prototype like the functions | |
31e9a9ec MK |
61 | .BR malloc (), |
62 | .BR realloc (), | |
63 | .BR memalign (), | |
64 | .BR free (), | |
fea681da MK |
65 | respectively, except that they have a final argument |
66 | .I caller | |
67 | that gives the address of the caller of | |
31e9a9ec | 68 | .BR malloc (), |
fea681da MK |
69 | etc. |
70 | .LP | |
71 | The variable | |
72 | .B __after_morecore_hook | |
73 | points at a function that is called each time after | |
31e9a9ec | 74 | .BR sbrk () |
4b4a8feb | 75 | was asked for more memory. |
fea681da | 76 | .SH "EXAMPLE" |
4b4a8feb | 77 | Here is a short example of how to use these variables. |
fea681da MK |
78 | .sp |
79 | .nf | |
80 | #include <stdio.h> | |
81 | #include <malloc.h> | |
82 | ||
83 | /* Prototypes for our hooks. */ | |
84 | static void my_init_hook(void); | |
85 | static void *my_malloc_hook(size_t, const void *); | |
86 | ||
87 | /* Variables to save original hooks. */ | |
88 | static void *(*old_malloc_hook)(size_t, const void *); | |
89 | ||
90 | /* Override initialising hook from the C library. */ | |
91 | void (*__malloc_initialize_hook) (void) = my_init_hook; | |
92 | ||
93 | static void | |
cf0a9ace MK |
94 | my_init_hook(void) |
95 | { | |
fea681da MK |
96 | old_malloc_hook = __malloc_hook; |
97 | __malloc_hook = my_malloc_hook; | |
98 | } | |
99 | ||
100 | static void * | |
cf0a9ace MK |
101 | my_malloc_hook(size_t size, const void *caller) |
102 | { | |
fea681da MK |
103 | void *result; |
104 | ||
105 | /* Restore all old hooks */ | |
106 | __malloc_hook = old_malloc_hook; | |
107 | ||
108 | /* Call recursively */ | |
cf0a9ace | 109 | result = malloc(size); |
fea681da MK |
110 | |
111 | /* Save underlying hooks */ | |
112 | old_malloc_hook = __malloc_hook; | |
113 | ||
114 | /* `printf' might call `malloc', so protect it too. */ | |
cf0a9ace MK |
115 | printf("malloc(%u) called from %p returns %p\\n", |
116 | (unsigned int) size, caller, result); | |
fea681da MK |
117 | |
118 | /* Restore our own hooks */ | |
119 | __malloc_hook = my_malloc_hook; | |
120 | ||
121 | return result; | |
122 | } | |
123 | .fi | |
a7fadb55 MK |
124 | .SH "CONFORMING TO" |
125 | These functions are GNU extensions. | |
fea681da MK |
126 | .SH "SEE ALSO" |
127 | .BR mallinfo (3), | |
128 | .BR malloc (3), | |
129 | .BR mcheck (3), | |
130 | .BR mtrace (3) |