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