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 "copyright.h" |
9 |
+ |
|
10 |
|
#include <stdio.h> |
11 |
+ |
#include <stdlib.h> |
12 |
+ |
#include <string.h> |
13 |
+ |
|
14 |
|
#include "lookup.h" |
15 |
|
|
16 |
< |
#define MEM_PTR char * |
16 |
> |
#ifdef NOSTRUCTASS |
17 |
> |
#define copystruct(d,s) memcpy((void *)(d),(void *)(s),sizeof(*(d))) |
18 |
> |
#else |
19 |
> |
#define copystruct(d,s) (*(d) = *(s)) |
20 |
> |
#endif |
21 |
|
|
16 |
– |
extern MEM_PTR calloc(); |
22 |
|
|
18 |
– |
|
23 |
|
int |
24 |
|
lu_init(tbl, nel) /* initialize tbl for at least nel elements */ |
25 |
|
register LUTAB *tbl; |
46 |
|
} |
47 |
|
|
48 |
|
|
49 |
< |
long |
49 |
> |
unsigned long |
50 |
|
lu_shash(s) /* hash a nul-terminated string */ |
51 |
|
char *s; |
52 |
|
{ |
77 |
|
41, 198, 99 |
78 |
|
}; |
79 |
|
register int i = 0; |
80 |
< |
register long h = 0; |
80 |
> |
register unsigned long h = 0; |
81 |
|
register unsigned char *t = (unsigned char *)s; |
82 |
|
|
83 |
|
while (*t) |
84 |
< |
h ^= (long)shuffle[*t++] << ((i+=11) & 0xf); |
84 |
> |
h ^= (unsigned long)shuffle[*t++] << ((i+=11) & 0xf); |
85 |
|
|
86 |
|
return(h); |
87 |
|
} |
92 |
|
register LUTAB *tbl; |
93 |
|
char *key; |
94 |
|
{ |
95 |
< |
long hval; |
95 |
> |
unsigned long hval; |
96 |
|
int i, n; |
97 |
|
register int ndx; |
98 |
|
register LUENT *le; |
99 |
|
/* look up object */ |
100 |
< |
if (tbl->tsiz == 0) |
101 |
< |
lu_init(tbl, 1); |
100 |
> |
if (tbl->tsiz == 0 && !lu_init(tbl, 1)) |
101 |
> |
return(NULL); |
102 |
|
hval = (*tbl->hashf)(key); |
103 |
|
tryagain: |
104 |
|
ndx = hval % tbl->tsiz; |
101 |
– |
le = &tbl->tabl[ndx]; |
105 |
|
for (i = 0, n = 1; i < tbl->tsiz; i++, n += 2) { |
106 |
+ |
le = &tbl->tabl[ndx]; |
107 |
|
if (le->key == NULL) { |
108 |
|
le->hval = hval; |
109 |
|
return(le); |
112 |
|
(tbl->keycmp == NULL || !(*tbl->keycmp)(le->key, key))) { |
113 |
|
return(le); |
114 |
|
} |
115 |
< |
le += n; |
112 |
< |
if ((ndx += n) >= tbl->tsiz) { /* this happens rarely */ |
115 |
> |
if ((ndx += n) >= tbl->tsiz) /* this happens rarely */ |
116 |
|
ndx = ndx % tbl->tsiz; |
114 |
– |
le = &tbl->tabl[ndx]; |
115 |
– |
} |
117 |
|
} |
118 |
|
/* table is full, reallocate */ |
119 |
|
le = tbl->tabl; |
125 |
|
tbl->ndel = i; |
126 |
|
return(NULL); |
127 |
|
} |
127 |
– |
if (!ndx) |
128 |
– |
goto tryagain; |
128 |
|
/* |
129 |
|
* The following code may fail if the user has reclaimed many |
130 |
|
* deleted entries and the system runs out of memory in a |
133 |
|
while (ndx--) |
134 |
|
if (le[ndx].key != NULL) |
135 |
|
if (le[ndx].data != NULL) |
136 |
< |
*lu_find(tbl, le[ndx].key) = le[ndx]; |
136 |
> |
copystruct(lu_find(tbl,le[ndx].key), &le[ndx]); |
137 |
|
else if (tbl->freek != NULL) |
138 |
|
(*tbl->freek)(le[ndx].key); |
139 |
< |
free((MEM_PTR)le); |
139 |
> |
free((void *)le); |
140 |
|
goto tryagain; /* should happen only once! */ |
141 |
|
} |
142 |
|
|
192 |
|
if (tp->data != NULL && tbl->freed != NULL) |
193 |
|
(*tbl->freed)(tp->data); |
194 |
|
} |
195 |
< |
free((MEM_PTR)tbl->tabl); |
195 |
> |
free((void *)tbl->tabl); |
196 |
|
tbl->tabl = NULL; |
197 |
|
tbl->tsiz = 0; |
198 |
|
tbl->ndel = 0; |