Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

563 lines
14 KiB

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