home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / include / crtdbg.h < prev    next >
C/C++ Source or Header  |  1998-06-16  |  15KB  |  563 lines

  1. /***
  2. *crtdbg.h - Supports debugging features of the C runtime library.
  3. *
  4. *       Copyright (c) 1994-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Support CRT debugging features.
  8. *
  9. *       [Public]
  10. *
  11. ****/
  12.  
  13. #if     _MSC_VER > 1000
  14. #pragma once
  15. #endif
  16.  
  17. #ifndef _INC_CRTDBG
  18. #define _INC_CRTDBG
  19.  
  20. #if     !defined(_WIN32) && !defined(_MAC)
  21. #error ERROR: Only Mac or Win32 targets supported!
  22. #endif
  23.  
  24.  
  25. #ifdef  __cplusplus
  26. extern "C" {
  27. #endif  /* __cplusplus */
  28.  
  29.  
  30.  /****************************************************************************
  31.  *
  32.  * Constants and types
  33.  *
  34.  ***************************************************************************/
  35.  
  36.  
  37. #ifndef _SIZE_T_DEFINED
  38. typedef unsigned int size_t;
  39. #define _SIZE_T_DEFINED
  40. #endif
  41.  
  42. /* Define NULL pointer value */
  43.  
  44. #ifndef NULL
  45. #ifdef  __cplusplus
  46. #define NULL    0
  47. #else
  48. #define NULL    ((void *)0)
  49. #endif
  50. #endif
  51.  
  52.  /****************************************************************************
  53.  *
  54.  * Debug Reporting
  55.  *
  56.  ***************************************************************************/
  57.  
  58. typedef void *_HFILE; /* file handle pointer */
  59.  
  60. #define _CRT_WARN           0
  61. #define _CRT_ERROR          1
  62. #define _CRT_ASSERT         2
  63. #define _CRT_ERRCNT         3
  64.  
  65. #define _CRTDBG_MODE_FILE      0x1
  66. #define _CRTDBG_MODE_DEBUG     0x2
  67. #define _CRTDBG_MODE_WNDW      0x4
  68. #define _CRTDBG_REPORT_MODE    -1
  69.  
  70. #define _CRTDBG_INVALID_HFILE ((_HFILE)-1)
  71. #define _CRTDBG_HFILE_ERROR   ((_HFILE)-2)
  72. #define _CRTDBG_FILE_STDOUT   ((_HFILE)-4)
  73. #define _CRTDBG_FILE_STDERR   ((_HFILE)-5)
  74. #define _CRTDBG_REPORT_FILE   ((_HFILE)-6)
  75.  
  76. typedef int (__cdecl * _CRT_REPORT_HOOK)(int, char *, int *);
  77.  
  78.  /****************************************************************************
  79.  *
  80.  * Heap
  81.  *
  82.  ***************************************************************************/
  83.  
  84.  /****************************************************************************.
  85.  *
  86.  * Client-defined allocation hook
  87.  *
  88.  ***************************************************************************/
  89.  
  90. #define _HOOK_ALLOC     1
  91. #define _HOOK_REALLOC   2
  92. #define _HOOK_FREE      3
  93.  
  94. typedef int (__cdecl * _CRT_ALLOC_HOOK)(int, void *, size_t, int, long, const unsigned char *, int);
  95.  
  96.  /****************************************************************************
  97.  *
  98.  * Memory management
  99.  *
  100.  ***************************************************************************/
  101.  
  102. /*
  103.  * Bit values for _crtDbgFlag flag:
  104.  *
  105.  * These bitflags control debug heap behavior.
  106.  */
  107.  
  108. #define _CRTDBG_ALLOC_MEM_DF        0x01  /* Turn on debug allocation */
  109. #define _CRTDBG_DELAY_FREE_MEM_DF   0x02  /* Don't actually free memory */
  110. #define _CRTDBG_CHECK_ALWAYS_DF     0x04  /* Check heap every alloc/dealloc */
  111. #define _CRTDBG_RESERVED_DF         0x08  /* Reserved - do not use */
  112. #define _CRTDBG_CHECK_CRT_DF        0x10  /* Leak check/diff CRT blocks */
  113. #define _CRTDBG_LEAK_CHECK_DF       0x20  /* Leak check at program exit */
  114.  
  115. #define _CRTDBG_REPORT_FLAG         -1    /* Query bitflag status */
  116.  
  117. #define _BLOCK_TYPE(block)          (block & 0xFFFF)
  118. #define _BLOCK_SUBTYPE(block)       (block >> 16 & 0xFFFF)
  119.  
  120.  
  121.  /****************************************************************************
  122.  *
  123.  * Memory state
  124.  *
  125.  ***************************************************************************/
  126.  
  127. /* Memory block identification */
  128. #define _FREE_BLOCK      0
  129. #define _NORMAL_BLOCK    1
  130. #define _CRT_BLOCK       2
  131. #define _IGNORE_BLOCK    3
  132. #define _CLIENT_BLOCK    4
  133. #define _MAX_BLOCKS      5
  134.  
  135. typedef void (__cdecl * _CRT_DUMP_CLIENT)(void *, size_t);
  136.  
  137. typedef struct _CrtMemState
  138. {
  139.         struct _CrtMemBlockHeader * pBlockHeader;
  140.         unsigned long lCounts[_MAX_BLOCKS];
  141.         unsigned long lSizes[_MAX_BLOCKS];
  142.         unsigned long lHighWaterCount;
  143.         unsigned long lTotalCount;
  144. } _CrtMemState;
  145.  
  146.  
  147.  /****************************************************************************
  148.  *
  149.  * Declarations, prototype and function-like macros
  150.  *
  151.  ***************************************************************************/
  152.  
  153.  
  154. #ifndef _DEBUG
  155.  
  156.  /****************************************************************************
  157.  *
  158.  * Debug OFF
  159.  * Debug OFF
  160.  * Debug OFF
  161.  *
  162.  ***************************************************************************/
  163.  
  164. #define _ASSERT(expr) ((void)0)
  165.  
  166. #define _ASSERTE(expr) ((void)0)
  167.  
  168.  
  169. #define _RPT0(rptno, msg)
  170.  
  171. #define _RPT1(rptno, msg, arg1)
  172.  
  173. #define _RPT2(rptno, msg, arg1, arg2)
  174.  
  175. #define _RPT3(rptno, msg, arg1, arg2, arg3)
  176.  
  177. #define _RPT4(rptno, msg, arg1, arg2, arg3, arg4)
  178.  
  179.  
  180. #define _RPTF0(rptno, msg)
  181.  
  182. #define _RPTF1(rptno, msg, arg1)
  183.  
  184. #define _RPTF2(rptno, msg, arg1, arg2)
  185.  
  186. #define _RPTF3(rptno, msg, arg1, arg2, arg3)
  187.  
  188. #define _RPTF4(rptno, msg, arg1, arg2, arg3, arg4)
  189.  
  190. #define _malloc_dbg(s, t, f, l)         malloc(s)
  191. #define _calloc_dbg(c, s, t, f, l)      calloc(c, s)
  192. #define _realloc_dbg(p, s, t, f, l)     realloc(p, s)
  193. #define _expand_dbg(p, s, t, f, l)      _expand(p, s)
  194. #define _free_dbg(p, t)                 free(p)
  195. #define _msize_dbg(p, t)                _msize(p)
  196.  
  197. #define _CrtSetReportHook(f)                ((void)0)
  198. #define _CrtSetReportMode(t, f)             ((int)0)
  199. #define _CrtSetReportFile(t, f)             ((void)0)
  200.  
  201. #define _CrtDbgBreak()                      ((void)0)
  202.  
  203. #define _CrtSetBreakAlloc(a)                ((long)0)
  204.  
  205. #define _CrtSetAllocHook(f)                 ((void)0)
  206.  
  207. #define _CrtCheckMemory()                   ((int)1)
  208. #define _CrtSetDbgFlag(f)                   ((int)0)
  209. #define _CrtDoForAllClientObjects(f, c)     ((void)0)
  210. #define _CrtIsValidPointer(p, n, r)         ((int)1)
  211. #define _CrtIsValidHeapPointer(p)           ((int)1)
  212. #define _CrtIsMemoryBlock(p, t, r, f, l)    ((int)1)
  213.  
  214. #define _CrtSetDumpClient(f)                ((void)0)
  215.  
  216. #define _CrtMemCheckpoint(s)                ((void)0)
  217. #define _CrtMemDifference(s1, s2, s3)       ((int)0)
  218. #define _CrtMemDumpAllObjectsSince(s)       ((void)0)
  219. #define _CrtMemDumpStatistics(s)            ((void)0)
  220. #define _CrtDumpMemoryLeaks()               ((int)0)
  221.  
  222.  
  223. #else   /* _DEBUG */
  224.  
  225.  
  226.  /****************************************************************************
  227.  *
  228.  * Debug ON
  229.  * Debug ON
  230.  * Debug ON
  231.  *
  232.  ***************************************************************************/
  233.  
  234.  
  235. /* Define _CRTIMP */
  236.  
  237. #ifndef _CRTIMP
  238. #ifdef  _DLL
  239. #define _CRTIMP __declspec(dllimport)
  240. #else   /* ndef _DLL */
  241. #define _CRTIMP
  242. #endif  /* _DLL */
  243. #endif  /* _CRTIMP */
  244.  
  245.  /****************************************************************************
  246.  *
  247.  * Debug Reporting
  248.  *
  249.  ***************************************************************************/
  250.  
  251. _CRTIMP extern long _crtAssertBusy;
  252.  
  253. _CRTIMP _CRT_REPORT_HOOK __cdecl _CrtSetReportHook(
  254.         _CRT_REPORT_HOOK
  255.         );
  256.  
  257. _CRTIMP int __cdecl _CrtSetReportMode(
  258.         int,
  259.         int
  260.         );
  261.  
  262. _CRTIMP _HFILE __cdecl _CrtSetReportFile(
  263.         int,
  264.         _HFILE
  265.         );
  266.  
  267. _CRTIMP int __cdecl _CrtDbgReport(
  268.         int,
  269.         const char *,
  270.         int,
  271.         const char *,
  272.         const char *,
  273.         ...);
  274.  
  275. /* Asserts */
  276.  
  277. #define _ASSERT(expr) \
  278.         do { if (!(expr) && \
  279.                 (1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, NULL, NULL))) \
  280.              _CrtDbgBreak(); } while (0)
  281.  
  282. #define _ASSERTE(expr) \
  283.         do { if (!(expr) && \
  284.                 (1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, NULL, #expr))) \
  285.              _CrtDbgBreak(); } while (0)
  286.  
  287.  
  288. /* Reports with no file/line info */
  289.  
  290. #define _RPT0(rptno, msg) \
  291.         do { if ((1 == _CrtDbgReport(rptno, NULL, 0, NULL, "%s", msg))) \
  292.                 _CrtDbgBreak(); } while (0)
  293.  
  294. #define _RPT1(rptno, msg, arg1) \
  295.         do { if ((1 == _CrtDbgReport(rptno, NULL, 0, NULL, msg, arg1))) \
  296.                 _CrtDbgBreak(); } while (0)
  297.  
  298. #define _RPT2(rptno, msg, arg1, arg2) \
  299.         do { if ((1 == _CrtDbgReport(rptno, NULL, 0, NULL, msg, arg1, arg2))) \
  300.                 _CrtDbgBreak(); } while (0)
  301.  
  302. #define _RPT3(rptno, msg, arg1, arg2, arg3) \
  303.         do { if ((1 == _CrtDbgReport(rptno, NULL, 0, NULL, msg, arg1, arg2, arg3))) \
  304.                 _CrtDbgBreak(); } while (0)
  305.  
  306. #define _RPT4(rptno, msg, arg1, arg2, arg3, arg4) \
  307.         do { if ((1 == _CrtDbgReport(rptno, NULL, 0, NULL, msg, arg1, arg2, arg3, arg4))) \
  308.                 _CrtDbgBreak(); } while (0)
  309.  
  310.  
  311. /* Reports with file/line info */
  312.  
  313. #define _RPTF0(rptno, msg) \
  314.         do { if ((1 == _CrtDbgReport(rptno, __FILE__, __LINE__, NULL, "%s", msg))) \
  315.                 _CrtDbgBreak(); } while (0)
  316.  
  317. #define _RPTF1(rptno, msg, arg1) \
  318.         do { if ((1 == _CrtDbgReport(rptno, __FILE__, __LINE__, NULL, msg, arg1))) \
  319.                 _CrtDbgBreak(); } while (0)
  320.  
  321. #define _RPTF2(rptno, msg, arg1, arg2) \
  322.         do { if ((1 == _CrtDbgReport(rptno, __FILE__, __LINE__, NULL, msg, arg1, arg2))) \
  323.                 _CrtDbgBreak(); } while (0)
  324.  
  325. #define _RPTF3(rptno, msg, arg1, arg2, arg3) \
  326.         do { if ((1 == _CrtDbgReport(rptno, __FILE__, __LINE__, NULL, msg, arg1, arg2, arg3))) \
  327.                 _CrtDbgBreak(); } while (0)
  328.  
  329. #define _RPTF4(rptno, msg, arg1, arg2, arg3, arg4) \
  330.         do { if ((1 == _CrtDbgReport(rptno, __FILE__, __LINE__, NULL, msg, arg1, arg2, arg3, arg4))) \
  331.                 _CrtDbgBreak(); } while (0)
  332.  
  333. #if     defined(_M_IX86) && !defined(_CRT_PORTABLE)
  334. #define _CrtDbgBreak() __asm { int 3 }
  335. #elif   defined(_M_ALPHA) && !defined(_CRT_PORTABLE)
  336. void _BPT();
  337. #pragma intrinsic(_BPT)
  338. #define _CrtDbgBreak() _BPT()
  339. #else
  340. _CRTIMP void __cdecl _CrtDbgBreak(
  341.         void
  342.         );
  343. #endif
  344.  
  345.  /****************************************************************************
  346.  *
  347.  * Heap routines
  348.  *
  349.  ***************************************************************************/
  350.  
  351. #ifdef  _CRTDBG_MAP_ALLOC
  352.  
  353. #define   malloc(s)         _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
  354. #define   calloc(c, s)      _calloc_dbg(c, s, _NORMAL_BLOCK, __FILE__, __LINE__)
  355. #define   realloc(p, s)     _realloc_dbg(p, s, _NORMAL_BLOCK, __FILE__, __LINE__)
  356. #define   _expand(p, s)     _expand_dbg(p, s, _NORMAL_BLOCK, __FILE__, __LINE__)
  357. #define   free(p)           _free_dbg(p, _NORMAL_BLOCK)
  358. #define   _msize(p)         _msize_dbg(p, _NORMAL_BLOCK)
  359.  
  360. #endif  /* _CRTDBG_MAP_ALLOC */
  361.  
  362. _CRTIMP extern long _crtBreakAlloc;      /* Break on this allocation */
  363.  
  364. _CRTIMP long __cdecl _CrtSetBreakAlloc(
  365.         long
  366.         );
  367.  
  368. /*
  369.  * Prototypes for malloc, free, realloc, etc are in malloc.h
  370.  */
  371.  
  372. _CRTIMP void * __cdecl _malloc_dbg(
  373.         size_t,
  374.         int,
  375.         const char *,
  376.         int
  377.         );
  378.  
  379. _CRTIMP void * __cdecl _calloc_dbg(
  380.         size_t,
  381.         size_t,
  382.         int,
  383.         const char *,
  384.         int
  385.         );
  386.  
  387. _CRTIMP void * __cdecl _realloc_dbg(
  388.         void *,
  389.         size_t,
  390.         int,
  391.         const char *,
  392.         int
  393.         );
  394.  
  395. _CRTIMP void * __cdecl _expand_dbg(
  396.         void *,
  397.         size_t,
  398.         int,
  399.         const char *,
  400.         int
  401.         );
  402.  
  403. _CRTIMP void __cdecl _free_dbg(
  404.         void *,
  405.         int
  406.         );
  407.  
  408. _CRTIMP size_t __cdecl _msize_dbg (
  409.         void *,
  410.         int
  411.         );
  412.  
  413.  
  414.  /****************************************************************************
  415.  *
  416.  * Client-defined allocation hook
  417.  *
  418.  ***************************************************************************/
  419.  
  420. _CRTIMP _CRT_ALLOC_HOOK __cdecl _CrtSetAllocHook(
  421.         _CRT_ALLOC_HOOK
  422.         );
  423.  
  424.  
  425.  /****************************************************************************
  426.  *
  427.  * Memory management
  428.  *
  429.  ***************************************************************************/
  430.  
  431. /*
  432.  * Bitfield flag that controls CRT heap behavior
  433.  * Default setting is _CRTDBG_ALLOC_MEM_DF
  434.  */
  435.  
  436. _CRTIMP extern int _crtDbgFlag;
  437.  
  438. _CRTIMP int __cdecl _CrtCheckMemory(
  439.         void
  440.         );
  441.  
  442. _CRTIMP int __cdecl _CrtSetDbgFlag(
  443.         int
  444.         );
  445.  
  446. _CRTIMP void __cdecl _CrtDoForAllClientObjects(
  447.         void (*pfn)(void *, void *),
  448.         void *
  449.         );
  450.  
  451. _CRTIMP int __cdecl _CrtIsValidPointer(
  452.         const void *,
  453.         unsigned int,
  454.         int
  455.         );
  456.  
  457. _CRTIMP int __cdecl _CrtIsValidHeapPointer(
  458.         const void *
  459.         );
  460.  
  461. _CRTIMP int __cdecl _CrtIsMemoryBlock(
  462.         const void *,
  463.         unsigned int,
  464.         long *,
  465.         char **,
  466.         int *
  467.         );
  468.  
  469.  
  470.  /****************************************************************************
  471.  *
  472.  * Memory state
  473.  *
  474.  ***************************************************************************/
  475.  
  476. _CRTIMP _CRT_DUMP_CLIENT __cdecl _CrtSetDumpClient(
  477.         _CRT_DUMP_CLIENT
  478.         );
  479.  
  480. _CRTIMP void __cdecl _CrtMemCheckpoint(
  481.         _CrtMemState *
  482.         );
  483.  
  484. _CRTIMP int __cdecl _CrtMemDifference(
  485.         _CrtMemState *,
  486.         const _CrtMemState *,
  487.         const _CrtMemState *
  488.         );
  489.  
  490. _CRTIMP void __cdecl _CrtMemDumpAllObjectsSince(
  491.         const _CrtMemState *
  492.         );
  493.  
  494. _CRTIMP void __cdecl _CrtMemDumpStatistics(
  495.         const _CrtMemState *
  496.         );
  497.  
  498. _CRTIMP int __cdecl _CrtDumpMemoryLeaks(
  499.         void
  500.         );
  501.  
  502. #endif  /* _DEBUG */
  503.  
  504. #ifdef  __cplusplus
  505. }
  506.  
  507. #ifndef _MFC_OVERRIDES_NEW
  508.  
  509. #ifndef _DEBUG
  510.  
  511.  /****************************************************************************
  512.  *
  513.  * Debug OFF
  514.  * Debug OFF
  515.  * Debug OFF
  516.  *
  517.  ***************************************************************************/
  518.  
  519. inline void* __cdecl operator new(unsigned int s, int, const char *, int)
  520.         { return ::operator new(s); }
  521.  
  522. #if    _MSC_VER >= 1200
  523. inline void __cdecl operator delete(void * _P, int, const char *, int)
  524.         { ::operator delete(_P); }
  525. #endif
  526. #else /* _DEBUG */
  527.  
  528.  /****************************************************************************
  529.  *
  530.  * Debug ON
  531.  * Debug ON
  532.  * Debug ON
  533.  *
  534.  ***************************************************************************/
  535.  
  536. _CRTIMP void * __cdecl operator new(
  537.         unsigned int,
  538.         int,
  539.         const char *,
  540.         int
  541.         );
  542.  
  543.  
  544. #if    _MSC_VER >= 1200
  545. inline void __cdecl operator delete(void * _P, int, const char *, int)
  546.         { ::operator delete(_P); }
  547. #endif
  548.  
  549. #ifdef _CRTDBG_MAP_ALLOC
  550.  
  551. inline void* __cdecl operator new(unsigned int s)
  552.         { return ::operator new(s, _NORMAL_BLOCK, __FILE__, __LINE__); }
  553.  
  554. #endif  /* _CRTDBG_MAP_ALLOC */
  555.  
  556. #endif  /* _DEBUG */
  557.  
  558. #endif _MFC_OVERRIDES_NEW
  559.  
  560. #endif  /* __cplusplus */
  561.  
  562. #endif  /* _INC_CRTDBG */
  563.