ismngfcb.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
  24. /*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
  25. /*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
  26. /*%% (c) Copyright 1993, 1994 Novell, Inc. */
  27. /*%% $XConsortium: ismngfcb.c /main/3 1995/10/23 11:42:28 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * ismngfcb.c
  33. *
  34. * Description:
  35. * Manager of open FCB blocks.
  36. *
  37. * This module keeps track of usage of the FCB blocks and finds a victim
  38. * on LRU basis.
  39. * It also provides associative access to the FCB by their isfhandles.
  40. *
  41. */
  42. #include "isam_impl.h"
  43. #define FCBHASHSIZE 101 /* Should be a prime for best hash */
  44. #if (MAXFCB_UNIXFD > FCBHASHSIZE)
  45. /*
  46. * Cause a syntax error. FCBHASHSIZE must be increased to be > MAXFCB_UNIXFD.
  47. * A good estimate is a prime approximately equal (2 * MAXFCB_UNIXFD).
  48. */
  49. MUST INCREASE FCBHASHSIZE
  50. #endif
  51. struct hashtable {
  52. Bytearray isfhandle;
  53. Fcb *fcb;
  54. long mrused;
  55. } hashtable [FCBHASHSIZE];
  56. #define unused(entry) ((entry).fcb == NULL)
  57. static int _hashisfhandle();
  58. static int mrused_last = 0; /* stamp generator */
  59. /*
  60. * _mngfcb_insert(fcb, isfhandle)
  61. *
  62. * Insert new FCB entry.
  63. */
  64. void
  65. _mngfcb_insert(Fcb *fcb, Bytearray *isfhandle)
  66. {
  67. int hashval = _hashisfhandle(isfhandle);
  68. int ind;
  69. int ntries;
  70. /* Try to find an unused entry in the hash table. */
  71. ind = hashval;
  72. for (ntries = 0; ntries < FCBHASHSIZE; ntries++) {
  73. if (unused(hashtable[ind]))
  74. break;
  75. if (++ind == FCBHASHSIZE)
  76. ind = 0; /* Wrap the table */
  77. }
  78. if (ntries == FCBHASHSIZE) {
  79. _isfatal_error("FCB hash table overflow");
  80. }
  81. /*
  82. * Create an entry at the index ind.
  83. * Duplicate the file handle and mark the entry with the current stamp.
  84. */
  85. hashtable[ind].isfhandle = _bytearr_dup(isfhandle);
  86. hashtable[ind].fcb = fcb;
  87. hashtable[ind].mrused = mrused_last++;
  88. }
  89. /*
  90. * fcb = _mngfcb_find(isfhandle)
  91. *
  92. * Return a pointer to the FCB, or NULL if the FCB is not found.
  93. * If the FCB is found, it is "touched" for the LRU algorithm purpose.
  94. */
  95. Fcb *
  96. _mngfcb_find(Bytearray *isfhandle)
  97. {
  98. int hashval = _hashisfhandle(isfhandle);
  99. int ind;
  100. int ntries;
  101. /* Find the entry. */
  102. ind = hashval;
  103. for (ntries = 0; ntries < FCBHASHSIZE; ntries++) {
  104. if (_bytearr_cmp(&hashtable[ind].isfhandle, isfhandle) == 0)
  105. break;
  106. if (++ind == FCBHASHSIZE)
  107. ind = 0; /* Wrap the table */
  108. }
  109. if (ntries == FCBHASHSIZE) {
  110. return (NULL); /* Not found */
  111. }
  112. else {
  113. /*
  114. * Mark the entry with the current stamp.
  115. */
  116. hashtable[ind].mrused = mrused_last++;
  117. return hashtable[ind].fcb;
  118. }
  119. }
  120. /*
  121. * _mngfcb_delete(isfname)
  122. *
  123. * Delete an entry.
  124. */
  125. void
  126. _mngfcb_delete(Bytearray *isfhandle)
  127. {
  128. int hashval = _hashisfhandle(isfhandle);
  129. int ind;
  130. int ntries;
  131. /* Find the entry */
  132. ind = hashval;
  133. for (ntries = 0; ntries < FCBHASHSIZE; ntries++) {
  134. if (_bytearr_cmp(&hashtable[ind].isfhandle, isfhandle) == 0)
  135. break;
  136. if (++ind == FCBHASHSIZE)
  137. ind = 0; /* Wrap the table */
  138. }
  139. if (ntries == FCBHASHSIZE) {
  140. _isfatal_error("_mngfcb_delete cannot find entry");
  141. }
  142. else {
  143. /*
  144. * Clear the entry.
  145. */
  146. _bytearr_free(&hashtable[ind].isfhandle);
  147. memset ((char *) &hashtable[ind], 0, sizeof(hashtable[ind]));
  148. }
  149. }
  150. /*
  151. * isfhandle = _mngfcb_victim()
  152. *
  153. * Find LRU used FCB.
  154. */
  155. Bytearray *
  156. _mngfcb_victim(void)
  157. {
  158. int victim_ind = -1;
  159. long victim_time = 0; /* Assign to shut up lint */
  160. int i;
  161. for (i = 0; i < FCBHASHSIZE; i++) {
  162. if (unused(hashtable[i])) /* Skip empty slots in table */
  163. continue;
  164. if (victim_ind == -1 || victim_time > hashtable[i].mrused) {
  165. victim_ind = i;
  166. victim_time = hashtable[i].mrused;
  167. }
  168. }
  169. return ((victim_ind == -1) ? NULL : &hashtable[victim_ind].isfhandle);
  170. }
  171. /*
  172. * _hashisfhandle(isfhandle)
  173. *
  174. * Hash isfhandle into an integer.
  175. */
  176. Static int
  177. _hashisfhandle(Bytearray *isfhandle)
  178. {
  179. char *p;
  180. unsigned h, g;
  181. int len;
  182. len = isfhandle->length;
  183. p = isfhandle->data;
  184. h = 0;
  185. while (len-- > 0) {
  186. h = (h << 4) + (*p++);
  187. if ((g = (h & 0xf0000000))) {
  188. h = h ^ (g >> 24);
  189. h = h ^ g;
  190. }
  191. }
  192. return (h % FCBHASHSIZE);
  193. }