]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/malloc_hook.3
Wrapped long lines, wrapped at sentence boundaries; stripped trailing
[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 .BR malloc (),
34 .BR realloc (),
35 and
36 .BR free ()
37 by specifying appropriate hook functions.
38 You can use these hooks
39 to help you debug programs that use dynamic memory allocation,
40 for example.
41 .LP
42 The variable
43 .B __malloc_initialize_hook
44 points at a function that is called once when the malloc implementation
45 is initialized.
46 This is a weak variable, so it can be overridden in
47 the application with a definition like the following:
48 .br
49 .nf
50 void (*__malloc_initialize_hook)(void) = my_init_hook;
51 .fi
52 .br
53 Now the function
54 .IR my_init_hook ()
55 can do the initialization of all hooks.
56 .LP
57 The four functions pointed to by
58 .BR __malloc_hook ,
59 .BR __realloc_hook ,
60 .BR __memalign_hook ,
61 .BR __free_hook
62 have a prototype like the functions
63 .BR malloc (),
64 .BR realloc (),
65 .BR memalign (),
66 .BR free (),
67 respectively, except that they have a final argument
68 .I caller
69 that gives the address of the caller of
70 .BR malloc (),
71 etc.
72 .LP
73 The variable
74 .B __after_morecore_hook
75 points at a function that is called each time after
76 .BR sbrk ()
77 was asked for more memory.
78 .SH "EXAMPLE"
79 Here is a short example of how to use these variables.
80 .sp
81 .nf
82 #include <stdio.h>
83 #include <malloc.h>
84
85 /* Prototypes for our hooks. */
86 static void my_init_hook(void);
87 static void *my_malloc_hook(size_t, const void *);
88
89 /* Variables to save original hooks. */
90 static void *(*old_malloc_hook)(size_t, const void *);
91
92 /* Override initialising hook from the C library. */
93 void (*__malloc_initialize_hook) (void) = my_init_hook;
94
95 static void
96 my_init_hook(void)
97 {
98 old_malloc_hook = __malloc_hook;
99 __malloc_hook = my_malloc_hook;
100 }
101
102 static void *
103 my_malloc_hook(size_t size, const void *caller)
104 {
105 void *result;
106
107 /* Restore all old hooks */
108 __malloc_hook = old_malloc_hook;
109
110 /* Call recursively */
111 result = malloc(size);
112
113 /* Save underlying hooks */
114 old_malloc_hook = __malloc_hook;
115
116 /* `printf' might call `malloc', so protect it too. */
117 printf("malloc(%u) called from %p returns %p\\n",
118 (unsigned int) size, caller, result);
119
120 /* Restore our own hooks */
121 __malloc_hook = my_malloc_hook;
122
123 return result;
124 }
125 .fi
126 .SH "CONFORMING TO"
127 These functions are GNU extensions.
128 .SH "SEE ALSO"
129 .BR mallinfo (3),
130 .BR malloc (3),
131 .BR mcheck (3),
132 .BR mtrace (3)