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.

160 lines
3.3 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORP., 1993-1995
  4. * TITLE: DEBUG.CPP
  5. * VERSION: 1.0
  6. * AUTHOR: jsenior
  7. * DATE: 10/28/1998
  8. *
  9. ********************************************************************************
  10. *
  11. * CHANGE LOG:
  12. *
  13. * DATE REV DESCRIPTION
  14. * ---------- ------- ----------------------------------------------------------
  15. * 10/28/1998 jsenior Original implementation.
  16. *
  17. *******************************************************************************/
  18. #include <windows.h>
  19. #include "debug.h"
  20. #include "vec.h"
  21. struct Info {
  22. PTCHAR File;
  23. ULONG Line;
  24. PVOID Mem;
  25. };
  26. struct AllocInfo {
  27. PTCHAR File;
  28. ULONG Line;
  29. ULONG Size;
  30. HLOCAL Mem;
  31. };
  32. typedef _Vec<AllocInfo> AllocInfoVector;
  33. typedef _Vec<Info> InfoVector;
  34. InfoVector chunks;
  35. AllocInfoVector allocs;
  36. void AddMemoryChunk(PVOID Mem, PTCHAR File, ULONG Line)
  37. {
  38. Info info;
  39. info.File = File;
  40. info.Line = Line;
  41. info.Mem = Mem;
  42. chunks.push_back(info);
  43. }
  44. void RemoveMemoryChunk(PVOID Mem, PTCHAR File, ULONG Line)
  45. {
  46. if (Mem) {
  47. Info *info;
  48. for (info = chunks.begin(); info; info = chunks.next()) {
  49. if (info->Mem == Mem) {
  50. chunks.eraseCurrent();
  51. return;
  52. }
  53. }
  54. TCHAR txt[1024];
  55. wsprintf(txt, TEXT("Invalid Delete: %s, Line %d, 0x%x\n"), File, Line, Mem);
  56. OutputDebugString(txt);
  57. }
  58. }
  59. void DumpOrphans()
  60. {
  61. Info *info;
  62. TCHAR txt[1024];
  63. for (info = chunks.begin(); info; info = chunks.next()) {
  64. wsprintf(txt, TEXT("Leak at: %s, Line %d, 0x%x\n"),
  65. info->File, info->Line, info->Mem);
  66. OutputDebugString(txt);
  67. }
  68. AllocInfo *aInfo;
  69. for (aInfo = allocs.begin(); aInfo; aInfo = allocs.next()) {
  70. wsprintf(txt, TEXT("Leak at: %s, Line %d, Size %d, Mem 0x%x\n"),
  71. aInfo->File, aInfo->Line, aInfo->Size, aInfo->Mem);
  72. OutputDebugString(txt);
  73. }
  74. }
  75. #undef LocalAlloc
  76. #undef LocalFree
  77. HLOCAL
  78. UsbAllocPrivate (
  79. const TCHAR *File,
  80. ULONG Line,
  81. ULONG Flags,
  82. DWORD dwBytes
  83. )
  84. {
  85. DWORD bytes;
  86. AllocInfo info;
  87. HLOCAL hMem=NULL;
  88. if (dwBytes) {
  89. bytes = dwBytes;
  90. hMem = LocalAlloc(Flags, bytes);
  91. if (hMem != NULL) {
  92. info.File = (TCHAR*) File;
  93. info.Line = Line;
  94. info.Size = dwBytes;
  95. info.Mem = hMem;
  96. allocs.push_back(info);
  97. return hMem;
  98. }
  99. }
  100. return hMem;
  101. }
  102. HLOCAL
  103. UsbFreePrivate (
  104. HLOCAL hMem
  105. )
  106. {
  107. if (hMem)
  108. {
  109. AllocInfo *info;
  110. for (info = allocs.begin(); info; info = allocs.next()) {
  111. if (info->Mem == hMem) {
  112. allocs.eraseCurrent();
  113. return LocalFree(hMem);
  114. }
  115. }
  116. TCHAR txt[1024];
  117. wsprintf(txt, TEXT("Invalid Memory Free: Memory 0x%x\n"), hMem);
  118. OutputDebugString(txt);
  119. }
  120. return LocalFree(hMem);
  121. }
  122. #if DBG
  123. ULONG USBUI_Debug_Trace_Level = LERROR;
  124. #endif // DBG
  125. void TRACE(LPCTSTR Format, ...)
  126. {
  127. va_list arglist;
  128. va_start(arglist, Format);
  129. TCHAR buf[200];
  130. wvsprintf(buf, Format, arglist);
  131. OutputDebugString(buf);
  132. va_end(arglist);
  133. }