Leaked source code of windows server 2003
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.

189 lines
3.4 KiB

  1. #include <windows.h>
  2. #include "listbase.h"
  3. #include "debug.h"
  4. CLinkedList::CLinkedList()
  5. {
  6. // OutputDebugString(">> CLinkedList CONstructor called <<\n");
  7. m_fInitialized = FALSE;
  8. m_pHead = NULL;
  9. m_pfnIsMatch = NULL;
  10. m_pfnFreeElt = NULL;
  11. }
  12. CLinkedList::~CLinkedList()
  13. {
  14. // OutputDebugString(">> CLinkedList DEstructor called <<\n");
  15. if(m_fInitialized)
  16. {
  17. #if DBG
  18. EnterCriticalSection(&m_critsecListBusy);
  19. ELT* ple = m_pHead;
  20. while(ple)
  21. {
  22. OutputDebugStringW(L"Caught list leak!\n");
  23. ple = ple->pNext;
  24. }
  25. LeaveCriticalSection(&m_critsecListBusy);
  26. #endif
  27. Reset();
  28. DeleteCriticalSection(&m_critsecListBusy);
  29. }
  30. }
  31. BOOL CLinkedList::Initialize()
  32. {
  33. __try
  34. {
  35. if(!m_fInitialized)
  36. {
  37. InitializeCriticalSection(&m_critsecListBusy);
  38. }
  39. m_fInitialized = TRUE;
  40. }
  41. __except(EXCEPTION_EXECUTE_HANDLER)
  42. {
  43. }
  44. return m_fInitialized;
  45. }
  46. BOOL CLinkedList::Reset()
  47. {
  48. //////////////////////
  49. // walk list, free
  50. ELT* ple;
  51. if(m_fInitialized)
  52. {
  53. EnterCriticalSection(&m_critsecListBusy);
  54. while (m_pHead)
  55. {
  56. ple = m_pHead;
  57. m_pHead = ple->pNext;
  58. m_pfnFreeElt(ple);
  59. }
  60. LeaveCriticalSection(&m_critsecListBusy);
  61. }
  62. return m_fInitialized;
  63. }
  64. BOOL CLinkedList::AddToList(ELT* pListItem)
  65. {
  66. if(m_fInitialized)
  67. {
  68. EnterCriticalSection(&m_critsecListBusy);
  69. pListItem->pNext = m_pHead; // insert into linked list
  70. m_pHead = pListItem;
  71. LeaveCriticalSection(&m_critsecListBusy);
  72. }
  73. return m_fInitialized;
  74. }
  75. BOOL CLinkedList::DelFromList(ELT* pv)
  76. {
  77. SS_ASSERT(pv != NULL);
  78. if(!m_fInitialized)
  79. {
  80. return FALSE;
  81. }
  82. EnterCriticalSection(&m_critsecListBusy);
  83. ELT* ple = m_pHead;
  84. ELT* plePrior = NULL;
  85. while (ple)
  86. {
  87. if (m_pfnIsMatch(ple, pv))
  88. break;
  89. plePrior = ple;
  90. ple = ple->pNext;
  91. }
  92. // if we didn't find a match, return
  93. if (NULL == ple)
  94. {
  95. LeaveCriticalSection(&m_critsecListBusy);
  96. return FALSE;
  97. }
  98. // else remove from list
  99. if (NULL == plePrior)
  100. m_pHead = ple->pNext;
  101. else
  102. plePrior->pNext = ple->pNext;
  103. LeaveCriticalSection(&m_critsecListBusy);
  104. // delete extracted item
  105. m_pfnFreeElt(ple);
  106. return TRUE;
  107. }
  108. ELT* CLinkedList::SearchList(ELT* pv)
  109. {
  110. SS_ASSERT(pv != NULL);
  111. if(!m_fInitialized)
  112. {
  113. return NULL;
  114. }
  115. ELT* ple;
  116. EnterCriticalSection(&m_critsecListBusy);
  117. ple = m_pHead;
  118. while (ple)
  119. {
  120. if (m_pfnIsMatch(ple, pv))
  121. {
  122. LeaveCriticalSection(&m_critsecListBusy);
  123. return ple;
  124. }
  125. ple = ple->pNext;
  126. }
  127. LeaveCriticalSection(&m_critsecListBusy);
  128. return NULL;
  129. }
  130. BOOL CLinkedList::LockList()
  131. {
  132. if(m_fInitialized)
  133. {
  134. EnterCriticalSection(&m_critsecListBusy);
  135. }
  136. return m_fInitialized;
  137. }
  138. BOOL CLinkedList::UnlockList()
  139. {
  140. if(m_fInitialized)
  141. {
  142. LeaveCriticalSection(&m_critsecListBusy);
  143. }
  144. return m_fInitialized;
  145. }