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