# HG changeset patch # User Merten Sach # Date 1317210334 -7200 # Node ID 8bafd14e9fde67082fb08186463a4803bc25b428 # Parent e6fe47763ee6a98f6bfc5b9188dbc36f99fe84c7# Parent 7c4d2bf121a98083469f129303f567059fc1d281 merged VMS__malloc_brch diff -r e6fe47763ee6 -r 8bafd14e9fde .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Wed Sep 28 13:45:34 2011 +0200 @@ -0,0 +1,3 @@ +syntax: glob + +*.o diff -r e6fe47763ee6 -r 8bafd14e9fde PrivateHash.c --- a/PrivateHash.c Sat Sep 11 07:57:41 2010 -0700 +++ b/PrivateHash.c Wed Sep 28 13:45:34 2011 +0200 @@ -13,16 +13,17 @@ #include #include "PrivateHash.h" +#include "../vmalloc.h" HashTable * makeHashTable( int numHashSlots, FreeEntryContentFnPtr freeFn ) { HashTable * retTable; - retTable = malloc( sizeof( HashTable ) ); + retTable = VMS__malloc( sizeof( HashTable ) ); retTable->freeEntryContentFn = freeFn; - retTable->entries = malloc( numHashSlots * sizeof(HashEntry *) ); + retTable->entries = VMS__malloc( numHashSlots * sizeof(HashEntry *) ); retTable->numEntries = 0; retTable->tableSz = numHashSlots; @@ -40,7 +41,7 @@ oldEntries = table->entries; newTableSz = 2 * oldTableSz + 1; - newEntries = malloc( newTableSz * sizeof(HashEntry *) ); + newEntries = VMS__malloc( newTableSz * sizeof(HashEntry *) ); table->tableSz = newTableSz; table->entries = newEntries; @@ -70,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; } @@ -87,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; } @@ -112,10 +117,11 @@ hashEntry = getEntryFromTable( key, table ); if( hashEntry == NULL ) { hashIdx = hashThisKey( key, table->tableSz ); - hashEntry = (HashEntry*) malloc( sizeof( HashEntry ) ); + hashEntry = (HashEntry*) VMS__malloc( sizeof( HashEntry ) ); if( hashEntry == NULL ) return 0; - hashEntry->key = strdup( key ); //TODO: figure out soln for incr Sz + hashEntry->key = VMS__malloc( sizeof(hashkey_t) ); if( hashEntry->key == NULL ) return 0; + memcpy( hashEntry->key, key, sizeof(hashkey_t) ); hashEntry->next = (table->entries)[hashIdx]; (table->entries)[hashIdx] = hashEntry; table->numEntries += 1; @@ -143,13 +149,44 @@ } else { (*(table->freeEntryContentFn))( testEntry->content ); + //being lazy -- will create bug in code that relies on having ptr to + // elem given to insert into table! testEntry->content = entry->content; entry->content = NULL; - freeHashEntryUsing( entry, table ); + freeHashEntryButNotContent( entry ); } return 1; } +/*Better version + */ + void +untested_putEntryIntoTable( HashEntry *entry, HashTable * table ) + { HashEntry *testEntry, *prevEntry = NULL; + unsigned int + hashIndex = hashThisKey( entry->key, table->tableSz ); + + testEntry = table->entries[ hashIndex ]; + for( ; testEntry != NULL; testEntry = testEntry->next ) + { if( memcmp( testEntry->key, entry->key, sizeof(hashkey_t)) == 0 ) + { if( prevEntry == NULL ) + { table->entries[hashIndex] = entry; + entry->next = testEntry->next; + } + else + { prevEntry->next = entry; + entry->next = testEntry->next; + } + freeHashEntryUsing( testEntry, table ); //frees content too! + return; + } + } + //wasn't found, so insert + entry->next = table->entries[hashIndex]; + table->entries[hashIndex] = entry; + } + + bool8 deleteEntryFromTable( char *key, HashTable *table ) @@ -161,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? @@ -174,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; @@ -232,14 +236,14 @@ { if( entry->content != NULL ) (*(table->freeEntryContentFn))( entry->content ); - free( entry->key ); //was malloc'd above, so free it - free( entry ); + VMS__free( entry->key ); //was VMS__malloc'd above, so free it + VMS__free( entry ); } void freeHashEntryButNotContent( HashEntry *entry ) { - free( entry->key ); //was malloc'd above, so free it - free( entry ); + VMS__free( entry->key ); //was VMS__malloc'd above, so free it + VMS__free( entry ); } diff -r e6fe47763ee6 -r 8bafd14e9fde PrivateHash.h --- a/PrivateHash.h Sat Sep 11 07:57:41 2010 -0700 +++ b/PrivateHash.h Wed Sep 28 13:45:34 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