-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.cpp
42 lines (34 loc) · 1.13 KB
/
hash.cpp
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
#include "std.h"
#include "rope.h"
#include "hash.h"
void lockHashTable0(HashTable *table, Thread *self) {
if(!tryLockVMLock(table->lock, self)) {
disableSuspend(self);
lockVMLock(table->lock, self);
enableSuspend(self);
}
fastDisableSuspend(self);
}
void unlockHashTable0(HashTable *table, Thread *self) {
fastEnableSuspend(self);
unlockVMLock(table->lock, self);
}
void resizeHash(HashTable *table, int new_size) {
HashEntry *new_table = (HashEntry*)gcMemMalloc(sizeof(HashEntry)*new_size);
int i;
memset(new_table, 0, sizeof(HashEntry)*new_size);
for(i = table->hash_size-1; i >= 0; i--) {
void *ptr = table->hash_table[i].data;
if(ptr != NULL) {
int hash = table->hash_table[i].hash;
int new_index = hash & (new_size - 1);
while(new_table[new_index].data != NULL)
new_index = (new_index+1) & (new_size - 1);
new_table[new_index].hash = hash;
new_table[new_index].data = ptr;
}
}
gcMemFree(table->hash_table);
table->hash_table = new_table;
table->hash_size = new_size;
}