-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.h
159 lines (146 loc) · 10.7 KB
/
hash.h
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
#include "thread.h"
typedef struct hash_entry {
void *data;
int hash;
} HashEntry;
typedef struct hash_table {
HashEntry *hash_table;
int hash_size;
int hash_count;
VMLock lock;
} HashTable;
extern void resizeHash(HashTable *table, int new_size);
extern void lockHashTable0(HashTable *table, Thread *self);
extern void unlockHashTable0(HashTable *table, Thread *self);
#define initHashTable(table, initial_size, create_lock) \
{ \
table.hash_table = (HashEntry*)gcMemMalloc(sizeof(HashEntry)*initial_size); \
memset(table.hash_table, 0, sizeof(HashEntry)*initial_size); \
table.hash_size = initial_size; \
table.hash_count = 0; \
if(create_lock) \
initVMLock(table.lock); \
}
#define lockHashTable(table) \
lockHashTable0(&table, threadSelf());
#define unlockHashTable(table) \
unlockHashTable0(&table, threadSelf());
#define findHashEntry(table, ptr, ptr2, add_if_absent, scavenge, locked, ptr2type) \
{ \
int hash = HASH(ptr); \
int i; \
\
Thread *self; \
if(locked) { \
self = threadSelf(); \
lockHashTable0(&table, self); \
} \
\
i = hash & (table.hash_size - 1); \
\
for(;;) { \
ptr2 = (ptr2type)table.hash_table[i].data; \
if((ptr2 == NULL) || (COMPARE(ptr, ptr2, hash, table.hash_table[i].hash))) \
break; \
\
i = (i+1) & (table.hash_size - 1); \
} \
\
if(ptr2) { \
ptr2 = FOUND(ptr2); \
} else \
if(add_if_absent) { \
table.hash_table[i].hash = hash; \
table.hash_table[i].data = PREPARE(ptr); \
ptr2 = (ptr2type)PREPARE(ptr); \
\
if(ptr2) { \
table.hash_count++; \
if((table.hash_count * 4) > (table.hash_size * 3)) { \
int new_size; \
if(scavenge) { \
HashEntry *entry = table.hash_table; \
int cnt = table.hash_count; \
for(; cnt; entry++) { \
void *data = entry->data; \
if(data) { \
if(SCAVENGE(data)) { \
entry->data = NULL; \
table.hash_count--; \
} \
cnt--; \
} \
} \
if((table.hash_count * 3) > (table.hash_size * 2)) \
new_size = table.hash_size*2; \
else \
new_size = table.hash_size; \
} else \
new_size = table.hash_size*2; \
\
resizeHash(&table, new_size); \
} \
} \
} \
\
if(locked) \
unlockHashTable0(&table, self); \
}
#define deleteHashEntry(table, ptr, locked) \
{ \
int hash = HASH(ptr); \
void *ptr2; \
int i; \
\
Thread *self; \
if(locked) { \
self = threadSelf(); \
lockHashTable0(&table, self); \
} \
\
i = hash & (table.hash_size - 1); \
\
for(;;) { \
ptr2 = table.hash_table[i].data; \
if((ptr2 == NULL) || (COMPARE(ptr, ptr2, hash, table.hash_table[i].hash))) \
break; \
\
i = (i+1) & (table.hash_size - 1); \
} \
\
if(ptr2) \
table.hash_table[i].data = DELETED; \
\
if(locked) \
unlockHashTable0(&table, self); \
}
#define hashIterate(table) \
{ \
HashEntry *entry = table.hash_table; \
int cnt = table.hash_count; \
\
while(cnt) { \
void *data = entry++->data; \
if(data) { \
ITERATE(data); \
cnt--; \
} \
} \
}
#define hashIterateP(table) \
{ \
HashEntry *entry = table.hash_table; \
int cnt = table.hash_count; \
\
while(cnt) { \
void **data_pntr = &entry++->data; \
if(*data_pntr) { \
ITERATE(data_pntr); \
cnt--; \
} \
} \
}
#define gcFreeHashTable(table) \
gcMemFree(table.hash_table);
#define freeHashTable(table) \
gcMemFree(table.hash_table);