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.

116 lines
4.3 KiB

  1. #ifndef __MAP_KV_H__
  2. #define __MAP_KV_H__
  3. #include <memapi.hxx>
  4. /////////////////////////////////////////////////////////////////////////////
  5. // class CMapKeyToValue - a mapping from 'KEY's to 'VALUE's, passed in as
  6. // pv/cb pairs. The keys can be variable length, although we optmizize the
  7. // case when they are all the same.
  8. //
  9. /////////////////////////////////////////////////////////////////////////////
  10. STDAPI_(UINT) MKVDefaultHashKey(LPVOID pKey, UINT cbKey);
  11. #ifdef WIN32
  12. DECLARE_HANDLE(HMAPKEY);
  13. #else
  14. DECLARE_HANDLE32(HMAPKEY);
  15. #endif
  16. typedef UINT (STDAPICALLTYPE FAR* LPFNHASHKEY)(LPVOID, UINT);
  17. class FAR CMapKeyToValue : public CPrivAlloc
  18. {
  19. public:
  20. CMapKeyToValue(UINT cbValue, UINT cbKey = 0,
  21. int nBlockSize=10,
  22. LPFNHASHKEY lpfnHashKey = NULL,
  23. UINT nHashSize = 17);
  24. ~CMapKeyToValue();
  25. // number of elements
  26. int GetCount() const { return m_nCount; }
  27. BOOL IsEmpty() const { return m_nCount == 0; }
  28. // Lookup; return FALSE if not found
  29. BOOL Lookup(LPVOID pKey, UINT cbKey, LPVOID pValue) const;
  30. BOOL LookupHKey(HMAPKEY hKey, LPVOID pValue) const;
  31. BOOL LookupAdd(LPVOID pKey, UINT cbKey, LPVOID pValue) const;
  32. // add a new (key, value) pair; return FALSE if out of memory
  33. BOOL SetAt(LPVOID pKey, UINT cbKey, LPVOID pValue);
  34. BOOL SetAtHKey(HMAPKEY hKey, LPVOID pValue);
  35. // removing existing (key, ?) pair; return FALSE if no such key
  36. BOOL RemoveKey(LPVOID pKey, UINT cbKey);
  37. BOOL RemoveHKey(HMAPKEY hKey);
  38. void RemoveAll();
  39. // iterating all (key, value) pairs
  40. POSITION GetStartPosition() const
  41. { return (m_nCount == 0) ? (POSITION)NULL : BEFORE_START_POSITION; }
  42. void GetNextAssoc(POSITION FAR* pNextPosition, LPVOID pKey,
  43. UINT FAR* pcbKey, LPVOID pValue) const;
  44. // return HMAPKEY for given key; returns NULL if not currently in map
  45. HMAPKEY GetHKey(LPVOID pKey, UINT cbKey) const;
  46. void AssertValid() const;
  47. private:
  48. // abstracts, somewhat, variable and fixed sized keys; size is really
  49. // m_cbKeyInAssoc.
  50. union CKeyWrap
  51. {
  52. BYTE rgbKey[sizeof(LPVOID) + sizeof(UINT)];
  53. struct
  54. {
  55. LPVOID pKey;
  56. UINT cbKey;
  57. };
  58. };
  59. // Association of one key and one value; NOTE: even though in general
  60. // the size of the key and value varies, for any given map,
  61. // the size of an assoc is fixed.
  62. struct CAssoc
  63. {
  64. CAssoc FAR* pNext;
  65. UINT nHashValue; // needed for efficient iteration
  66. CKeyWrap key; // size is really m_cbKeyInAssoc
  67. // BYTE rgbValue[m_cbValue];
  68. };
  69. UINT SizeAssoc() const
  70. { return sizeof(CAssoc)-sizeof(CKeyWrap) + m_cbKeyInAssoc + m_cbValue; }
  71. CAssoc FAR* NewAssoc(UINT hash, LPVOID pKey, UINT cbKey, LPVOID pValue);
  72. void FreeAssoc(CAssoc FAR* pAssoc);
  73. BOOL CompareAssocKey(CAssoc FAR* pAssoc, LPVOID pKey, UINT cbKey) const;
  74. CAssoc FAR* GetAssocAt(LPVOID pKey, UINT cbKey, UINT FAR& nHash) const;
  75. BOOL SetAssocKey(CAssoc FAR* pAssoc, LPVOID pKey, UINT cbKey) const;
  76. void GetAssocKeyPtr(CAssoc FAR* pAssoc, LPVOID FAR* ppKey,UINT FAR* pcbKey) const;
  77. void FreeAssocKey(CAssoc FAR* pAssoc) const;
  78. void GetAssocValuePtr(CAssoc FAR* pAssoc, LPVOID FAR* ppValue) const;
  79. void GetAssocValue(CAssoc FAR* pAssoc, LPVOID pValue) const;
  80. void SetAssocValue(CAssoc FAR* pAssoc, LPVOID pValue) const;
  81. BOOL InitHashTable();
  82. UINT m_cbValue;
  83. UINT m_cbKey; // variable length if 0
  84. UINT m_cbKeyInAssoc; // always non-zero
  85. CAssoc FAR* FAR* m_pHashTable;
  86. UINT m_nHashTableSize;
  87. LPFNHASHKEY m_lpfnHashKey;
  88. int m_nCount;
  89. CAssoc FAR* m_pFreeList;
  90. struct CPlex FAR* m_pBlocks;
  91. int m_nBlockSize;
  92. };
  93. #endif // !__MAP_KV_H__