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