hash.c
3.5 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libaudio.h>
#include "midiDmon.h"
#include "midiApp.h"
#include "midiGlobals.h"
#ifdef SYSEX_IMPL
#define M1 71
#define HASH_MIN_SIZE 0x1000
typedef struct {
u32 oldOffset;
u32 newOffset;
} ptrPair;
ALSymObj **gHashTable;
int gHashMask;
/* only valid when compacting the mirror */
u32 gNumHashMask;
ptrPair *gNumHashTable;
int AllocNameHash(int numObjs)
{
int hashTblSize;
if(gHashTable)
free(gHashTable);
hashTblSize = NameHashSize(numObjs);
gHashTable = (ALSymObj**)calloc(hashTblSize,sizeof(ALSymObj *));
if (!gHashTable)
{
fprintf(stderr, "%s: couldn't allocate memory for hash table\n",gAppName);
return(MEMORY_NOT_OK);
}
return(MEMORY_OK);
}
/* This returns an integer hash value for a string. */
int HashName(char *str)
{
char c;
int h = 0;
while ((c = *str++) != '\0')
h = (h * M1 + (int) c);
return(h);
}
int AddHashName(char *str, ALSymObj *obj)
{
int h = HashName(str);
while (1)
{
h &= gHashMask;
if (!(int)gHashTable[h])
{
gHashTable[h] = obj;
return(h);
}
else
h++; /* try again */
}
}
ALSymObj *MatchHashName(char *name)
{
char *str;
ALSymObj *entry;
int h;
h = HashName(name);
while (1)
{
h &= gHashMask;
entry = gHashTable[h];
if (entry == NULL)
return 0;
str = (char *)&gNameBuffer[entry->SFObj.stringOffset];
if (strcmp(name, str) == 0)
return(entry);
else
h++; /* try again */
}
}
int NameHashSize(int numObjs)
{
int size = HASH_MIN_SIZE;
int hashSize =(int)( numObjs * 1.5);
while(hashSize > size)
size = size << 1;
gHashMask = size - 1;
return size;
}
int AllocNumHash(int numObj)
{
int hashSize;
hashSize = NumHashSize(numObj);
gNumHashTable = (ptrPair*)calloc(hashSize,sizeof(ptrPair));
if(!gNumHashTable)
{
fprintf(stderr,"%s: Unable to allocate mememory for number hash table\n",gAppName);
return(MEMORY_NOT_OK);
}
return(MEMORY_OK);
}
void DeallocNumHash(void)
{
free(gNumHashTable);
}
int AddNumHash(u32 oldoffset, u32 newoffset)
{
int h;
if(oldoffset==0)
oldoffset=1;
h = oldoffset;
while (1)
{
h &= gNumHashMask;
if (!(int)gNumHashTable[h].oldOffset)
{
gNumHashTable[h].oldOffset = oldoffset;
gNumHashTable[h].newOffset = newoffset;
return(h);
}
else
h++; /* try again */
}
}
u32 MatchNumHash(u32 oldOffset)
{
int h;
if(oldOffset==0)
oldOffset = 1;
h = oldOffset;
while (1)
{
h &= gNumHashMask;
if(!gNumHashTable[h].oldOffset)
{
fprintf(stderr,"%s: Didn't match hash number! %d\n",gAppName,oldOffset);
return 0;
}
if (gNumHashTable[h].oldOffset == oldOffset)
return(gNumHashTable[h].newOffset);
else
h++; /* try again */
}
}
int NumHashSize(int numObjs)
{
int size = HASH_MIN_SIZE;
int HashSize =(int)( numObjs * 2);
while(HashSize > size)
size = size << 1;
gNumHashMask = size - 1;
return size;
}
#endif /* SYSEX_IMPL */