1 /* 2 ** MEMWATCH.H 3 ** Nonintrusive ANSI C memory leak / overwrite detection 4 ** Copyright (C) 1992-99 Johan Lindh 5 ** All rights reserved. 6 ** Version 2.64 7 ** 8 ************************************************************************ 9 ** 10 ** PURPOSE: 11 ** 12 ** MEMWATCH has been written to allow guys and gals that like to 13 ** program in C a public-domain memory error control product. 14 ** I hope you'll find it's as advanced as most commercial packages. 15 ** The idea is that you use it during the development phase and 16 ** then remove the MEMWATCH define to produce your final product. 17 ** MEMWATCH is distributed in source code form in order to allow 18 ** you to compile it for your platform with your own compiler. 19 ** It's aim is to be 100% ANSI C, but some compilers are more stingy 20 ** than others. If it doesn't compile without warnings, please mail 21 ** me the configuration of operating system and compiler you are using 22 ** along with a description of how to modify the source, and the version 23 ** number of MEMWATCH that you are using. 24 ** 25 ************************************************************************ 26 ** 27 ** And now for some legalese... 28 ** 29 ** LICENSE: 30 ** 31 ** You are granted a non-exclusive right to use MEMWATCH in your 32 ** programs, provided that you agree to the following terms: 33 ** 34 ** 1. Johan Lindh retains the full copyright of MEMWATCH, and owns 35 ** all rights to it. This means you can't sell it yourself, or 36 ** ship it as part of another product, or as part of a package. 37 ** 2. If you modify any of the files, you must not give them to 38 ** anyone else. But please send me a copy of the changes, 39 ** along with a text as to why they should be implemented. 40 ** 3. You read and agree to the DISCLAIMER, below. 41 ** 42 ** DISCLAIMER: 43 ** 44 ** THIS SOFTWARE IS PROVIDED FREE OF CHARGE 'AS IS' AND JOHAN LINDH MAKES 45 ** NO EXPRESS OR IMPLIED WARRANTIES WITH RESPECT TO IT. JOHAN LINDH WILL 46 ** NOT BE LIABLE FOR ANY DAMAGES, DIRECT OR INDIRECT, ARISING FROM THE 47 ** USAGE OR HANDLING OF THIS SOFTWARE. 48 ** 49 ************************************************************************ 50 ** 51 ** REVISION HISTORY: 52 ** 53 ** 920810 JLI [1.00] 54 ** 920830 JLI [1.10 double-free detection] 55 ** 920912 JLI [1.15 mwPuts, mwGrab/Drop, mwLimit] 56 ** 921022 JLI [1.20 ASSERT and VERIFY] 57 ** 921105 JLI [1.30 C++ support and TRACE] 58 ** 921116 JLI [1.40 mwSetOutFunc] 59 ** 930215 JLI [1.50 modified ASSERT/VERIFY] 60 ** 930327 JLI [1.51 better auto-init & PC-lint support] 61 ** 930506 JLI [1.55 MemWatch class, improved C++ support] 62 ** 930507 JLI [1.60 mwTest & CHECK()] 63 ** 930809 JLI [1.65 Abort/Retry/Ignore] 64 ** 930820 JLI [1.70 data dump when unfreed] 65 ** 931016 JLI [1.72 modified C++ new/delete handling] 66 ** 931108 JLI [1.77 mwSetAssertAction() & some small changes] 67 ** 940110 JLI [1.80 no-mans-land alloc/checking] 68 ** 940328 JLI [2.00 version 2.0 rewrite] 69 ** Improved NML (no-mans-land) support. 70 ** Improved performance (especially for free()ing!). 71 ** Support for 'read-only' buffers (checksums) 72 ** ^^ NOTE: I never did this... maybe I should? 73 ** FBI (free'd block info) tagged before freed blocks 74 ** Exporting of the mwCounter variable 75 ** mwBreakOut() localizes debugger support 76 ** Allocation statistics (global, per-module, per-line) 77 ** Self-repair ability with relinking 78 ** 950913 JLI [2.10 improved garbage handling] 79 ** 951201 JLI [2.11 improved auto-free in emergencies] 80 ** 960125 JLI [X.01 implemented auto-checking using mwAutoCheck()] 81 ** 960514 JLI [2.12 undefining of existing macros] 82 ** 960515 JLI [2.13 possibility to use default new() & delete()] 83 ** 960516 JLI [2.20 suppression of file flushing on unfreed msgs] 84 ** 960516 JLI [2.21 better support for using MEMWATCH with DLL's] 85 ** 960710 JLI [X.02 multiple logs and mwFlushNow()] 86 ** 960801 JLI [2.22 merged X.01 version with current] 87 ** 960805 JLI [2.30 mwIsXXXXAddr() to avoid unneeded GP's] 88 ** 960805 JLI [2.31 merged X.02 version with current] 89 ** 961002 JLI [2.32 support for realloc() + fixed STDERR bug] 90 ** 961222 JLI [2.40 added mwMark() & mwUnmark()] 91 ** 970101 JLI [2.41 added over/underflow checking after failed ASSERT/VERIFY] 92 ** 970113 JLI [2.42 added support for PC-Lint 7.00g] 93 ** 970207 JLI [2.43 added support for strdup()] 94 ** 970209 JLI [2.44 changed default filename to lowercase] 95 ** 970405 JLI [2.45 fixed bug related with atexit() and some C++ compilers] 96 ** 970723 JLI [2.46 added MW_ARI_NULLREAD flag] 97 ** 970813 JLI [2.47 stabilized marker handling] 98 ** 980317 JLI [2.48 ripped out C++ support; wasn't working good anyway] 99 ** 980318 JLI [2.50 improved self-repair facilities & SIGSEGV support] 100 ** 980417 JLI [2.51 more checks for invalid addresses] 101 ** 980512 JLI [2.52 moved MW_ARI_NULLREAD to occur before aborting] 102 ** 990112 JLI [2.53 added check for empty heap to mwIsOwned] 103 ** 990217 JLI [2.55 improved the emergency repairs diagnostics and NML] 104 ** 990224 JLI [2.56 changed ordering of members in structures] 105 ** 990303 JLI [2.57 first maybe-fixit-for-hpux test] 106 ** 990516 JLI [2.58 added 'static' to the definition of mwAutoInit] 107 ** 990517 JLI [2.59 fixed some high-sensitivity warnings] 108 ** 990610 JLI [2.60 fixed some more high-sensitivity warnings] 109 ** 990715 JLI [2.61 changed TRACE/ASSERT/VERIFY macro names] 110 ** 991001 JLI [2.62 added CHECK_BUFFER() and mwTestBuffer()] 111 ** 991007 JLI [2.63 first shot at a 64-bit compatible version] 112 ** 991009 JLI [2.64 undef's strdup() if defined, mwStrdup made const] 113 ** 114 ** To use, simply include 'MEMWATCH.H' as a header file, 115 ** and add MEMWATCH.C to your list of files, and define the macro 116 ** 'MEMWATCH'. If this is not defined, MEMWATCH will disable itself. 117 ** 118 ** To call the standard C malloc / realloc / calloc / free; use mwMalloc_(), 119 ** mwCalloc_() and mwFree_(). Note that mwFree_() will correctly 120 ** free both malloc()'d memory as well as mwMalloc()'d. 121 ** 122 ** 980317: C++ support has been disabled. 123 ** The code remains, but is not compiled. 124 ** 125 ** For use with C++, which allows use of inlining in header files 126 ** and class specific new/delete, you must also define 'new' as 127 ** 'mwNew' and 'delete' as 'mwDelete'. Do this *after* you include 128 ** C++ header files from libraries, otherwise you can mess up their 129 ** class definitions. If you don't define these, the C++ allocations 130 ** will not have source file and line number information. Also note, 131 ** most C++ class libraries implement their own C++ memory management, 132 ** and don't allow anyone to override them. MFC belongs to this crew. 133 ** In these cases, the only thing to do is to use MEMWATCH_NOCPP. 134 ** 135 ** You can capture output from MEMWATCH using mwSetOutFunc(). 136 ** Just give it the adress of a "void myOutFunc(int c)" function, 137 ** and all characters to be output will be redirected there. 138 ** 139 ** A failing ASSERT() or VERIFY() will normally always abort your 140 ** program. This can be changed using mwSetAriFunc(). Give it a 141 ** pointer to a "int myAriFunc(const char *)" function. Your function 142 ** must ask the user whether to Abort, Retry or Ignore the trap. 143 ** Return 2 to Abort, 1 to Retry or 0 to Ignore. Beware retry; it 144 ** causes the expression to be evaluated again! MEMWATCH has a 145 ** default ARI handler. It's disabled by default, but you can enable 146 ** it by calling 'mwDefaultAri()'. Note that this will STILL abort 147 ** your program unless you define MEMWATCH_STDIO to allow MEMWATCH 148 ** to use the standard C I/O streams. Also, setting the ARI function 149 ** will cause MEMWATCH *NOT* to write the ARI error to stderr. The 150 ** error string is passed to the ARI function instead, as the 151 ** 'const char *' parameter. 152 ** 153 ** You can disable MEMWATCH's ASSERT/VERIFY and/or TRACE implementations. 154 ** This can be useful if you're using a debug terminal or smart debugger. 155 ** Disable them by defining MW_NOASSERT, MW_NOVERIFY or MW_NOTRACE. 156 ** 157 ** MEMWATCH fills all allocated memory with the byte 0xFE, so if 158 ** you're looking at erroneous data which are all 0xFE:s, the 159 ** data probably was not initialized by you. The exception is 160 ** calloc(), which will fill with zero's. All freed buffers are 161 ** zapped with 0xFD. If this is what you look at, you're using 162 ** data that has been freed. If this is the case, be aware that 163 ** MEMWATCH places a 'free'd block info' structure immediately 164 ** before the freed data. This block contains info about where 165 ** the block was freed. The information is in readable text, 166 ** in the format "FBI<counter>filename(line)", for example: 167 ** "FBI<267>test.c(12)". Using FBI's slows down free(), so it's 168 ** disabled by default. Use mwFreeBufferInfo(1) to enable it. 169 ** 170 ** To aid in tracking down wild pointer writes, MEMWATCH can perform 171 ** no-mans-land allocations. No-mans-land will contain the byte 0xFC. 172 ** MEMWATCH will, when this is enabled, convert recently free'd memory 173 ** into NML allocations. 174 ** 175 ** MEMWATCH protects it's own data buffers with checksums. If you 176 ** get an internal error, it means you're overwriting wildly, 177 ** or using an uninitialized pointer. 178 ** 179 ************************************************************************ 180 ** 181 ** Note when compiling with Microsoft C: 182 ** - MSC ignores fflush() by default. This is overridden, so that 183 ** the disk log will always be current. 184 ** 185 ** This utility has been tested with: 186 ** PC-lint 7.0k, passed as 100% ANSI C compatible 187 ** Microsoft Visual C++ on Win16 and Win32 188 ** Microsoft C on DOS 189 ** SAS C on an Amiga 500 190 ** Gnu C on a PC running Red Hat Linux 191 ** ...and using an (to me) unknown compiler on an Atari machine. 192 ** 193 ************************************************************************ 194 ** 195 ** Format of error messages in MEMWATCH.LOG: 196 ** message: <sequence-number> filename(linenumber), information 197 ** 198 ** Errors caught by MemWatch, when they are detected, and any 199 ** actions taken besides writing to the log file MEMWATCH.LOG: 200 ** 201 ** Double-freeing: 202 ** A pointer that was recently freed and has not since been 203 ** reused was freed again. The place where the previous free() 204 ** was executed is displayed. 205 ** Detect: delete or free() using the offending pointer. 206 ** Action: The delete or free() is cancelled, execution continues. 207 ** Underflow: 208 ** You have written just ahead of the allocated memory. 209 ** The size and place of the allocation is displayed. 210 ** Detect: delete or free() of the damaged buffer. 211 ** Action: The buffer is freed, but there may be secondary damage. 212 ** Overflow: 213 ** Like underflow, but you've written after the end of the buffer. 214 ** Detect: see Underflow. 215 ** Action: see Underflow. 216 ** WILD free: 217 ** An unrecognized pointer was passed to delete or free(). 218 ** The pointer may have been returned from a library function; 219 ** in that case, use mwFree_() to force free() of it. 220 ** Also, this may be a double-free, but the previous free was 221 ** too long ago, causing MEMWATCH to 'forget' it. 222 ** Detect: delete or free() of the offending pointer. 223 ** Action: The delete or free() is cancelled, execution continues. 224 ** NULL free: 225 ** It's unclear to me whether or not freeing of NULL pointers 226 ** is legal in ANSI C, therefore a warning is written to the log file, 227 ** but the error counter remains the same. This is legal using C++, 228 ** so the warning does not appear with delete. 229 ** Detect: When you free(NULL). 230 ** Action: The free() is cancelled. 231 ** Failed: 232 ** A request to allocate memory failed. If the allocation is 233 ** small, this may be due to memory depletion, but is more likely 234 ** to be memory fragmentation problems. The amount of memory 235 ** allocated so far is displayed also. 236 ** Detect: When you new, malloc(), realloc() or calloc() memory. 237 ** Action: NULL is returned. 238 ** Realloc: 239 ** A request to re-allocate a memory buffer failed for reasons 240 ** other than out-of-memory. The specific reason is shown. 241 ** Detect: When you realloc() 242 ** Action: realloc() is cancelled, NULL is returned 243 ** Limit fail: 244 ** A request to allocate memory failed since it would violate 245 ** the limit set using mwLimit(). mwLimit() is used to stress-test 246 ** your code under simulated low memory conditions. 247 ** Detect: At new, malloc(), realloc() or calloc(). 248 ** Action: NULL is returned. 249 ** Assert trap: 250 ** An ASSERT() failed. The ASSERT() macro works like C's assert() 251 ** macro/function, except that it's interactive. See your C manual. 252 ** Detect: On the ASSERT(). 253 ** Action: Program ends with an advisory message to stderr, OR 254 ** Program writes the ASSERT to the log and continues, OR 255 ** Program asks Abort/Retry/Ignore? and takes that action. 256 ** Verify trap: 257 ** A VERIFY() failed. The VERIFY() macro works like ASSERT(), 258 ** but if MEMWATCH is not defined, it still evaluates the 259 ** expression, but it does not act upon the result. 260 ** Detect: On the VERIFY(). 261 ** Action: Program ends with an advisory message to stderr, OR 262 ** Program writes the VERIFY to the log and continues, OR 263 ** Program asks Abort/Retry/Ignore? and takes that action. 264 ** Wild pointer: 265 ** A no-mans-land buffer has been written into. MEMWATCH can 266 ** allocate and distribute chunks of memory solely for the 267 ** purpose of trying to catch random writes into memory. 268 ** Detect: Always on CHECK(), but can be detected in several places. 269 ** Action: The error is logged, and if an ARI handler is installed, 270 ** it is executed, otherwise, execution continues. 271 ** Unfreed: 272 ** A memory buffer you allocated has not been freed. 273 ** You are informed where it was allocated, and whether any 274 ** over or underflow has occured. MemWatch also displays up to 275 ** 16 bytes of the data, as much as it can, in hex and text. 276 ** Detect: When MemWatch terminates. 277 ** Action: The buffer is freed. 278 ** Check: 279 ** An error was detected during a CHECK() operation. 280 ** The associated pointer is displayed along with 281 ** the file and line where the CHECK() was executed. 282 ** Followed immediately by a normal error message. 283 ** Detect: When you CHECK() 284 ** Action: Depends on the error 285 ** Relink: 286 ** After a MEMWATCH internal control block has been trashed, 287 ** MEMWATCH tries to repair the damage. If successful, program 288 ** execution will continue instead of aborting. Some information 289 ** about the block may be gone permanently, though. 290 ** Detect: N/A 291 ** Action: Relink successful: program continues. 292 ** Relink fails: program aborts. 293 ** Internal: 294 ** An internal error is flagged by MEMWATCH when it's control 295 ** structures have been damaged. You are likely using an uninitialized 296 ** pointer somewhere in your program, or are zapping memory all over. 297 ** The message may give you additional diagnostic information. 298 ** If possible, MEMWATCH will recover and continue execution. 299 ** Detect: Various actions. 300 ** Action: Whatever is needed 301 ** Mark: 302 ** The program terminated without umarking all marked pointers. Marking 303 ** can be used to track resources other than memory. mwMark(pointer,text,...) 304 ** when the resource is allocated, and mwUnmark(pointer) when it's freed. 305 ** The 'text' is displayed for still marked pointers when the program 306 ** ends. 307 ** Detect: When MemWatch terminates. 308 ** Action: The error is logged. 309 ** 310 ** 311 ************************************************************************ 312 ** 313 ** The author may be reached by e-mail at the address below. If you 314 ** mail me about source code changes in MEMWATCH, remember to include 315 ** MW's version number. 316 ** 317 ** Johan Lindh 318 ** johan@link-data.com 319 ** 320 ** The latest version of MEMWATCH may be downloaded from 321 ** http://www.link-data.com/ 322 */ 323 324 #ifndef __MEMWATCH_H 325 #define __MEMWATCH_H 326 327 /* Make sure that malloc(), realloc(), calloc() and free() are declared. */ 328 /*lint -save -e537 */ 329 #include <stdlib.h> 330 /*lint -restore */ 331 332 #ifdef __cplusplus 333 extern "C" { 334 #endif 335 336 337 /* 338 ** Constants used 339 ** All MEMWATCH constants start with the prefix MW_, followed by 340 ** a short mnemonic which indicates where the constant is used, 341 ** followed by a descriptive text about it. 342 */ 343 344 #define MW_ARI_NULLREAD 0x10 /* Null read (to start debugger) */ 345 #define MW_ARI_ABORT 0x04 /* ARI handler says: abort program! */ 346 #define MW_ARI_RETRY 0x02 /* ARI handler says: retry action! */ 347 #define MW_ARI_IGNORE 0x01 /* ARI handler says: ignore error! */ 348 349 #define MW_VAL_NEW 0xFE /* value in newly allocated memory */ 350 #define MW_VAL_DEL 0xFD /* value in newly deleted memory */ 351 #define MW_VAL_NML 0xFC /* value in no-mans-land */ 352 #define MW_VAL_GRB 0xFB /* value in grabbed memory */ 353 354 #define MW_TEST_ALL 0xFFFF /* perform all tests */ 355 #define MW_TEST_CHAIN 0x0001 /* walk the heap chain */ 356 #define MW_TEST_ALLOC 0x0002 /* test allocations & NML guards */ 357 #define MW_TEST_NML 0x0004 /* test all-NML areas for modifications */ 358 359 #define MW_NML_NONE 0 /* no NML */ 360 #define MW_NML_FREE 1 /* turn FREE'd memory into NML */ 361 #define MW_NML_ALL 2 /* all unused memory is NML */ 362 #define MW_NML_DEFAULT 0 /* the default NML setting */ 363 364 #define MW_STAT_GLOBAL 0 /* only global statistics collected */ 365 #define MW_STAT_MODULE 1 /* collect statistics on a module basis */ 366 #define MW_STAT_LINE 2 /* collect statistics on a line basis */ 367 #define MW_STAT_DEFAULT 0 /* the default statistics setting */ 368 369 /* 370 ** MemWatch internal constants 371 ** You may change these and recompile MemWatch to change the limits 372 ** of some parameters. Respect the recommended minimums! 373 */ 374 #define MW_TRACE_BUFFER 256 /* (min 160) size of TRACE()'s output buffer */ 375 #define MW_FREE_LIST 64 /* (min 4) number of free()'s to track */ 376 377 /* 378 ** Exported variables 379 ** In case you have to remove the 'const' keyword because your compiler 380 ** doesn't support it, be aware that changing the values may cause 381 ** unpredictable behaviour. 382 ** - mwCounter contains the current action count. You can use this to 383 ** place breakpoints using a debugger, if you want. 384 */ 385 #ifndef __MEMWATCH_C 386 extern const unsigned long mwCounter; 387 #endif 388 389 /* 390 ** System functions 391 ** Normally, it is not nessecary to call any of these. MEMWATCH will 392 ** automatically initialize itself on the first MEMWATCH function call, 393 ** and set up a call to mwAbort() using atexit(). Some C++ implementations 394 ** run the atexit() chain before the program has terminated, so you 395 ** may have to use mwInit() or the MemWatch C++ class to get good 396 ** behaviour. 397 ** - mwInit() can be called to disable the atexit() usage. If mwInit() 398 ** is called directly, you must call mwTerm() to end MemWatch, or 399 ** mwAbort(). 400 ** - mwTerm() is usually not nessecary to call; but if called, it will 401 ** call mwAbort() if it finds that it is cancelling the 'topmost' 402 ** mwInit() call. 403 ** - mwAbort() cleans up after MEMWATCH, reports unfreed buffers, etc. 404 */ 405 void mwInit( void ); 406 void mwTerm( void ); 407 void mwAbort( void ); 408 409 /* 410 ** Setup functions 411 ** These functions control the operation of MEMWATCH's protective features. 412 ** - mwFlushNow() causes MEMWATCH to flush it's buffers. 413 ** - mwDoFlush() controls whether MEMWATCH flushes the disk buffers after 414 ** writes. The default is smart flushing: MEMWATCH will not flush buffers 415 ** explicitly until memory errors are detected. Then, all writes are 416 ** flushed until program end or mwDoFlush(0) is called. 417 ** - mwLimit() sets the allocation limit, an arbitrary limit on how much 418 ** memory your program may allocate in bytes. Used to stress-test app. 419 ** Also, in virtual-memory or multitasking environs, puts a limit on 420 ** how much MW_NML_ALL can eat up. 421 ** - mwGrab() grabs up X kilobytes of memory. Allocates actual memory, 422 ** can be used to stress test app & OS both. 423 ** - mwDrop() drops X kilobytes of grabbed memory. 424 ** - mwNoMansLand() sets the behaviour of the NML logic. See the 425 ** MW_NML_xxx for more information. The default is MW_NML_DEFAULT. 426 ** - mwStatistics() sets the behaviour of the statistics collector. See 427 ** the MW_STAT_xxx defines for more information. Default MW_STAT_DEFAULT. 428 ** - mwFreeBufferInfo() enables or disables the tagging of free'd buffers 429 ** with freeing information. This information is written in text form, 430 ** using sprintf(), so it's pretty slow. Disabled by default. 431 ** - mwAutoCheck() performs a CHECK() operation whenever a MemWatch function 432 ** is used. Slows down performance, of course. 433 ** - mwCalcCheck() calculates checksums for all data buffers. Slow! 434 ** - mwDumpCheck() logs buffers where stored & calc'd checksums differ. Slow!! 435 ** - mwMark() sets a generic marker. Returns the pointer given. 436 ** - mwUnmark() removes a generic marker. If, at the end of execution, some 437 ** markers are still in existence, these will be reported as leakage. 438 ** returns the pointer given. 439 */ 440 void mwFlushNow( void ); 441 void mwDoFlush( int onoff ); 442 void mwLimit( long bytes ); 443 unsigned mwGrab( unsigned kilobytes ); 444 unsigned mwDrop( unsigned kilobytes ); 445 void mwNoMansLand( int mw_nml_level ); 446 void mwStatistics( int level ); 447 void mwFreeBufferInfo( int onoff ); 448 void mwAutoCheck( int onoff ); 449 void mwCalcCheck( void ); 450 void mwDumpCheck( void ); 451 void * mwMark( void *p, const char *description, const char *file, unsigned line ); 452 void * mwUnmark( void *p, const char *file, unsigned line ); 453 454 /* 455 ** Testing/verification/tracing 456 ** All of these macros except VERIFY() evaluates to a null statement 457 ** if MEMWATCH is not defined during compilation. 458 ** - mwIsReadAddr() checks a memory area for read privilige. 459 ** - mwIsSafeAddr() checks a memory area for both read & write privilige. 460 ** This function and mwIsReadAddr() is highly system-specific and 461 ** may not be implemented. If this is the case, they will default 462 ** to returning nonzero for any non-NULL pointer. 463 ** - CHECK() does a complete memory integrity test. Slow! 464 ** - CHECK_THIS() checks only selected components. 465 ** - CHECK_BUFFER() checks the indicated buffer for errors. 466 ** - mwASSERT() or ASSERT() If the expression evaluates to nonzero, execution continues. 467 ** Otherwise, the ARI handler is called, if present. If not present, 468 ** the default ARI action is taken (set with mwSetAriAction()). 469 ** ASSERT() can be disabled by defining MW_NOASSERT. 470 ** - mwVERIFY() or VERIFY() works just like ASSERT(), but when compiling without 471 ** MEMWATCH the macro evaluates to the expression. 472 ** VERIFY() can be disabled by defining MW_NOVERIFY. 473 ** - mwTRACE() or TRACE() writes some text and data to the log. Use like printf(). 474 ** Note: there is a limit to the maximum resulting string length that 475 ** can be written. This defaults to MW_TRACE_BUFFER characters. 476 ** TRACE() can be disabled by defining MW_NOTRACE. 477 */ 478 int mwIsReadAddr( const void *p, unsigned len ); 479 int mwIsSafeAddr( void *p, unsigned len ); 480 int mwTest( const char *file, int line, int mw_test_flags ); 481 int mwTestBuffer( const char *file, int line, void *p ); 482 int mwAssert( int, const char*, const char*, int ); 483 int mwVerify( int, const char*, const char*, int ); 484 485 /* 486 ** User I/O functions 487 ** - mwTrace() works like printf(), but dumps output either to the 488 ** function specified with mwSetOutFunc(), or the log file. 489 ** - mwPuts() works like puts(), dumps output like mwTrace(). 490 ** - mwSetOutFunc() allows you to give the adress of a function 491 ** where all user output will go. (exeption: see mwSetAriFunc) 492 ** Specifying NULL will direct output to the log file. 493 ** - mwSetAriFunc() gives MEMWATCH the adress of a function to call 494 ** when an 'Abort, Retry, Ignore' question is called for. The 495 ** actual error message is NOT printed when you've set this adress, 496 ** but instead it is passed as an argument. If you call with NULL 497 ** for an argument, the ARI handler is disabled again. When the 498 ** handler is disabled, MEMWATCH will automatically take the 499 ** action specified by mwSetAriAction(). 500 ** - mwSetAriAction() sets the default ARI return value MEMWATCH should 501 ** use if no ARI handler is specified. Defaults to MW_ARI_ABORT. 502 ** - mwAriHandler() is an ANSI ARI handler you can use if you like. It 503 ** dumps output to stderr, and expects input from stdin. 504 ** - mwBreakOut() is called in certain cases when MEMWATCH feels it would 505 ** be nice to break into a debugger. If you feel like MEMWATCH, place 506 ** an execution breakpoint on this function. 507 */ 508 void mwTrace( const char* format_string, ... ); 509 void mwPuts( const char* text ); 510 void mwSetOutFunc( void (*func)(int) ); 511 void mwSetAriFunc( int (*func)(const char*) ); 512 void mwSetAriAction( int mw_ari_value ); 513 int mwAriHandler( const char* cause ); 514 void mwBreakOut( const char* cause ); 515 516 /* 517 ** Allocation/deallocation functions 518 ** These functions are the ones actually to perform allocations 519 ** when running MEMWATCH, for both C and C++ calls. 520 ** - mwMalloc() debugging allocator 521 ** - mwMalloc_() always resolves to a clean call of malloc() 522 ** - mwRealloc() debugging re-allocator 523 ** - mwRealloc_() always resolves to a clean call of realloc() 524 ** - mwCalloc() debugging allocator, fills with zeros 525 ** - mwCalloc_() always resolves to a clean call of calloc() 526 ** - mwFree() debugging free. Can only free memory which has 527 ** been allocated by MEMWATCH. 528 ** - mwFree_() resolves to a) normal free() or b) debugging free. 529 ** Can free memory allocated by MEMWATCH and malloc() both. 530 ** Does not generate any runtime errors. 531 */ 532 void* mwMalloc( size_t, const char*, int ); 533 void* mwMalloc_( size_t ); 534 void* mwRealloc( void *, size_t, const char*, int ); 535 void* mwRealloc_( void *, size_t ); 536 void* mwCalloc( size_t, size_t, const char*, int ); 537 void* mwCalloc_( size_t, size_t ); 538 void mwFree( void*, const char*, int ); 539 void mwFree_( void* ); 540 char* mwStrdup( const char *, const char*, int ); 541 542 /* 543 ** Enable/disable precompiler block 544 ** This block of defines and if(n)defs make sure that references 545 ** to MEMWATCH is completely removed from the code if the MEMWATCH 546 ** manifest constant is not defined. 547 */ 548 #ifndef __MEMWATCH_C 549 #ifdef MEMWATCH 550 551 #define mwASSERT(exp) while(mwAssert((int)(exp),#exp,__FILE__,__LINE__)) 552 #ifndef MW_NOASSERT 553 #ifndef ASSERT 554 #define ASSERT mwASSERT 555 #endif /* !ASSERT */ 556 #endif /* !MW_NOASSERT */ 557 #define mwVERIFY(exp) while(mwVerify((int)(exp),#exp,__FILE__,__LINE__)) 558 #ifndef MW_NOVERIFY 559 #ifndef VERIFY 560 #define VERIFY mwVERIFY 561 #endif /* !VERIFY */ 562 #endif /* !MW_NOVERIFY */ 563 #define mwTRACE mwTrace 564 #ifndef MW_NOTRACE 565 #ifndef TRACE 566 #define TRACE mwTRACE 567 #endif /* !TRACE */ 568 #endif /* !MW_NOTRACE */ 569 570 /* some compilers use a define and not a function */ 571 /* for strdup(). */ 572 #ifdef strdup 573 #undef strdup 574 #endif 575 576 #define malloc(n) mwMalloc(n,__FILE__,__LINE__) 577 #define strdup(p) mwStrdup(p,__FILE__,__LINE__) 578 #define realloc(p,n) mwRealloc(p,n,__FILE__,__LINE__) 579 #define calloc(n,m) mwCalloc(n,m,__FILE__,__LINE__) 580 #define free(p) mwFree(p,__FILE__,__LINE__) 581 #define CHECK() mwTest(__FILE__,__LINE__,MW_TEST_ALL) 582 #define CHECK_THIS(n) mwTest(__FILE__,__LINE__,n) 583 #define CHECK_BUFFER(b) mwTestBuffer(__FILE__,__LINE__,b) 584 #define MARK(p) mwMark(p,#p,__FILE__,__LINE__) 585 #define UNMARK(p) mwUnmark(p,__FILE__,__LINE__) 586 587 #else /* MEMWATCH */ 588 589 #define mwASSERT(exp) 590 #ifndef MW_NOASSERT 591 #ifndef ASSERT 592 #define ASSERT mwASSERT 593 #endif /* !ASSERT */ 594 #endif /* !MW_NOASSERT */ 595 596 #define mwVERIFY(exp) exp 597 #ifndef MW_NOVERIFY 598 #ifndef VERIFY 599 #define VERIFY mwVERIFY 600 #endif /* !VERIFY */ 601 #endif /* !MW_NOVERIFY */ 602 603 /*lint -esym(773,mwTRACE) */ 604 #define mwTRACE /*lint -save -e506 */ 1?(void)0:mwDummyTraceFunction /*lint -restore */ 605 #ifndef MW_NOTRACE 606 #ifndef TRACE 607 /*lint -esym(773,TRACE) */ 608 #define TRACE mwTRACE 609 #endif /* !TRACE */ 610 #endif /* !MW_NOTRACE */ 611 612 extern void mwDummyTraceFunction(const char *,...); 613 /*lint -save -e652 */ 614 #define mwDoFlush(n) 615 #define mwPuts(s) 616 #define mwInit() 617 #define mwGrab(n) 618 #define mwDrop(n) 619 #define mwLimit(n) 620 #define mwTest(f,l) 621 #define mwSetOutFunc(f) 622 #define mwSetAriFunc(f) 623 #define mwDefaultAri() 624 #define mwNomansland() 625 #define mwStatistics(f) 626 #define mwMark(p,t,f,n) (p) 627 #define mwUnmark(p,f,n) (p) 628 #define mwMalloc(n,f,l) malloc(n) 629 #define mwStrdup(p,f,l) strdup(p) 630 #define mwRealloc(p,n,f,l) realloc(p,n) 631 #define mwCalloc(n,m,f,l) calloc(n,m) 632 #define mwFree(p) free(p) 633 #define mwMalloc_(n) malloc(n) 634 #define mwRealloc_(p,n) realloc(p,n) 635 #define mwCalloc_(n,m) calloc(n,m) 636 #define mwFree_(p) free(p) 637 #define mwAssert(e,es,f,l) 638 #define mwVerify(e,es,f,l) (e) 639 #define mwTrace mwDummyTrace 640 #define mwTestBuffer(f,l,b) (0) 641 #define CHECK() 642 #define CHECK_THIS(n) 643 #define CHECK_BUFFER(b) 644 #define MARK(p) (p) 645 #define UNMARK(p) (p) 646 /*lint -restore */ 647 648 #endif /* MEMWATCH */ 649 #endif /* !__MEMWATCH_C */ 650 651 #ifdef __cplusplus 652 } 653 #endif 654 655 #if 0 /* 980317: disabled C++ */ 656 657 /* 658 ** C++ support section 659 ** Implements the C++ support. Please note that in order to avoid 660 ** messing up library classes, C++ support is disabled by default. 661 ** You must NOT enable it until AFTER the inclusion of all header 662 ** files belonging to code that are not compiled with MEMWATCH, and 663 ** possibly for some that are! The reason for this is that a C++ 664 ** class may implement it's own new() function, and the preprocessor 665 ** would substitute this crucial declaration for MEMWATCH new(). 666 ** You can forcibly deny C++ support by defining MEMWATCH_NOCPP. 667 ** To enble C++ support, you must be compiling C++, MEMWATCH must 668 ** be defined, MEMWATCH_NOCPP must not be defined, and finally, 669 ** you must define 'new' to be 'mwNew', and 'delete' to be 'mwDelete'. 670 ** Unlike C, C++ code can begin executing *way* before main(), for 671 ** example if a global variable is created. For this reason, you can 672 ** declare a global variable of the class 'MemWatch'. If this is 673 ** is the first variable created, it will then check ALL C++ allocations 674 ** and deallocations. Unfortunately, this evaluation order is not 675 ** guaranteed by C++, though the compilers I've tried evaluates them 676 ** in the order encountered. 677 */ 678 #ifdef __cplusplus 679 #ifndef __MEMWATCH_C 680 #ifdef MEMWATCH 681 #ifndef MEMWATCH_NOCPP 682 extern int mwNCur; 683 extern const char *mwNFile; 684 extern int mwNLine; 685 class MemWatch { 686 public: 687 MemWatch(); 688 ~MemWatch(); 689 }; 690 void * operator new(size_t); 691 void * operator new(size_t,const char *,int); 692 void operator delete(void *); 693 #define mwNew new(__FILE__,__LINE__) 694 #define mwDelete (mwNCur=1,mwNFile=__FILE__,mwNLine=__LINE__),delete 695 #endif /* MEMWATCH_NOCPP */ 696 #endif /* MEMWATCH */ 697 #endif /* !__MEMWATCH_C */ 698 #endif /* __cplusplus */ 699 700 #endif /* 980317: disabled C++ */ 701 702 #endif /* __MEMWATCH_H */ 703 704 /* EOF MEMWATCH.H */ 705