-
Notifications
You must be signed in to change notification settings - Fork 20
/
symtab.c
160 lines (142 loc) · 3.42 KB
/
symtab.c
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
/* symtab.c hashtable file for vasm */
/* (c) in 2002-2004,2008,2011,2014 by Volker Barthelmann and Frank Wille */
#include "vasm.h"
hashtable *new_hashtable(size_t size)
{
hashtable *new = mymalloc(sizeof(*new));
#ifdef LOWMEM
/* minimal hash tables */
if (size > 0x100)
size = 0x100;
#endif
new->size = size;
new->collisions = 0;
new->entries = mycalloc(size*sizeof(*new->entries));
return new;
}
size_t hashcode(const char *name)
{
size_t h = 5381;
int c;
while (c = (unsigned char)*name++)
h = ((h << 5) + h) + c;
return h;
}
size_t hashcodelen(const char *name,int len)
{
size_t h = 5381;
while (len--)
h = ((h << 5) + h) + (unsigned char)*name++;
return h;
}
size_t hashcode_nc(const char *name)
{
size_t h = 5381;
int c;
while (c = (unsigned char)*name++)
h = ((h << 5) + h) + tolower(c);
return h;
}
size_t hashcodelen_nc(const char *name,int len)
{
size_t h = 5381;
while (len--)
h = ((h << 5) + h) + tolower((unsigned char)*name++);
return h;
}
/* add to hashtable; name must be unique */
void add_hashentry(hashtable *ht,const char *name,hashdata data)
{
size_t i=nocase?(hashcode_nc(name)%ht->size):(hashcode(name)%ht->size);
hashentry *new=mymalloc(sizeof(*new));
new->name=name;
new->data=data;
if(debug){
if(ht->entries[i])
ht->collisions++;
}
new->next=ht->entries[i];
ht->entries[i]=new;
}
/* remove from hashtable; name must be unique */
void rem_hashentry(hashtable *ht,const char *name,int no_case)
{
size_t i=no_case?(hashcode_nc(name)%ht->size):(hashcode(name)%ht->size);
hashentry *p,*last;
for(p=ht->entries[i],last=NULL;p;p=p->next){
if(!strcmp(name,p->name)||(no_case&&!stricmp(name,p->name))){
if(last==NULL)
ht->entries[i]=p->next;
else
last->next=p->next;
myfree(p);
return;
}
last=p;
}
ierror(0);
}
/* finds unique entry in hashtable */
int find_name(hashtable *ht,const char *name,hashdata *result)
{
if(nocase)
return find_name_nc(ht,name,result);
else{
size_t i=hashcode(name)%ht->size;
hashentry *p;
for(p=ht->entries[i];p;p=p->next){
if(!strcmp(name,p->name)){
*result=p->data;
return 1;
}else
ht->collisions++;
}
}
return 0;
}
/* same as above, but uses len instead of zero-terminated string */
int find_namelen(hashtable *ht,const char *name,int len,hashdata *result)
{
if(nocase)
return find_namelen_nc(ht,name,len,result);
else{
size_t i=hashcodelen(name,len)%ht->size;
hashentry *p;
for(p=ht->entries[i];p;p=p->next){
if(!strncmp(name,p->name,len)&&p->name[len]==0){
*result=p->data;
return 1;
}else
ht->collisions++;
}
}
return 0;
}
/* finds unique entry in hashtable - case insensitive */
int find_name_nc(hashtable *ht,const char *name,hashdata *result)
{
size_t i=hashcode_nc(name)%ht->size;
hashentry *p;
for(p=ht->entries[i];p;p=p->next){
if(!stricmp(name,p->name)){
*result=p->data;
return 1;
}else
ht->collisions++;
}
return 0;
}
/* same as above, but uses len instead of zero-terminated string */
int find_namelen_nc(hashtable *ht,const char *name,int len,hashdata *result)
{
size_t i=hashcodelen_nc(name,len)%ht->size;
hashentry *p;
for(p=ht->entries[i];p;p=p->next){
if(!strnicmp(name,p->name,len)&&p->name[len]==0){
*result=p->data;
return 1;
}else
ht->collisions++;
}
return 0;
}