# HG changeset patch # User Merten Sach # Date 1317209895 -7200 # Node ID 7c4d2bf121a98083469f129303f567059fc1d281 # Parent 8b225987970d62aaa99f0ad1f616f908f16ede0f Bugfix: Strings were used to handle keys. So 0 byte in key will terminate all functions. diff -r 8b225987970d -r 7c4d2bf121a9 .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Wed Sep 28 13:38:15 2011 +0200 @@ -0,0 +1,3 @@ +syntax: glob + +*.o diff -r 8b225987970d -r 7c4d2bf121a9 PrivateHash.c --- a/PrivateHash.c Mon Jul 11 18:10:14 2011 +0200 +++ b/PrivateHash.c Wed Sep 28 13:38:15 2011 +0200 @@ -71,11 +71,13 @@ } unsigned int -hashThisKey( char *s, int hashSz ) +hashThisKey( char* s, int hashSz ) { unsigned int h = 0; + unsigned int i; + hashkey_t* key = (hashkey_t*)s; - for( ; *s != 0; s++ ) - h = *s + h*31; + for(i=0 ; ihashable[i] + h*31; return h % hashSz; } @@ -88,7 +90,9 @@ HashEntry* hashEntry = table->entries[ hashIndex ]; for( ; hashEntry != NULL; hashEntry = hashEntry->next ) - { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry; + { + if( memcmp( hashEntry->key, key, sizeof(hashkey_t) ) == 0 ) + return hashEntry; } return NULL; } @@ -115,9 +119,9 @@ { hashIdx = hashThisKey( key, table->tableSz ); hashEntry = (HashEntry*) VMS__malloc( sizeof( HashEntry ) ); if( hashEntry == NULL ) return 0; - hashEntry->key = VMS__malloc( strlen(key) ); + hashEntry->key = VMS__malloc( sizeof(hashkey_t) ); if( hashEntry->key == NULL ) return 0; - strcpy( hashEntry->key, key ); + memcpy( hashEntry->key, key, sizeof(hashkey_t) ); hashEntry->next = (table->entries)[hashIdx]; (table->entries)[hashIdx] = hashEntry; table->numEntries += 1; @@ -164,7 +168,7 @@ testEntry = table->entries[ hashIndex ]; for( ; testEntry != NULL; testEntry = testEntry->next ) - { if( strcmp( testEntry->key, entry->key ) == 0 ) + { if( memcmp( testEntry->key, entry->key, sizeof(hashkey_t)) == 0 ) { if( prevEntry == NULL ) { table->entries[hashIndex] = entry; entry->next = testEntry->next; @@ -194,7 +198,7 @@ addrOfHashEntryPtr = &( table->entries[ hashIndex ] ); hashEntry = *addrOfHashEntryPtr; while( hashEntry != NULL ) - { if( strcmp( hashEntry->key, key ) == 0 ) + { if( memcmp( hashEntry->key, key, sizeof(hashkey_t) ) == 0 ) { *addrOfHashEntryPtr = hashEntry->next; //TODO: Free the contents of entry? @@ -207,40 +211,7 @@ } return FALSE; } - -/* debugging function displays the hashtable in (key.value) pairs -*/ -/*void hashTableToString( HashTable * table ) - { int i; - HashEntry *t; - for( i = 0; i < table->tableSz; i++ ) - { t = entries[i]; - if( t == NULL ) - strcat_m( retStr, &"()" ); - else - { strcat_m( retStr, &"(" ); - for( ; t != NULL; t = t->next ) - { strcat_m( retStr, &" " ); - strcat_m( retStr, t->key ); - strcat_m( retStr, &"." ); - strcat_m( retStr, paramToString( t->param ) ); - strcat_m( retStr, &" " ); - } - strcat_m( retStr, &")" ); - } - } - } -*/ - -/* - void -setFnToFreeEntryContent( HashTable *table, FreeEntryContentFnPtr fnPtr ) - { - table->freeEntryContentFn = fnPtr; - } -*/ - void freeHashTable( HashTable *table ) { int i; diff -r 8b225987970d -r 7c4d2bf121a9 PrivateHash.h --- a/PrivateHash.h Mon Jul 11 18:10:14 2011 +0200 +++ b/PrivateHash.h Wed Sep 28 13:38:15 2011 +0200 @@ -13,6 +13,12 @@ #define TRUE 1 #define FALSE 0 +union hashkey_t{ + char hashable[8]; + int parts[2]; +}; + +typedef union hashkey_t hashkey_t; #define DEFAULT_HASHSIZE 1 << 10