changeset 9:7c4d2bf121a9 VMS__malloc_brch

Bugfix: Strings were used to handle keys. So 0 byte in key will terminate all functions.
author Merten Sach <msach@mailbox.tu-berlin.de>
date Wed, 28 Sep 2011 13:38:15 +0200
parents 8b225987970d
children 8bafd14e9fde c934e7d8ab55
files .hgignore PrivateHash.c PrivateHash.h
diffstat 3 files changed, 21 insertions(+), 41 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.hgignore	Wed Sep 28 13:38:15 2011 +0200
     1.3 @@ -0,0 +1,3 @@
     1.4 +syntax: glob
     1.5 +
     1.6 +*.o
     2.1 --- a/PrivateHash.c	Mon Jul 11 18:10:14 2011 +0200
     2.2 +++ b/PrivateHash.c	Wed Sep 28 13:38:15 2011 +0200
     2.3 @@ -71,11 +71,13 @@
     2.4   }
     2.5  
     2.6  unsigned int
     2.7 -hashThisKey( char *s, int hashSz )
     2.8 +hashThisKey( char* s, int hashSz )
     2.9   { unsigned int h = 0;
    2.10 +   unsigned int i;
    2.11 +   hashkey_t* key = (hashkey_t*)s;
    2.12  
    2.13 -   for( ; *s != 0; s++ )
    2.14 -      h = *s + h*31;
    2.15 +   for(i=0 ; i<sizeof(hashkey_t); i++ )
    2.16 +      h = key->hashable[i] + h*31;
    2.17     return h % hashSz;
    2.18   }
    2.19  
    2.20 @@ -88,7 +90,9 @@
    2.21      HashEntry*
    2.22     hashEntry = table->entries[ hashIndex ];
    2.23     for( ; hashEntry != NULL; hashEntry = hashEntry->next )
    2.24 -    { if( strcmp( hashEntry->key, key ) == 0 )  return hashEntry;
    2.25 +    {
    2.26 +       if( memcmp( hashEntry->key, key, sizeof(hashkey_t) ) == 0 )
    2.27 +           return hashEntry;
    2.28      }
    2.29     return NULL;
    2.30   }
    2.31 @@ -115,9 +119,9 @@
    2.32      { hashIdx = hashThisKey( key, table->tableSz );
    2.33        hashEntry = (HashEntry*) VMS__malloc( sizeof( HashEntry ) );
    2.34              if( hashEntry == NULL )  return 0;
    2.35 -      hashEntry->key = VMS__malloc( strlen(key) );
    2.36 +      hashEntry->key = VMS__malloc( sizeof(hashkey_t) );
    2.37              if( hashEntry->key == NULL ) return 0;
    2.38 -      strcpy( hashEntry->key, key );
    2.39 +      memcpy( hashEntry->key, key, sizeof(hashkey_t) );
    2.40        hashEntry->next = (table->entries)[hashIdx];
    2.41        (table->entries)[hashIdx] = hashEntry;
    2.42        table->numEntries += 1;
    2.43 @@ -164,7 +168,7 @@
    2.44     
    2.45     testEntry = table->entries[ hashIndex ];
    2.46     for( ; testEntry != NULL; testEntry = testEntry->next )
    2.47 -    { if( strcmp( testEntry->key, entry->key ) == 0 )
    2.48 +    { if( memcmp( testEntry->key, entry->key, sizeof(hashkey_t)) == 0 )
    2.49         { if( prevEntry == NULL )
    2.50            { table->entries[hashIndex] = entry;
    2.51              entry->next = testEntry->next;
    2.52 @@ -194,7 +198,7 @@
    2.53     addrOfHashEntryPtr = &( table->entries[ hashIndex ] );
    2.54     hashEntry = *addrOfHashEntryPtr;
    2.55     while( hashEntry != NULL )
    2.56 -    { if( strcmp( hashEntry->key, key ) == 0 )
    2.57 +    { if( memcmp( hashEntry->key, key, sizeof(hashkey_t) ) == 0 )
    2.58         {
    2.59           *addrOfHashEntryPtr = hashEntry->next;
    2.60           //TODO: Free the contents of entry?
    2.61 @@ -207,40 +211,7 @@
    2.62      }
    2.63     return FALSE;
    2.64   }
    2.65 -
    2.66   
    2.67 -/* debugging function displays the hashtable in (key.value) pairs
    2.68 -*/
    2.69 -/*void hashTableToString( HashTable * table )
    2.70 - { int i;
    2.71 -   HashEntry *t;
    2.72 -   for( i = 0; i < table->tableSz; i++ )
    2.73 -    { t = entries[i];
    2.74 -      if( t == NULL )
    2.75 -         strcat_m( retStr, &"()" );
    2.76 -      else
    2.77 -       { strcat_m( retStr, &"(" );
    2.78 -         for( ; t != NULL; t = t->next )
    2.79 -          { strcat_m( retStr, &" " );
    2.80 -            strcat_m( retStr, t->key );
    2.81 -            strcat_m( retStr, &"." );
    2.82 -            strcat_m( retStr, paramToString( t->param ) );
    2.83 -            strcat_m( retStr, &" " );
    2.84 -          }
    2.85 -         strcat_m( retStr, &")" );
    2.86 -       }
    2.87 -    }
    2.88 - }
    2.89 -*/
    2.90 -
    2.91 -/*
    2.92 - void
    2.93 -setFnToFreeEntryContent( HashTable *table, FreeEntryContentFnPtr fnPtr )
    2.94 - {
    2.95 -   table->freeEntryContentFn = fnPtr;
    2.96 - }
    2.97 -*/
    2.98 -
    2.99  void
   2.100  freeHashTable( HashTable *table )
   2.101   { int i;
     3.1 --- a/PrivateHash.h	Mon Jul 11 18:10:14 2011 +0200
     3.2 +++ b/PrivateHash.h	Wed Sep 28 13:38:15 2011 +0200
     3.3 @@ -13,6 +13,12 @@
     3.4  #define TRUE     1
     3.5  #define FALSE    0
     3.6  
     3.7 +union hashkey_t{
     3.8 +    char hashable[8];
     3.9 +    int  parts[2];
    3.10 +};
    3.11 +
    3.12 +typedef union hashkey_t hashkey_t;
    3.13  
    3.14  #define DEFAULT_HASHSIZE 1 << 10
    3.15