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