OSDN Git Service

(split) LDP v3.24 -> v3.29 の定型的な変更内容を反映。
[linuxjm/LDP_man-pages.git] / draft / 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 .\"
6 .\" FIXME
7 .\" According to a Fedora downstream patch, malloc hooks are deprecated
8 .\" https://bugzilla.redhat.com/show_bug.cgi?id=450187
9 .\" Integrate this upstream?
10 .\"
11 .\" Japanese Version Copyright (c) 2002 Akihiro MOTOKI all rights reserved.
12 .\" Translated Thu 05 Dec 2002 by Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
13 .\"
14 .TH MALLOC_HOOK 3 2002-07-20 "GNU" "Linux Programmer's Manual"
15 .\"O .SH NAME
16 .SH Ì¾Á°
17 .\"O __malloc_hook, __malloc_initialize_hook,
18 .\"O __memalign_hook, __free_hook, __realloc_hook,
19 .\"O __after_morecore_hook \- malloc debugging variables
20 __malloc_hook, __malloc_initialize_hook,
21 __memalign_hook, __free_hook, __realloc_hook,
22 __after_morecore_hook \- malloc ¥Ç¥Ð¥Ã¥°ÍѤÎÊÑ¿ô
23 .\"O .SH SYNOPSIS
24 .SH ½ñ¼°
25 .nf
26 .B "#include <malloc.h>"
27 .sp
28 .BI "void *(*__malloc_hook)(size_t " size ", const void *" caller );
29 .sp
30 .BI "void *(*__realloc_hook)(void *" ptr ", size_t " size \
31 ", const void *" caller );
32 .sp
33 .BI "void *(*__memalign_hook)(size_t " alignment ", size_t " size ,
34 .BI "                         const void *" caller );
35 .sp
36 .BI "void (*__free_hook)(void *" ptr ", const void *" caller );
37 .sp
38 .B "void (*__malloc_initialize_hook)(void);"
39 .sp
40 .B "void (*__after_morecore_hook)(void);"
41 .fi
42 .\"O .SH DESCRIPTION
43 .SH ÀâÌÀ
44 .\"O The GNU C library lets you modify the behavior of
45 .\"O .BR malloc (3),
46 .\"O .BR realloc (3),
47 .\"O and
48 .\"O .BR free (3)
49 .\"O by specifying appropriate hook functions.
50 .\"O You can use these hooks
51 .\"O to help you debug programs that use dynamic memory allocation,
52 .\"O for example.
53 GNU C ¥é¥¤¥Ö¥é¥ê¤Ç¤Ï¡¢Å¬Àڤʥե寴ؿô (hook function) ¤ò»ØÄꤹ¤ë¤³¤È¤Ç
54 .BR malloc (3),
55 .BR realloc (3),
56 .BR free (3)
57 ¤ÎÆ°ºî¤òÊѹ¹¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£Î㤨¤Ð¡¢Æ°Åª¤Ë¥á¥â¥ê³ä¤êÅö¤Æ¤ò¹Ô¤¦
58 ¥×¥í¥°¥é¥à¤Î¥Ç¥Ð¥Ã¥°¤Ë¤³¤ì¤é¤Î¥Õ¥Ã¥¯¤ò»È¤¦¤³¤È¤¬¤Ç¤­¤ë¡£
59 .LP
60 .\"O The variable
61 .\"O .B __malloc_initialize_hook
62 .\"O points at a function that is called once when the malloc implementation
63 .\"O is initialized.
64 .\"O This is a weak variable, so it can be overridden in
65 .\"O the application with a definition like the following:
66 ÊÑ¿ô
67 .B __malloc_initialize_hook
68 ¤Ï malloc ¤Î¼ÂÁõ¤¬½é´ü²½¤µ¤ì¤ëºÝ¤Ë°ìÅÙ¤À¤±¸Æ¤Ð¤ì¤ë´Ø¿ô¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
69 ¤³¤ÎÊÑ¿ô¤Ï½ñ¤­´¹¤¨²Äǽ (weak) ¤Ç¤¢¤ê¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥óÆâ¤Ç
70 °Ê²¼¤Î¤è¤¦¤ÊÄêµÁ¤Ç¾å½ñ¤­¤Ç¤­¤ë:
71
72 .nf
73     void (*__malloc_initialize_hook)(void) = my_init_hook;
74 .fi
75
76 .\"O Now the function
77 .\"O .IR my_init_hook ()
78 .\"O can do the initialization of all hooks.
79 ¤Ê¤ª¡¢´Ø¿ô
80 .IR my_init_hook ()
81 ¤ÇÁ´¤Æ¤Î¥Õ¥Ã¥¯¤Î½é´ü²½¤ò¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
82 .LP
83 .\"O The four functions pointed to by
84 .\"O .BR __malloc_hook ,
85 .\"O .BR __realloc_hook ,
86 .\"O .BR __memalign_hook ,
87 .\"O .B __free_hook
88 .\"O have a prototype like the functions
89 .\"O .BR malloc (3),
90 .\"O .BR realloc (3),
91 .\"O .BR memalign (3),
92 .\"O .BR free (3),
93 .\"O respectively, except that they have a final argument
94 .\"O .I caller
95 .\"O that gives the address of the caller of
96 .\"O .BR malloc (3),
97 .\"O etc.
98 .BR __malloc_hook ,
99 .BR __realloc_hook ,
100 .BR __memalign_hook ,
101 .B __free_hook
102 ¤Ç»Ø¤µ¤ì¤ë 4 ¤Ä¤Î´Ø¿ô¤Ï¡¢³Æ¡¹
103 .BR malloc (3),
104 .BR realloc (3),
105 .BR memalign (3),
106 .BR free (3)
107 ¤È¤è¤¯»÷¤¿¥×¥í¥È¥¿¥¤¥×¤ò»ý¤Ã¤Æ¤¤¤ë¤¬¡¢
108 °ìÈֺǸå¤Î°ú¤­¿ô
109 .I caller
110 ¤ò¤È¤ëÅÀ¤¬°Û¤Ê¤ë¡£
111 °ú¤­¿ô
112 .I caller
113 ¤Ë¤Ï¡¢
114 .BR malloc (3)
115 ¤Ê¤É¤Î¸Æ¤Ó½Ð¤·¸µ (caller) ¤Î¥¢¥É¥ì¥¹¤¬³ÊǼ¤µ¤ì¤ë¡£
116 .LP
117 .\"O The variable
118 .\"O .B __after_morecore_hook
119 .\"O points at a function that is called each time after
120 .\"O .BR sbrk (2)
121 .\"O was asked for more memory.
122 ÊÑ¿ô
123 .B __after_morecore_hook
124 ¤Ï¡¢Îΰè¤ÎÄɲÃÍ׵᤬¤¢¤ê
125 .BR sbrk (2)
126 ¤¬¸Æ¤Ð¤ì¤¿¸å¤ÇËè²ó¸Æ¤Ó½Ð¤µ¤ì¤ë´Ø¿ô¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
127 .\"O .SH "CONFORMING TO"
128 .SH ½àµò
129 .\"O These functions are GNU extensions.
130 ¤³¤ì¤é¤Î´Ø¿ô¤Ï GNU ¤Ë¤è¤ë³ÈÄ¥¤Ç¤¢¤ë¡£
131 .\"O .SH "EXAMPLE"
132 .SH Îã
133 .\"O Here is a short example of how to use these variables.
134 ¤³¤ì¤é¤ÎÊÑ¿ô¤Î»È¤¤Êý¤Î´Êñ¤ÊÎã¤ò°Ê²¼¤Ë¼¨¤¹¡£
135 .sp
136 .nf
137 #include <stdio.h>
138 #include <malloc.h>
139
140 .\"O /* Prototypes for our hooks.  */
141 /* »È¤ª¤¦¤È¤¹¤ë¥Õ¥Ã¥¯¤Î¥×¥í¥È¥¿¥¤¥×Àë¸À */
142 static void my_init_hook(void);
143 static void *my_malloc_hook(size_t, const void *);
144
145 .\"O /* Variables to save original hooks. */
146 /* ¸µ¡¹¤Î¥Õ¥Ã¥¯¤òÊݸ¤¹¤ë¤¿¤á¤ÎÊÑ¿ô */
147 static void *(*old_malloc_hook)(size_t, const void *);
148
149 .\"O /* Override initializing hook from the C library. */
150 /* C ¥é¥¤¥Ö¥é¥ê¤«¤é¸Æ¤Ð¤ì¤ë½é´ü²½¥Õ¥Ã¥¯¤ò¾å½ñ¤­¤¹¤ë */
151 void (*__malloc_initialize_hook) (void) = my_init_hook;
152
153 static void
154 my_init_hook(void)
155 {
156     old_malloc_hook = __malloc_hook;
157     __malloc_hook = my_malloc_hook;
158 }
159
160 static void *
161 my_malloc_hook(size_t size, const void *caller)
162 {
163     void *result;
164
165 .\"O     /* Restore all old hooks */
166     /* ¸µ¡¹¤Î¥Õ¥Ã¥¯¤òÁ´¤ÆÌ᤹ */
167     __malloc_hook = old_malloc_hook;
168
169 .\"O     /* Call recursively */
170     /* malloc ¤ÎºÆµ¢Åª¸Æ¤Ó½Ð¤· */
171     result = malloc(size);
172
173 .\"O     /* Save underlying hooks */
174     /* ¸½ºßÀßÄꤵ¤ì¤Æ¤¤¤ë¥Õ¥Ã¥¯ (underlying hook) ¤òÊݸ¤¹¤ë */
175     old_malloc_hook = __malloc_hook;
176
177 .\"O     /* printf() might call malloc(), so protect it too. */
178     /* printf() ¤Ï malloc() ¤ò¸Æ¤Ó½Ð¤¹²ÄǽÀ­¤¬¤¢¤ë¤Î¤Ç
179         ¤³¤³¤Ç¤â¥¬¡¼¥É¤ò¹Ô¤¦ (¸µ¡¹¤Î¥Õ¥Ã¥¯¤Î¤Þ¤Þ¤Ë¤·¤Æ¤ª¤¯) */
180     printf("malloc(%u) called from %p returns %p\\n",
181             (unsigned int) size, caller, result);
182
183 .\"O     /* Restore our own hooks */
184     /* ¥æ¡¼¥¶¤¬»È¤ª¤¦¤È¤¹¤ë¥Õ¥Ã¥¯¤òºÆÀßÄꤹ¤ë */
185     __malloc_hook = my_malloc_hook;
186
187     return result;
188 }
189 .fi
190 .\"O .SH "SEE ALSO"
191 .SH ´ØÏ¢¹àÌÜ
192 .BR mallinfo (3),
193 .BR malloc (3),
194 .BR mcheck (3),
195 .BR mtrace (3)