home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / man / lib / tclx / Memory.man < prev    next >
Encoding:
Text File  |  1991-11-19  |  12.7 KB  |  400 lines

  1. .\"----------------------------------------------------------------------------
  2. .\" The definitions below are for supplemental macros used in Sprite
  3. .\" manual entries.
  4. .\"
  5. .\" .HS name section [date [version]]
  6. .\"    Replacement for .TH in other man pages.  See below for valid
  7. .\"    section names.
  8. .\"
  9. .\" .AP type name in/out [indent]
  10. .\"    Start paragraph describing an argument to a library procedure.
  11. .\"    type is type of argument (int, etc.), in/out is either "in", "out",
  12. .\"    or "in/out" to describe whether procedure reads or modifies arg,
  13. .\"    and indent is equivalent to second arg of .IP (shouldn't ever be
  14. .\"    needed;  use .AS below instead)
  15. .\"
  16. .\" .AS [type [name]]
  17. .\"    Give maximum sizes of arguments for setting tab stops.  Type and
  18. .\"    name are examples of largest possible arguments that will be passed
  19. .\"    to .AP later.  If args are omitted, default tab stops are used.
  20. .\"
  21. .\" .BS
  22. .\"    Start box enclosure.  From here until next .BE, everything will be
  23. .\"    enclosed in one large box.
  24. .\"
  25. .\" .BE
  26. .\"    End of box enclosure.
  27. .\"
  28. .\" .VS
  29. .\"    Begin vertical sidebar, for use in marking newly-changed parts
  30. .\"    of man pages.
  31. .\"
  32. .\" .VE
  33. .\"    End of vertical sidebar.
  34. .\"
  35. .\" .DS
  36. .\"    Begin an indented unfilled display.
  37. .\"
  38. .\" .DE
  39. .\"    End of indented unfilled display.
  40. .\"
  41. '    # Heading for Sprite man pages
  42. .de HS
  43. .if '\\$2'cmds'       .TH \\$1 1 \\$3 \\$4
  44. .if '\\$2'lib'        .TH \\$1 3 \\$3 \\$4
  45. .if '\\$2'tcl'        .TH \\$1 3 \\$3 \\$4
  46. .if '\\$2'tk'         .TH \\$1 3 \\$3 \\$4
  47. .if t .wh -1.3i ^B
  48. .nr ^l \\n(.l
  49. .ad b
  50. ..
  51. '    # Start an argument description
  52. .de AP
  53. .ie !"\\$4"" .TP \\$4
  54. .el \{\
  55. .   ie !"\\$2"" .TP \\n()Cu
  56. .   el          .TP 15
  57. .\}
  58. .ie !"\\$3"" \{\
  59. .ta \\n()Au \\n()Bu
  60. \&\\$1    \\fI\\$2\\fP    (\\$3)
  61. .\".b
  62. .\}
  63. .el \{\
  64. .br
  65. .ie !"\\$2"" \{\
  66. \&\\$1    \\fI\\$2\\fP
  67. .\}
  68. .el \{\
  69. \&\\fI\\$1\\fP
  70. .\}
  71. .\}
  72. ..
  73. '    # define tabbing values for .AP
  74. .de AS
  75. .nr )A 10n
  76. .if !"\\$1"" .nr )A \\w'\\$1'u+3n
  77. .nr )B \\n()Au+15n
  78. .\"
  79. .if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n
  80. .nr )C \\n()Bu+\\w'(in/out)'u+2n
  81. ..
  82. '    # BS - start boxed text
  83. '    # ^y = starting y location
  84. '    # ^b = 1
  85. .de BS
  86. .br
  87. .mk ^y
  88. .nr ^b 1u
  89. .if n .nf
  90. .if n .ti 0
  91. .if n \l'\\n(.lu\(ul'
  92. .if n .fi
  93. ..
  94. '    # BE - end boxed text (draw box now)
  95. .de BE
  96. .nf
  97. .ti 0
  98. .mk ^t
  99. .ie n \l'\\n(^lu\(ul'
  100. .el \{\
  101. .\"    Draw four-sided box normally, but don't draw top of
  102. .\"    box if the box started on an earlier page.
  103. .ie !\\n(^b-1 \{\
  104. \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
  105. .\}
  106. .el \}\
  107. \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
  108. .\}
  109. .\}
  110. .fi
  111. .br
  112. .nr ^b 0
  113. ..
  114. '    # VS - start vertical sidebar
  115. '    # ^Y = starting y location
  116. '    # ^v = 1 (for troff;  for nroff this doesn't matter)
  117. .de VS
  118. .mk ^Y
  119. .ie n 'mc \s12\(br\s0
  120. .el .nr ^v 1u
  121. ..
  122. '    # VE - end of vertical sidebar
  123. .de VE
  124. .ie n 'mc
  125. .el \{\
  126. .ev 2
  127. .nf
  128. .ti 0
  129. .mk ^t
  130. \h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n'
  131. .sp -1
  132. .fi
  133. .ev
  134. .\}
  135. .nr ^v 0
  136. ..
  137. '    # Special macro to handle page bottom:  finish off current
  138. '    # box/sidebar if in box/sidebar mode, then invoked standard
  139. '    # page bottom macro.
  140. .de ^B
  141. .ev 2
  142. 'ti 0
  143. 'nf
  144. .mk ^t
  145. .if \\n(^b \{\
  146. .\"    Draw three-sided box if this is the box's first page,
  147. .\"    draw two sides but no top otherwise.
  148. .ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
  149. .el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
  150. .\}
  151. .if \\n(^v \{\
  152. .nr ^x \\n(^tu+1v-\\n(^Yu
  153. \kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c
  154. .\}
  155. .bp
  156. 'fi
  157. .ev
  158. .if \\n(^b \{\
  159. .mk ^y
  160. .nr ^b 2
  161. .\}
  162. .if \\n(^v \{\
  163. .mk ^Y
  164. .\}
  165. ..
  166. '    # DS - begin display
  167. .de DS
  168. .RS
  169. .nf
  170. .sp
  171. ..
  172. '    # DE - end display
  173. .de DE
  174. .fi
  175. .RE
  176. .sp .5
  177. ..
  178. .\"----------------------------------------------------------------------------
  179. .HS Memory tcl
  180. .BS
  181. '@index: memory ckalloc ckfree Tcl_DisplayMemory Tcl_InitMemory Tcl_ValidateAllMemory
  182. .SH NAME
  183. ckalloc, memory, ckfree, Tcl_DisplayMemory, Tcl_InitMemory, 
  184. Tcl_ValidateAllMemory - Validated memory allocation interface.
  185. .SH SYNOPSIS
  186. .B memory \fBinfo\fR
  187. .br
  188. .B memory \fBtrace\fR [\fBon|off\fR]
  189. .br
  190. .B memory \fBvalidate\fR [\fBon|off\fR]
  191. .br
  192. .B memory \fBtrace_on_at_malloc\fR \fInnn\fR
  193. .br
  194. .B memory \fBbreak_on_malloc\fR \fInnn\fR
  195. .br
  196. .B memory \fBdisplay\fR \fIfile\fR
  197. .br
  198. .sp 2
  199. .nf
  200. \fB#include <tcl.h>\fR or \fB<ckalloc.h>\fR
  201. .sp
  202. char *
  203. \fBckalloc\fR (\fIsize\fR)
  204. .sp
  205. void
  206. \fBckfree\fR (\fIptr\fR)
  207. .sp
  208. void
  209. \fBTcl_DisplayMemory\fR (fileName)
  210. .sp
  211. void
  212. \fBTcl_InitMemory\fR (\fIinterp\fR)
  213. .sp
  214. void
  215. \fBTcl_ValidateAllMemory\fR (\fIfile, line\fR)
  216. .SH ARGUMENTS
  217. .AS Tcl_Interp *fileName
  218. .AP uint size in
  219. The size of the memory block to be allocated.
  220. .AP char *ptr in
  221. The address of a block to free, as returned by ckalloc.
  222. .AP Tcl_Interp *interp in
  223. A pointer to the Tcl interpreter.
  224. .AP char *file in
  225. The filename of the caller of Tcl_ValidateAllMemory.
  226. .AP int line in
  227. The line number of the caller of Tcl_ValidateAllMemory.
  228. .AP char *fileName in
  229. File to display list of active memory.
  230. .BE
  231.  
  232. .SH DESCRIPTION
  233. .PP
  234. The macro
  235. \fBckalloc\fR allocates memory, in the same manner as \fBmalloc\fR, with the
  236. following differences: One, \fBckalloc\fR checks the value returned from
  237. \fBmalloc\fR (it calls \fBmalloc\fR for you) and panics if the allocation
  238. request fails.  Two, if enabled at compile time, a version of \fBckalloc\fR
  239. with special memory debugging capabilities replaces the normal version of
  240. \fBckalloc\fR, which aids in detecting memory overwrites and leaks (repeated
  241. allocations not matched by corresponding frees).
  242. .PP
  243. \fBckfree\fR frees memory allocated by \fBckalloc\fR.  Like \fBckalloc\fR,
  244. when memory debugging is enabled, \fBckfree\fR has enhanced capabilities
  245. for detecting memory overwrites and leaks.
  246. .PP
  247. It is very important that you use \fBckalloc\fR when you need to allocate
  248. memory, and that you use \fBckfree\fR to free it.  Should you use \fBmalloc\fR
  249. to allocate and \fBckfree\fR to free, spurious memory
  250. validation errors will occur when memory debugging is enabled.  Should you
  251. use \fBfree\fR to free memory allocated by \fBckalloc\fR, memory corruption 
  252. will occur when memory debugging is enabled.  Any memory that is to be become
  253. the property of the Tcl interpreter, such as result space, must be allocated
  254. with \fBckalloc\fR.  If it is absolutely necessary for an application to
  255. pass back \fBmalloc\fRed memory to Tcl, it will work only if Tcl is complied
  256. with the \fBTCL_MEM_DEBUG\fR flag turned off.  If you convert your application to
  257. use this facility, it will help you find memory over runs and lost memory.
  258. Note that memory allocated by a C library routine requiring freeing should
  259. still be freed with \fBfree\fR, since it calls \fBmalloc\fR rather than
  260. \fBckalloc\fR to do the allocation.
  261. '
  262. .SH FINDING MEMORY LEAKS
  263. .PP
  264. The function \fBTcl_DisplayMemory\fR will display a list of all currently
  265. allocated memory to the specified file.  The following information is
  266. displayed for each allocated block of memory: starting and ending addresses
  267. (excluding guard zone), size, source file where \fBckalloc\fR was called to
  268. allocate the block and line number in that file.  It is especially useful to
  269. call \fBTcl_DisplayMemory\fR after the Tcl interpreter has been deleted.
  270. '
  271. .SH ENABLING MEMORY DEBUGGING
  272. .PP
  273. To enable memory debugging, Tcl should be recompiled from scratch with
  274. \fBTCL_MEM_DEBUG\fR defined.  This will also compile in
  275. a non-stub version of \fBTcl_InitMemory\fR
  276. to add the \fBmemory\fR command to Tcl.
  277. .PP
  278. \fBTCL_MEM_DEBUG\fR must be either left defined for all modules or undefined
  279. for all modules that are going to be linked together.  If they are not, link
  280. errors will occur, with either \fBTclDbCkfree\fR and \fBTcl_DbCkalloc\fR or
  281. \fBTcl_Ckalloc\fR and \fBTcl_Ckfree\fR being undefined.
  282. '
  283. .SH GUARD ZONES
  284. .PP
  285. When memory debugging is enabled, whenever a call to \fBckalloc\fR is
  286. made, slightly more memory than requested is allocated so the memory debugging
  287. code can keep track
  288. of the allocated memory, and also 
  289. eight-byte ``guard zones'' are placed in front of and behind the space that 
  290. will be returned to the caller.  (The size of the guard zone is defined
  291. by the C #define \fBGUARD_SIZE\fR in \fIbaseline/src/ckalloc.c\fR -- it
  292. can be extended if you suspect large overwrite problems, at some cost in
  293. performance.)  A known pattern is written into the guard zones and,
  294. on a call to \fBckfree\fR, the guard zones of the space being freed
  295. are checked to see if either zone has been modified in any way.
  296. If one has been, the guard bytes and their new contents are identified,
  297. and a ``low guard failed'' or ``high guard failed'' message is issued.
  298. The ``guard failed'' message includes the address of the memory packet 
  299. and the file name and line number of the code that called \fBckfree\fR.
  300. This allows you to detect the common sorts of one-off problems, where
  301. not enough space was allocated to contain the data written, for example.
  302. '
  303. .SH THE MEMORY COMMAND
  304. '@help: misc/memory
  305. '@brief: display and debug memory problems
  306. '
  307. .TP
  308. .B memory \fIoptions\fR
  309. .br
  310. The Tcl \fBmemory\fR command gives the Tcl developer control of Tcl's memory
  311. debugging capabilities.  The memory command has several suboptions, which are
  312. described below.  It is only available when Tcl has been compiled with memory
  313. debugging enabled.
  314. '
  315. .TP
  316. .B memory \fBinfo\fR
  317. .br
  318. Produces a report containing the total allocations and frees since 
  319. Tcl began, the current packets allocated (the current
  320. number of calls to \fBckalloc\fR not met by a corresponding call 
  321. to \fBckfree\fR), the current bytes allocated, and the maximum number
  322. of packets and bytes allocated.
  323. '
  324. .TP
  325. .B memory \fBtrace\fR [\fBon|off\fR]
  326. .br
  327. Turns memory tracing on or off.
  328. When memory tracing is on, every call to \fBckalloc\fR causes a line of
  329. trace information to be written to \fIstderr\fR, consisting of the
  330. word \fIckalloc\fR, followed by the address returned, the amount of
  331. memory allocated, and the C filename and line number of the code performing
  332. the allocation, for example...
  333. .sp
  334.    \fBckalloc 40e478 98 tclProc.c 1406\fR
  335. .sp
  336. Calls to \fBckfree\fR are traced in the same manner, except that the
  337. word \fIckalloc\fR is replaced by the word \fIckfree\fR.
  338. '
  339. .TP
  340. .B memory \fBvalidate\fR [\fBon|off\fR]
  341. .br
  342. Turns memory vaidation on or off.
  343. When memory validation is enabled, on every call to
  344. \fBckalloc\fR or \fBckfree\fR, the guard zones are checked for every
  345. piece of memory currently in existence that was allocated by \fBckalloc\fR.
  346. This has a large performance impact and should only be used when
  347. overwrite problems are strongly suspected.  The advantage of enabling
  348. memory validation is that a guard zone overwrite can be detected on
  349. the first call to \fBckalloc\fR or \fBckfree\fR after the overwrite
  350. occurred, rather than when the specific memory with the overwritten
  351. guard zone(s) is freed, which may occur long after the overwrite occurred.
  352. '
  353. .TP
  354. .B memory \fBtrace_on_at_malloc\fR \fInnn\fR
  355. .br
  356. Enable memory tracing after \fInnn\fR \fBckallocs\fR have been performed.
  357. For example, if you enter \fBmemory trace_on_at_malloc 100\fR,
  358. after the 100th call to \fBckalloc\fR, memory trace information will begin
  359. being displayed for all allocations and frees.  Since there can be a lot
  360. of memory activity before a problem occurs, judicious use of this option
  361. can reduce the slowdown caused by tracing (and the amount of trace information
  362. produced), if you can identify a number of allocations that occur before
  363. the problem sets in.  The current number of memory allocations that have 
  364. occured since Tcl started is printed on a guard zone failure.
  365. .TP
  366. .B memory \fBbreak_on_malloc\fR \fInnn\fR
  367. .br
  368. After the \fBnnn\fR allocations have been performed, \fBckallocs\fR
  369. output a message to this effect and that it is now attempting to enter
  370. the C debugger.  Tcl will then issue a \fISIGINT\fR signal against itself.
  371. If you are running Tcl under a C debugger, it should then enter the debugger
  372. command mode.
  373. '
  374. .TP
  375. .B memory \fBdisplay\fR \fIfile\fR
  376. .br
  377. Write a list of all currently allocated memory to the specified file.
  378. '@endhelp
  379. '
  380. .SH DEBUGGING DIFFICULT MEMORY CORRUPTION PROBLEMS
  381. .PP
  382. Normally, Tcl compiled with memory debugging enabled will make it easy to isolate
  383. a corruption problem.  Turning on memory validation with the memory command
  384. can help isolate difficult problems.
  385. If you suspect (or know) that corruption is 
  386. occurring before the Tcl interpreter comes up far enough for you to
  387. issue commands, you can set \fBMEM_VALIDATE\fR define, recompile 
  388. tclCkalloc.c and rebuild Tcl.  This will enable memory validation
  389. from the first call to \fBckalloc\fR, again, at a large performance impact.
  390. .PP
  391. If you are desperate and validating memory on every call to \fBckalloc\fR
  392. and \fBckfree\fR isn't enough, you can explicitly call
  393. \fBTcl_ValidateAllMemory\fR directly at any point.  It takes a \fIchar *\fR
  394. and an \fIint\fR which are normally the filename and line number of the
  395. caller, but they can actually be anything you want.  Remember to remove
  396. the calls after you find the problem.
  397. '
  398. .SH KEYWORDS
  399. ckalloc, ckfree, free, memory, malloc
  400.