00001 /* 00002 ** MEMWATCH.H 00003 ** Nonintrusive ANSI C memory leak / overwrite detection 00004 ** Copyright (C) 1992-2002 Johan Lindh 00005 ** All rights reserved. 00006 ** Version 2.71 00007 ** 00008 ************************************************************************ 00009 ** 00010 ** PURPOSE: 00011 ** 00012 ** MEMWATCH has been written to allow guys and gals that like to 00013 ** program in C a public-domain memory error control product. 00014 ** I hope you'll find it's as advanced as most commercial packages. 00015 ** The idea is that you use it during the development phase and 00016 ** then remove the MEMWATCH define to produce your final product. 00017 ** MEMWATCH is distributed in source code form in order to allow 00018 ** you to compile it for your platform with your own compiler. 00019 ** It's aim is to be 100% ANSI C, but some compilers are more stingy 00020 ** than others. If it doesn't compile without warnings, please mail 00021 ** me the configuration of operating system and compiler you are using 00022 ** along with a description of how to modify the source, and the version 00023 ** number of MEMWATCH that you are using. 00024 ** 00025 ************************************************************************ 00026 00027 This file is part of MEMWATCH. 00028 00029 MEMWATCH is free software; you can redistribute it and/or modify 00030 it under the terms of the GNU General Public License as published by 00031 the Free Software Foundation; either version 2 of the License, or 00032 (at your option) any later version. 00033 00034 MEMWATCH is distributed in the hope that it will be useful, 00035 but WITHOUT ANY WARRANTY; without even the implied warranty of 00036 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00037 GNU General Public License for more details. 00038 00039 You should have received a copy of the GNU General Public License 00040 along with MEMWATCH; if not, write to the Free Software 00041 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00042 00043 ************************************************************************ 00044 ** 00045 ** REVISION HISTORY: 00046 ** 00047 ** 920810 JLI [1.00] 00048 ** 920830 JLI [1.10 double-free detection] 00049 ** 920912 JLI [1.15 mwPuts, mwGrab/Drop, mwLimit] 00050 ** 921022 JLI [1.20 ASSERT and VERIFY] 00051 ** 921105 JLI [1.30 C++ support and TRACE] 00052 ** 921116 JLI [1.40 mwSetOutFunc] 00053 ** 930215 JLI [1.50 modified ASSERT/VERIFY] 00054 ** 930327 JLI [1.51 better auto-init & PC-lint support] 00055 ** 930506 JLI [1.55 MemWatch class, improved C++ support] 00056 ** 930507 JLI [1.60 mwTest & CHECK()] 00057 ** 930809 JLI [1.65 Abort/Retry/Ignore] 00058 ** 930820 JLI [1.70 data dump when unfreed] 00059 ** 931016 JLI [1.72 modified C++ new/delete handling] 00060 ** 931108 JLI [1.77 mwSetAssertAction() & some small changes] 00061 ** 940110 JLI [1.80 no-mans-land alloc/checking] 00062 ** 940328 JLI [2.00 version 2.0 rewrite] 00063 ** Improved NML (no-mans-land) support. 00064 ** Improved performance (especially for free()ing!). 00065 ** Support for 'read-only' buffers (checksums) 00066 ** ^^ NOTE: I never did this... maybe I should? 00067 ** FBI (free'd block info) tagged before freed blocks 00068 ** Exporting of the mwCounter variable 00069 ** mwBreakOut() localizes debugger support 00070 ** Allocation statistics (global, per-module, per-line) 00071 ** Self-repair ability with relinking 00072 ** 950913 JLI [2.10 improved garbage handling] 00073 ** 951201 JLI [2.11 improved auto-free in emergencies] 00074 ** 960125 JLI [X.01 implemented auto-checking using mwAutoCheck()] 00075 ** 960514 JLI [2.12 undefining of existing macros] 00076 ** 960515 JLI [2.13 possibility to use default new() & delete()] 00077 ** 960516 JLI [2.20 suppression of file flushing on unfreed msgs] 00078 ** 960516 JLI [2.21 better support for using MEMWATCH with DLL's] 00079 ** 960710 JLI [X.02 multiple logs and mwFlushNow()] 00080 ** 960801 JLI [2.22 merged X.01 version with current] 00081 ** 960805 JLI [2.30 mwIsXXXXAddr() to avoid unneeded GP's] 00082 ** 960805 JLI [2.31 merged X.02 version with current] 00083 ** 961002 JLI [2.32 support for realloc() + fixed STDERR bug] 00084 ** 961222 JLI [2.40 added mwMark() & mwUnmark()] 00085 ** 970101 JLI [2.41 added over/underflow checking after failed ASSERT/VERIFY] 00086 ** 970113 JLI [2.42 added support for PC-Lint 7.00g] 00087 ** 970207 JLI [2.43 added support for strdup()] 00088 ** 970209 JLI [2.44 changed default filename to lowercase] 00089 ** 970405 JLI [2.45 fixed bug related with atexit() and some C++ compilers] 00090 ** 970723 JLI [2.46 added MW_ARI_NULLREAD flag] 00091 ** 970813 JLI [2.47 stabilized marker handling] 00092 ** 980317 JLI [2.48 ripped out C++ support; wasn't working good anyway] 00093 ** 980318 JLI [2.50 improved self-repair facilities & SIGSEGV support] 00094 ** 980417 JLI [2.51 more checks for invalid addresses] 00095 ** 980512 JLI [2.52 moved MW_ARI_NULLREAD to occur before aborting] 00096 ** 990112 JLI [2.53 added check for empty heap to mwIsOwned] 00097 ** 990217 JLI [2.55 improved the emergency repairs diagnostics and NML] 00098 ** 990224 JLI [2.56 changed ordering of members in structures] 00099 ** 990303 JLI [2.57 first maybe-fixit-for-hpux test] 00100 ** 990516 JLI [2.58 added 'static' to the definition of mwAutoInit] 00101 ** 990517 JLI [2.59 fixed some high-sensitivity warnings] 00102 ** 990610 JLI [2.60 fixed some more high-sensitivity warnings] 00103 ** 990715 JLI [2.61 changed TRACE/ASSERT/VERIFY macro names] 00104 ** 991001 JLI [2.62 added CHECK_BUFFER() and mwTestBuffer()] 00105 ** 991007 JLI [2.63 first shot at a 64-bit compatible version] 00106 ** 991009 JLI [2.64 undef's strdup() if defined, mwStrdup made const] 00107 ** 000704 JLI [2.65 added some more detection for 64-bits] 00108 ** 010502 JLI [2.66 incorporated some user fixes] 00109 ** [mwRelink() could print out garbage pointer (thanks mac@phobos.ca)] 00110 ** [added array destructor for C++ (thanks rdasilva@connecttel.com)] 00111 ** [added mutex support (thanks rdasilva@connecttel.com)] 00112 ** 010531 JLI [2.67 fix: mwMutexXXX() was declared even if MW_HAVE_MUTEX was not defined] 00113 ** 010619 JLI [2.68 fix: mwRealloc() could leave the mutex locked] 00114 ** 020918 JLI [2.69 changed to GPL, added C++ array allocation by Howard Cohen] 00115 ** 030212 JLI [2.70 mwMalloc() bug for very large allocations (4GB on 32bits)] 00116 ** 030520 JLI [2.71 added ULONG_LONG_MAX as a 64-bit detector (thanks Sami Salonen)] 00117 ** 00118 ** To use, simply include 'MEMWATCH.H' as a header file, 00119 ** and add MEMWATCH.C to your list of files, and define the macro 00120 ** 'MEMWATCH'. If this is not defined, MEMWATCH will disable itself. 00121 ** 00122 ** To call the standard C malloc / realloc / calloc / free; use mwMalloc_(), 00123 ** mwCalloc_() and mwFree_(). Note that mwFree_() will correctly 00124 ** free both malloc()'d memory as well as mwMalloc()'d. 00125 ** 00126 ** 980317: C++ support has been disabled. 00127 ** The code remains, but is not compiled. 00128 ** 00129 ** For use with C++, which allows use of inlining in header files 00130 ** and class specific new/delete, you must also define 'new' as 00131 ** 'mwNew' and 'delete' as 'mwDelete'. Do this *after* you include 00132 ** C++ header files from libraries, otherwise you can mess up their 00133 ** class definitions. If you don't define these, the C++ allocations 00134 ** will not have source file and line number information. Also note, 00135 ** most C++ class libraries implement their own C++ memory management, 00136 ** and don't allow anyone to override them. MFC belongs to this crew. 00137 ** In these cases, the only thing to do is to use MEMWATCH_NOCPP. 00138 ** 00139 ** You can capture output from MEMWATCH using mwSetOutFunc(). 00140 ** Just give it the adress of a "void myOutFunc(int c)" function, 00141 ** and all characters to be output will be redirected there. 00142 ** 00143 ** A failing ASSERT() or VERIFY() will normally always abort your 00144 ** program. This can be changed using mwSetAriFunc(). Give it a 00145 ** pointer to a "int myAriFunc(const char *)" function. Your function 00146 ** must ask the user whether to Abort, Retry or Ignore the trap. 00147 ** Return 2 to Abort, 1 to Retry or 0 to Ignore. Beware retry; it 00148 ** causes the expression to be evaluated again! MEMWATCH has a 00149 ** default ARI handler. It's disabled by default, but you can enable 00150 ** it by calling 'mwDefaultAri()'. Note that this will STILL abort 00151 ** your program unless you define MEMWATCH_STDIO to allow MEMWATCH 00152 ** to use the standard C I/O streams. Also, setting the ARI function 00153 ** will cause MEMWATCH *NOT* to write the ARI error to stderr. The 00154 ** error string is passed to the ARI function instead, as the 00155 ** 'const char *' parameter. 00156 ** 00157 ** You can disable MEMWATCH's ASSERT/VERIFY and/or TRACE implementations. 00158 ** This can be useful if you're using a debug terminal or smart debugger. 00159 ** Disable them by defining MW_NOASSERT, MW_NOVERIFY or MW_NOTRACE. 00160 ** 00161 ** MEMWATCH fills all allocated memory with the byte 0xFE, so if 00162 ** you're looking at erroneous data which are all 0xFE:s, the 00163 ** data probably was not initialized by you. The exception is 00164 ** calloc(), which will fill with zero's. All freed buffers are 00165 ** zapped with 0xFD. If this is what you look at, you're using 00166 ** data that has been freed. If this is the case, be aware that 00167 ** MEMWATCH places a 'free'd block info' structure immediately 00168 ** before the freed data. This block contains info about where 00169 ** the block was freed. The information is in readable text, 00170 ** in the format "FBI<counter>filename(line)", for example: 00171 ** "FBI<267>test.c(12)". Using FBI's slows down free(), so it's 00172 ** disabled by default. Use mwFreeBufferInfo(1) to enable it. 00173 ** 00174 ** To aid in tracking down wild pointer writes, MEMWATCH can perform 00175 ** no-mans-land allocations. No-mans-land will contain the byte 0xFC. 00176 ** MEMWATCH will, when this is enabled, convert recently free'd memory 00177 ** into NML allocations. 00178 ** 00179 ** MEMWATCH protects it's own data buffers with checksums. If you 00180 ** get an internal error, it means you're overwriting wildly, 00181 ** or using an uninitialized pointer. 00182 ** 00183 ************************************************************************ 00184 ** 00185 ** Note when compiling with Microsoft C: 00186 ** - MSC ignores fflush() by default. This is overridden, so that 00187 ** the disk log will always be current. 00188 ** 00189 ** This utility has been tested with: 00190 ** PC-lint 7.0k, passed as 100% ANSI C compatible 00191 ** Microsoft Visual C++ on Win16 and Win32 00192 ** Microsoft C on DOS 00193 ** SAS C on an Amiga 500 00194 ** Gnu C on a PC running Red Hat Linux 00195 ** ...and using an (to me) unknown compiler on an Atari machine. 00196 ** 00197 ************************************************************************ 00198 ** 00199 ** Format of error messages in MEMWATCH.LOG: 00200 ** message: <sequence-number> filename(linenumber), information 00201 ** 00202 ** Errors caught by MemWatch, when they are detected, and any 00203 ** actions taken besides writing to the log file MEMWATCH.LOG: 00204 ** 00205 ** Double-freeing: 00206 ** A pointer that was recently freed and has not since been 00207 ** reused was freed again. The place where the previous free() 00208 ** was executed is displayed. 00209 ** Detect: delete or free() using the offending pointer. 00210 ** Action: The delete or free() is cancelled, execution continues. 00211 ** Underflow: 00212 ** You have written just ahead of the allocated memory. 00213 ** The size and place of the allocation is displayed. 00214 ** Detect: delete or free() of the damaged buffer. 00215 ** Action: The buffer is freed, but there may be secondary damage. 00216 ** Overflow: 00217 ** Like underflow, but you've written after the end of the buffer. 00218 ** Detect: see Underflow. 00219 ** Action: see Underflow. 00220 ** WILD free: 00221 ** An unrecognized pointer was passed to delete or free(). 00222 ** The pointer may have been returned from a library function; 00223 ** in that case, use mwFree_() to force free() of it. 00224 ** Also, this may be a double-free, but the previous free was 00225 ** too long ago, causing MEMWATCH to 'forget' it. 00226 ** Detect: delete or free() of the offending pointer. 00227 ** Action: The delete or free() is cancelled, execution continues. 00228 ** NULL free: 00229 ** It's unclear to me whether or not freeing of NULL pointers 00230 ** is legal in ANSI C, therefore a warning is written to the log file, 00231 ** but the error counter remains the same. This is legal using C++, 00232 ** so the warning does not appear with delete. 00233 ** Detect: When you free(NULL). 00234 ** Action: The free() is cancelled. 00235 ** Failed: 00236 ** A request to allocate memory failed. If the allocation is 00237 ** small, this may be due to memory depletion, but is more likely 00238 ** to be memory fragmentation problems. The amount of memory 00239 ** allocated so far is displayed also. 00240 ** Detect: When you new, malloc(), realloc() or calloc() memory. 00241 ** Action: NULL is returned. 00242 ** Realloc: 00243 ** A request to re-allocate a memory buffer failed for reasons 00244 ** other than out-of-memory. The specific reason is shown. 00245 ** Detect: When you realloc() 00246 ** Action: realloc() is cancelled, NULL is returned 00247 ** Limit fail: 00248 ** A request to allocate memory failed since it would violate 00249 ** the limit set using mwLimit(). mwLimit() is used to stress-test 00250 ** your code under simulated low memory conditions. 00251 ** Detect: At new, malloc(), realloc() or calloc(). 00252 ** Action: NULL is returned. 00253 ** Assert trap: 00254 ** An ASSERT() failed. The ASSERT() macro works like C's assert() 00255 ** macro/function, except that it's interactive. See your C manual. 00256 ** Detect: On the ASSERT(). 00257 ** Action: Program ends with an advisory message to stderr, OR 00258 ** Program writes the ASSERT to the log and continues, OR 00259 ** Program asks Abort/Retry/Ignore? and takes that action. 00260 ** Verify trap: 00261 ** A VERIFY() failed. The VERIFY() macro works like ASSERT(), 00262 ** but if MEMWATCH is not defined, it still evaluates the 00263 ** expression, but it does not act upon the result. 00264 ** Detect: On the VERIFY(). 00265 ** Action: Program ends with an advisory message to stderr, OR 00266 ** Program writes the VERIFY to the log and continues, OR 00267 ** Program asks Abort/Retry/Ignore? and takes that action. 00268 ** Wild pointer: 00269 ** A no-mans-land buffer has been written into. MEMWATCH can 00270 ** allocate and distribute chunks of memory solely for the 00271 ** purpose of trying to catch random writes into memory. 00272 ** Detect: Always on CHECK(), but can be detected in several places. 00273 ** Action: The error is logged, and if an ARI handler is installed, 00274 ** it is executed, otherwise, execution continues. 00275 ** Unfreed: 00276 ** A memory buffer you allocated has not been freed. 00277 ** You are informed where it was allocated, and whether any 00278 ** over or underflow has occured. MemWatch also displays up to 00279 ** 16 bytes of the data, as much as it can, in hex and text. 00280 ** Detect: When MemWatch terminates. 00281 ** Action: The buffer is freed. 00282 ** Check: 00283 ** An error was detected during a CHECK() operation. 00284 ** The associated pointer is displayed along with 00285 ** the file and line where the CHECK() was executed. 00286 ** Followed immediately by a normal error message. 00287 ** Detect: When you CHECK() 00288 ** Action: Depends on the error 00289 ** Relink: 00290 ** After a MEMWATCH internal control block has been trashed, 00291 ** MEMWATCH tries to repair the damage. If successful, program 00292 ** execution will continue instead of aborting. Some information 00293 ** about the block may be gone permanently, though. 00294 ** Detect: N/A 00295 ** Action: Relink successful: program continues. 00296 ** Relink fails: program aborts. 00297 ** Internal: 00298 ** An internal error is flagged by MEMWATCH when it's control 00299 ** structures have been damaged. You are likely using an uninitialized 00300 ** pointer somewhere in your program, or are zapping memory all over. 00301 ** The message may give you additional diagnostic information. 00302 ** If possible, MEMWATCH will recover and continue execution. 00303 ** Detect: Various actions. 00304 ** Action: Whatever is needed 00305 ** Mark: 00306 ** The program terminated without umarking all marked pointers. Marking 00307 ** can be used to track resources other than memory. mwMark(pointer,text,...) 00308 ** when the resource is allocated, and mwUnmark(pointer) when it's freed. 00309 ** The 'text' is displayed for still marked pointers when the program 00310 ** ends. 00311 ** Detect: When MemWatch terminates. 00312 ** Action: The error is logged. 00313 ** 00314 ** 00315 ************************************************************************ 00316 ** 00317 ** The author may be reached by e-mail at the address below. If you 00318 ** mail me about source code changes in MEMWATCH, remember to include 00319 ** MW's version number. 00320 ** 00321 ** Johan Lindh 00322 ** johan@linkdata.se 00323 ** 00324 ** The latest version of MEMWATCH may be downloaded from 00325 ** http://www.linkdata.se/ 00326 */ 00327 00328 #ifndef __MEMWATCH_H 00329 #define __MEMWATCH_H 00330 00331 /* Make sure that malloc(), realloc(), calloc() and free() are declared. */ 00332 /*lint -save -e537 */ 00333 #include <stdlib.h> 00334 /*lint -restore */ 00335 00336 #ifdef __cplusplus 00337 extern "C" { 00338 #endif 00339 00340 00341 /* 00342 ** Constants used 00343 ** All MEMWATCH constants start with the prefix MW_, followed by 00344 ** a short mnemonic which indicates where the constant is used, 00345 ** followed by a descriptive text about it. 00346 */ 00347 00348 #define MW_ARI_NULLREAD 0x10 /* Null read (to start debugger) */ 00349 #define MW_ARI_ABORT 0x04 /* ARI handler says: abort program! */ 00350 #define MW_ARI_RETRY 0x02 /* ARI handler says: retry action! */ 00351 #define MW_ARI_IGNORE 0x01 /* ARI handler says: ignore error! */ 00352 00353 #define MW_VAL_NEW 0xFE /* value in newly allocated memory */ 00354 #define MW_VAL_DEL 0xFD /* value in newly deleted memory */ 00355 #define MW_VAL_NML 0xFC /* value in no-mans-land */ 00356 #define MW_VAL_GRB 0xFB /* value in grabbed memory */ 00357 00358 #define MW_TEST_ALL 0xFFFF /* perform all tests */ 00359 #define MW_TEST_CHAIN 0x0001 /* walk the heap chain */ 00360 #define MW_TEST_ALLOC 0x0002 /* test allocations & NML guards */ 00361 #define MW_TEST_NML 0x0004 /* test all-NML areas for modifications */ 00362 00363 #define MW_NML_NONE 0 /* no NML */ 00364 #define MW_NML_FREE 1 /* turn FREE'd memory into NML */ 00365 #define MW_NML_ALL 2 /* all unused memory is NML */ 00366 #define MW_NML_DEFAULT 0 /* the default NML setting */ 00367 00368 #define MW_STAT_GLOBAL 0 /* only global statistics collected */ 00369 #define MW_STAT_MODULE 1 /* collect statistics on a module basis */ 00370 #define MW_STAT_LINE 2 /* collect statistics on a line basis */ 00371 #define MW_STAT_DEFAULT 0 /* the default statistics setting */ 00372 00373 /* 00374 ** MemWatch internal constants 00375 ** You may change these and recompile MemWatch to change the limits 00376 ** of some parameters. Respect the recommended minimums! 00377 */ 00378 #define MW_TRACE_BUFFER 2048 /* (min 160) size of TRACE()'s output buffer */ 00379 #define MW_FREE_LIST 64 /* (min 4) number of free()'s to track */ 00380 00381 /* 00382 ** Exported variables 00383 ** In case you have to remove the 'const' keyword because your compiler 00384 ** doesn't support it, be aware that changing the values may cause 00385 ** unpredictable behaviour. 00386 ** - mwCounter contains the current action count. You can use this to 00387 ** place breakpoints using a debugger, if you want. 00388 */ 00389 #ifndef __MEMWATCH_C 00390 extern const unsigned long mwCounter; 00391 #endif 00392 00393 /* 00394 ** System functions 00395 ** Normally, it is not nessecary to call any of these. MEMWATCH will 00396 ** automatically initialize itself on the first MEMWATCH function call, 00397 ** and set up a call to mwAbort() using atexit(). Some C++ implementations 00398 ** run the atexit() chain before the program has terminated, so you 00399 ** may have to use mwInit() or the MemWatch C++ class to get good 00400 ** behaviour. 00401 ** - mwInit() can be called to disable the atexit() usage. If mwInit() 00402 ** is called directly, you must call mwTerm() to end MemWatch, or 00403 ** mwAbort(). 00404 ** - mwTerm() is usually not nessecary to call; but if called, it will 00405 ** call mwAbort() if it finds that it is cancelling the 'topmost' 00406 ** mwInit() call. 00407 ** - mwAbort() cleans up after MEMWATCH, reports unfreed buffers, etc. 00408 */ 00409 void mwInit( void ); 00410 void mwTerm( void ); 00411 void mwAbort( void ); 00412 00413 /* 00414 ** Setup functions 00415 ** These functions control the operation of MEMWATCH's protective features. 00416 ** - mwFlushNow() causes MEMWATCH to flush it's buffers. 00417 ** - mwDoFlush() controls whether MEMWATCH flushes the disk buffers after 00418 ** writes. The default is smart flushing: MEMWATCH will not flush buffers 00419 ** explicitly until memory errors are detected. Then, all writes are 00420 ** flushed until program end or mwDoFlush(0) is called. 00421 ** - mwLimit() sets the allocation limit, an arbitrary limit on how much 00422 ** memory your program may allocate in bytes. Used to stress-test app. 00423 ** Also, in virtual-memory or multitasking environs, puts a limit on 00424 ** how much MW_NML_ALL can eat up. 00425 ** - mwGrab() grabs up X kilobytes of memory. Allocates actual memory, 00426 ** can be used to stress test app & OS both. 00427 ** - mwDrop() drops X kilobytes of grabbed memory. 00428 ** - mwNoMansLand() sets the behaviour of the NML logic. See the 00429 ** MW_NML_xxx for more information. The default is MW_NML_DEFAULT. 00430 ** - mwStatistics() sets the behaviour of the statistics collector. See 00431 ** the MW_STAT_xxx defines for more information. Default MW_STAT_DEFAULT. 00432 ** - mwFreeBufferInfo() enables or disables the tagging of free'd buffers 00433 ** with freeing information. This information is written in text form, 00434 ** using sprintf(), so it's pretty slow. Disabled by default. 00435 ** - mwAutoCheck() performs a CHECK() operation whenever a MemWatch function 00436 ** is used. Slows down performance, of course. 00437 ** - mwCalcCheck() calculates checksums for all data buffers. Slow! 00438 ** - mwDumpCheck() logs buffers where stored & calc'd checksums differ. Slow!! 00439 ** - mwMark() sets a generic marker. Returns the pointer given. 00440 ** - mwUnmark() removes a generic marker. If, at the end of execution, some 00441 ** markers are still in existence, these will be reported as leakage. 00442 ** returns the pointer given. 00443 */ 00444 void mwFlushNow( void ); 00445 void mwDoFlush( int onoff ); 00446 void mwLimit( long bytes ); 00447 unsigned mwGrab( unsigned kilobytes ); 00448 unsigned mwDrop( unsigned kilobytes ); 00449 void mwNoMansLand( int mw_nml_level ); 00450 void mwStatistics( int level ); 00451 void mwFreeBufferInfo( int onoff ); 00452 void mwAutoCheck( int onoff ); 00453 void mwCalcCheck( void ); 00454 void mwDumpCheck( void ); 00455 void * mwMark( void *p, const char *description, const char *file, unsigned line ); 00456 void * mwUnmark( void *p, const char *file, unsigned line ); 00457 00458 /* 00459 ** Testing/verification/tracing 00460 ** All of these macros except VERIFY() evaluates to a null statement 00461 ** if MEMWATCH is not defined during compilation. 00462 ** - mwIsReadAddr() checks a memory area for read privilige. 00463 ** - mwIsSafeAddr() checks a memory area for both read & write privilige. 00464 ** This function and mwIsReadAddr() is highly system-specific and 00465 ** may not be implemented. If this is the case, they will default 00466 ** to returning nonzero for any non-NULL pointer. 00467 ** - CHECK() does a complete memory integrity test. Slow! 00468 ** - CHECK_THIS() checks only selected components. 00469 ** - CHECK_BUFFER() checks the indicated buffer for errors. 00470 ** - mwASSERT() or ASSERT() If the expression evaluates to nonzero, execution continues. 00471 ** Otherwise, the ARI handler is called, if present. If not present, 00472 ** the default ARI action is taken (set with mwSetAriAction()). 00473 ** ASSERT() can be disabled by defining MW_NOASSERT. 00474 ** - mwVERIFY() or VERIFY() works just like ASSERT(), but when compiling without 00475 ** MEMWATCH the macro evaluates to the expression. 00476 ** VERIFY() can be disabled by defining MW_NOVERIFY. 00477 ** - mwTRACE() or TRACE() writes some text and data to the log. Use like printf(). 00478 ** TRACE() can be disabled by defining MW_NOTRACE. 00479 */ 00480 int mwIsReadAddr( const void *p, unsigned len ); 00481 int mwIsSafeAddr( void *p, unsigned len ); 00482 int mwTest( const char *file, int line, int mw_test_flags ); 00483 int mwTestBuffer( const char *file, int line, void *p ); 00484 int mwAssert( int, const char*, const char*, int ); 00485 int mwVerify( int, const char*, const char*, int ); 00486 00487 /* 00488 ** User I/O functions 00489 ** - mwTrace() works like printf(), but dumps output either to the 00490 ** function specified with mwSetOutFunc(), or the log file. 00491 ** - mwPuts() works like puts(), dumps output like mwTrace(). 00492 ** - mwSetOutFunc() allows you to give the adress of a function 00493 ** where all user output will go. (exeption: see mwSetAriFunc) 00494 ** Specifying NULL will direct output to the log file. 00495 ** - mwSetAriFunc() gives MEMWATCH the adress of a function to call 00496 ** when an 'Abort, Retry, Ignore' question is called for. The 00497 ** actual error message is NOT printed when you've set this adress, 00498 ** but instead it is passed as an argument. If you call with NULL 00499 ** for an argument, the ARI handler is disabled again. When the 00500 ** handler is disabled, MEMWATCH will automatically take the 00501 ** action specified by mwSetAriAction(). 00502 ** - mwSetAriAction() sets the default ARI return value MEMWATCH should 00503 ** use if no ARI handler is specified. Defaults to MW_ARI_ABORT. 00504 ** - mwAriHandler() is an ANSI ARI handler you can use if you like. It 00505 ** dumps output to stderr, and expects input from stdin. 00506 ** - mwBreakOut() is called in certain cases when MEMWATCH feels it would 00507 ** be nice to break into a debugger. If you feel like MEMWATCH, place 00508 ** an execution breakpoint on this function. 00509 */ 00510 void mwTrace( const char* format_string, ... ); 00511 void mwPuts( const char* text ); 00512 void mwSetOutFunc( void (*func)(int) ); 00513 void mwSetAriFunc( int (*func)(const char*) ); 00514 void mwSetAriAction( int mw_ari_value ); 00515 int mwAriHandler( const char* cause ); 00516 void mwBreakOut( const char* cause ); 00517 00518 /* 00519 ** Allocation/deallocation functions 00520 ** These functions are the ones actually to perform allocations 00521 ** when running MEMWATCH, for both C and C++ calls. 00522 ** - mwMalloc() debugging allocator 00523 ** - mwMalloc_() always resolves to a clean call of malloc() 00524 ** - mwRealloc() debugging re-allocator 00525 ** - mwRealloc_() always resolves to a clean call of realloc() 00526 ** - mwCalloc() debugging allocator, fills with zeros 00527 ** - mwCalloc_() always resolves to a clean call of calloc() 00528 ** - mwFree() debugging free. Can only free memory which has 00529 ** been allocated by MEMWATCH. 00530 ** - mwFree_() resolves to a) normal free() or b) debugging free. 00531 ** Can free memory allocated by MEMWATCH and malloc() both. 00532 ** Does not generate any runtime errors. 00533 */ 00534 void* mwMalloc( size_t, const char*, int ); 00535 void* mwMalloc_( size_t ); 00536 void* mwRealloc( void *, size_t, const char*, int ); 00537 void* mwRealloc_( void *, size_t ); 00538 void* mwCalloc( size_t, size_t, const char*, int ); 00539 void* mwCalloc_( size_t, size_t ); 00540 void mwFree( void*, const char*, int ); 00541 void mwFree_( void* ); 00542 char* mwStrdup( const char *, const char*, int ); 00543 00544 /* 00545 ** Enable/disable precompiler block 00546 ** This block of defines and if(n)defs make sure that references 00547 ** to MEMWATCH is completely removed from the code if the MEMWATCH 00548 ** manifest constant is not defined. 00549 */ 00550 #ifndef __MEMWATCH_C 00551 #ifdef MEMWATCH 00552 00553 #define mwASSERT(exp) while(mwAssert((int)(exp),#exp,__FILE__,__LINE__)) 00554 #ifndef MW_NOASSERT 00555 #ifndef ASSERT 00556 #define ASSERT mwASSERT 00557 #endif /* !ASSERT */ 00558 #endif /* !MW_NOASSERT */ 00559 #define mwVERIFY(exp) while(mwVerify((int)(exp),#exp,__FILE__,__LINE__)) 00560 #ifndef MW_NOVERIFY 00561 #ifndef VERIFY 00562 #define VERIFY mwVERIFY 00563 #endif /* !VERIFY */ 00564 #endif /* !MW_NOVERIFY */ 00565 #define mwTRACE mwTrace 00566 #ifndef MW_NOTRACE 00567 #ifndef TRACE 00568 #define TRACE mwTRACE 00569 #endif /* !TRACE */ 00570 #endif /* !MW_NOTRACE */ 00571 00572 /* some compilers use a define and not a function */ 00573 /* for strdup(). */ 00574 #ifdef strdup 00575 #undef strdup 00576 #endif 00577 00578 #define malloc(n) mwMalloc(n,__FILE__,__LINE__) 00579 #define strdup(p) mwStrdup(p,__FILE__,__LINE__) 00580 #define realloc(p,n) mwRealloc(p,n,__FILE__,__LINE__) 00581 #define calloc(n,m) mwCalloc(n,m,__FILE__,__LINE__) 00582 #define free(p) mwFree(p,__FILE__,__LINE__) 00583 #define CHECK() mwTest(__FILE__,__LINE__,MW_TEST_ALL) 00584 #define CHECK_THIS(n) mwTest(__FILE__,__LINE__,n) 00585 #define CHECK_BUFFER(b) mwTestBuffer(__FILE__,__LINE__,b) 00586 #define MARK(p) mwMark(p,#p,__FILE__,__LINE__) 00587 #define UNMARK(p) mwUnmark(p,__FILE__,__LINE__) 00588 00589 #else /* MEMWATCH */ 00590 00591 #define mwASSERT(exp) 00592 #ifndef MW_NOASSERT 00593 #ifndef ASSERT 00594 #define ASSERT mwASSERT 00595 #endif /* !ASSERT */ 00596 #endif /* !MW_NOASSERT */ 00597 00598 #define mwVERIFY(exp) exp 00599 #ifndef MW_NOVERIFY 00600 #ifndef VERIFY 00601 #define VERIFY mwVERIFY 00602 #endif /* !VERIFY */ 00603 #endif /* !MW_NOVERIFY */ 00604 00605 /*lint -esym(773,mwTRACE) */ 00606 #define mwTRACE /*lint -save -e506 */ 1?(void)0:mwDummyTraceFunction /*lint -restore */ 00607 #ifndef MW_NOTRACE 00608 #ifndef TRACE 00609 /*lint -esym(773,TRACE) */ 00610 #define TRACE mwTRACE 00611 #endif /* !TRACE */ 00612 #endif /* !MW_NOTRACE */ 00613 00614 extern void mwDummyTraceFunction(const char *,...); 00615 /*lint -save -e652 */ 00616 #define mwDoFlush(n) 00617 #define mwPuts(s) 00618 #define mwInit() 00619 #define mwGrab(n) 00620 #define mwDrop(n) 00621 #define mwLimit(n) 00622 #define mwTest(f,l) 00623 #define mwSetOutFunc(f) 00624 #define mwSetAriFunc(f) 00625 #define mwDefaultAri() 00626 #define mwNomansland() 00627 #define mwStatistics(f) 00628 #define mwMark(p,t,f,n) (p) 00629 #define mwUnmark(p,f,n) (p) 00630 #define mwMalloc(n,f,l) malloc(n) 00631 #define mwStrdup(p,f,l) strdup(p) 00632 #define mwRealloc(p,n,f,l) realloc(p,n) 00633 #define mwCalloc(n,m,f,l) calloc(n,m) 00634 #define mwFree(p) free(p) 00635 #define mwMalloc_(n) malloc(n) 00636 #define mwRealloc_(p,n) realloc(p,n) 00637 #define mwCalloc_(n,m) calloc(n,m) 00638 #define mwFree_(p) free(p) 00639 #define mwAssert(e,es,f,l) 00640 #define mwVerify(e,es,f,l) (e) 00641 #define mwTrace mwDummyTrace 00642 #define mwTestBuffer(f,l,b) (0) 00643 #define CHECK() 00644 #define CHECK_THIS(n) 00645 #define CHECK_BUFFER(b) 00646 #define MARK(p) (p) 00647 #define UNMARK(p) (p) 00648 /*lint -restore */ 00649 00650 #endif /* MEMWATCH */ 00651 #endif /* !__MEMWATCH_C */ 00652 00653 #ifdef __cplusplus 00654 } 00655 #endif 00656 00657 #if 0 /* 980317: disabled C++ */ 00658 00659 /* 00660 ** C++ support section 00661 ** Implements the C++ support. Please note that in order to avoid 00662 ** messing up library classes, C++ support is disabled by default. 00663 ** You must NOT enable it until AFTER the inclusion of all header 00664 ** files belonging to code that are not compiled with MEMWATCH, and 00665 ** possibly for some that are! The reason for this is that a C++ 00666 ** class may implement it's own new() function, and the preprocessor 00667 ** would substitute this crucial declaration for MEMWATCH new(). 00668 ** You can forcibly deny C++ support by defining MEMWATCH_NOCPP. 00669 ** To enble C++ support, you must be compiling C++, MEMWATCH must 00670 ** be defined, MEMWATCH_NOCPP must not be defined, and finally, 00671 ** you must define 'new' to be 'mwNew', and 'delete' to be 'mwDelete'. 00672 ** Unlike C, C++ code can begin executing *way* before main(), for 00673 ** example if a global variable is created. For this reason, you can 00674 ** declare a global variable of the class 'MemWatch'. If this is 00675 ** is the first variable created, it will then check ALL C++ allocations 00676 ** and deallocations. Unfortunately, this evaluation order is not 00677 ** guaranteed by C++, though the compilers I've tried evaluates them 00678 ** in the order encountered. 00679 */ 00680 #ifdef __cplusplus 00681 #ifndef __MEMWATCH_C 00682 #ifdef MEMWATCH 00683 #ifndef MEMWATCH_NOCPP 00684 extern int mwNCur; 00685 extern const char *mwNFile; 00686 extern int mwNLine; 00687 class MemWatch { 00688 public: 00689 MemWatch(); 00690 ~MemWatch(); 00691 }; 00692 void * operator new(size_t); 00693 void * operator new(size_t,const char *,int); 00694 void * operator new[] (size_t,const char *,int); // hjc 07/16/02 00695 void operator delete(void *); 00696 #define mwNew new(__FILE__,__LINE__) 00697 #define mwDelete (mwNCur=1,mwNFile=__FILE__,mwNLine=__LINE__),delete 00698 #endif /* MEMWATCH_NOCPP */ 00699 #endif /* MEMWATCH */ 00700 #endif /* !__MEMWATCH_C */ 00701 #endif /* __cplusplus */ 00702 00703 #endif /* 980317: disabled C++ */ 00704 00705 #endif /* __MEMWATCH_H */ 00706 00707 /* EOF MEMWATCH.H */