📄 malloc.3
字号:
.TH MALLOC 3 "" "" "1.0".ds ]T .\"/*.\" * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil). .\" * You may copy, distribute, and use this software as long as this.\" * copyright statement is not removed..\" */.\" $Id: malloc.3,v 1.1 90/05/11 15:53:20 cpcahil Exp $.SH NAMEmalloc \t- debugging malloc library.SH SYNOPSIS.ft B.nf#include <malloc.h>char * calloc(nelem,elsize);void free(ptr);char * malloc(size);int malloc_chain_check(flag);void malloc_dump(fd);int mallopt(cmd,value)char * realloc(ptr,size);int cmd,fd,flag;unsigned elsize,nelem,size;char * ptr;union val value;.fi.ft R.SH DESCRIPTIONThis malloc library is a replacement for the standard library to be usedduring software development/debugging. See the standard malloc(3) pagesfor more information on the use of the following functions:.nf.in +.5icalloc(), free(), malloc(), realloc().in -.5i.fi.spThis library differs from the standard malloc library in thefollowing ways:.P1. Each malloc segment contains a magic number so that free can verify that the pointer passed points to a valid malloc segment..P2. Each malloc segment is filled with a non-zero pattern so that code thatdepends upon malloc segments being null will fail..P3. The size of each segment will be at least 1 byte larger than requestedand the extra bytes will be filled with a non-zero pattern. When free iscalled, it will verify that you did not go beyond the number of bytes you asked for..P4. When a segment is freed, it will be filled with a different non-zero patternto ensure that the program doesn't depend upon the use of already freed data..P5. Whenever any of the string or memory functions (str*, b*, mem*) are called with a pointer that is within the malloc arena, the operation ischecked to verify that it does not overrun the malloced segment. A failureof this check is considered a "warning level error" (described later) andis handled accordingly..P7. Run time checking can include verification of the malloc chain at eachand every call to one of the malloc functions or manually by calling themalloc_chain_check function..P6. When a problem is found, the action taken is specified at runtime byenvironment variables or at compile time by the use of the mallopt()function..PThere are two arbitrary levels of errors, warning and fatal, that thislibrary will detect. They are broken down as follows:.P.nf.in +.25iWarning messages include:.sp.in +.5i.ti -.25iCalling free with a bad pointer.br.ti -.25iCalling a bstring/string/memory (3) function which will go beyondthe end of a malloc block. Note that the library function isnot modified to refuse the operation..sp.in -.5iFatal errors are:.in +.5i.ti -.25iDetectable corruption to the malloc chain..in -.5i.in -.25i.PThe error handling for each level (warning or fatal) are specified usingenvironment variables or mallopt(). The coding for the error handling isas follows:.sp.nf.in +.5i.ti -.25i 0 - continue operations.ti -.25i 1 - drop core and exit.ti -.25i 2 - just exit.ti -.25i 3 - drop core, but continue executing. Core files willbe placed into core.[PID].[counter] i.e: core.00123.001.ti -.25i128 - dump malloc chain and continue.ti -.25i129 - dump malloc chain, dump core, and exit.ti -.25i130 - dump malloc chain, exit.ti -.25i131 - dump malloc chain, dump core, continue processing.in -.5i.PIn addition error messages can be placed into an error file..P\fBmalloc_opt\fP() is used to set the malloc debugging options. Thefollowing options can be set:.br.sp.in +.5iMALLOC_WARN - set the error handling for warning level errors. \fBval\fP isan integer that can contain any one of the following values:.sp.in +.5iM_HANDLE_IGNORE - ignore error.brM_HANDLE_ABORT - drop core and exit.brM_HANDLE_EXIT - just exit (no core drop).brM_HANDLE_CORE - drop core, but keep on going.br.in -.5i.spIn addition, M_HANDLE_DUMP may be or'd in to cause a dump of the currentmalloc chain..br.spMALLOC_FATAL - set the error handling for fatal level errors. \fBval\fP isequivalent to \fBval\fP for MALLOC_WARN..br.spMALLOC_ERRFILE - set the destination for malloc error messages. \fBval\fP isa pointer to a character string containing the name of the file to be usedfor error messages..br.spMALLOC_CKCHAIN - set the malloc chain checking flag. If \fBval\fP isnon-zero, chain checking at every call to malloc is turned on..br.spFor example, to set up the session to generate a core file forevery malloc warning, to drop core and exit on a malloc fatal, and to log all messages to the file "malloc_log" do the following:.sp.nf.in +.5i#include <malloc.h>malloc_opt(MALLOC_WARN,131);malloc_opt(MALLOC_FATAL,1);malloc_opt(MALLOC_ERRFILE,"malloc_log");.in -.5i.fi.in -.5i.sp\fBmalloc_opt\fP() can be used to set/alter the debugging options at anytime..P\fBmalloc_dump\fP() will dump a table of the malloc arena showing allallocated/freed segments and the first few bytes of data in each segment.\fBfd\fP is the file descriptor to write the data to..P\fBmalloc_chain_check\fP() will check the status of the malloc arena.If \fBflag\fP is non-zero, an error found in the chain will cause a fatal error. \fBmalloc_chain_check\fP() returns zero when there are noproblems found in the malloc chain, non-zero otherwise..SH "ENVIRONMENT VARIABLES"Environment variables can be used to control error handling, error loggingand malloc chain checking at run time. The following environment variablesare used:.PMALLOC_WARN - specifies the error handling for warning errors.brMALLOC_FATAL - specifies the error handling for fatal errors.brMALLOC_ERRFILE - specifies the error log file for error messages. .brMALLOC_CKCHAIN - if 1, turns on malloc chain checking at every call to anyof the malloc functions..PFor example, to set up the session to generate a core file forevery malloc warning, to drop core and exit on a malloc fatal, and to log all messages to the file "malloc_log" do the following:.sp.nf.in +.5iMALLOC_WARN=131MALLOC_FATAL=1MALLOC_ERRFILE=malloc_logexport MALLOC_WARN MALLOC_FATAL MALLOC_ERRFILE.in -.5i.fi.SH WARNINGSThis malloc library and it's associated string and memory functions aremuch less efficient than the standard functions due in part to the extraerror checking. You do not want to use this library when generating aproduction (i.e. releasable) version of your software. It should onlybe used during development and testing..SH SEE ALSOstat(2).SH AUTHORConor P. CahillVirtual Technologies Incorporated.spuunet!virtech!cpcahil
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -