]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/malloc_hook.3
Import of man-pages 1.70
[thirdparty/man-pages.git] / man3 / malloc_hook.3
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
33 .IR malloc (),
34 .IR realloc (),
35 and
36 .IR free ()
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
61 .IR malloc (),
62 .IR realloc (),
63 .IR memalign (),
64 .IR free (),
65 respectively, except that they have a final argument
66 .I caller
67 that gives the address of the caller of
68 .IR malloc (),
69 etc.
70 .LP
71 The variable
72 .B __after_morecore_hook
73 points at a function that is called each time after
74 .IR sbrk ()
75 was asked for more core.
76 .SH "EXAMPLE"
77 Here a short example how to use these variables.
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
94 my_init_hook(void) {
95 old_malloc_hook = __malloc_hook;
96 __malloc_hook = my_malloc_hook;
97 }
98
99 static void *
100 my_malloc_hook (size_t size, const void *caller) {
101 void *result;
102
103 /* Restore all old hooks */
104 __malloc_hook = old_malloc_hook;
105
106 /* Call recursively */
107 result = malloc (size);
108
109 /* Save underlying hooks */
110 old_malloc_hook = __malloc_hook;
111
112 /* `printf' might call `malloc', so protect it too. */
113 printf ("malloc(%u) called from %p returns %p\n",
114 (unsigned int) size, caller, result);
115
116 /* Restore our own hooks */
117 __malloc_hook = my_malloc_hook;
118
119 return result;
120 }
121 .fi
122 .SH "SEE ALSO"
123 .BR mallinfo (3),
124 .BR malloc (3),
125 .BR mcheck (3),
126 .BR mtrace (3)