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.

161 lines
3.5 KiB

  1. #ifndef __DEBUG_H__
  2. #define __DEBUG_H__
  3. #ifdef DEBUG
  4. #ifndef _DEBUG
  5. #define _DEBUG
  6. #endif
  7. #endif
  8. #ifdef _DEBUG
  9. /*
  10. // Put in include files to insure that necessary files have been included
  11. // to get all function prototypes and structure defined before use
  12. */
  13. #include <windows.h>
  14. /*
  15. // Need a macro to output messages to the debugger
  16. */
  17. #define DEBUG_OUT(str) OutputDebugString(str)
  18. /*
  19. // Define our trapping related debugging stuff
  20. // Define 4 levels of trap to allow us to break only in certain
  21. // conditions
  22. */
  23. #define TRAP_LEVEL_1 1
  24. #define TRAP_LEVEL_2 2
  25. #define TRAP_LEVEL_3 3
  26. #define TRAP_LEVEL_4 4
  27. #ifndef __DEBUG_C__
  28. extern INT Debug_TrapLevel;
  29. extern BOOL Debug_TrapOn;
  30. #endif
  31. #define TRAP_ON() Debug_TrapOn = TRUE
  32. #define TRAP_OFF() Debug_TrapOn = FALSE
  33. #define GET_TRAP_STATE() Debug_TrapOn
  34. #define SET_TRAP_LEVEL(lvl) Debug_TrapLevel = lvl
  35. /*
  36. // Define the trap macro which will break only when the trap levels
  37. // are matching and also displays an optional message
  38. */
  39. #undef TRAP
  40. #define TRAP(lvl, msg) \
  41. { \
  42. if (Debug_TrapOn && ((lvl) >= Debug_TrapLevel)) { \
  43. if (NULL != (msg)) \
  44. DEBUG_OUT((msg)); \
  45. \
  46. DebugBreak(); \
  47. } \
  48. }
  49. /*
  50. // Define the ASSERT macro to test the string. Current it will trap
  51. // whenever is on and the expression fails
  52. */
  53. #undef ASSERT
  54. #define ASSERT(exp) \
  55. { \
  56. if ((!(exp))) { \
  57. static CHAR TempStr[1024]; \
  58. \
  59. wsprintf(TempStr, \
  60. "Assertion Failed: %s, file: %s, line %s", \
  61. #exp, __FILE__, __LINE__); \
  62. \
  63. DEBUG_OUT(TempStr); \
  64. TRAP(TRAP_LEVEL_1, NULL); \
  65. } \
  66. }
  67. /*
  68. // Memory allocation routines
  69. */
  70. /*
  71. // Function definitions
  72. */
  73. HGLOBAL __cdecl
  74. Debug_Alloc(
  75. IN PCHAR FileName,
  76. IN ULONG LineNumber,
  77. IN DWORD AllocSize
  78. );
  79. HGLOBAL __cdecl
  80. Debug_Realloc(
  81. IN PCHAR FileName,
  82. IN ULONG LineNumber,
  83. IN PVOID MemoryBlock,
  84. IN DWORD AllocSize
  85. );
  86. HGLOBAL __cdecl
  87. Debug_Free(
  88. IN PVOID Buffer
  89. );
  90. BOOL __cdecl
  91. Debug_ValidateMemoryAlloc(
  92. IN PVOID Header,
  93. OUT PVOID AllocStatus
  94. );
  95. VOID __cdecl
  96. Debug_CheckForMemoryLeaks();
  97. /*
  98. // Wrapper macros
  99. */
  100. #define ALLOC(siz) Debug_Alloc(__FILE__, __LINE__, siz)
  101. #define REALLOC(blk, siz) Debug_Realloc(__FILE__, __LINE__, blk, siz)
  102. #define FREE(ptr) Debug_Free(ptr)
  103. #define VALIDATEMEM(ptr) Debug_ValidateMemoryAlloc(ptr, NULL)
  104. #define CHECKFORLEAKS() Debug_CheckForMemoryLeaks()
  105. #else
  106. /*
  107. // Non-debug versions of the above routines
  108. */
  109. #define DEBUG_OUT(str)
  110. #define TRAP_ON()
  111. #define TRAP_OFF()
  112. #define GET_TRAP_STATE()
  113. #define SET_TRAP_LEVEL(lvl)
  114. #undef TRAP
  115. #define TRAP(lvl, msg)
  116. #undef ASSERT
  117. #define ASSERT(exp)
  118. #define ALLOC(siz) GlobalAlloc(GPTR, siz)
  119. #define REALLOC(blk, siz) GlobalReAlloc(blk, siz, GMEM_ZEROINIT)
  120. #define FREE(ptr) GlobalFree(ptr)
  121. #define VALIDATEMEM(ptr) TRUE
  122. #define CHECKFORLEAKS()
  123. #endif
  124. #endif