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.

149 lines
4.1 KiB

  1. // Helper funcs for string maps
  2. #ifndef _BSTRHASH_INC
  3. #define _BSTRHASH_INC
  4. #pragma warning( disable : 4786 )
  5. #include <map>
  6. using namespace std;
  7. #include "lkrhash.h"
  8. template <class _Key, class _Val>
  9. class CLKWrap
  10. {
  11. public:
  12. _Key m_k;
  13. _Val m_v;
  14. mutable LONG m_cRefs;
  15. CLKWrap(_Key k, _Val v, char* id = "CLKWrap") : m_k(k), m_v(v), m_cRefs(0)
  16. {
  17. }
  18. ~CLKWrap()
  19. {
  20. }
  21. };
  22. template <class _Val>
  23. class CRawCIBstrHash
  24. : public CTypedHashTable<CRawCIBstrHash<_Val>, const CLKWrap<BSTR,_Val>, BSTR>
  25. {
  26. public:
  27. typedef CLKWrap<BSTR,_Val> ValueType;
  28. CRawCIBstrHash(LPCSTR name) :
  29. CTypedHashTable<CRawCIBstrHash, const CLKWrap<BSTR,_Val>, BSTR>(name)
  30. {}
  31. CRawCIBstrHash(LPCSTR name, double maxload, DWORD initsize, DWORD num_subtbls) :
  32. CTypedHashTable<CRawCIBstrHash, const CLKWrap<BSTR,_Val>, BSTR>(name,maxload,initsize,num_subtbls)
  33. {}
  34. static BSTR ExtractKey(const CLKWrap<BSTR,_Val> *pEntry)
  35. { return pEntry->m_k; }
  36. static DWORD CalcKeyHash(BSTR pstrKey)
  37. { return HashStringNoCase(pstrKey); }
  38. static bool EqualKeys(BSTR x, BSTR y)
  39. {
  40. if (x == NULL)
  41. {
  42. return (y==NULL ? TRUE : FALSE);
  43. }
  44. if (!y) return FALSE;
  45. return (_wcsicmp(x,y) == 0);
  46. }
  47. static void AddRefRecord(const CLKWrap<BSTR,_Val>* pTest, int nIncr)
  48. {
  49. IRTLTRACE(_TEXT("AddRef(%p, %s) %d, cRefs == %d\n"),
  50. pTest, pTest->m_k, nIncr, pTest->m_cRefs);
  51. if (nIncr == +1)
  52. {
  53. // or, perhaps, pIFoo->AddRef() (watch out for marshalling)
  54. // or ++pTest->m_cRefs (single-threaded only)
  55. InterlockedIncrement(&pTest->m_cRefs);
  56. }
  57. else if (nIncr == -1)
  58. {
  59. // or, perhaps, pIFoo->Release() or --pTest->m_cRefs;
  60. LONG l = InterlockedDecrement(&pTest->m_cRefs);
  61. // For some hashtables, it may also make sense to add the following
  62. if (l == 0) delete pTest;
  63. // but that would typically only apply when InsertRecord was
  64. // used thus
  65. // lkrc = ht.InsertRecord(new CTest(foo, bar));
  66. }
  67. else
  68. IRTLASSERT(0);
  69. }
  70. };
  71. // For normal built in types as keys
  72. template <class _Key,class _Val>
  73. class CGenericHash
  74. : public CTypedHashTable<CGenericHash<_Key,_Val>, const CLKWrap<_Key,_Val>, _Key>
  75. {
  76. public:
  77. typedef CLKWrap<_Key,_Val> ValueType;
  78. CGenericHash(LPCSTR name) :
  79. CTypedHashTable<CGenericHash, const ValueType, _Key>(name)
  80. {}
  81. CGenericHash(LPCSTR name, double maxload, DWORD initsize, DWORD num_subtbls) :
  82. CTypedHashTable<CGenericHash, const ValueType, _Key>(name,maxload,initsize,num_subtbls)
  83. {}
  84. static _Key ExtractKey(const CLKWrap<_Key,_Val> *pEntry)
  85. { return pEntry->m_k; }
  86. static DWORD CalcKeyHash(_Key psKey)
  87. { return Hash(psKey); }
  88. static bool EqualKeys(_Key x, _Key y)
  89. { return (x==y); }
  90. static void AddRefRecord(const CLKWrap<_Key,_Val>* pTest, int nIncr)
  91. {
  92. IRTLTRACE(_TEXT("AddRef(%p, %s) %d, cRefs == %d\n"),
  93. pTest, pTest->m_k, nIncr, pTest->m_cRefs);
  94. if (nIncr == +1)
  95. {
  96. // or, perhaps, pIFoo->AddRef() (watch out for marshalling)
  97. // or ++pTest->m_cRefs (single-threaded only)
  98. InterlockedIncrement(&pTest->m_cRefs);
  99. }
  100. else if (nIncr == -1)
  101. {
  102. // or, perhaps, pIFoo->Release() or --pTest->m_cRefs;
  103. LONG l = InterlockedDecrement(&pTest->m_cRefs);
  104. // For some hashtables, it may also make sense to add the following
  105. if (l == 0) delete pTest;
  106. // but that would typically only apply when InsertRecord was
  107. // used thus
  108. // lkrc = ht.InsertRecord(new CTest(foo, bar));
  109. }
  110. else
  111. IRTLASSERT(0);
  112. }
  113. };
  114. #include <map>
  115. using namespace std;
  116. class RawBstrLT
  117. {
  118. public:
  119. bool operator()(const BSTR& x, const BSTR& y) const
  120. {
  121. return (_wcsicmp(x,y) < 0);
  122. }
  123. };
  124. #endif