Imported upstream version 1.5
[manpages-zh.git] / raw / mann / ckalloc.n
blobaf18db51659fc2be5f86d29e401b70e61c70debb
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: ckalloc.n,v 1.1 2003/12/20 03:31:54 bbbush Exp $
16 .\"----------------------------------------------------------------------------
17 .\"
18 .TH "Memory" TCL "" "Tcl"
19 .BS
20 .SH NAME
21 ckalloc, memory, ckfree, Tcl_DisplayMemory, Tcl_InitMemory, Tcl_ValidateAllMemory - Validated memory allocation interface.
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 Thi 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 Produces a report containing the total allocations and frees since
199 Tcl began, the current packets allocated (the current
200 number of calls to \fBckalloc\fR not met by a corresponding call
201 to \fBckfree\fR), the current bytes allocated, and the maximum number
202 of packets and bytes allocated.
205 .B memory \fBtrace\fR [\fBon|off\fR]
207 Turns memory tracing on or off.
208 When memory tracing is on, every call to \fBckalloc\fR causes a line of
209 trace information to be written to \fIstderr\fR, consisting of the
210 word \fIckalloc\fR, followed by the address returned, the amount of
211 memory allocated, and the C filename and line number of the code performing
212 the allocation, for example...
214 \fBckalloc 40e478 98 tclProc.c 1406\fR
216 Calls to \fBckfree\fR are traced in the same manner, except that the
217 word \fIckalloc\fR is replaced by the word \fIckfree\fR.
220 .B memory \fBvalidate\fR [\fBon|off\fR]
222 Turns memory validation on or off.
223 When memory validation is enabled, on every call to
224 \fBckalloc\fR or \fBckfree\fR, the guard zones are checked for every
225 piece of memory currently in existence that was allocated by \fBckalloc\fR.
226 This has a large performance impact and should only be used when
227 overwrite problems are strongly suspected. The advantage of enabling
228 memory validation is that a guard zone overwrite can be detected on
229 the first call to \fBckalloc\fR or \fBckfree\fR after the overwrite
230 occurred, rather than when the specific memory with the overwritten
231 guard zone(s) is freed, which may occur long after the overwrite occurred.
234 .B memory \fBtrace_on_at_malloc\fR \fInnn\fR
236 Enable memory tracing after \fInnn\fR \fBckallocs\fR have been performed.
237 For example, if you enter \fBmemory trace_on_at_malloc 100\fR,
238 after the 100th call to \fBckalloc\fR, memory trace information will begin
239 being displayed for all allocations and frees. Since there can be a lot
240 of memory activity before a problem occurs, judicious use of this option
241 can reduce the slowdown caused by tracing (and the amount of trace information
242 produced), if you can identify a number of allocations that occur before
243 the problem sets in. The current number of memory allocations that have
244 occurred since Tcl started is printed on a guard zone failure.
246 .B memory \fBbreak_on_malloc\fR \fInnn\fR
248 After the \fBnnn\fR allocations have been performed, \fBckallocs\fR
249 output a message to this effect and that it is now attempting to enter
250 the C debugger. Tcl will then issue a \fISIGINT\fR signal against itself.
251 If you are running Tcl under a C debugger, it should then enter the debugger
252 command mode.
255 .B memory \fBdisplay\fR \fIfile\fR
257 Write a list of all currently allocated memory to the specified file.
258 '@endhelp
260 .SH DEBUGGING DIFFICULT MEMORY CORRUPTION PROBLEMS
262 Normally, Tcl compiled with memory debugging enabled will make it easy to isolate
263 a corruption problem. Turning on memory validation with the memory command
264 can help isolate difficult problems.
265 If you suspect (or know) that corruption is
266 occurring before the Tcl interpreter comes up far enough for you to
267 issue commands, you can set \fBMEM_VALIDATE\fR define, recompile
268 tclCkalloc.c and rebuild Tcl. This will enable memory validation
269 from the first call to \fBckalloc\fR, again, at a large performance impact.
271 If you are desperate and validating memory on every call to \fBckalloc\fR
272 and \fBckfree\fR isn't enough, you can explicitly call
273 \fBTcl_ValidateAllMemory\fR directly at any point. It takes a \fIchar *\fR
274 and an \fIint\fR which are normally the filename and line number of the
275 caller, but they can actually be anything you want. Remember to remove
276 the calls after you find the problem.
278 .SH KEYWORDS
279 ckalloc, ckfree, free, memory, malloc