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.

223 lines
5.7 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. hashtable.cpp
  5. Abstract:
  6. This file contains the class definitions for hashtables
  7. Author:
  8. Vishnu Patankar (VishnuP) 7-April-2000
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. --*/
  13. ///////////////////////////////////////////////////////////////////////////////
  14. // //
  15. // Includes //
  16. // //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #include "hashtable.h"
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // Constructor
  21. //
  22. // in: dwNumBuckets - the hashtable size
  23. // out:
  24. // return value:
  25. //
  26. // description: creates the hashtable
  27. // if unable to get memory, bInitialized is set to FALSE
  28. ///////////////////////////////////////////////////////////////////////////////
  29. ScepHashTable::ScepHashTable(DWORD dwNumBuckets){
  30. if (aTable = (PSCE_PRECEDENCE_NAME_LIST*)ScepAlloc(LMEM_ZEROINIT,
  31. dwNumBuckets * sizeof(PSCE_PRECEDENCE_NAME_LIST))){
  32. NumBuckets = dwNumBuckets;
  33. bInitialized = TRUE;
  34. }
  35. else
  36. bInitialized = FALSE;
  37. }
  38. ///////////////////////////////////////////////////////////////////////////////
  39. // Destructor
  40. //
  41. // in:
  42. // out:
  43. // return value:
  44. //
  45. // description: frees memory associated with the hashtable
  46. ///////////////////////////////////////////////////////////////////////////////
  47. ScepHashTable::~ScepHashTable(){
  48. if (bInitialized) {
  49. for (DWORD BucketNo = 0 ; BucketNo < NumBuckets; BucketNo++)
  50. if (aTable[BucketNo])
  51. ScepFreeNameStatusList(aTable[BucketNo]);
  52. ScepFree(aTable);
  53. bInitialized = FALSE;
  54. }
  55. }
  56. ///////////////////////////////////////////////////////////////////////////////
  57. // Lookup() method
  58. //
  59. // in: pName - key to search for in the hashtable
  60. // out:
  61. // return value: pointer to hashtable node if found or NULL if none
  62. //
  63. // description: searches the bucket that pName hashed into
  64. // if found, returns the node, else returns NULL
  65. ///////////////////////////////////////////////////////////////////////////////
  66. PSCE_PRECEDENCE_NAME_LIST
  67. ScepHashTable::Lookup(
  68. PWSTR pName
  69. )
  70. {
  71. PSCE_PRECEDENCE_NAME_LIST pNameList;
  72. for (pNameList = aTable[ScepGenericHash(pName)]; pNameList != NULL; pNameList = pNameList->Next)
  73. if (_wcsicmp(pName, pNameList->Name) == 0)
  74. return pNameList;
  75. return NULL;
  76. }
  77. ///////////////////////////////////////////////////////////////////////////////
  78. // LookupAdd() method
  79. //
  80. // in: pName - key to search for in the hashtable
  81. // out: ppSettingPrecedence - pointer to pointer to the precedence of key
  82. // return value: if out of resources error status, otherwise success
  83. //
  84. // description: if pName found, it returns a pointer to its precedence by reference
  85. // else, it attempts to create a node and copy the name
  86. // else it returns error out of resources
  87. ///////////////////////////////////////////////////////////////////////////////
  88. DWORD
  89. ScepHashTable::LookupAdd(
  90. PWSTR pName,
  91. DWORD **ppSettingPrecedence
  92. )
  93. {
  94. DWORD rc = NO_ERROR;
  95. if (bInitialized) {
  96. if (pName && ppSettingPrecedence && *ppSettingPrecedence == NULL) {
  97. PSCE_PRECEDENCE_NAME_LIST pNameList = Lookup(pName);
  98. if (pNameList == NULL) {
  99. if (NO_ERROR == (rc = ScepAddToNameStatusList(
  100. &(aTable[ScepGenericHash(pName)]),
  101. pName,
  102. wcslen(pName),
  103. 0)))
  104. *ppSettingPrecedence = &(aTable[ScepGenericHash(pName)]->Status);
  105. }
  106. else {
  107. *ppSettingPrecedence = &(pNameList->Status);
  108. }
  109. } else {
  110. rc = ERROR_INVALID_PARAMETER;
  111. }
  112. }
  113. else {
  114. rc = ERROR_NOT_ENOUGH_MEMORY;
  115. }
  116. return rc;
  117. }
  118. ///////////////////////////////////////////////////////////////////////////////
  119. // ScepGenericHash() method
  120. //
  121. // in: pwszName - key to hash
  122. // out:
  123. // return value: hashvalue
  124. //
  125. // description: calculates hash value for the name (to be made virtual)
  126. //
  127. ///////////////////////////////////////////////////////////////////////////////
  128. DWORD
  129. ScepHashTable::ScepGenericHash(
  130. PWSTR pwszName
  131. )
  132. {
  133. DWORD hashval = 0;
  134. for (; *pwszName != L'\0'; pwszName++)
  135. hashval = towlower(*pwszName) + 47 * hashval;
  136. return hashval % NumBuckets;
  137. }
  138. #ifdef _DEBUG
  139. void
  140. ScepHashTable::ScepDumpTable()
  141. {
  142. if (bInitialized) {
  143. for (DWORD BucketNo = 0 ; BucketNo < NumBuckets; BucketNo++) {
  144. PSCE_PRECEDENCE_NAME_LIST pNameList, pNode;
  145. for (pNameList = aTable[BucketNo]; pNameList != NULL;) {
  146. pNode = pNameList;
  147. pNameList = pNameList->Next;
  148. wprintf(L"\nBucket: %i, Name: %s, Precedence %i", BucketNo, pNode->Name, pNode->Status);
  149. }
  150. }
  151. }
  152. }
  153. #endif