1 |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Routines for tracking object modifiers |
9 |
*/ |
10 |
|
11 |
#include "standard.h" |
12 |
|
13 |
#include "object.h" |
14 |
|
15 |
|
16 |
extern int (*addobjnotify[])(); /* people to notify of new objects */ |
17 |
|
18 |
static struct ohtab { |
19 |
int hsiz; /* current table size */ |
20 |
OBJECT *htab; /* table, if allocated */ |
21 |
} modtab = {100, NULL}, objtab = {1000, NULL}; /* modifiers and objects */ |
22 |
|
23 |
|
24 |
#ifdef GETOBJ |
25 |
int |
26 |
object(oname) /* get an object number from its name */ |
27 |
char *oname; |
28 |
{ |
29 |
register int ndx; |
30 |
|
31 |
ndx = otndx(oname, &objtab); |
32 |
return(objtab.htab[ndx]); |
33 |
} |
34 |
#endif |
35 |
|
36 |
|
37 |
int |
38 |
modifier(mname) /* get a modifier number from its name */ |
39 |
char *mname; |
40 |
{ |
41 |
register int ndx; |
42 |
|
43 |
ndx = otndx(mname, &modtab); |
44 |
return(modtab.htab[ndx]); |
45 |
} |
46 |
|
47 |
|
48 |
insertobject(obj) /* insert new object into our list */ |
49 |
register OBJECT obj; |
50 |
{ |
51 |
register int i; |
52 |
|
53 |
#ifdef GETOBJ |
54 |
i = otndx(objptr(obj)->oname, &objtab); |
55 |
objtab.htab[i] = obj; |
56 |
#endif |
57 |
if (ismodifier(objptr(obj)->otype)) { |
58 |
i = otndx(objptr(obj)->oname, &modtab); |
59 |
modtab.htab[i] = obj; |
60 |
} |
61 |
for (i = 0; addobjnotify[i] != NULL; i++) |
62 |
(*addobjnotify[i])(obj); |
63 |
} |
64 |
|
65 |
|
66 |
static int |
67 |
nexthsiz(oldsiz) /* return next hash table size */ |
68 |
int oldsiz; |
69 |
{ |
70 |
static int hsiztab[] = { |
71 |
251, 509, 1021, 2039, 4093, 8191, 16381, 0 |
72 |
}; |
73 |
register int *hsp; |
74 |
|
75 |
for (hsp = hsiztab; *hsp; hsp++) |
76 |
if (*hsp > oldsiz) |
77 |
return(*hsp); |
78 |
return(oldsiz*2 + 1); /* not always prime */ |
79 |
} |
80 |
|
81 |
|
82 |
static int |
83 |
shash(s) /* hash a string */ |
84 |
register char *s; |
85 |
{ |
86 |
register int h = 0; |
87 |
|
88 |
while (*s) |
89 |
h = (h<<1 & 0x7fff) ^ *s++; |
90 |
return(h); |
91 |
} |
92 |
|
93 |
|
94 |
static int |
95 |
otndx(name, tab) /* get object table index for name */ |
96 |
char *name; |
97 |
register struct ohtab *tab; |
98 |
{ |
99 |
OBJECT *oldhtab; |
100 |
int hval, i; |
101 |
register int ndx; |
102 |
|
103 |
if (tab->htab == NULL) { /* new table */ |
104 |
tab->hsiz = nexthsiz(tab->hsiz); |
105 |
tab->htab = (OBJECT *)malloc(tab->hsiz*sizeof(OBJECT)); |
106 |
if (tab->htab == NULL) |
107 |
error(SYSTEM, "out of memory in otndx"); |
108 |
ndx = tab->hsiz; |
109 |
while (ndx--) /* empty it */ |
110 |
tab->htab[ndx] = OVOID; |
111 |
} |
112 |
/* look up object */ |
113 |
hval = shash(name); |
114 |
tryagain: |
115 |
for (i = 0; i < tab->hsiz; i++) { |
116 |
ndx = (hval + i*i) % tab->hsiz; |
117 |
if (tab->htab[ndx] == OVOID || |
118 |
!strcmp(objptr(tab->htab[ndx])->oname, name)) |
119 |
return(ndx); |
120 |
} |
121 |
/* table is full, reallocate */ |
122 |
oldhtab = tab->htab; |
123 |
ndx = tab->hsiz; |
124 |
tab->htab = NULL; |
125 |
while (ndx--) |
126 |
if (oldhtab[ndx] != OVOID) { |
127 |
i = otndx(objptr(oldhtab[ndx])->oname, tab); |
128 |
tab->htab[i] = oldhtab[ndx]; |
129 |
} |
130 |
free((char *)oldhtab); |
131 |
goto tryagain; /* should happen only once! */ |
132 |
} |