rename foo.n to foo.3tk
[manpages-zh.git] / src / mann / memory.n
bloba01ef3eaf6a2a1a886065939d65e1e64ead4cbfc
1 .\"
2 .\" Memory.man
3 .\"
4 .\" Extended Tcl memory leak locator.
5 .\"----------------------------------------------------------------------------
6 .\" Copyright 1992-1999 Karl Lehenbauer and Mark Diekhans.
7 .\"
8 .\" Permission to use, copy, modify, and distribute this software and its
9 .\" documentation for any purpose and without fee is hereby granted, provided
10 .\" that the above copyright notice appear in all copies. Karl Lehenbauer and
11 .\" Mark Diekhans make no representations about the suitability of this
12 .\" software for any purpose. It is provided "as is" without express or
13 .\" implied warranty.
14 .\"----------------------------------------------------------------------------
15 .\" $Id: memory.n,v 1.2 2003/11/24 05:09:59 bbbush Exp $
16 .\"----------------------------------------------------------------------------
17 .\"
18 .TH "Memory" 3tcl "" "Tcl"
19 .BS
20 .SH NAME
21 ckalloc, memory, ckfree, Tcl_DisplayMemory, Tcl_InitMemory, Tcl_ValidateAllMemory - 合法的内存分配接口
22 .SH "总览 SYNOPSIS"
23 .nf
24 .B memory \fBinfo\fR
26 .B memory \fBtrace\fR [\fBon|off\fR]
28 .B memory \fBvalidate\fR [\fBon|off\fR]
30 .B memory \fBtrace_on_at_malloc\fR \fInnn\fR
32 .B memory \fBbreak_on_malloc\fR \fInnn\fR
34 .B memory \fBdisplay\fR \fIfile\fR
36 .sp 2
37 .ft CW
38 #include <tcl.h>
39 .sp
40 char *
41 ckalloc (unsigned size)
42 .sp
43 void
44 ckfree (char *ptr)
45 .sp
46 int
47 Tcl_DumpActiveMemory (char *fileName);
48 .sp
49 void
50 Tcl_ValidateAllMemory (char *file,
51 int line)
53 void
54 Tcl_InitMemory (interp)
55 .ft R
57 .SH ARGUMENTS
58 .AS Tcl_Interp *fileName
59 .AP uint size in
61 .AP char *ptr in
62 .AP Tcl_Interp *interp in
63 A pointer to the Tcl interpreter.
64 .AP char *file in
65 The filename of the caller of Tcl_ValidateAllMemory.
66 .AP int line in
67 The line number of the caller of Tcl_ValidateAllMemory.
68 .AP char *fileName in
69 File to display list of active memory.
70 .BE
72 .SH "描述 DESCRIPTION"
73 .SS ckalloc
74 .PP
75 This macro allocates memory, in the same manner as \fBmalloc\fR, with the
76 following differences: One, \fBckalloc\fR checks the value returned from
77 \fBmalloc\fR (it calls \fBmalloc\fR for you) and panics if the allocation
78 request fails. Two, if enabled at compile time, a version of \fBckalloc\fR
79 with special memory debugging capabilities replaces the normal version of
80 \fBckalloc\fR, which aids in detecting memory overwrites and leaks (repeated
81 allocations not matched by corresponding frees).
82 .PP
83 Parameters:
84 .RS 2
85 \fBo \fIsize\fR - The size of the memory block to be allocated.
86 .RE
87 .PP
88 Returns:
89 .RS 2
90 A pointer to the allocated memory block.
91 .RE
93 .SS ckfree
94 .PP
95 This macro frees memory allocated by \fBckalloc\fR. Like \fBckalloc\fR,
96 when memory debugging is enabled, \fBckfree\fR has enhanced capabilities
97 for detecting memory overwrites and leaks.
98 .PP
99 It is very important that you use \fBckalloc\fR when you need to allocate
100 memory, and that you use \fBckfree\fR to free it. Should you use \fBmalloc\fR
101 to allocate and \fBckfree\fR to free, spurious memory validation errors will
102 occur when memory debugging is enabled. Should you use \fBfree\fR to free
103 memory allocated by \fBckalloc\fR, memory corruption will occur when memory
104 debugging is enabled. Any memory that is to be become the property of the Tcl
105 interpreter, such as result space, must be allocated with \fBckalloc\fR. If
106 it is absolutely necessary for an application to pass back \fBmalloc\fRed
107 memory to Tcl, it will work only if Tcl is complied with the
108 \fBTCL_MEM_DEBUG\fR flag turned off. If you convert your application to use
109 this facility, it will help you find memory over runs and lost memory. Note
110 that memory allocated by a C library routine requiring freeing should still be
111 freed with \fBfree\fR, since it calls \fBmalloc\fR rather than \fBckalloc\fR
112 to do the allocation.
114 Parmeters:
115 .RS 2
116 \fBo \fIptr\fR - The address of a block to free, as returned by ckalloc.
120 .SS Tcl_DumpActiveMemory
121 .PP
122 This function will output a list of all currently allocated memory to the
123 specified file. The following information is outputted for each allocated
124 block of memory: starting and ending addresses (excluding guard zone), size,
125 source file where \fBckalloc\fR was called to allocate the block and line
126 number in that file. It is especially useful to call
127 \fBTcl_DumpActiveMemory\fR after the Tcl interpreter has been deleted.
129 Parameters:
130 .RS 2
131 \fBo \fIfileName\fR - The name of the file to output the memory list to.
134 .SS Tcl_ValidateAllMemory
136 Forces a validation of the guard zones of all currently allocated blocks
137 of memory. Normally validation of a block occurs when its freed, unless
138 full validation is enabled, in which case validation of all blocks
139 occurs when \fBckalloc\fR and \fBckfree\fR are called. This function forces
140 the validation to occur at any point.
142 Parameters:
143 .RS 2
144 \fBo \fIfile\fR - The file that this routine is being called from, normally
145 \fB__FILE__\fR.
147 \fBo \fIline\fR - The line that this routine is being called from, normally
148 \fB__LINE__\fR.
151 .SH ENABLING MEMORY DEBUGGING
153 To enable memory debugging, Tcl should be recompiled from scratch with
154 \fBTCL_MEM_DEBUG\fR defined. This will also compile in
155 a non-stub version of \fBTcl_InitMemory\fR
156 to add the \fBmemory\fR command to Tcl.
158 \fBTCL_MEM_DEBUG\fR must be either left defined for all modules or undefined
159 for all modules that are going to be linked together. If they are not, link
160 errors will occur, with either \fBTclDbCkfree\fR and \fBTcl_DbCkalloc\fR or
161 \fBTcl_Ckalloc\fR and \fBTcl_Ckfree\fR being undefined.
163 .SH GUARD ZONES
165 When memory debugging is enabled, whenever a call to \fBckalloc\fR is
166 made, slightly more memory than requested is allocated so the memory debugging
167 code can keep track
168 of the allocated memory, and also
169 eight-byte ``guard zones'' are placed in front of and behind the space that
170 will be returned to the caller. (The size of the guard zone is defined
171 by the C #define \fBGUARD_SIZE\fR in \fIbaseline/src/ckalloc.c\fR -- it
172 can be extended if you suspect large overwrite problems, at some cost in
173 performance.) A known pattern is written into the guard zones and,
174 on a call to \fBckfree\fR, the guard zones of the space being freed
175 are checked to see if either zone has been modified in any way.
176 If one has been, the guard bytes and their new contents are identified,
177 and a ``low guard failed'' or ``high guard failed'' message is issued.
178 The ``guard failed'' message includes the address of the memory packet
179 and the file name and line number of the code that called \fBckfree\fR.
180 This allows you to detect the common sorts of one-off problems, where
181 not enough space was allocated to contain the data written, for example.
183 .SH THE MEMORY COMMAND
184 '@help: debug/memory
185 '@brief: display and debug memory problems
188 .B memory \fIoptions\fR
190 The Tcl \fBmemory\fR command gives the Tcl developer control of Tcl's memory
191 debugging capabilities. The memory command has several suboptions, which are
192 described below. It is only available when Tcl has been compiled with memory
193 debugging enabled.
196 .B memory \fBinfo\fR
198 生成一个报告,包含自从 Tcl 启动以来分配和释放的(内存)总数,当前分配的包(未遇到相应的到 \fBckfree \fR的调用的到 \fBckalloc\fR 的调用的当前数目)的数目,当前分配的字节数,和已分配的包和字节的最大的数目。
201 .B memory \fBtrace\fR [\fBon|off\fR]
203 使内存跟踪开启或关闭。在开启内存跟踪的时候,对 \fBckalloc\fR 的每次调用都导致向 \fIstderr \fR写一行跟踪信息,其组成有字 \fIckalloc\fR,随后是返回的地址,分配的内存总数,和进行分配的 C 文件名和代码的行数。例如:...
205 \fBckalloc 40e478 98 tclProc.c 1406\fR
207 Calls to \fBckfree\fR are traced in the same manner, except that the
208 word \fIckalloc\fR is replaced by the word \fIckfree\fR.
211 .B memory \fBvalidate\fR [\fBon|off\fR]
213 使内存生效(validation)开启或关闭。在开启内存生效的时候,在对\fBckalloc\fR 或 \fBckfree \fR的每次调用上,检查用 \fBckalloc \fR分配的每块现存的内存的守卫区(guard zone)。这有很大的性能影响而只在强烈怀疑有覆写(overwrite)问题的时候才使用。开启内存生效的益处是在覆写发生之后第一次调用 \fBckalloc\fR 或 \fBckfree\fR 的时候就能检测到守卫区覆写,而不是在释放有覆写守卫区的内存的时候,释放可能在内存覆写发生之后才发生。
216 .B memory \fBtrace_on_at_malloc\fR \fInnn\fR
218 在进行了 \fIcount\fR\fI \fR数目 \fBckalloc \fR之后启用内存跟踪。例如,如果你键入了 \fBmemory trace_on_at_malloc 100\fR,在第 100 次调用 \fBckalloc \fR之后,将对所有分配和释放的内存显示内存跟踪信息。因为在一个问题发生之前可能有许多内存活动,如果你能在问题出现( sets in)之前标识出一定数目的分配,决定(judicious)使用这个选项可以减轻跟踪导致的速度变慢(和生成的跟踪信息总数)。在发生一个守卫区错误时,输出自从 Tcl 启动以来发生的内存分配的当前数目。
220 .B memory \fBbreak_on_malloc\fR \fInnn\fR
222 在进行了 \fB\fIcount\fR\fR 数目的 \fBckalloc\fR 分配之后,输出一个(中断)消息,表示它现在想进入 C 调试器。 Tcl 将向自身发出一个 \fISIGINT\fR 信号。如果你在一个 C 调试器下运行 Tcl,它将接着进入调试器命令模式。
225 .B memory \fBdisplay\fR \fIfile\fR
227 向指定文件写当前所有分配的内存的一个列表。
228 '@endhelp
230 .SH DEBUGGING DIFFICULT MEMORY CORRUPTION PROBLEMS
232 Normally, Tcl compiled with memory debugging enabled will make it easy to isolate
233 a corruption problem. Turning on memory validation with the memory command
234 can help isolate difficult problems.
235 If you suspect (or know) that corruption is
236 occurring before the Tcl interpreter comes up far enough for you to
237 issue commands, you can set \fBMEM_VALIDATE\fR define, recompile
238 tclCkalloc.c and rebuild Tcl. This will enable memory validation
239 from the first call to \fBckalloc\fR, again, at a large performance impact.
241 If you are desperate and validating memory on every call to \fBckalloc\fR
242 and \fBckfree\fR isn't enough, you can explicitly call
243 \fBTcl_ValidateAllMemory\fR directly at any point. It takes a \fIchar *\fR
244 and an \fIint\fR which are normally the filename and line number of the
245 caller, but they can actually be anything you want. Remember to remove
246 the calls after you find the problem.
248 .SH "关键字 KEYWORDS"
249 ckalloc, ckfree, free, memory, malloc
252 .SH "[中文版维护人]"
253 .B 寒蝉退士
254 .SH "[中文版最新更新]"
255 .B 2001/09/28
256 .SH "《中国 Linux 论坛 man 手册页翻译计划》:"
257 .BI http://cmpp.linuxforum.net