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.

109 lines
3.6 KiB

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