1 .TH MALLOC 3 "" "" "1.0"
4 .\" * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).
5 .\" * You may copy, distribute, and use this software as long as this
6 .\" * copyright statement is not removed.
8 .\" $Id: malloc.3,v 1.1.1.1 2000-09-22 15:33:26 hr Exp $
10 malloc \t- debugging malloc library
16 char * calloc(nelem,elsize);
19 int malloc_chain_check(flag);
22 char * realloc(ptr,size);
25 unsigned elsize,nelem,size;
32 This malloc library is a replacement for the standard library to be used
33 during software development/debugging. See the standard malloc(3) pages
34 for more information on the use of the following functions:
37 calloc(), free(), malloc(), realloc()
41 This library differs from the standard malloc library in the
44 1. Each malloc segment contains a magic number so that free can
45 verify that the pointer passed points to a valid malloc segment.
47 2. Each malloc segment is filled with a non-zero pattern so that code that
48 depends upon malloc segments being null will fail.
50 3. The size of each segment will be at least 1 byte larger than requested
51 and the extra bytes will be filled with a non-zero pattern. When free is
52 called, it will verify that you did not go beyond the number of bytes
55 4. When a segment is freed, it will be filled with a different non-zero pattern
56 to ensure that the program doesn't depend upon the use of already freed data.
58 5. Whenever any of the string or memory functions (str*, b*, mem*) are
59 called with a pointer that is within the malloc arena, the operation is
60 checked to verify that it does not overrun the malloced segment. A failure
61 of this check is considered a "warning level error" (described later) and
62 is handled accordingly.
64 7. Run time checking can include verification of the malloc chain at each
65 and every call to one of the malloc functions or manually by calling the
66 malloc_chain_check function.
68 6. When a problem is found, the action taken is specified at runtime by
69 environment variables or at compile time by the use of the mallopt()
72 There are two arbitrary levels of errors, warning and fatal, that this
73 library will detect. They are broken down as follows:
77 Warning messages include:
81 Calling free with a bad pointer
84 Calling a bstring/string/memory (3) function which will go beyond
85 the end of a malloc block. Note that the library function is
86 not modified to refuse the operation.
92 Detectable corruption to the malloc chain.
96 The error handling for each level (warning or fatal) are specified using
97 environment variables or mallopt(). The coding for the error handling is
103 0 - continue operations
105 1 - drop core and exit
109 3 - drop core, but continue executing. Core files will
110 be placed into core.[PID].[counter] i.e: core.00123.001
112 128 - dump malloc chain and continue
114 129 - dump malloc chain, dump core, and exit
116 130 - dump malloc chain, exit
118 131 - dump malloc chain, dump core, continue processing
121 In addition error messages can be placed into an error file.
123 \fBmalloc_opt\fP() is used to set the malloc debugging options. The
124 following options can be set:
128 MALLOC_WARN - set the error handling for warning level errors. \fBval.i\fP is
129 an integer that can contain any one of the following values:
132 M_HANDLE_IGNORE - ignore error
134 M_HANDLE_ABORT - drop core and exit
136 M_HANDLE_EXIT - just exit (no core drop)
138 M_HANDLE_CORE - drop core, but keep on going
142 In addition, M_HANDLE_DUMP may be or'd in to cause a dump of the current
146 MALLOC_FATAL - set the error handling for fatal level errors. \fBval.i\fP is
147 equivalent to \fBval.i\fP for MALLOC_WARN.
150 MALLOC_ERRFILE - set the destination for malloc error messages. \fBval.str\fP
151 is a pointer to a character string containing the name of the file to be used
155 MALLOC_CKCHAIN - set the malloc chain checking flag. If \fBval.i\fP is
156 non-zero, chain checking at every call to malloc is turned on.
159 For example, to set up the session to generate a core file for
160 every malloc warning, to drop core and exit on a malloc fatal, and
161 to log all messages to the file "malloc_log" do the following:
166 malloc_opt(MALLOC_WARN,131);
167 malloc_opt(MALLOC_FATAL,1);
168 malloc_opt(MALLOC_ERRFILE,"malloc_log");
173 \fBmalloc_opt\fP() can be used to set/alter the debugging options at any
176 \fBmalloc_dump\fP() will dump a table of the malloc arena showing all
177 allocated/freed segments and the first few bytes of data in each segment.
178 \fBfd\fP is the file descriptor to write the data to.
180 \fBmalloc_chain_check\fP() will check the status of the malloc arena.
181 If \fBflag\fP is non-zero, an error found in the chain will cause a
182 fatal error. \fBmalloc_chain_check\fP() returns zero when there are no
183 problems found in the malloc chain, non-zero otherwise.
184 .SH "ENVIRONMENT VARIABLES"
185 Environment variables can be used to control error handling, error logging
186 and malloc chain checking at run time. The following environment variables
189 MALLOC_WARN - specifies the error handling for warning errors
191 MALLOC_FATAL - specifies the error handling for fatal errors
193 MALLOC_ERRFILE - specifies the error log file for error messages.
195 MALLOC_CKCHAIN - if 1, turns on malloc chain checking at every call to any
196 of the malloc functions.
198 For example, to set up the session to generate a core file for
199 every malloc warning, to drop core and exit on a malloc fatal, and
200 to log all messages to the file "malloc_log" do the following:
206 MALLOC_ERRFILE=malloc_log
208 export MALLOC_WARN MALLOC_FATAL MALLOC_ERRFILE
212 This malloc library and it's associated string and memory functions are
213 much less efficient than the standard functions due in part to the extra
214 error checking. You do not want to use this library when generating a
215 production (i.e. releasable) version of your software. It should only
216 be used during development and testing.
221 Virtual Technologies Incorporated
223 uunet!virtech!cpcahil