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.

233 lines
4.0 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1994 - 1999
  6. //
  7. // File: hashtabl.cxx
  8. //
  9. //--------------------------------------------------------------------------
  10. /*++
  11. Module Name:
  12. hashtabl.cxx
  13. Abstract:
  14. Author:
  15. Jeff Roberts (jroberts) 9-Nov-1994
  16. Revision History:
  17. 9-Nov-1994 jroberts
  18. Created this module.
  19. --*/
  20. #include <precomp.hxx>
  21. #include "hashtabl.hxx"
  22. UUID_HASH_TABLE::UUID_HASH_TABLE(
  23. RPC_STATUS * pStatus,
  24. unsigned long SpinCount
  25. )
  26. /*++
  27. Routine Description:
  28. Arguments:
  29. Return Value:
  30. Exceptions:
  31. --*/
  32. {
  33. unsigned u;
  34. for (u = 0; u < BUCKET_COUNT; ++u)
  35. {
  36. #ifdef DEBUGRPC
  37. Counts[u] = 0;
  38. #endif
  39. Buckets[u] = 0;
  40. }
  41. //
  42. // Allocate bucket mutexes. If one or more fail to initialize,
  43. // continue anyway so that the pointer is valid for all 'u'.
  44. // They will be deleted in the destructor.
  45. // BUBUG this modification needs testing
  46. //
  47. for (u = 0; u < MUTEX_COUNT; ++u)
  48. {
  49. BucketMutexes[u] = new MUTEX(pStatus);
  50. if ((BucketMutexes[u] == NULL) || (*pStatus))
  51. {
  52. if (BucketMutexes[u] == NULL)
  53. *pStatus = RPC_S_OUT_OF_MEMORY;
  54. }
  55. else
  56. {
  57. BucketMutexes[u]->SetSpinCount(SpinCount);
  58. }
  59. }
  60. }
  61. UUID_HASH_TABLE::~UUID_HASH_TABLE(
  62. )
  63. {
  64. unsigned u;
  65. for (u = 0; u < MUTEX_COUNT; ++u)
  66. {
  67. delete BucketMutexes[u];
  68. }
  69. }
  70. unsigned
  71. UUID_HASH_TABLE::Add(
  72. UUID_HASH_TABLE_NODE * pNode,
  73. unsigned Hash
  74. )
  75. {
  76. if (Hash == NO_HASH)
  77. {
  78. Hash = MakeHash(&pNode->Uuid);
  79. }
  80. ASSERT( Hash < BUCKET_COUNT );
  81. if (Buckets[Hash])
  82. {
  83. ASSERT(Buckets[Hash]->pPrev == 0);
  84. }
  85. #ifdef DEBUGRPC
  86. BOOL Seen = FALSE;
  87. unsigned Count = 0;
  88. UUID_HASH_TABLE_NODE * pScan = Buckets[Hash];
  89. while (pScan)
  90. {
  91. ++Count;
  92. ASSERT(Count <= Counts[Hash]);
  93. if (pScan == pNode)
  94. {
  95. Seen = TRUE;
  96. }
  97. if (pScan->pNext)
  98. {
  99. ASSERT(pScan->pNext->pPrev == pScan);
  100. }
  101. pScan = pScan->pNext;
  102. }
  103. ASSERT(!Seen);
  104. ASSERT(Count == Counts[Hash]);
  105. ++Counts[Hash];
  106. #endif
  107. pNode->pPrev = 0;
  108. pNode->pNext = Buckets[Hash];
  109. Buckets[Hash] = pNode;
  110. if (pNode->pNext)
  111. {
  112. pNode->pNext->pPrev = pNode;
  113. }
  114. return Hash;
  115. }
  116. void
  117. UUID_HASH_TABLE::Remove(
  118. UUID_HASH_TABLE_NODE * pNode,
  119. unsigned Hash
  120. )
  121. {
  122. if (Hash == NO_HASH)
  123. {
  124. Hash = MakeHash(&pNode->Uuid);
  125. }
  126. ASSERT( Hash < BUCKET_COUNT );
  127. #ifdef DEBUGRPC
  128. BOOL Seen = FALSE;
  129. unsigned Count = 0;
  130. UUID_HASH_TABLE_NODE * pScan = Buckets[Hash];
  131. while (pScan)
  132. {
  133. ++Count;
  134. ASSERT(Count <= Counts[Hash]);
  135. if (pScan == pNode)
  136. {
  137. Seen = TRUE;
  138. }
  139. if (pScan->pNext)
  140. {
  141. ASSERT(pScan->pNext->pPrev == pScan);
  142. }
  143. pScan = pScan->pNext;
  144. }
  145. ASSERT(Seen);
  146. ASSERT(Count == Counts[Hash]);
  147. --Counts[Hash];
  148. #endif
  149. ASSERT(pNode->pPrev != pNode);
  150. ASSERT(pNode->pNext != pNode);
  151. if (pNode->pPrev != 0)
  152. {
  153. ASSERT(pNode->pPrev->pNext == pNode);
  154. pNode->pPrev->pNext = pNode->pNext;
  155. }
  156. else
  157. {
  158. ASSERT(Buckets[Hash] == pNode);
  159. Buckets[Hash] = pNode->pNext;
  160. }
  161. if (pNode->pNext != 0)
  162. {
  163. ASSERT(pNode->pNext->pPrev == pNode);
  164. pNode->pNext->pPrev = pNode->pPrev;
  165. }
  166. #ifdef DEBUGRPC
  167. pNode->pPrev = INVALID_NODE;
  168. pNode->pNext = INVALID_NODE;
  169. #endif
  170. }