Fix exceptaionally rare crash bug in hash tables (happens once in every 2^32 hash lookups

darcs-hash:20061018230148-ac50b-4e67ecf1f9aeae8239c40101ae2ad6aa164c31bb.gz
This commit is contained in:
axel 2006-10-19 09:01:48 +10:00
parent bab168f1d1
commit c7bc31fe50

4
util.c
View file

@ -207,7 +207,7 @@ static int hash_search( hash_table_t *h,
int pos; int pos;
hv = h->hash_func( key ); hv = h->hash_func( key );
pos = abs(hv) % h->size; pos = (hv & 0x7fffffff) % h->size;
while(1) while(1)
{ {
if( (h->arr[pos].key == 0 ) || if( (h->arr[pos].key == 0 ) ||
@ -370,7 +370,7 @@ void hash_remove( hash_table_t *h,
{ {
int hv = h->hash_func( h->arr[next_pos].key ); int hv = h->hash_func( h->arr[next_pos].key );
int ideal_pos = abs( hv ) % h->size; int ideal_pos = ( hv & 0x7fffffff) % h->size;
int dist_old = (next_pos - ideal_pos + h->size)%h->size; int dist_old = (next_pos - ideal_pos + h->size)%h->size;
int dist_new = (pos - ideal_pos + h->size)%h->size; int dist_new = (pos - ideal_pos + h->size)%h->size;
if ( dist_new < dist_old ) if ( dist_new < dist_old )