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.

309 lines
9.0 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: scmhash.hxx
  7. //
  8. // Contents: Class definitions used for SCM hash table.
  9. //
  10. // History: 20-Jan-93 Ricksa Created from map_kv.h
  11. // 04-Oct-00 JohnDoty Changed memory allocation to use
  12. // PrivAlloc
  13. //
  14. // Notes: The reason for creating this file rather than using the
  15. // original class is that the SCM has different memory allocation
  16. // needs depending on whether it is built for Win95 or NT.
  17. //
  18. // JohnDoty:
  19. // Of course, that's not strictly true, since we aren't
  20. // ever built for Win9x anymore...
  21. //
  22. //--------------------------------------------------------------------------
  23. #ifndef __SCMHASH_HXX__
  24. #define __SCMHASH_HXX__
  25. // Forward declaration
  26. class CScmHashIter;
  27. //+-------------------------------------------------------------------------
  28. //
  29. // Class: CScmHashEntry (she)
  30. //
  31. // Purpose: Base of hash table entries
  32. //
  33. // Interface: IsEqual - tells whether entry is equal to input key
  34. // GetNext - next next entry after this one
  35. // SetNext - set the next pointer for this etnry
  36. //
  37. // History: 20-Jan-95 Ricksa Created
  38. //
  39. // Notes:
  40. //
  41. //--------------------------------------------------------------------------
  42. class CScmHashEntry : public CPrivAlloc
  43. {
  44. public:
  45. CScmHashEntry(void);
  46. virtual ~CScmHashEntry(void);
  47. virtual BOOL IsEqual(LPVOID pKey, UINT cbKey) = 0;
  48. CScmHashEntry * GetNext(void);
  49. void SetNext(CScmHashEntry *);
  50. private:
  51. // Points to next hash entry if there is one
  52. // for the hash bucket.
  53. CScmHashEntry * _sheNext;
  54. };
  55. //+-------------------------------------------------------------------------
  56. //
  57. // Member: CScmHashEntry::CScmHashEntry
  58. //
  59. // Synopsis: Initalize base of hash entry
  60. //
  61. // History: 20-Jan-95 Ricksa Created
  62. //
  63. //--------------------------------------------------------------------------
  64. inline CScmHashEntry::CScmHashEntry(void) : _sheNext(NULL)
  65. {
  66. // Header does all the work
  67. }
  68. //+-------------------------------------------------------------------------
  69. //
  70. // Member: CScmHashEntry::GetNext
  71. //
  72. // Synopsis: Get next entry in the collision list
  73. //
  74. // History: 20-Jan-95 Ricksa Created
  75. //
  76. //--------------------------------------------------------------------------
  77. inline CScmHashEntry *CScmHashEntry::GetNext(void)
  78. {
  79. return _sheNext;
  80. }
  81. //+-------------------------------------------------------------------------
  82. //
  83. // Member: CScmHashEntry::SetNext
  84. //
  85. // Synopsis: Set the next entry in the collision list.
  86. //
  87. // History: 20-Jan-95 Ricksa Created
  88. //
  89. //--------------------------------------------------------------------------
  90. inline void CScmHashEntry::SetNext(CScmHashEntry *sheNew)
  91. {
  92. _sheNext = sheNew;
  93. }
  94. //+-------------------------------------------------------------------------
  95. //
  96. // Class: CScmHashTable (sht)
  97. //
  98. // Purpose: Hash table class
  99. //
  100. // Interface: InitOK - whether initialization succeeded
  101. // GetCount - Get count of items in the list
  102. // IsBucketEmpty - get whether collision list is empty
  103. // GetBucketList - get collision list for entry
  104. // Lookup - lookup an entry in the hash table
  105. // SetAt - add entry to hash table
  106. // RemoveEntry - remove entry from the hash table
  107. //
  108. // History: 20-Jan-95 Ricksa Created
  109. //
  110. // Notes:
  111. //
  112. //--------------------------------------------------------------------------
  113. class CScmHashTable : public CPrivAlloc
  114. {
  115. public:
  116. CScmHashTable(DWORD nHashSize = 17);
  117. ~CScmHashTable();
  118. // Tell whether object got correctly initialized
  119. BOOL InitOK(void) const;
  120. // Get count of entries in the table.
  121. DWORD GetCount(void) const;
  122. // Reports whether a particular buck is empty
  123. BOOL IsBucketEmpty(DWORD dwHash) const;
  124. // Gets list associated with the bucket. This
  125. // is used if there is some special search of the
  126. // list required.
  127. CScmHashEntry * GetBucketList(DWORD dwHash) const;
  128. // Lookup - return pointer to entry if found
  129. CScmHashEntry * Lookup(
  130. DWORD dwHash,
  131. LPVOID pKey,
  132. UINT cbKey) const;
  133. // Lookup based on entry pointer
  134. CScmHashEntry * Lookup(
  135. DWORD dwHash,
  136. LPVOID pvEntry) const;
  137. // Add new entry
  138. void SetAt(
  139. DWORD dwHash,
  140. CScmHashEntry *psheToAdd);
  141. // removing existing entry
  142. BOOL RemoveEntry(
  143. DWORD dwHash,
  144. CScmHashEntry *psheToRemove);
  145. private:
  146. friend CScmHashIter;
  147. CScmHashEntry ** _apsheHashTable;
  148. DWORD _ndwHashTableSize;
  149. DWORD _ndwCount;
  150. };
  151. //+-------------------------------------------------------------------------
  152. //
  153. // Member: CScmHashTable::CScmHashTable
  154. //
  155. // Synopsis: Create an empty hash table
  156. //
  157. // History: 20-Jan-95 Ricksa Created
  158. //
  159. //--------------------------------------------------------------------------
  160. inline CScmHashTable::CScmHashTable(DWORD nHashSize)
  161. : _ndwHashTableSize(nHashSize), _ndwCount(0)
  162. {
  163. DWORD dwSize = nHashSize * sizeof(CScmHashEntry *);
  164. _apsheHashTable = (CScmHashEntry **) PrivMemAlloc(dwSize);
  165. if (_apsheHashTable != NULL)
  166. {
  167. memset(_apsheHashTable, 0, dwSize);
  168. }
  169. }
  170. //+-------------------------------------------------------------------------
  171. //
  172. // Member: CScmHashTable::InitOK
  173. //
  174. // Synopsis: Determine whether the constructor succeeded
  175. //
  176. // History: 20-Jan-95 Ricksa Created
  177. //
  178. //--------------------------------------------------------------------------
  179. inline BOOL CScmHashTable::InitOK(void) const
  180. {
  181. return _apsheHashTable != NULL;
  182. }
  183. //+-------------------------------------------------------------------------
  184. //
  185. // Member: CScmHashTable::GetCount
  186. //
  187. // Synopsis: Get the number of entries in the table
  188. //
  189. // History: 20-Jan-95 Ricksa Created
  190. //
  191. //--------------------------------------------------------------------------
  192. inline DWORD CScmHashTable::GetCount(void) const
  193. {
  194. return _ndwCount;
  195. }
  196. //+-------------------------------------------------------------------------
  197. //
  198. // Member: CScmHashTable::IsBucketEmpty
  199. //
  200. // Synopsis: Determine whether a particular hash bucket is empty
  201. //
  202. // History: 20-Jan-95 Ricksa Created
  203. //
  204. //--------------------------------------------------------------------------
  205. inline BOOL CScmHashTable::IsBucketEmpty(DWORD dwHash) const
  206. {
  207. return _apsheHashTable[dwHash] == NULL;
  208. }
  209. //+-------------------------------------------------------------------------
  210. //
  211. // Member: CScmHashTable::GetBucketList
  212. //
  213. // Synopsis: Get a list of entries associated with a particular hash
  214. //
  215. // History: 20-Jan-95 Ricksa Created
  216. //
  217. //--------------------------------------------------------------------------
  218. inline CScmHashEntry *CScmHashTable::GetBucketList(DWORD dwHash) const
  219. {
  220. return _apsheHashTable[dwHash];
  221. }
  222. //+-------------------------------------------------------------------------
  223. //
  224. // Class: CScmHashIter (shi)
  225. //
  226. // Purpose: Iterate through list of hash entries sequentially
  227. //
  228. // Interface: GetNext - get next entry in the list
  229. //
  230. // History: 20-Jan-95 Ricksa Created
  231. //
  232. // Notes:
  233. //
  234. //--------------------------------------------------------------------------
  235. class CScmHashIter
  236. {
  237. public:
  238. CScmHashIter(CScmHashTable *psht);
  239. CScmHashEntry * GetNext(void);
  240. private:
  241. CScmHashEntry * FindNextBucketWithEntry(void);
  242. CScmHashTable * _psht;
  243. DWORD _dwBucket;
  244. CScmHashEntry * _psheNext;
  245. };
  246. //+-------------------------------------------------------------------------
  247. //
  248. // Member: CScmHashIter::CScmHashIter
  249. //
  250. // Synopsis: Initialize iteration
  251. //
  252. // History: 20-Jan-95 Ricksa Created
  253. //
  254. //--------------------------------------------------------------------------
  255. inline CScmHashIter::CScmHashIter(CScmHashTable *psht)
  256. : _psht(psht), _dwBucket(0), _psheNext(NULL)
  257. {
  258. FindNextBucketWithEntry();
  259. }
  260. #endif // __SCMHASH_HXX__