1 |
greg |
2.2 |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
greg |
1.1 |
|
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 |
greg |
1.2 |
#include "otypes.h" |
16 |
|
|
|
17 |
greg |
1.1 |
|
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 |
greg |
1.3 |
static int otndx(); |
26 |
|
|
|
27 |
greg |
1.1 |
|
28 |
|
|
int |
29 |
greg |
2.3 |
objndx(op) /* get object number from pointer */ |
30 |
|
|
register OBJREC *op; |
31 |
greg |
1.1 |
{ |
32 |
greg |
2.3 |
register int i, j; |
33 |
greg |
1.1 |
|
34 |
greg |
2.3 |
for (i = nobjects>>6; i >= 0; i--) { |
35 |
|
|
j = op - objblock[i]; |
36 |
|
|
if (j >= 0 && j < 077) |
37 |
|
|
return((i<<6) + j); |
38 |
|
|
} |
39 |
|
|
return(OVOID); |
40 |
greg |
1.1 |
} |
41 |
|
|
|
42 |
|
|
|
43 |
|
|
int |
44 |
greg |
2.3 |
lastmod(obj, mname) /* find modifier definition before obj */ |
45 |
|
|
OBJECT obj; |
46 |
|
|
char *mname; |
47 |
|
|
{ |
48 |
|
|
register OBJREC *op; |
49 |
|
|
register int i; |
50 |
|
|
|
51 |
greg |
2.4 |
i = modifier(mname); /* try hash table first */ |
52 |
|
|
if (i < obj) |
53 |
|
|
return(i); |
54 |
|
|
for (i = obj; i-- > 0; ) { /* need to search */ |
55 |
greg |
2.3 |
op = objptr(i); |
56 |
|
|
if (ismodifier(op->otype) && !strcmp(op->oname, mname)) |
57 |
|
|
return(i); |
58 |
|
|
} |
59 |
|
|
return(OVOID); |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
|
63 |
|
|
int |
64 |
greg |
1.1 |
modifier(mname) /* get a modifier number from its name */ |
65 |
|
|
char *mname; |
66 |
|
|
{ |
67 |
|
|
register int ndx; |
68 |
|
|
|
69 |
|
|
ndx = otndx(mname, &modtab); |
70 |
|
|
return(modtab.htab[ndx]); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
|
74 |
greg |
2.3 |
#ifdef GETOBJ |
75 |
|
|
int |
76 |
|
|
object(oname) /* get an object number from its name */ |
77 |
|
|
char *oname; |
78 |
|
|
{ |
79 |
|
|
register int ndx; |
80 |
|
|
|
81 |
|
|
ndx = otndx(oname, &objtab); |
82 |
|
|
return(objtab.htab[ndx]); |
83 |
|
|
} |
84 |
|
|
#endif |
85 |
|
|
|
86 |
|
|
|
87 |
greg |
1.1 |
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 |
greg |
2.3 |
#ifdef GETOBJ |
97 |
|
|
else { |
98 |
|
|
i = otndx(objptr(obj)->oname, &objtab); |
99 |
|
|
objtab.htab[i] = obj; |
100 |
|
|
} |
101 |
|
|
#endif |
102 |
greg |
1.1 |
for (i = 0; addobjnotify[i] != NULL; i++) |
103 |
|
|
(*addobjnotify[i])(obj); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
|
107 |
|
|
static int |
108 |
|
|
nexthsiz(oldsiz) /* return next hash table size */ |
109 |
|
|
int oldsiz; |
110 |
|
|
{ |
111 |
|
|
static int hsiztab[] = { |
112 |
|
|
251, 509, 1021, 2039, 4093, 8191, 16381, 0 |
113 |
|
|
}; |
114 |
|
|
register int *hsp; |
115 |
|
|
|
116 |
|
|
for (hsp = hsiztab; *hsp; hsp++) |
117 |
|
|
if (*hsp > oldsiz) |
118 |
|
|
return(*hsp); |
119 |
|
|
return(oldsiz*2 + 1); /* not always prime */ |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
|
123 |
|
|
static int |
124 |
|
|
otndx(name, tab) /* get object table index for name */ |
125 |
|
|
char *name; |
126 |
|
|
register struct ohtab *tab; |
127 |
|
|
{ |
128 |
|
|
OBJECT *oldhtab; |
129 |
|
|
int hval, i; |
130 |
|
|
register int ndx; |
131 |
|
|
|
132 |
|
|
if (tab->htab == NULL) { /* new table */ |
133 |
|
|
tab->hsiz = nexthsiz(tab->hsiz); |
134 |
|
|
tab->htab = (OBJECT *)malloc(tab->hsiz*sizeof(OBJECT)); |
135 |
|
|
if (tab->htab == NULL) |
136 |
|
|
error(SYSTEM, "out of memory in otndx"); |
137 |
|
|
ndx = tab->hsiz; |
138 |
|
|
while (ndx--) /* empty it */ |
139 |
|
|
tab->htab[ndx] = OVOID; |
140 |
|
|
} |
141 |
|
|
/* look up object */ |
142 |
|
|
hval = shash(name); |
143 |
|
|
tryagain: |
144 |
|
|
for (i = 0; i < tab->hsiz; i++) { |
145 |
|
|
ndx = (hval + i*i) % tab->hsiz; |
146 |
|
|
if (tab->htab[ndx] == OVOID || |
147 |
|
|
!strcmp(objptr(tab->htab[ndx])->oname, name)) |
148 |
|
|
return(ndx); |
149 |
|
|
} |
150 |
|
|
/* table is full, reallocate */ |
151 |
|
|
oldhtab = tab->htab; |
152 |
|
|
ndx = tab->hsiz; |
153 |
|
|
tab->htab = NULL; |
154 |
|
|
while (ndx--) |
155 |
|
|
if (oldhtab[ndx] != OVOID) { |
156 |
|
|
i = otndx(objptr(oldhtab[ndx])->oname, tab); |
157 |
|
|
tab->htab[i] = oldhtab[ndx]; |
158 |
|
|
} |
159 |
|
|
free((char *)oldhtab); |
160 |
|
|
goto tryagain; /* should happen only once! */ |
161 |
|
|
} |