1 /*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
2 /*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
3 /*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
4 /*%% (c) Copyright 1993, 1994 Novell, Inc. */
5 /*%% $XConsortium: ismngfcb.c /main/3 1995/10/23 11:42:28 rswiston $ */
7 static char sccsid[] = "@(#)ismngfcb.c 1.4 89/07/17 Copyr 1988 Sun Micro";
10 * Copyright (c) 1988 by Sun Microsystems, Inc.
17 * Manager of open FCB blocks.
19 * This module keeps track of usage of the FCB blocks and finds a victim
21 * It also provides associative access to the FCB by their isfhandles.
24 #include "isam_impl.h"
27 #define FCBHASHSIZE 101 /* Should be a prime for best hash */
29 #if (MAXFCB_UNIXFD > FCBHASHSIZE)
31 * Cause a syntax error. FCBHASHSIZE must be increased to be > MAXFCB_UNIXFD.
32 * A good estimate is a prime approximately equal (2 * MAXFCB_UNIXFD).
34 MUST INCREASE FCBHASHSIZE
41 } hashtable [FCBHASHSIZE];
42 #define unused(entry) ((entry).fcb == NULL)
44 static int _hashisfhandle();
46 static mrused_last = 0; /* stamp generator */
50 * _mngfcb_insert(fcb, isfhandle)
52 * Insert new FCB entry.
56 _mngfcb_insert(fcb, isfhandle)
60 int hashval = _hashisfhandle(isfhandle);
64 /* Try to find an unused entry in the hash table. */
66 for (ntries = 0; ntries < FCBHASHSIZE; ntries++) {
67 if (unused(hashtable[ind]))
69 if (++ind == FCBHASHSIZE)
70 ind = 0; /* Wrap the table */
73 if (ntries == FCBHASHSIZE) {
74 _isfatal_error("FCB hash table overflow");
78 * Create an entry at the index ind.
79 * Duplicate the file handle and mark the entry with the current stamp.
81 hashtable[ind].isfhandle = _bytearr_dup(isfhandle);
82 hashtable[ind].fcb = fcb;
83 hashtable[ind].mrused = mrused_last++;
88 * fcb = _mngfcb_find(isfhandle)
90 * Return a pointer to the FCB, or NULL if the FCB is not found.
91 * If the FCB is found, it is "touched" for the LRU algorithm purpose.
95 _mngfcb_find(isfhandle)
98 int hashval = _hashisfhandle(isfhandle);
102 /* Find the entry. */
104 for (ntries = 0; ntries < FCBHASHSIZE; ntries++) {
105 if (_bytearr_cmp(&hashtable[ind].isfhandle, isfhandle) == 0)
107 if (++ind == FCBHASHSIZE)
108 ind = 0; /* Wrap the table */
111 if (ntries == FCBHASHSIZE) {
112 return (NULL); /* Not found */
117 * Mark the entry with the current stamp.
119 hashtable[ind].mrused = mrused_last++;
120 return hashtable[ind].fcb;
125 * _mngfcb_delete(isfname)
131 _mngfcb_delete(isfhandle)
132 Bytearray *isfhandle;
134 int hashval = _hashisfhandle(isfhandle);
140 for (ntries = 0; ntries < FCBHASHSIZE; ntries++) {
141 if (_bytearr_cmp(&hashtable[ind].isfhandle, isfhandle) == 0)
143 if (++ind == FCBHASHSIZE)
144 ind = 0; /* Wrap the table */
147 if (ntries == FCBHASHSIZE) {
148 _isfatal_error("_mngfcb_delete cannot find entry");
155 _bytearr_free(&hashtable[ind].isfhandle);
156 memset ((char *) &hashtable[ind], 0, sizeof(hashtable[ind]));
162 * isfhandle = _mngfcb_victim()
171 long victim_time = 0; /* Assign to shut up lint */
174 for (i = 0; i < FCBHASHSIZE; i++) {
176 if (unused(hashtable[i])) /* Skip empty slots in table */
179 if (victim_ind == -1 || victim_time > hashtable[i].mrused) {
181 victim_time = hashtable[i].mrused;
184 return ((victim_ind == -1) ? NULL : &hashtable[victim_ind].isfhandle);
189 * _hashisfhandle(isfhandle)
191 * Hash isfhandle into an integer.
195 _hashisfhandle(isfhandle)
196 Bytearray *isfhandle;
199 register unsigned h, g;
202 len = isfhandle->length;
207 h = (h << 4) + (*p++);
208 if (g = h&0xf0000000) {
213 return (h % FCBHASHSIZE);