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.

689 lines
17 KiB

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