1 |
– |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Table lookup routines |
6 |
|
*/ |
7 |
|
|
8 |
|
#include <stdio.h> |
9 |
+ |
#include <stdlib.h> |
10 |
+ |
#include <string.h> |
11 |
+ |
|
12 |
|
#include "lookup.h" |
13 |
|
|
14 |
< |
#define MEM_PTR char * |
14 |
> |
extern int |
15 |
> |
lu_strcmp( |
16 |
> |
const void *s1, |
17 |
> |
const void *s2 |
18 |
> |
) |
19 |
> |
{ |
20 |
> |
return strcmp((const char*)s1,(const char*)s2); |
21 |
> |
} |
22 |
|
|
23 |
< |
extern MEM_PTR calloc(); |
24 |
< |
|
25 |
< |
|
26 |
< |
int |
27 |
< |
lu_init(tbl, nel) /* initialize tbl for at least nel elements */ |
21 |
< |
register LUTAB *tbl; |
22 |
< |
int nel; |
23 |
> |
extern int |
24 |
> |
lu_init( /* initialize tbl for at least nel elements */ |
25 |
> |
register LUTAB *tbl, |
26 |
> |
int nel |
27 |
> |
) |
28 |
|
{ |
29 |
|
static int hsiztab[] = { |
30 |
|
31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381, |
47 |
|
} |
48 |
|
|
49 |
|
|
50 |
< |
long |
51 |
< |
lu_shash(s) /* hash a nul-terminated string */ |
52 |
< |
char *s; |
50 |
> |
extern unsigned long |
51 |
> |
lu_shash( /* hash a nul-terminated string */ |
52 |
> |
const void *s |
53 |
> |
) |
54 |
|
{ |
55 |
|
static unsigned char shuffle[256] = { |
56 |
|
0, 157, 58, 215, 116, 17, 174, 75, 232, 133, 34, |
79 |
|
41, 198, 99 |
80 |
|
}; |
81 |
|
register int i = 0; |
82 |
< |
register long h = 0; |
83 |
< |
register unsigned char *t = (unsigned char *)s; |
82 |
> |
register unsigned long h = 0; |
83 |
> |
register unsigned const char *t = (unsigned const char *)s; |
84 |
|
|
85 |
|
while (*t) |
86 |
< |
h ^= (long)shuffle[*t++] << ((i+=11) & 0xf); |
86 |
> |
h ^= (unsigned long)shuffle[*t++] << ((i+=11) & 0xf); |
87 |
|
|
88 |
|
return(h); |
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
< |
LUENT * |
93 |
< |
lu_find(tbl, key) /* find a table entry */ |
94 |
< |
register LUTAB *tbl; |
95 |
< |
char *key; |
92 |
> |
extern LUENT * |
93 |
> |
lu_find( /* find a table entry */ |
94 |
> |
register LUTAB *tbl, |
95 |
> |
const char *key |
96 |
> |
) |
97 |
|
{ |
98 |
< |
long hval; |
98 |
> |
unsigned long hval; |
99 |
|
int i, n; |
100 |
|
register int ndx; |
101 |
|
register LUENT *le; |
102 |
|
/* look up object */ |
103 |
< |
if (tbl->tsiz == 0) |
104 |
< |
lu_init(tbl, 1); |
103 |
> |
if (tbl->tsiz == 0 && !lu_init(tbl, 1)) |
104 |
> |
return(NULL); |
105 |
|
hval = (*tbl->hashf)(key); |
106 |
|
tryagain: |
107 |
|
ndx = hval % tbl->tsiz; |
101 |
– |
le = &tbl->tabl[ndx]; |
108 |
|
for (i = 0, n = 1; i < tbl->tsiz; i++, n += 2) { |
109 |
+ |
le = &tbl->tabl[ndx]; |
110 |
|
if (le->key == NULL) { |
111 |
|
le->hval = hval; |
112 |
|
return(le); |
115 |
|
(tbl->keycmp == NULL || !(*tbl->keycmp)(le->key, key))) { |
116 |
|
return(le); |
117 |
|
} |
118 |
< |
le += n; |
112 |
< |
if ((ndx += n) >= tbl->tsiz) { /* this happens rarely */ |
118 |
> |
if ((ndx += n) >= tbl->tsiz) /* this happens rarely */ |
119 |
|
ndx = ndx % tbl->tsiz; |
114 |
– |
le = &tbl->tabl[ndx]; |
115 |
– |
} |
120 |
|
} |
121 |
|
/* table is full, reallocate */ |
122 |
|
le = tbl->tabl; |
128 |
|
tbl->ndel = i; |
129 |
|
return(NULL); |
130 |
|
} |
127 |
– |
if (!ndx) |
128 |
– |
goto tryagain; |
131 |
|
/* |
132 |
|
* The following code may fail if the user has reclaimed many |
133 |
|
* deleted entries and the system runs out of memory in a |
134 |
|
* recursive call to lu_find(). |
135 |
|
*/ |
136 |
|
while (ndx--) |
137 |
< |
if (le[ndx].key != NULL) |
137 |
> |
if (le[ndx].key != NULL) { |
138 |
|
if (le[ndx].data != NULL) |
139 |
< |
*lu_find(tbl, le[ndx].key) = le[ndx]; |
139 |
> |
*lu_find(tbl,le[ndx].key) = le[ndx]; |
140 |
|
else if (tbl->freek != NULL) |
141 |
|
(*tbl->freek)(le[ndx].key); |
142 |
< |
free((MEM_PTR)le); |
142 |
> |
} |
143 |
> |
free((void *)le); |
144 |
|
goto tryagain; /* should happen only once! */ |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
< |
void |
149 |
< |
lu_delete(tbl, key) /* delete a table entry */ |
150 |
< |
register LUTAB *tbl; |
151 |
< |
char *key; |
148 |
> |
extern void |
149 |
> |
lu_delete( /* delete a table entry */ |
150 |
> |
register LUTAB *tbl, |
151 |
> |
const char *key |
152 |
> |
) |
153 |
|
{ |
154 |
|
register LUENT *le; |
155 |
|
|
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
< |
void |
168 |
< |
lu_done(tbl) /* free table and contents */ |
169 |
< |
register LUTAB *tbl; |
167 |
> |
extern int |
168 |
> |
lu_doall( /* loop through all valid table entries */ |
169 |
> |
register const LUTAB *tbl, |
170 |
> |
/* int (*f)(const LUENT *) */ |
171 |
> |
lut_doallf_t *f, |
172 |
> |
void *p |
173 |
> |
) |
174 |
|
{ |
175 |
+ |
int rval = 0; |
176 |
+ |
register const LUENT *tp; |
177 |
+ |
|
178 |
+ |
for (tp = tbl->tabl + tbl->tsiz; tp-- > tbl->tabl; ) |
179 |
+ |
if (tp->data != NULL) { |
180 |
+ |
if (f != NULL) { |
181 |
+ |
int r = (*f)(tp, p); |
182 |
+ |
if (r < 0) |
183 |
+ |
return(-1); |
184 |
+ |
rval += r; |
185 |
+ |
} else |
186 |
+ |
rval++; |
187 |
+ |
} |
188 |
+ |
return(rval); |
189 |
+ |
} |
190 |
+ |
|
191 |
+ |
|
192 |
+ |
extern void |
193 |
+ |
lu_done( /* free table and contents */ |
194 |
+ |
register LUTAB *tbl |
195 |
+ |
) |
196 |
+ |
{ |
197 |
|
register LUENT *tp; |
198 |
|
|
199 |
|
if (!tbl->tsiz) |
205 |
|
if (tp->data != NULL && tbl->freed != NULL) |
206 |
|
(*tbl->freed)(tp->data); |
207 |
|
} |
208 |
< |
free((MEM_PTR)tbl->tabl); |
208 |
> |
free((void *)tbl->tabl); |
209 |
|
tbl->tabl = NULL; |
210 |
|
tbl->tsiz = 0; |
211 |
|
tbl->ndel = 0; |