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.

680 lines
17 KiB

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