memwatch.h 31 KB

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