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.

136 lines
2.5 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. BList.cxx
  5. Abstract:
  6. Implements out of line methods on blists.
  7. Author:
  8. Mario Goertzel [MarioGo]
  9. Revision History:
  10. MarioGo 95-03-02 Bits 'n pieces
  11. MarioGo 95-09-07 Was blist.inl, change from template to generic class for PPC.
  12. --*/
  13. #include<or.hxx>
  14. ULONG
  15. CBList::Hash(PVOID p)
  16. {
  17. ULONG t = PtrToUlong(p);
  18. return ( ((t << 9) ^ (t >> 5) ^ (t >> 15)) % _ulmaxData);
  19. }
  20. ORSTATUS
  21. CBList::Insert(PVOID p)
  22. {
  23. if ( _data == 0
  24. || _ulcElements > (_ulmaxData - (_ulmaxData/8 + 1)))
  25. {
  26. // Table getting full, grow it.
  27. // See Kenuth on linear probe hash performance as the table fills.
  28. ULONG i, ulmaxOldData = _ulmaxData;
  29. PVOID *ppOldData = _data;
  30. _data = new PVOID[_ulmaxData * 2];
  31. if (0 == _data)
  32. {
  33. _data = ppOldData;
  34. return(OR_NOMEM);
  35. }
  36. _ulmaxData *= 2;
  37. _ulcElements = 0;
  38. OrMemorySet(_data, 0, _ulmaxData*sizeof(PVOID));
  39. if (ppOldData)
  40. {
  41. for(i = 0; i < ulmaxOldData; i++)
  42. {
  43. if (ppOldData[i])
  44. {
  45. ORSTATUS status = Insert(ppOldData[i]);
  46. ASSERT(status == OR_OK);
  47. }
  48. }
  49. delete ppOldData;
  50. }
  51. }
  52. register ULONG i = Hash(p);
  53. while(_data[i])
  54. {
  55. i = (i + 1) % _ulmaxData;
  56. }
  57. _data[i] = p;
  58. _ulcElements++;
  59. return(OR_OK);
  60. }
  61. PVOID
  62. CBList::Remove(PVOID p)
  63. {
  64. register ULONG i, hash;
  65. if (_data)
  66. {
  67. i = hash = Hash(p);
  68. if (_data[i] != p)
  69. {
  70. do
  71. {
  72. i = (i + 1) % _ulmaxData;
  73. }
  74. while(_data[i] != p && i != hash);
  75. }
  76. if (_data[i] == p)
  77. {
  78. _data[i] = 0;
  79. _ulcElements--;
  80. return(p);
  81. }
  82. ASSERT(i == hash);
  83. }
  84. return(0);
  85. }
  86. BOOL
  87. CBList::Member(PVOID p)
  88. {
  89. int i, hash;
  90. if (0 == _data)
  91. return(FALSE);
  92. i = hash = Hash(p);
  93. if (_data[i] == p)
  94. return(TRUE);
  95. do
  96. {
  97. i = (i + 1) % _ulmaxData;
  98. }
  99. while(_data[i] != p && i != hash);
  100. return(_data[i] == p);
  101. }