Main Page | Modules | Data Structures | File List | Data Fields | Related Pages

memwatch.h

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 #ifdef MEMWATCH
00329 
00330 #ifndef __MEMWATCH_H
00331 #define __MEMWATCH_H
00332 
00333 /* Make sure that malloc(), realloc(), calloc() and free() are declared. */
00334 /*lint -save -e537 */
00335 #include <stdlib.h>
00336 /*lint -restore */
00337 
00338 #ifdef __cplusplus
00339 extern "C" {
00340 #endif
00341 
00342 
00343 /*
00344 ** Constants used
00345 **  All MEMWATCH constants start with the prefix MW_, followed by
00346 **  a short mnemonic which indicates where the constant is used,
00347 **  followed by a descriptive text about it.
00348 */
00349 
00350 #define MW_ARI_NULLREAD 0x10    /* Null read (to start debugger) */
00351 #define MW_ARI_ABORT    0x04    /* ARI handler says: abort program! */
00352 #define MW_ARI_RETRY    0x02    /* ARI handler says: retry action! */
00353 #define MW_ARI_IGNORE   0x01    /* ARI handler says: ignore error! */
00354 
00355 #define MW_VAL_NEW      0xFE    /* value in newly allocated memory */
00356 #define MW_VAL_DEL      0xFD    /* value in newly deleted memory */
00357 #define MW_VAL_NML      0xFC    /* value in no-mans-land */
00358 #define MW_VAL_GRB      0xFB    /* value in grabbed memory */
00359 
00360 #define MW_TEST_ALL     0xFFFF  /* perform all tests */
00361 #define MW_TEST_CHAIN   0x0001  /* walk the heap chain */
00362 #define MW_TEST_ALLOC   0x0002  /* test allocations & NML guards */
00363 #define MW_TEST_NML     0x0004  /* test all-NML areas for modifications */
00364 
00365 #define MW_NML_NONE     0       /* no NML */
00366 #define MW_NML_FREE     1       /* turn FREE'd memory into NML */
00367 #define MW_NML_ALL      2       /* all unused memory is NML */
00368 #define MW_NML_DEFAULT  0       /* the default NML setting */
00369 
00370 #define MW_STAT_GLOBAL  0       /* only global statistics collected */
00371 #define MW_STAT_MODULE  1       /* collect statistics on a module basis */
00372 #define MW_STAT_LINE    2       /* collect statistics on a line basis */
00373 #define MW_STAT_DEFAULT 0       /* the default statistics setting */
00374 
00375 /*
00376 ** MemWatch internal constants
00377 **  You may change these and recompile MemWatch to change the limits
00378 **  of some parameters. Respect the recommended minimums!
00379 */
00380 #define MW_TRACE_BUFFER 2048    /* (min 160) size of TRACE()'s output buffer */
00381 #define MW_FREE_LIST    64      /* (min 4) number of free()'s to track */
00382 
00383 /*
00384 ** Exported variables
00385 **  In case you have to remove the 'const' keyword because your compiler
00386 **  doesn't support it, be aware that changing the values may cause
00387 **  unpredictable behaviour.
00388 **  - mwCounter contains the current action count. You can use this to
00389 **      place breakpoints using a debugger, if you want.
00390 */
00391 #ifndef __MEMWATCH_C
00392 extern const unsigned long mwCounter;
00393 #endif
00394 
00395 /*
00396 ** System functions
00397 **  Normally, it is not nessecary to call any of these. MEMWATCH will
00398 **  automatically initialize itself on the first MEMWATCH function call,
00399 **  and set up a call to mwAbort() using atexit(). Some C++ implementations
00400 **  run the atexit() chain before the program has terminated, so you
00401 **  may have to use mwInit() or the MemWatch C++ class to get good
00402 **  behaviour.
00403 **  - mwInit() can be called to disable the atexit() usage. If mwInit()
00404 **      is called directly, you must call mwTerm() to end MemWatch, or
00405 **      mwAbort().
00406 **  - mwTerm() is usually not nessecary to call; but if called, it will
00407 **      call mwAbort() if it finds that it is cancelling the 'topmost'
00408 **      mwInit() call.
00409 **  - mwAbort() cleans up after MEMWATCH, reports unfreed buffers, etc.
00410 */
00411 void  mwInit( void );
00412 void  mwTerm( void );
00413 void  mwAbort( void );
00414 
00415 /*
00416 ** Setup functions
00417 **  These functions control the operation of MEMWATCH's protective features.
00418 **  - mwFlushNow() causes MEMWATCH to flush it's buffers.
00419 **  - mwDoFlush() controls whether MEMWATCH flushes the disk buffers after
00420 **      writes. The default is smart flushing: MEMWATCH will not flush buffers
00421 **      explicitly until memory errors are detected. Then, all writes are
00422 **      flushed until program end or mwDoFlush(0) is called.
00423 **  - mwLimit() sets the allocation limit, an arbitrary limit on how much
00424 **      memory your program may allocate in bytes. Used to stress-test app.
00425 **      Also, in virtual-memory or multitasking environs, puts a limit on
00426 **      how much MW_NML_ALL can eat up.
00427 **  - mwGrab() grabs up X kilobytes of memory. Allocates actual memory,
00428 **      can be used to stress test app & OS both.
00429 **  - mwDrop() drops X kilobytes of grabbed memory.
00430 **  - mwNoMansLand() sets the behaviour of the NML logic. See the
00431 **      MW_NML_xxx for more information. The default is MW_NML_DEFAULT.
00432 **  - mwStatistics() sets the behaviour of the statistics collector. See
00433 **      the MW_STAT_xxx defines for more information. Default MW_STAT_DEFAULT.
00434 **  - mwFreeBufferInfo() enables or disables the tagging of free'd buffers
00435 **      with freeing information. This information is written in text form,
00436 **      using sprintf(), so it's pretty slow. Disabled by default.
00437 **  - mwAutoCheck() performs a CHECK() operation whenever a MemWatch function
00438 **      is used. Slows down performance, of course.
00439 **  - mwCalcCheck() calculates checksums for all data buffers. Slow!
00440 **  - mwDumpCheck() logs buffers where stored & calc'd checksums differ. Slow!!
00441 **  - mwMark() sets a generic marker. Returns the pointer given.
00442 **  - mwUnmark() removes a generic marker. If, at the end of execution, some
00443 **      markers are still in existence, these will be reported as leakage.
00444 **      returns the pointer given.
00445 */
00446 void        mwFlushNow( void );
00447 void        mwDoFlush( int onoff );
00448 void        mwLimit( long bytes );
00449 unsigned    mwGrab( unsigned kilobytes );
00450 unsigned    mwDrop( unsigned kilobytes );
00451 void        mwNoMansLand( int mw_nml_level );
00452 void        mwStatistics( int level );
00453 void        mwFreeBufferInfo( int onoff );
00454 void        mwAutoCheck( int onoff );
00455 void        mwCalcCheck( void );
00456 void        mwDumpCheck( void );
00457 void *      mwMark( void *p, const char *description, const char *file, unsigned line );
00458 void *      mwUnmark( void *p, const char *file, unsigned line );
00459 
00460 /*
00461 ** Testing/verification/tracing
00462 **  All of these macros except VERIFY() evaluates to a null statement
00463 **  if MEMWATCH is not defined during compilation.
00464 **  - mwIsReadAddr() checks a memory area for read privilige.
00465 **  - mwIsSafeAddr() checks a memory area for both read & write privilige.
00466 **      This function and mwIsReadAddr() is highly system-specific and
00467 **      may not be implemented. If this is the case, they will default
00468 **      to returning nonzero for any non-NULL pointer.
00469 **  - CHECK() does a complete memory integrity test. Slow!
00470 **  - CHECK_THIS() checks only selected components.
00471 **  - CHECK_BUFFER() checks the indicated buffer for errors.
00472 **  - mwASSERT() or ASSERT() If the expression evaluates to nonzero, execution continues.
00473 **      Otherwise, the ARI handler is called, if present. If not present,
00474 **      the default ARI action is taken (set with mwSetAriAction()).
00475 **      ASSERT() can be disabled by defining MW_NOASSERT.
00476 **  - mwVERIFY() or VERIFY() works just like ASSERT(), but when compiling without
00477 **      MEMWATCH the macro evaluates to the expression.
00478 **      VERIFY() can be disabled by defining MW_NOVERIFY.
00479 **  - mwTRACE() or TRACE() writes some text and data to the log. Use like printf().
00480 **      TRACE() can be disabled by defining MW_NOTRACE.
00481 */
00482 int   mwIsReadAddr( const void *p, unsigned len );
00483 int   mwIsSafeAddr( void *p, unsigned len );
00484 int   mwTest( const char *file, int line, int mw_test_flags );
00485 int   mwTestBuffer( const char *file, int line, void *p );
00486 int   mwAssert( int, const char*, const char*, int );
00487 int   mwVerify( int, const char*, const char*, int );
00488 
00489 /*
00490 ** User I/O functions
00491 **  - mwTrace() works like printf(), but dumps output either to the
00492 **      function specified with mwSetOutFunc(), or the log file.
00493 **  - mwPuts() works like puts(), dumps output like mwTrace().
00494 **  - mwSetOutFunc() allows you to give the adress of a function
00495 **      where all user output will go. (exeption: see mwSetAriFunc)
00496 **      Specifying NULL will direct output to the log file.
00497 **  - mwSetAriFunc() gives MEMWATCH the adress of a function to call
00498 **      when an 'Abort, Retry, Ignore' question is called for. The
00499 **      actual error message is NOT printed when you've set this adress,
00500 **      but instead it is passed as an argument. If you call with NULL
00501 **      for an argument, the ARI handler is disabled again. When the
00502 **      handler is disabled, MEMWATCH will automatically take the
00503 **      action specified by mwSetAriAction().
00504 **  - mwSetAriAction() sets the default ARI return value MEMWATCH should
00505 **      use if no ARI handler is specified. Defaults to MW_ARI_ABORT.
00506 **  - mwAriHandler() is an ANSI ARI handler you can use if you like. It
00507 **      dumps output to stderr, and expects input from stdin.
00508 **  - mwBreakOut() is called in certain cases when MEMWATCH feels it would
00509 **      be nice to break into a debugger. If you feel like MEMWATCH, place
00510 **      an execution breakpoint on this function.
00511 */
00512 void  mwTrace( const char* format_string, ... );
00513 void  mwPuts( const char* text );
00514 void  mwSetOutFunc( void (*func)(int) );
00515 void  mwSetAriFunc( int (*func)(const char*) );
00516 void  mwSetAriAction( int mw_ari_value );
00517 int   mwAriHandler( const char* cause );
00518 void  mwBreakOut( const char* cause );
00519 
00520 /*
00521 ** Allocation/deallocation functions
00522 **  These functions are the ones actually to perform allocations
00523 **  when running MEMWATCH, for both C and C++ calls.
00524 **  - mwMalloc() debugging allocator
00525 **  - mwMalloc_() always resolves to a clean call of malloc()
00526 **  - mwRealloc() debugging re-allocator
00527 **  - mwRealloc_() always resolves to a clean call of realloc()
00528 **  - mwCalloc() debugging allocator, fills with zeros
00529 **  - mwCalloc_() always resolves to a clean call of calloc()
00530 **  - mwFree() debugging free. Can only free memory which has
00531 **      been allocated by MEMWATCH.
00532 **  - mwFree_() resolves to a) normal free() or b) debugging free.
00533 **      Can free memory allocated by MEMWATCH and malloc() both.
00534 **      Does not generate any runtime errors.
00535 */
00536 void* mwMalloc( size_t, const char*, int );
00537 void* mwMalloc_( size_t );
00538 void* mwRealloc( void *, size_t, const char*, int );
00539 void* mwRealloc_( void *, size_t );
00540 void* mwCalloc( size_t, size_t, const char*, int );
00541 void* mwCalloc_( size_t, size_t );
00542 void  mwFree( void*, const char*, int );
00543 void  mwFree_( void* );
00544 char* mwStrdup( const char *, const char*, int );
00545 
00546 /*
00547 ** Enable/disable precompiler block
00548 **  This block of defines and if(n)defs make sure that references
00549 **  to MEMWATCH is completely removed from the code if the MEMWATCH
00550 **  manifest constant is not defined.
00551 */
00552 #ifndef __MEMWATCH_C
00553 #ifdef MEMWATCH
00554 
00555 #define mwASSERT(exp)   while(mwAssert((int)(exp),#exp,__FILE__,__LINE__))
00556 #ifndef MW_NOASSERT
00557 #ifndef ASSERT
00558 #define ASSERT          mwASSERT
00559 #endif /* !ASSERT */
00560 #endif /* !MW_NOASSERT */
00561 #define mwVERIFY(exp)   while(mwVerify((int)(exp),#exp,__FILE__,__LINE__))
00562 #ifndef MW_NOVERIFY
00563 #ifndef VERIFY
00564 #define VERIFY          mwVERIFY
00565 #endif /* !VERIFY */
00566 #endif /* !MW_NOVERIFY */
00567 #define mwTRACE         mwTrace
00568 #ifndef MW_NOTRACE
00569 #ifndef TRACE
00570 #define TRACE           mwTRACE
00571 #endif /* !TRACE */
00572 #endif /* !MW_NOTRACE */
00573 
00574 /* some compilers use a define and not a function */
00575 /* for strdup(). */
00576 #ifdef strdup
00577 #undef strdup
00578 #endif
00579 
00580 #define malloc(n)       mwMalloc(n,__FILE__,__LINE__)
00581 #define strdup(p)       mwStrdup(p,__FILE__,__LINE__)
00582 #define realloc(p,n)    mwRealloc(p,n,__FILE__,__LINE__)
00583 #define calloc(n,m)     mwCalloc(n,m,__FILE__,__LINE__)
00584 #define free(p)         mwFree(p,__FILE__,__LINE__)
00585 #define CHECK()         mwTest(__FILE__,__LINE__,MW_TEST_ALL)
00586 #define CHECK_THIS(n)   mwTest(__FILE__,__LINE__,n)
00587 #define CHECK_BUFFER(b) mwTestBuffer(__FILE__,__LINE__,b)
00588 #define MARK(p)         mwMark(p,#p,__FILE__,__LINE__)
00589 #define UNMARK(p)       mwUnmark(p,__FILE__,__LINE__)
00590 
00591 #else /* MEMWATCH */
00592 
00593 #define mwASSERT(exp)
00594 #ifndef MW_NOASSERT
00595 #ifndef ASSERT
00596 #define ASSERT          mwASSERT
00597 #endif /* !ASSERT */
00598 #endif /* !MW_NOASSERT */
00599 
00600 #define mwVERIFY(exp)    exp
00601 #ifndef MW_NOVERIFY
00602 #ifndef VERIFY
00603 #define VERIFY          mwVERIFY
00604 #endif /* !VERIFY */
00605 #endif /* !MW_NOVERIFY */
00606 
00607 /*lint -esym(773,mwTRACE) */
00608 #define mwTRACE         /*lint -save -e506 */ 1?(void)0:mwDummyTraceFunction /*lint -restore */
00609 #ifndef MW_NOTRACE
00610 #ifndef TRACE
00611 /*lint -esym(773,TRACE) */
00612 #define TRACE           mwTRACE
00613 #endif /* !TRACE */
00614 #endif /* !MW_NOTRACE */
00615 
00616 extern void mwDummyTraceFunction(const char *,...);
00617 /*lint -save -e652 */
00618 #define mwDoFlush(n)
00619 #define mwPuts(s)
00620 #define mwInit()
00621 #define mwGrab(n)
00622 #define mwDrop(n)
00623 #define mwLimit(n)
00624 #define mwTest(f,l)
00625 #define mwSetOutFunc(f)
00626 #define mwSetAriFunc(f)
00627 #define mwDefaultAri()
00628 #define mwNomansland()
00629 #define mwStatistics(f)
00630 #define mwMark(p,t,f,n)     (p)
00631 #define mwUnmark(p,f,n)     (p)
00632 #define mwMalloc(n,f,l)     malloc(n)
00633 #define mwStrdup(p,f,l)     strdup(p)
00634 #define mwRealloc(p,n,f,l)  realloc(p,n)
00635 #define mwCalloc(n,m,f,l)   calloc(n,m)
00636 #define mwFree(p)           free(p)
00637 #define mwMalloc_(n)        malloc(n)
00638 #define mwRealloc_(p,n)     realloc(p,n)
00639 #define mwCalloc_(n,m)      calloc(n,m)
00640 #define mwFree_(p)          free(p)
00641 #define mwAssert(e,es,f,l)
00642 #define mwVerify(e,es,f,l)  (e)
00643 #define mwTrace             mwDummyTrace
00644 #define mwTestBuffer(f,l,b) (0)
00645 #define CHECK()
00646 #define CHECK_THIS(n)
00647 #define CHECK_BUFFER(b)
00648 #define MARK(p)             (p)
00649 #define UNMARK(p)           (p)
00650 /*lint -restore */
00651 
00652 #endif /* MEMWATCH */
00653 #endif /* !__MEMWATCH_C */
00654 
00655 #ifdef __cplusplus
00656     }
00657 #endif
00658 
00659 #if 0 /* 980317: disabled C++ */
00660 
00661 /*
00662 ** C++ support section
00663 **  Implements the C++ support. Please note that in order to avoid
00664 **  messing up library classes, C++ support is disabled by default.
00665 **  You must NOT enable it until AFTER the inclusion of all header
00666 **  files belonging to code that are not compiled with MEMWATCH, and
00667 **  possibly for some that are! The reason for this is that a C++
00668 **  class may implement it's own new() function, and the preprocessor
00669 **  would substitute this crucial declaration for MEMWATCH new().
00670 **  You can forcibly deny C++ support by defining MEMWATCH_NOCPP.
00671 **  To enble C++ support, you must be compiling C++, MEMWATCH must
00672 **  be defined, MEMWATCH_NOCPP must not be defined, and finally,
00673 **  you must define 'new' to be 'mwNew', and 'delete' to be 'mwDelete'.
00674 **  Unlike C, C++ code can begin executing *way* before main(), for
00675 **  example if a global variable is created. For this reason, you can
00676 **  declare a global variable of the class 'MemWatch'. If this is
00677 **  is the first variable created, it will then check ALL C++ allocations
00678 **  and deallocations. Unfortunately, this evaluation order is not
00679 **  guaranteed by C++, though the compilers I've tried evaluates them
00680 **  in the order encountered.
00681 */
00682 #ifdef __cplusplus
00683 #ifndef __MEMWATCH_C
00684 #ifdef MEMWATCH
00685 #ifndef MEMWATCH_NOCPP
00686 extern int mwNCur;
00687 extern const char *mwNFile;
00688 extern int mwNLine;
00689 class MemWatch {
00690 public:
00691     MemWatch();
00692     ~MemWatch();
00693     };
00694 void * operator new(size_t);
00695 void * operator new(size_t,const char *,int);
00696 void * operator new[] (size_t,const char *,int);    // hjc 07/16/02
00697 void operator delete(void *);
00698 #define mwNew new(__FILE__,__LINE__)
00699 #define mwDelete (mwNCur=1,mwNFile=__FILE__,mwNLine=__LINE__),delete
00700 #endif /* MEMWATCH_NOCPP */
00701 #endif /* MEMWATCH */
00702 #endif /* !__MEMWATCH_C */
00703 #endif /* __cplusplus */
00704 
00705 #endif /* 980317: disabled C++ */
00706 
00707 #endif /* __MEMWATCH_H */
00708 
00709 #endif /* MEMWATCH */
00710     
00711 /* EOF MEMWATCH.H */

Generated on Wed Mar 30 13:43:26 2005 for Mntd by  doxygen 1.3.9.1