2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
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 $ */
29 static char sccsid[] = "@(#)ismngfcb.c 1.4 89/07/17 Copyr 1988 Sun Micro";
32 * Copyright (c) 1988 by Sun Microsystems, Inc.
39 * Manager of open FCB blocks.
41 * This module keeps track of usage of the FCB blocks and finds a victim
43 * It also provides associative access to the FCB by their isfhandles.
46 #include "isam_impl.h"
49 #define FCBHASHSIZE 101 /* Should be a prime for best hash */
51 #if (MAXFCB_UNIXFD > FCBHASHSIZE)
53 * Cause a syntax error. FCBHASHSIZE must be increased to be > MAXFCB_UNIXFD.
54 * A good estimate is a prime approximately equal (2 * MAXFCB_UNIXFD).
56 MUST INCREASE FCBHASHSIZE
63 } hashtable [FCBHASHSIZE];
64 #define unused(entry) ((entry).fcb == NULL)
66 static int _hashisfhandle();
68 static mrused_last = 0; /* stamp generator */
72 * _mngfcb_insert(fcb, isfhandle)
74 * Insert new FCB entry.
78 _mngfcb_insert(Fcb *fcb, Bytearray *isfhandle)
80 int hashval = _hashisfhandle(isfhandle);
84 /* Try to find an unused entry in the hash table. */
86 for (ntries = 0; ntries < FCBHASHSIZE; ntries++) {
87 if (unused(hashtable[ind]))
89 if (++ind == FCBHASHSIZE)
90 ind = 0; /* Wrap the table */
93 if (ntries == FCBHASHSIZE) {
94 _isfatal_error("FCB hash table overflow");
98 * Create an entry at the index ind.
99 * Duplicate the file handle and mark the entry with the current stamp.
101 hashtable[ind].isfhandle = _bytearr_dup(isfhandle);
102 hashtable[ind].fcb = fcb;
103 hashtable[ind].mrused = mrused_last++;
108 * fcb = _mngfcb_find(isfhandle)
110 * Return a pointer to the FCB, or NULL if the FCB is not found.
111 * If the FCB is found, it is "touched" for the LRU algorithm purpose.
115 _mngfcb_find(Bytearray *isfhandle)
117 int hashval = _hashisfhandle(isfhandle);
121 /* Find the entry. */
123 for (ntries = 0; ntries < FCBHASHSIZE; ntries++) {
124 if (_bytearr_cmp(&hashtable[ind].isfhandle, isfhandle) == 0)
126 if (++ind == FCBHASHSIZE)
127 ind = 0; /* Wrap the table */
130 if (ntries == FCBHASHSIZE) {
131 return (NULL); /* Not found */
136 * Mark the entry with the current stamp.
138 hashtable[ind].mrused = mrused_last++;
139 return hashtable[ind].fcb;
144 * _mngfcb_delete(isfname)
150 _mngfcb_delete(Bytearray *isfhandle)
152 int hashval = _hashisfhandle(isfhandle);
158 for (ntries = 0; ntries < FCBHASHSIZE; ntries++) {
159 if (_bytearr_cmp(&hashtable[ind].isfhandle, isfhandle) == 0)
161 if (++ind == FCBHASHSIZE)
162 ind = 0; /* Wrap the table */
165 if (ntries == FCBHASHSIZE) {
166 _isfatal_error("_mngfcb_delete cannot find entry");
173 _bytearr_free(&hashtable[ind].isfhandle);
174 memset ((char *) &hashtable[ind], 0, sizeof(hashtable[ind]));
180 * isfhandle = _mngfcb_victim()
189 long victim_time = 0; /* Assign to shut up lint */
192 for (i = 0; i < FCBHASHSIZE; i++) {
194 if (unused(hashtable[i])) /* Skip empty slots in table */
197 if (victim_ind == -1 || victim_time > hashtable[i].mrused) {
199 victim_time = hashtable[i].mrused;
202 return ((victim_ind == -1) ? NULL : &hashtable[victim_ind].isfhandle);
207 * _hashisfhandle(isfhandle)
209 * Hash isfhandle into an integer.
213 _hashisfhandle(Bytearray *isfhandle)
216 register unsigned h, g;
219 len = isfhandle->length;
224 h = (h << 4) + (*p++);
225 if (g = h&0xf0000000) {
230 return (h % FCBHASHSIZE);