1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* Routines for tracking object modifiers |
6 |
* |
7 |
* External symbols declared in object.h |
8 |
*/ |
9 |
|
10 |
#include "copyright.h" |
11 |
|
12 |
#include "standard.h" |
13 |
|
14 |
#include "object.h" |
15 |
|
16 |
#include "otypes.h" |
17 |
|
18 |
|
19 |
static struct ohtab { |
20 |
int hsiz; /* current table size */ |
21 |
OBJECT *htab; /* table, if allocated */ |
22 |
} modtab = {100, NULL}, objtab = {1000, NULL}; /* modifiers and objects */ |
23 |
|
24 |
static int otndx(); |
25 |
|
26 |
|
27 |
int |
28 |
objndx(op) /* get object number from pointer */ |
29 |
register OBJREC *op; |
30 |
{ |
31 |
register int i, j; |
32 |
|
33 |
for (i = nobjects>>OBJBLKSHFT; i >= 0; i--) { |
34 |
j = op - objblock[i]; |
35 |
if (j >= 0 && j < OBJBLKSIZ) |
36 |
return((i<<OBJBLKSHFT) + j); |
37 |
} |
38 |
return(OVOID); |
39 |
} |
40 |
|
41 |
|
42 |
int |
43 |
lastmod(obj, mname) /* find modifier definition before obj */ |
44 |
OBJECT obj; |
45 |
char *mname; |
46 |
{ |
47 |
register OBJREC *op; |
48 |
register int i; |
49 |
|
50 |
i = modifier(mname); /* try hash table first */ |
51 |
if (i < obj) |
52 |
return(i); |
53 |
for (i = obj; i-- > 0; ) { /* need to search */ |
54 |
op = objptr(i); |
55 |
if (ismodifier(op->otype) && !strcmp(op->oname, mname)) |
56 |
return(i); |
57 |
} |
58 |
return(OVOID); |
59 |
} |
60 |
|
61 |
|
62 |
int |
63 |
modifier(mname) /* get a modifier number from its name */ |
64 |
char *mname; |
65 |
{ |
66 |
register int ndx; |
67 |
|
68 |
ndx = otndx(mname, &modtab); |
69 |
return(modtab.htab[ndx]); |
70 |
} |
71 |
|
72 |
|
73 |
#ifdef GETOBJ |
74 |
int |
75 |
object(oname) /* get an object number from its name */ |
76 |
char *oname; |
77 |
{ |
78 |
register int ndx; |
79 |
|
80 |
ndx = otndx(oname, &objtab); |
81 |
return(objtab.htab[ndx]); |
82 |
} |
83 |
#endif |
84 |
|
85 |
|
86 |
void |
87 |
insertobject(obj) /* insert new object into our list */ |
88 |
register OBJECT obj; |
89 |
{ |
90 |
register int i; |
91 |
|
92 |
if (ismodifier(objptr(obj)->otype)) { |
93 |
i = otndx(objptr(obj)->oname, &modtab); |
94 |
modtab.htab[i] = obj; |
95 |
} |
96 |
#ifdef GETOBJ |
97 |
else { |
98 |
i = otndx(objptr(obj)->oname, &objtab); |
99 |
objtab.htab[i] = obj; |
100 |
} |
101 |
#endif |
102 |
for (i = 0; addobjnotify[i] != NULL; i++) |
103 |
(*addobjnotify[i])(obj); |
104 |
} |
105 |
|
106 |
|
107 |
void |
108 |
clearobjndx() /* clear object hash tables */ |
109 |
{ |
110 |
if (modtab.htab != NULL) { |
111 |
free((void *)modtab.htab); |
112 |
modtab.htab = NULL; |
113 |
modtab.hsiz = 100; |
114 |
} |
115 |
if (objtab.htab != NULL) { |
116 |
free((void *)objtab.htab); |
117 |
objtab.htab = NULL; |
118 |
objtab.hsiz = 100; |
119 |
} |
120 |
} |
121 |
|
122 |
|
123 |
static int |
124 |
nexthsiz(oldsiz) /* return next hash table size */ |
125 |
int oldsiz; |
126 |
{ |
127 |
static int hsiztab[] = { |
128 |
251, 509, 1021, 2039, 4093, 8191, 16381, 0 |
129 |
}; |
130 |
register int *hsp; |
131 |
|
132 |
for (hsp = hsiztab; *hsp; hsp++) |
133 |
if (*hsp > oldsiz) |
134 |
return(*hsp); |
135 |
return(oldsiz*2 + 1); /* not always prime */ |
136 |
} |
137 |
|
138 |
|
139 |
static int |
140 |
otndx(name, tab) /* get object table index for name */ |
141 |
char *name; |
142 |
register struct ohtab *tab; |
143 |
{ |
144 |
OBJECT *oldhtab; |
145 |
int hval, i; |
146 |
register int ndx; |
147 |
|
148 |
if (tab->htab == NULL) { /* new table */ |
149 |
tab->hsiz = nexthsiz(tab->hsiz); |
150 |
tab->htab = (OBJECT *)malloc(tab->hsiz*sizeof(OBJECT)); |
151 |
if (tab->htab == NULL) |
152 |
error(SYSTEM, "out of memory in otndx"); |
153 |
ndx = tab->hsiz; |
154 |
while (ndx--) /* empty it */ |
155 |
tab->htab[ndx] = OVOID; |
156 |
} |
157 |
/* look up object */ |
158 |
hval = shash(name); |
159 |
tryagain: |
160 |
for (i = 0; i < tab->hsiz; i++) { |
161 |
ndx = (hval + i*i) % tab->hsiz; |
162 |
if (tab->htab[ndx] == OVOID || |
163 |
!strcmp(objptr(tab->htab[ndx])->oname, name)) |
164 |
return(ndx); |
165 |
} |
166 |
/* table is full, reallocate */ |
167 |
oldhtab = tab->htab; |
168 |
ndx = tab->hsiz; |
169 |
tab->htab = NULL; |
170 |
while (ndx--) |
171 |
if (oldhtab[ndx] != OVOID) { |
172 |
i = otndx(objptr(oldhtab[ndx])->oname, tab); |
173 |
tab->htab[i] = oldhtab[ndx]; |
174 |
} |
175 |
free((void *)oldhtab); |
176 |
goto tryagain; /* should happen only once! */ |
177 |
} |