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 librararies 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, isfhandle)
82 int hashval = _hashisfhandle(isfhandle);
86 /* Try to find an unused entry in the hash table. */
88 for (ntries = 0; ntries < FCBHASHSIZE; ntries++) {
89 if (unused(hashtable[ind]))
91 if (++ind == FCBHASHSIZE)
92 ind = 0; /* Wrap the table */
95 if (ntries == FCBHASHSIZE) {
96 _isfatal_error("FCB hash table overflow");
100 * Create an entry at the index ind.
101 * Duplicate the file handle and mark the entry with the current stamp.
103 hashtable[ind].isfhandle = _bytearr_dup(isfhandle);
104 hashtable[ind].fcb = fcb;
105 hashtable[ind].mrused = mrused_last++;
110 * fcb = _mngfcb_find(isfhandle)
112 * Return a pointer to the FCB, or NULL if the FCB is not found.
113 * If the FCB is found, it is "touched" for the LRU algorithm purpose.
117 _mngfcb_find(isfhandle)
118 Bytearray *isfhandle;
120 int hashval = _hashisfhandle(isfhandle);
124 /* Find the entry. */
126 for (ntries = 0; ntries < FCBHASHSIZE; ntries++) {
127 if (_bytearr_cmp(&hashtable[ind].isfhandle, isfhandle) == 0)
129 if (++ind == FCBHASHSIZE)
130 ind = 0; /* Wrap the table */
133 if (ntries == FCBHASHSIZE) {
134 return (NULL); /* Not found */
139 * Mark the entry with the current stamp.
141 hashtable[ind].mrused = mrused_last++;
142 return hashtable[ind].fcb;
147 * _mngfcb_delete(isfname)
153 _mngfcb_delete(isfhandle)
154 Bytearray *isfhandle;
156 int hashval = _hashisfhandle(isfhandle);
162 for (ntries = 0; ntries < FCBHASHSIZE; ntries++) {
163 if (_bytearr_cmp(&hashtable[ind].isfhandle, isfhandle) == 0)
165 if (++ind == FCBHASHSIZE)
166 ind = 0; /* Wrap the table */
169 if (ntries == FCBHASHSIZE) {
170 _isfatal_error("_mngfcb_delete cannot find entry");
177 _bytearr_free(&hashtable[ind].isfhandle);
178 memset ((char *) &hashtable[ind], 0, sizeof(hashtable[ind]));
184 * isfhandle = _mngfcb_victim()
193 long victim_time = 0; /* Assign to shut up lint */
196 for (i = 0; i < FCBHASHSIZE; i++) {
198 if (unused(hashtable[i])) /* Skip empty slots in table */
201 if (victim_ind == -1 || victim_time > hashtable[i].mrused) {
203 victim_time = hashtable[i].mrused;
206 return ((victim_ind == -1) ? NULL : &hashtable[victim_ind].isfhandle);
211 * _hashisfhandle(isfhandle)
213 * Hash isfhandle into an integer.
217 _hashisfhandle(isfhandle)
218 Bytearray *isfhandle;
221 register unsigned h, g;
224 len = isfhandle->length;
229 h = (h << 4) + (*p++);
230 if (g = h&0xf0000000) {
235 return (h % FCBHASHSIZE);