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