[go: up one dir, main page]

Menu

[897c37]: / runtime / hash.c  Maximize  Restore  History

Download this file

237 lines (209 with data), 5.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
** $Id: hash.c,v 1.6 2008/12/18 04:17:02 dredd Exp $
**
** $Source: /cvsroot/swlpc/swlpc/runtime/hash.c,v $
** $Revision: 1.6 $
** $Date: 2008/12/18 04:17:02 $
** $State: Exp $
**
** Author: Mike McGaughey & Geoff Wong, 1993-1998
**
** See the file "Copying" distributed with this file.
*/
#include "proto.h"
#include "hash.h"
int nhash = 0;
/* s - string, maxn - maximum # of chars, hashs - htable size */
int hashstr(char *s, int maxn, int hashs)
{
register unsigned int h = 0;
register unsigned char *p;
/*
* What's faster - table memory access (with 1 cycle delays)
* and 2 passes through the string, or a few adds? I'm betting on the adds.
*
* The following loop is carefully juggled so that the compiler
* can fill the memory load delay slot with a multiply (multiply
* is 1 cycle on mips, apparently) and doesn't need a temp for the
* pointer increment (hence it's a pre-increment; saves 1 load and
* brings the loop pipeline below 8 instructions). Mips compiler (-O2)
* isn't very clever about detecting this - hence the obtuse code.
* Also: Removing the tests on maxn makes the inner loop 30% faster
* (from 7 to 5 instructions), so it's out. Note that the hash
* value computed is a multiple of 3 - rectified in the return statement.
* NB: gcc chooses to use a shift and add instead of *3, but produces
* worse code for the rest of the loop!
*/
#if 1
p = ((unsigned char *) s) - 1;
do
{
h = h * 3 + (*++p);
}
while (*p);
#else
while (*s)
{
h = h * 3 + (*s);
s++;
}
#endif
return (h / 3) % hashs; /* nb: h is unsigned, so this is positive */
#if 0
for (h = 0, i = 0, p = (unsigned char *) s; *p && i < maxn; i++, p++)
h = T[h ^ *p];
if (hashs > 256 && *s)
{
int oh = h;
for (i = 1, p = (unsigned char *) s, h = (*p++ + 1) & 0xff; *p && i < maxn; i++, p++)
h = T[h ^ *p];
h += (oh << 8);
}
return h % hashs; /* nb: h is unsigned, so this is positive */
#endif
}
/*
* Internal Hash table Stuff
*
* * quadratic probing.
* * written by Geoff Wong for Shattered World.
* (NEED: should double size of table when 90% full)
* (NEED: should be malloced etc. - just get it working first!)
*/
static int hash_entries = 0;
static int hash_table_size = IHSIZE;
int iHash(char *s, char *p, int tsize)
{
int h = 0, countx = 120;
while (*s && --countx)
h = (h * P1 + *(s++) * P2 + P3) % tsize;
while (*p && --countx)
h = (h * P1 + *(s++) * P2 + P3) % tsize;
return (h);
}
i_element *IHtable;
static int first = 0;
void init_Htable()
{
int i;
IHtable = (i_element *) malloc(hash_table_size * sizeof(i_element));
for (i = 0; i < IHSIZE; i++)
{
IHtable[i].used = 0;
IHtable[i].deleted = 0;
}
first = 1;
}
void free_Htable()
{
free(IHtable);
}
int remove_Hfunction(Shared * func_name, Shared * obj)
{
int gap = 1;
int h = hashstr(func_name->str, 0, (IHSIZE + 1) / 2)
+ hashstr(obj->str, 0, (IHSIZE + 1) / 2);
int n;
#if 0
fprintf(stderr, "removing %s in %s.\n",
func_name, obj);
#endif
while (1)
{
if (IHtable[h].used && !(IHtable[h].deleted) &&
(IHtable[h].obj == obj) &&
(func_name == ((IHtable[h]).program)->name))
{
n = (h + gap) % IHSIZE;
IHtable[h].deleted = IHtable[n].used;
IHtable[h].used = IHtable[n].used;
hash_entries--;
return 1;
}
if (IHtable[h].used == 0 && IHtable[h].deleted == 0)
{
return 0;
}
h = (h + gap) % IHSIZE;
gap++;
}
return 0;
}
int add_Hfunction(Func * pr, Shared * obj, short offset)
{
int gap = 1;
int h = hashstr(pr->name->str, 0, (IHSIZE + 1) / 2)
+ hashstr(obj->str, 0, (IHSIZE + 1) / 2);
if (!first) init_Htable();
/* only 1 named function from each object! */
if (Hfunction(pr->name, obj))
{
#if 0
fprintf(stderr, "Function %s in %s already exists.\n",
pr->name, obj);
#endif
return 0;
}
#if 0
fprintf(stderr, "Adding function %s in %s.\n",
pr->name, obj);
#endif
while ((IHtable[h]).used == 1 && ((IHtable[h]).deleted == 0))
{
h = (h + gap) % IHSIZE;
gap++;
}
(IHtable[h]).used = 1;
(IHtable[h]).deleted = 0;
(IHtable[h]).obj = obj;
(IHtable[h]).program = pr;
(IHtable[h]).offset = offset;
#ifdef PROFILE
(IHtable[h]).called = 0;
#endif
hash_entries++;
return 1;
}
int prog_offset_hack = 0;
/* func_name NOT shared! */
Func * Hfunction(Shared * func_name, Shared * obj)
{
int gap = 1;
int h = hashstr(func_name->str, 0, (IHSIZE + 1) / 2)
+ hashstr(obj->str, 0, (IHSIZE + 1) / 2);
if (!first) init_Htable();
while (1)
{
if ( (IHtable[h]).used && !((IHtable[h]).deleted) &&
((IHtable[h]).obj == obj) &&
(func_name == ((IHtable[h]).program)->name) )
{
#if 0
fprintf(stderr, "Found function %s in %s.\n",
func_name, obj);
#endif
prog_offset_hack = (IHtable[h]).offset;
#ifdef PROFILE
(IHtable[h]).called++;
#endif
return (IHtable[h]).program;
}
if ((IHtable[h]).used == 0 && (IHtable[h]).deleted == 0)
{
#if 0
fprintf(stderr, "Function NOT FOUND.\n");
#endif
prog_offset_hack = 0;
return 0;
}
h = (h + gap) % IHSIZE;
gap++;
}
return NULL;
}
int show_ihash_table_status()
{
add_message("IH size = %d, IH entries = %d\n", hash_table_size,
hash_entries);
return (hash_table_size * sizeof(i_element));
}