diff PrivateHash.c @ 11:8bafd14e9fde

merged VMS__malloc_brch
author Merten Sach <msach@mailbox.tu-berlin.de>
date Wed, 28 Sep 2011 13:45:34 +0200
parents 8b225987970d
children 5b89d57e5d10 093cad17d992
line diff
     1.1 --- a/PrivateHash.c	Sat Sep 11 07:57:41 2010 -0700
     1.2 +++ b/PrivateHash.c	Wed Sep 28 13:45:34 2011 +0200
     1.3 @@ -13,16 +13,17 @@
     1.4  #include <stdlib.h>
     1.5  
     1.6  #include "PrivateHash.h"
     1.7 +#include "../vmalloc.h"
     1.8  
     1.9  
    1.10   HashTable *
    1.11  makeHashTable( int numHashSlots, FreeEntryContentFnPtr freeFn )
    1.12   { HashTable * retTable;
    1.13 -   retTable = malloc( sizeof( HashTable ) );
    1.14 +   retTable = VMS__malloc( sizeof( HashTable ) );
    1.15  
    1.16     retTable->freeEntryContentFn = freeFn;
    1.17     
    1.18 -   retTable->entries    = malloc( numHashSlots * sizeof(HashEntry *) );
    1.19 +   retTable->entries    = VMS__malloc( numHashSlots * sizeof(HashEntry *) );
    1.20     retTable->numEntries = 0;
    1.21     retTable->tableSz    = numHashSlots;
    1.22  
    1.23 @@ -40,7 +41,7 @@
    1.24     oldEntries = table->entries;
    1.25  
    1.26     newTableSz = 2 * oldTableSz + 1;
    1.27 -   newEntries = malloc( newTableSz * sizeof(HashEntry *) );
    1.28 +   newEntries = VMS__malloc( newTableSz * sizeof(HashEntry *) );
    1.29  
    1.30     table->tableSz    = newTableSz;
    1.31     table->entries    = newEntries;
    1.32 @@ -70,11 +71,13 @@
    1.33   }
    1.34  
    1.35  unsigned int
    1.36 -hashThisKey( char *s, int hashSz )
    1.37 +hashThisKey( char* s, int hashSz )
    1.38   { unsigned int h = 0;
    1.39 +   unsigned int i;
    1.40 +   hashkey_t* key = (hashkey_t*)s;
    1.41  
    1.42 -   for( ; *s != 0; s++ )
    1.43 -      h = *s + h*31;
    1.44 +   for(i=0 ; i<sizeof(hashkey_t); i++ )
    1.45 +      h = key->hashable[i] + h*31;
    1.46     return h % hashSz;
    1.47   }
    1.48  
    1.49 @@ -87,7 +90,9 @@
    1.50      HashEntry*
    1.51     hashEntry = table->entries[ hashIndex ];
    1.52     for( ; hashEntry != NULL; hashEntry = hashEntry->next )
    1.53 -    { if( strcmp( hashEntry->key, key ) == 0 )  return hashEntry;
    1.54 +    {
    1.55 +       if( memcmp( hashEntry->key, key, sizeof(hashkey_t) ) == 0 )
    1.56 +           return hashEntry;
    1.57      }
    1.58     return NULL;
    1.59   }
    1.60 @@ -112,10 +117,11 @@
    1.61     hashEntry = getEntryFromTable( key, table );
    1.62     if( hashEntry == NULL )
    1.63      { hashIdx = hashThisKey( key, table->tableSz );
    1.64 -      hashEntry = (HashEntry*) malloc( sizeof( HashEntry ) );
    1.65 +      hashEntry = (HashEntry*) VMS__malloc( sizeof( HashEntry ) );
    1.66              if( hashEntry == NULL )  return 0;
    1.67 -      hashEntry->key = strdup( key ); //TODO: figure out soln for incr Sz
    1.68 +      hashEntry->key = VMS__malloc( sizeof(hashkey_t) );
    1.69              if( hashEntry->key == NULL ) return 0;
    1.70 +      memcpy( hashEntry->key, key, sizeof(hashkey_t) );
    1.71        hashEntry->next = (table->entries)[hashIdx];
    1.72        (table->entries)[hashIdx] = hashEntry;
    1.73        table->numEntries += 1;
    1.74 @@ -143,13 +149,44 @@
    1.75      }
    1.76     else
    1.77      { (*(table->freeEntryContentFn))( testEntry->content );
    1.78 +         //being lazy -- will create bug in code that relies on having ptr to
    1.79 +         // elem given to insert into table!
    1.80        testEntry->content = entry->content;
    1.81        entry->content = NULL;
    1.82 -      freeHashEntryUsing( entry, table );
    1.83 +      freeHashEntryButNotContent( entry );
    1.84      }
    1.85     return 1;
    1.86   }
    1.87  
    1.88 +/*Better version
    1.89 + */
    1.90 + void
    1.91 +untested_putEntryIntoTable( HashEntry *entry, HashTable * table )
    1.92 + { HashEntry *testEntry, *prevEntry = NULL;
    1.93 +    unsigned int
    1.94 +   hashIndex = hashThisKey( entry->key, table->tableSz );
    1.95 +   
    1.96 +   testEntry = table->entries[ hashIndex ];
    1.97 +   for( ; testEntry != NULL; testEntry = testEntry->next )
    1.98 +    { if( memcmp( testEntry->key, entry->key, sizeof(hashkey_t)) == 0 )
    1.99 +       { if( prevEntry == NULL )
   1.100 +          { table->entries[hashIndex] = entry;
   1.101 +            entry->next = testEntry->next;
   1.102 +          }
   1.103 +         else
   1.104 +          { prevEntry->next = entry;
   1.105 +            entry->next = testEntry->next;
   1.106 +          }
   1.107 +         freeHashEntryUsing( testEntry, table ); //frees content too!
   1.108 +         return;
   1.109 +       }
   1.110 +    }
   1.111 +      //wasn't found, so insert
   1.112 +   entry->next = table->entries[hashIndex];
   1.113 +   table->entries[hashIndex] = entry;
   1.114 + }
   1.115 +
   1.116 +
   1.117  
   1.118   bool8
   1.119  deleteEntryFromTable( char *key, HashTable *table )
   1.120 @@ -161,7 +198,7 @@
   1.121     addrOfHashEntryPtr = &( table->entries[ hashIndex ] );
   1.122     hashEntry = *addrOfHashEntryPtr;
   1.123     while( hashEntry != NULL )
   1.124 -    { if( strcmp( hashEntry->key, key ) == 0 )
   1.125 +    { if( memcmp( hashEntry->key, key, sizeof(hashkey_t) ) == 0 )
   1.126         {
   1.127           *addrOfHashEntryPtr = hashEntry->next;
   1.128           //TODO: Free the contents of entry?
   1.129 @@ -174,40 +211,7 @@
   1.130      }
   1.131     return FALSE;
   1.132   }
   1.133 -
   1.134   
   1.135 -/* debugging function displays the hashtable in (key.value) pairs
   1.136 -*/
   1.137 -/*void hashTableToString( HashTable * table )
   1.138 - { int i;
   1.139 -   HashEntry *t;
   1.140 -   for( i = 0; i < table->tableSz; i++ )
   1.141 -    { t = entries[i];
   1.142 -      if( t == NULL )
   1.143 -         strcat_m( retStr, &"()" );
   1.144 -      else
   1.145 -       { strcat_m( retStr, &"(" );
   1.146 -         for( ; t != NULL; t = t->next )
   1.147 -          { strcat_m( retStr, &" " );
   1.148 -            strcat_m( retStr, t->key );
   1.149 -            strcat_m( retStr, &"." );
   1.150 -            strcat_m( retStr, paramToString( t->param ) );
   1.151 -            strcat_m( retStr, &" " );
   1.152 -          }
   1.153 -         strcat_m( retStr, &")" );
   1.154 -       }
   1.155 -    }
   1.156 - }
   1.157 -*/
   1.158 -
   1.159 -/*
   1.160 - void
   1.161 -setFnToFreeEntryContent( HashTable *table, FreeEntryContentFnPtr fnPtr )
   1.162 - {
   1.163 -   table->freeEntryContentFn = fnPtr;
   1.164 - }
   1.165 -*/
   1.166 -
   1.167  void
   1.168  freeHashTable( HashTable *table )
   1.169   { int i;
   1.170 @@ -232,14 +236,14 @@
   1.171   {
   1.172     if( entry->content != NULL )
   1.173        (*(table->freeEntryContentFn))( entry->content );
   1.174 -   free( entry->key ); //was malloc'd above, so free it
   1.175 -   free( entry );
   1.176 +   VMS__free( entry->key ); //was VMS__malloc'd above, so free it
   1.177 +   VMS__free( entry );
   1.178   }
   1.179  
   1.180  void
   1.181  freeHashEntryButNotContent( HashEntry *entry )
   1.182   {
   1.183 -   free( entry->key ); //was malloc'd above, so free it
   1.184 -   free( entry );
   1.185 +   VMS__free( entry->key ); //was VMS__malloc'd above, so free it
   1.186 +   VMS__free( entry );
   1.187   }
   1.188