]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/malloc_hook.3
Convert to American spelling conventions
[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 (3),
34 .BR realloc (3),
35 and
36 .BR free (3)
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 (3),
64 .BR realloc (3),
65 .BR memalign (3),
66 .BR free (3),
67 respectively, except that they have a final argument
68 .I caller
69 that gives the address of the caller of
70 .BR malloc (3),
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 (2)
77 was asked for more memory.
78 .SH "CONFORMING TO"
79 These functions are GNU extensions.
80 .SH "EXAMPLE"
81 Here is a short example of how to use these variables.
82 .sp
83 .nf
84 #include <stdio.h>
85 #include <malloc.h>
86
87 /* Prototypes for our hooks. */
88 static void my_init_hook(void);
89 static void *my_malloc_hook(size_t, const void *);
90
91 /* Variables to save original hooks. */
92 static void *(*old_malloc_hook)(size_t, const void *);
93
94 /* Override initializing hook from the C library. */
95 void (*__malloc_initialize_hook) (void) = my_init_hook;
96
97 static void
98 my_init_hook(void)
99 {
100 old_malloc_hook = __malloc_hook;
101 __malloc_hook = my_malloc_hook;
102 }
103
104 static void *
105 my_malloc_hook(size_t size, const void *caller)
106 {
107 void *result;
108
109 /* Restore all old hooks */
110 __malloc_hook = old_malloc_hook;
111
112 /* Call recursively */
113 result = malloc(size);
114
115 /* Save underlying hooks */
116 old_malloc_hook = __malloc_hook;
117
118 /* `printf' might call `malloc', so protect it too. */
119 printf("malloc(%u) called from %p returns %p\\n",
120 (unsigned int) size, caller, result);
121
122 /* Restore our own hooks */
123 __malloc_hook = my_malloc_hook;
124
125 return result;
126 }
127 .fi
128 .SH "SEE ALSO"
129 .BR mallinfo (3),
130 .BR malloc (3),
131 .BR mcheck (3),
132 .BR mtrace (3)