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.

257 lines
5.2 KiB

  1. //
  2. // Copyright (C) 2000, Microsoft Corporation
  3. //
  4. // File: shash.h
  5. //
  6. // Contents: Generic hashtable
  7. // Classes:
  8. //
  9. // History: April. 9 2001, Author: Rohanp
  10. //
  11. //-----------------------------------------------------------------------------
  12. #ifndef __SHASHTABLE_H__
  13. #define __SHASHTABLE_H__
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. //forward declaration
  18. struct _SHASH_TABLE;
  19. struct _SHASH_HEADER;
  20. // This function computes the has value
  21. //
  22. typedef ULONG (*PFNHASH)( void* pvKey ) ;
  23. //
  24. // This function compares the keys
  25. //
  26. typedef int (*PFNMATCHKEY)( void* key1, void* key2 ) ;
  27. //
  28. // This function gets called when the keys get removed
  29. //
  30. typedef int (*PFNREMOVEKEY)(struct _SHASH_HEADER * pHeader) ;
  31. //
  32. // This function gets called when the keys get removed
  33. //
  34. typedef DWORD (*PFNENUMERATEKEY)(struct _SHASH_HEADER * pHeader, void * pContext ) ;
  35. // memory allocations function
  36. typedef void* (*PFNALLOC)( ULONG cb ) ;
  37. // memory free function
  38. typedef void (*PFNFREE)( void* lpv ) ;
  39. //
  40. // This function allocs memory the tables lock
  41. //
  42. typedef void * (*PFNALLOCLOCK)( void ) ;
  43. //
  44. // This function releases the lock memory
  45. //
  46. typedef void (*PFNFREELOCK)( void * ) ;
  47. typedef BOOLEAN (*PFNAQUIREWRITELOCK)( struct _SHASH_TABLE * pTable ) ;
  48. typedef BOOLEAN (*PFNAQUIREREADLOCK)( struct _SHASH_TABLE * pTable ) ;
  49. typedef BOOLEAN (*PFNRELEASEWRITELOCK)( struct _SHASH_TABLE * pTable ) ;
  50. typedef BOOLEAN (*PFNRELEASEREADLOCK)( struct _SHASH_TABLE * pTable ) ;
  51. #define SHASH_DEFAULT_HASH_SIZE 625
  52. //
  53. // There are flags that are not used by shash, they can be used by
  54. // the caller.
  55. //
  56. #define SHASH_USER_FLAGS 0xFFFF0000
  57. typedef struct _SHASH_HEADER
  58. {
  59. ULONG Signature;
  60. ULONG Flags;
  61. PVOID pvKey;
  62. PVOID pData;
  63. LONG RefCount;
  64. ULONG dwHash;
  65. FILETIME Time;
  66. LIST_ENTRY ListEntry;
  67. }SHASH_HEADER, *PSHASH_HEADER;
  68. #define SHASH_FLAG_DELETE_PENDING 0x00000001
  69. typedef struct _SHASHFUNCTABLE
  70. {
  71. PFNHASH HashFunc;
  72. PFNMATCHKEY CompareFunc;
  73. PFNALLOC AllocFunc;
  74. PFNFREE FreeFunc;
  75. PFNALLOCLOCK AllocLockFunc;
  76. PFNFREELOCK FreeLockFunc;
  77. PFNAQUIREWRITELOCK WriteLockFunc;
  78. PFNAQUIREREADLOCK ReadLockFunc;
  79. PFNRELEASEWRITELOCK ReleaseWriteLockFunc;
  80. PFNRELEASEREADLOCK ReleaseReadLockFunc;
  81. ULONG Flags;
  82. ULONG NumBuckets;
  83. } SHASH_FUNCTABLE, *PSHASH_FUNCTABLE;
  84. typedef struct _SHASH_BUCKET
  85. {
  86. ULONG Count;
  87. LIST_ENTRY ListHead;
  88. } SHASH_BUCKET, *PHASH_SBUCKET;
  89. typedef struct _SHASH_ENTRY
  90. {
  91. PVOID pvKey;
  92. LONG RefCount;
  93. ULONG Flags;
  94. PVOID pData;
  95. LARGE_INTEGER EntryTime;
  96. LARGE_INTEGER ExpireTime;
  97. LIST_ENTRY ListEntry;
  98. } SHASH_ENTRY, *PSHASH_ENTRY;
  99. typedef struct _SHASH_TABLE
  100. {
  101. PFNHASH HashFunc;
  102. PFNMATCHKEY CompareFunc;
  103. PFNALLOC AllocFunc;
  104. PFNFREE FreeFunc;
  105. PFNALLOCLOCK AllocLockFunc;
  106. PFNFREELOCK FreeLockFunc;
  107. PFNAQUIREWRITELOCK WriteLockFunc;
  108. PFNAQUIREREADLOCK ReadLockFunc;
  109. PFNRELEASEWRITELOCK ReleaseWriteLockFunc;
  110. PFNRELEASEREADLOCK ReleaseReadLockFunc;
  111. LONG Version;
  112. ULONG Flags;
  113. ULONG NumBuckets;
  114. ULONG TotalItems;
  115. PVOID pLock;
  116. SHASH_BUCKET HashBuckets[0];
  117. } SHASH_TABLE, *PSHASH_TABLE;
  118. #define SHASH_CAP_POWER_OF2 0x000001
  119. #define SHASH_CAP_TABLE_LOCKED 0x000002
  120. #define SHASH_CAP_NOSEARCH_INSERT 0x000004
  121. #define SHASH_DEFAULT_HASHTIMEOUT (15 * 60) //timeout entries in 15 minutes
  122. #define SHASH_REPLACE_IFFOUND 1
  123. #ifdef KERNEL_MODE
  124. #define SHASH_GET_TIME(Time) KeQuerySystemTime(Time)
  125. #else
  126. #define SHASH_GET_TIME(Time) NtQuerySystemTime(Time)
  127. #endif
  128. typedef struct _SHASH_ITERATOR {
  129. //
  130. // index
  131. //
  132. ULONG index;
  133. LIST_ENTRY* pListHead;
  134. //
  135. //
  136. LIST_ENTRY* ple;
  137. PSHASH_HEADER pEntry;
  138. } SHASH_ITERATOR,*PSHASH_ITERATOR;
  139. NTSTATUS
  140. ShashInitHashTable(
  141. PSHASH_TABLE *ppHashTable,
  142. PSHASH_FUNCTABLE pFuncTable);
  143. void
  144. ShashTerminateHashTable(
  145. PSHASH_TABLE pHashTable
  146. );
  147. NTSTATUS
  148. SHashInsertKey(IN PSHASH_TABLE pTable,
  149. IN void * pData,
  150. IN void * pvKeyIn,
  151. IN DWORD InsertFlag
  152. );
  153. NTSTATUS
  154. SHashRemoveKey( IN PSHASH_TABLE pTable,
  155. IN void * pvKeyIn,
  156. IN PFNREMOVEKEY pRemoveFunc
  157. );
  158. PSHASH_HEADER
  159. SHashLookupKeyEx( IN PSHASH_TABLE pTable,
  160. IN void* pvKeyIn
  161. );
  162. NTSTATUS
  163. SHashIsKeyInTable( IN PSHASH_TABLE pTable,
  164. IN void* pvKeyIn
  165. );
  166. NTSTATUS
  167. SHashGetDataFromTable( IN PSHASH_TABLE pTable,
  168. IN void* pvKeyIn,
  169. IN void ** ppData
  170. );
  171. NTSTATUS
  172. ShashEnumerateItems(IN PSHASH_TABLE pTable,
  173. IN PFNENUMERATEKEY pfnCallback,
  174. IN LPVOID lpvClientContext
  175. );
  176. NTSTATUS
  177. SHashReleaseReference( IN PSHASH_TABLE pTable,
  178. IN PSHASH_HEADER pData
  179. );
  180. PSHASH_HEADER
  181. SHashStartEnumerate(
  182. IN PSHASH_ITERATOR pIterator,
  183. IN PSHASH_TABLE pTable
  184. );
  185. PSHASH_HEADER
  186. SHashNextEnumerate(
  187. IN PSHASH_ITERATOR pIterator,
  188. IN PSHASH_TABLE pTable);
  189. VOID
  190. SHashFinishEnumerate(
  191. IN PSHASH_ITERATOR pIterator,
  192. IN PSHASH_TABLE pTable
  193. );
  194. #ifdef __cplusplus
  195. }
  196. #endif
  197. #endif