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.

138 lines
2.4 KiB

  1. // TreeChecker.cpp
  2. #include "precomp.h"
  3. //#pragma warning(disable:4786)
  4. #include <wbemcomn.h>
  5. #include <genutils.h>
  6. #include "evaltree.h"
  7. #include "evaltree.inl"
  8. #include "TreeChecker.h"
  9. #ifdef CHECK_TREES
  10. CTreeChecker g_treeChecker;
  11. void Trace(LPCTSTR szFormat, ...)
  12. {
  13. va_list ap;
  14. char szMessage[512];
  15. va_start(ap, szFormat);
  16. vsprintf(szMessage, szFormat, ap);
  17. va_end(ap);
  18. lstrcat(szMessage, "\n");
  19. OutputDebugString(szMessage);
  20. }
  21. void CTreeChecker::AddTree(CEvalTree *pTree)
  22. {
  23. CInCritSec cs1(&m_cs);
  24. m_mapTrees[pTree] = TRUE;
  25. }
  26. void CTreeChecker::RemoveTree(CEvalTree *pTree)
  27. {
  28. CInCritSec cs(&m_cs);
  29. m_mapTrees.erase(pTree);
  30. }
  31. //int g_nAdded = 0;
  32. void CTreeChecker::AddNode(CEvalNode *pNode)
  33. {
  34. CInCritSec cs(&m_cs);
  35. //g_nAdded++;
  36. Trace("Adding node = 0x%p", pNode);
  37. m_mapNodes[pNode] = TRUE;
  38. }
  39. void CTreeChecker::RemoveNode(CEvalNode *pNode)
  40. {
  41. CInCritSec cs(&m_cs);
  42. Trace("Removing node = 0x%p", pNode);
  43. m_mapNodes.erase(pNode);
  44. }
  45. void CTreeChecker::CheckoffNode(CEvalNode *pNode)
  46. {
  47. // No critsec needed--already obtained by CheckTrees().
  48. CNodeMapItor node = m_pmapNodesTemp->find(pNode);
  49. if (node != m_pmapNodesTemp->end())
  50. m_pmapNodesTemp->erase(node);
  51. else
  52. {
  53. //DebugBreak();
  54. Trace("CEvalTree is holding on to invalid node: 0x%p", pNode);
  55. }
  56. }
  57. BOOL CTreeChecker::CheckTreeNodes()
  58. {
  59. CInCritSec cs(&m_cs);
  60. CNodeMap mapNodesTemp(m_mapNodes);
  61. BOOL bRet = TRUE;
  62. m_pmapNodesTemp = &mapNodesTemp;
  63. // This can be helpful to see what the trees look like before they're checked.
  64. #if 0
  65. FILE *pFile = fopen("c:\\temp\\trees.txt", "a");
  66. #endif
  67. for (CTreeMapItor tree = m_mapTrees.begin();
  68. tree != m_mapTrees.end();
  69. tree++)
  70. {
  71. CEvalTree *pTree = (*tree).first;
  72. // This can be helpful to see what the trees look like before they're checked.
  73. #if 0
  74. fprintf(pFile, "Tree: 0x%p\n");
  75. pTree->Dump(pFile);
  76. fprintf(pFile, "\n");
  77. fflush(pFile);
  78. #endif
  79. pTree->CheckNodes(this);
  80. }
  81. // This can be helpful to see what the trees look like before they're checked.
  82. #if 0
  83. fclose(pFile);
  84. #endif
  85. int nLeaks = 0;
  86. for (CNodeMapItor node = mapNodesTemp.begin();
  87. node != mapNodesTemp.end();
  88. node++)
  89. {
  90. CEvalNode *pNode = (*node).first;
  91. //DebugBreak();
  92. Trace("CEvalTree leaked node: 0x%p", pNode);
  93. nLeaks++;
  94. bRet = FALSE;
  95. }
  96. if (nLeaks)
  97. Trace("%d CEvalTree nodes leaked.", nLeaks);
  98. return bRet;
  99. }
  100. #endif // #ifdef CHECK_TREES