changeset 5:e072db5aa783 VMS__malloc_brch

Fixed bug -- strdup snuck a malloc in -- and added better insert Entry
author Me
date Tue, 02 Nov 2010 16:45:50 -0700
parents 1ee100564408
children ec5c00d4023e bac20745c52e
files PrivateHash.c
diffstat 1 files changed, 34 insertions(+), 2 deletions(-) [+]
line diff
     1.1 --- a/PrivateHash.c	Sun Oct 31 20:25:30 2010 -0700
     1.2 +++ b/PrivateHash.c	Tue Nov 02 16:45:50 2010 -0700
     1.3 @@ -114,8 +114,9 @@
     1.4      { hashIdx = hashThisKey( key, table->tableSz );
     1.5        hashEntry = (HashEntry*) VMS__malloc( sizeof( HashEntry ) );
     1.6              if( hashEntry == NULL )  return 0;
     1.7 -      hashEntry->key = strdup( key ); //TODO: figure out soln for incr Sz
     1.8 +      hashEntry->key = VMS__malloc( strlen(key) );
     1.9              if( hashEntry->key == NULL ) return 0;
    1.10 +      strcpy( hashEntry->key, key );
    1.11        hashEntry->next = (table->entries)[hashIdx];
    1.12        (table->entries)[hashIdx] = hashEntry;
    1.13        table->numEntries += 1;
    1.14 @@ -143,13 +144,44 @@
    1.15      }
    1.16     else
    1.17      { (*(table->freeEntryContentFn))( testEntry->content );
    1.18 +         //being lazy -- will create bug in code that relies on having ptr to
    1.19 +         // elem given to insert into table!
    1.20        testEntry->content = entry->content;
    1.21        entry->content = NULL;
    1.22 -      freeHashEntryUsing( entry, table );
    1.23 +      freeHashEntryButNotContent( entry );
    1.24      }
    1.25     return 1;
    1.26   }
    1.27  
    1.28 +/*Better version
    1.29 + */
    1.30 + int
    1.31 +untested_putEntryIntoTable( HashEntry *entry, HashTable * table )
    1.32 + { HashEntry *testEntry, *prevEntry = NULL;
    1.33 +    unsigned int
    1.34 +   hashIndex = hashThisKey( entry->key, table->tableSz );
    1.35 +   
    1.36 +   testEntry = table->entries[ hashIndex ];
    1.37 +   for( ; testEntry != NULL; testEntry = testEntry->next )
    1.38 +    { if( strcmp( testEntry->key, entry->key ) == 0 )
    1.39 +       { if( prevEntry == NULL )
    1.40 +          { table->entries[hashIndex] = entry;
    1.41 +            entry->next = testEntry->next;
    1.42 +          }
    1.43 +         else
    1.44 +          { prevEntry->next = entry;
    1.45 +            entry->next = testEntry->next;
    1.46 +          }
    1.47 +         freeHashEntryUsing( testEntry, table ); //frees content too!
    1.48 +         return;
    1.49 +       }
    1.50 +    }
    1.51 +      //wasn't found, so insert
    1.52 +   entry->next = table->entries[hashIndex];
    1.53 +   table->entries[hashIndex] = entry;
    1.54 + }
    1.55 +
    1.56 +
    1.57  
    1.58   bool8
    1.59  deleteEntryFromTable( char *key, HashTable *table )