Provided by: manpages-zh_188.8.131.52-1_all
ckalloc, memory, ckfree, Tcl_DisplayMemory, Tcl_InitMemory, Tcl_ValidateAllMemory - 合法的內存分配接口
memory info memory trace [on|off] memory validate [on|off] memory trace_on_at_malloc nnn memory break_on_malloc nnn memory display file #include <tcl.h> char * ckalloc (unsigned size) void ckfree (char *ptr) int Tcl_DumpActiveMemory (char *fileName); void Tcl_ValidateAllMemory (char *file, int line) void Tcl_InitMemory (interp)
Tcl_Interp *fileName uint size in char *ptr in Tcl_Interp *interp in A pointer to the Tcl interpreter. char *file in The filename of the caller of Tcl_ValidateAllMemory. int line in The line number of the caller of Tcl_ValidateAllMemory. char *fileName in File to display list of active memory.
ckalloc This macro allocates memory, in the same manner as malloc, with the following differences: One, ckalloc checks the value returned from malloc (it calls malloc for you) and panics if the allocation request fails. Two, if enabled at compile time, a version of ckalloc with special memory debugging capabilities replaces the normal version of ckalloc, which aids in detecting memory overwrites and leaks (repeated allocations not matched by corresponding frees). Parameters: o size - The size of the memory block to be allocated. Returns: A pointer to the allocated memory block. ckfree This macro frees memory allocated by ckalloc. Like ckalloc, when memory debugging is enabled, ckfree has enhanced capabilities for detecting memory overwrites and leaks. It is very important that you use ckalloc when you need to allocate memory, and that you use ckfree to free it. Should you use malloc to allocate and ckfree to free, spurious memory validation errors will occur when memory debugging is enabled. Should you use free to free memory allocated by ckalloc, memory corruption will occur when memory debugging is enabled. Any memory that is to be become the property of the Tcl interpreter, such as result space, must be allocated with ckalloc. If it is absolutely necessary for an application to pass back malloced memory to Tcl, it will work only if Tcl is complied with the TCL_MEM_DEBUG flag turned off. If you convert your application to use this facility, it will help you find memory over runs and lost memory. Note that memory allocated by a C library routine requiring freeing should still be freed with free, since it calls malloc rather than ckalloc to do the allocation. Parmeters: o ptr - The address of a block to free, as returned by ckalloc. Tcl_DumpActiveMemory This function will output a list of all currently allocated memory to the specified file. The following information is outputted for each allocated block of memory: starting and ending addresses (excluding guard zone), size, source file where ckalloc was called to allocate the block and line number in that file. It is especially useful to call Tcl_DumpActiveMemory after the Tcl interpreter has been deleted. Parameters: o fileName - The name of the file to output the memory list to. Tcl_ValidateAllMemory Forces a validation of the guard zones of all currently allocated blocks of memory. Normally validation of a block occurs when its freed, unless full validation is enabled, in which case validation of all blocks occurs when ckalloc and ckfree are called. This function forces the validation to occur at any point. Parameters: o file - The file that this routine is being called from, normally __FILE__. o line - The line that this routine is being called from, normally __LINE__.
ENABLING MEMORY DEBUGGING
To enable memory debugging, Tcl should be recompiled from scratch with TCL_MEM_DEBUG defined. This will also compile in a non-stub version of Tcl_InitMemory to add the memory command to Tcl. TCL_MEM_DEBUG must be either left defined for all modules or undefined for all modules that are going to be linked together. If they are not, link errors will occur, with either TclDbCkfree and Tcl_DbCkalloc or Tcl_Ckalloc and Tcl_Ckfree being undefined.
When memory debugging is enabled, whenever a call to ckalloc is made, slightly more memory than requested is allocated so the memory debugging code can keep track of the allocated memory, and also eight-byte ``guard zones'' are placed in front of and behind the space that will be returned to the caller. (The size of the guard zone is defined by the C #define GUARD_SIZE in baseline/src/ckalloc.c -- it can be extended if you suspect large overwrite problems, at some cost in performance.) A known pattern is written into the guard zones and, on a call to ckfree, the guard zones of the space being freed are checked to see if either zone has been modified in any way. If one has been, the guard bytes and their new contents are identified, and a ``low guard failed'' or ``high guard failed'' message is issued. The ``guard failed'' message includes the address of the memory packet and the file name and line number of the code that called ckfree. This allows you to detect the common sorts of one-off problems, where not enough space was allocated to contain the data written, for example.
THE MEMORY COMMAND
memory options The Tcl memory command gives the Tcl developer control of Tcl's memory debugging capabilities. The memory command has several suboptions, which are described below. It is only available when Tcl has been compiled with memory debugging enabled. memory info 生成一個報告，包含自從 Tcl 啓動以來分配和釋放的(內存)總數，當前分配的包(未遇到相應的到 ckfree 的調用的到 ckalloc 的調用的當前數目)的數目，當前分配的字節數，和已分配的包和字節的最大的數目。 memory trace [on|off] 使內存跟蹤開啓或關閉。在開啓內存跟蹤的時候，對 ckalloc 的每次調用都導致向 stderr 寫一行跟蹤信息，其組成有字 ckalloc，隨後是返回的地址，分配的內存總數，和進行分配的 C 文件名和代碼的行數。例如:... ckalloc 40e478 98 tclProc.c 1406 Calls to ckfree are traced in the same manner, except that the word ckalloc is replaced by the word ckfree. memory validate [on|off] 使內存生效(validation)開啓或關閉。在開啓內存生效的時候，在對ckalloc 或 ckfree 的每次調用上，檢查用 ckalloc 分配的每塊現存的內存的守衛區(guard zone)。這有很大的性能影響而只在強烈懷疑有覆寫(overwrite)問題的時候才使用。開啓內存生效的益處是在覆寫發生之後第一次調用 ckalloc 或 ckfree 的時候就能檢測到守衛區覆寫，而不是在釋放有覆寫守衛區的內存的時候，釋放可能在內存覆寫發生之後才發生。 memory trace_on_at_malloc nnn 在進行了 count 數目 ckalloc 之後啓用內存跟蹤。例如，如果你鍵入了 memory trace_on_at_malloc 100，在第 100 次調用 ckalloc 之後，將對所有分配和釋放的內存顯示內存跟蹤信息。因爲在一個問題發生之前可能有許多內存活動，如果你能在問題出現( sets in)之前標識出一定數目的分配，決定(judicious)使用這個選項可以減輕跟蹤導致的速度變慢(和生成的跟蹤信息總數)。在發生一個守衛區錯誤時，輸出自從 Tcl 啓動以來發生的內存分配的當前數目。 memory break_on_malloc nnn 在進行了 count 數目的 ckalloc 分配之後，輸出一個(中斷)消息，表示它現在想進入 C 調試器。 Tcl 將向自身發出一個 SIGINT 信號。如果你在一個 C 調試器下運行 Tcl，它將接着進入調試器命令模式。 memory display file 向指定文件寫當前所有分配的內存的一個列表。
DEBUGGING DIFFICULT MEMORY CORRUPTION PROBLEMS
Normally, Tcl compiled with memory debugging enabled will make it easy to isolate a corruption problem. Turning on memory validation with the memory command can help isolate difficult problems. If you suspect (or know) that corruption is occurring before the Tcl interpreter comes up far enough for you to issue commands, you can set MEM_VALIDATE define, recompile tclCkalloc.c and rebuild Tcl. This will enable memory validation from the first call to ckalloc, again, at a large performance impact. If you are desperate and validating memory on every call to ckalloc and ckfree isn't enough, you can explicitly call Tcl_ValidateAllMemory directly at any point. It takes a char * and an int which are normally the filename and line number of the caller, but they can actually be anything you want. Remember to remove the calls after you find the problem.
ckalloc, ckfree, free, memory, malloc
《中國 Linux 論壇 man 手冊頁翻譯計劃》:
本頁面中文版由中文 man 手冊頁計劃提供。 中文 man 手冊頁計劃：https://github.com/man-pages-zh/manpages-zh