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.

170 lines
4.3 KiB

  1. /*===================================================================
  2. Microsoft Denali
  3. Microsoft Confidential.
  4. Copyright 1996 Microsoft Corporation. All Rights Reserved.
  5. Component: Hash tables with LRU threading
  6. File: LinkHash.cpp
  7. Owner: DGottner
  8. This is the Link list and Hash table for use by any classes which
  9. also need LRU access to items. (This includes cache manager,
  10. script manager, and session deletion code)
  11. ===================================================================*/
  12. #include "denpre.h"
  13. #pragma hdrstop
  14. #include "LinkHash.h"
  15. #include "memchk.h"
  16. /*------------------------------------------------------------------
  17. * C L i n k H a s h
  18. */
  19. /*===================================================================
  20. CLinkHash::CLinkHash
  21. Constructor for CLinkHash
  22. Parameters:
  23. NONE
  24. Returns:
  25. NONE
  26. ===================================================================*/
  27. CLinkHash::CLinkHash( HashFunction pfnHash )
  28. : CHashTable( pfnHash )
  29. {
  30. }
  31. /*===================================================================
  32. CLinkHash::AddElem
  33. Parameters:
  34. pElem - item to add to the table. The item is marked as the most
  35. recently accessed.
  36. Returns:
  37. Returns a pointer to the item added
  38. ===================================================================*/
  39. CLruLinkElem *CLinkHash::AddElem(CLruLinkElem *pElem, BOOL fTestDups)
  40. {
  41. AssertValid();
  42. CLruLinkElem *pElemAdded = static_cast<CLruLinkElem *>(CHashTable::AddElem(pElem, fTestDups));
  43. pElemAdded->PrependTo(m_lruHead);
  44. AssertValid();
  45. return pElemAdded;
  46. }
  47. /*===================================================================
  48. CLinkHash::FindElem
  49. Parameters:
  50. pvKey - pointer to the key to insert
  51. cbKey - number of bytes in the key
  52. Returns:
  53. NULL if the key is not in the hash table, otherwise it returns
  54. a pointer to the key's record. If the key is found, it is
  55. moved to the front of the list.
  56. ===================================================================*/
  57. CLruLinkElem *CLinkHash::FindElem(const void *pvKey, int cbKey)
  58. {
  59. AssertValid();
  60. CLruLinkElem *pElemFound = static_cast<CLruLinkElem *>(CHashTable::FindElem(pvKey, cbKey));
  61. if (pElemFound)
  62. {
  63. pElemFound->PrependTo(m_lruHead);
  64. AssertValid();
  65. }
  66. return pElemFound;
  67. }
  68. /*===================================================================
  69. CLinkHash::DeleteElem
  70. Parameters:
  71. pvKey - pointer to the key to delete
  72. cbKey - number of bytes in the key
  73. Returns:
  74. NULL if the key is not in the hash table, otherwise it returns
  75. a pointer to the key's record. If the key is found, it is
  76. removed from the hash table and the LRU list.
  77. ===================================================================*/
  78. CLruLinkElem *CLinkHash::DeleteElem(const void *pvKey, int cbKey)
  79. {
  80. AssertValid();
  81. CLruLinkElem *pElemFound = static_cast<CLruLinkElem *>(CHashTable::DeleteElem(pvKey, cbKey));
  82. if (pElemFound)
  83. pElemFound->UnLink();
  84. AssertValid();
  85. return pElemFound;
  86. }
  87. /*===================================================================
  88. CLinkHash::RemoveElem
  89. Parameters:
  90. pvKey - pointer to the key to delete
  91. cbKey - number of bytes in the key
  92. Returns:
  93. NULL if the key is not in the hash table, otherwise it returns
  94. a pointer to the key's record. If the key is found, it is
  95. removed from the hash table and the LRU list.
  96. ===================================================================*/
  97. CLruLinkElem *CLinkHash::RemoveElem(CLruLinkElem *pElem)
  98. {
  99. AssertValid();
  100. CLruLinkElem *pElemRemoved = static_cast<CLruLinkElem *>(CHashTable::RemoveElem(pElem));
  101. Assert (pElemRemoved);
  102. pElemRemoved->UnLink();
  103. AssertValid();
  104. return pElemRemoved;
  105. }
  106. /*===================================================================
  107. CLinkHash::AssertValid
  108. verify the integrity of the data structure
  109. ===================================================================*/
  110. #ifdef DBG
  111. void CLinkHash::AssertValid() const
  112. {
  113. // NOTE: avoid calling CHashTable::AssertValid as long as hash table primitives are calling it.
  114. // CHashTable::AssertValid();
  115. m_lruHead.AssertValid();
  116. for (CDblLink *pLink = m_lruHead.PNext(); pLink != &m_lruHead; pLink = pLink->PNext())
  117. pLink->AssertValid();
  118. }
  119. #endif