Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > Hash_impl
comparison PrivateHash.c @ 19:9b75ea156895
added pure C brch
| author | Me@portablequad |
|---|---|
| date | Mon, 13 Feb 2012 13:10:28 -0800 |
| parents | 7c4d2bf121a9 |
| children |
comparison
equal
deleted
inserted
replaced
| 8:19a6bc503b35 | 10:3a5a04be9ccb |
|---|---|
| 4 * | 4 * |
| 5 * | 5 * |
| 6 * Author: seanhalle@yahoo.com | 6 * Author: seanhalle@yahoo.com |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | |
| 10 #include <stdio.h> | |
| 11 #include <string.h> | |
| 12 #include <errno.h> | |
| 13 #include <stdlib.h> | |
| 14 | |
| 15 #include "PrivateHash.h" | 9 #include "PrivateHash.h" |
| 16 #include "../vmalloc.h" | |
| 17 | 10 |
| 18 | 11 |
| 19 HashTable * | 12 HashTable * |
| 20 makeHashTable( int numHashSlots, FreeEntryContentFnPtr freeFn ) | 13 makeHashTable( int numHashSlots, FreeEntryContentFnPtr freeFn ) |
| 21 { HashTable * retTable; | 14 { HashTable * retTable; |
| 22 retTable = VMS__malloc( sizeof( HashTable ) ); | 15 retTable = malloc( sizeof( HashTable ) ); |
| 23 | 16 |
| 24 retTable->freeEntryContentFn = freeFn; | 17 retTable->freeEntryContentFn = freeFn; |
| 25 | 18 |
| 26 retTable->entries = VMS__malloc( numHashSlots * sizeof(HashEntry *) ); | 19 retTable->entries = malloc( numHashSlots * sizeof(HashEntry *) ); |
| 27 retTable->numEntries = 0; | 20 retTable->numEntries = 0; |
| 28 retTable->tableSz = numHashSlots; | 21 retTable->tableSz = numHashSlots; |
| 29 | 22 |
| 30 nullOutTablesArray( retTable ); | 23 nullOutTablesArray( retTable ); |
| 31 | 24 |
| 39 | 32 |
| 40 oldTableSz = table->tableSz; | 33 oldTableSz = table->tableSz; |
| 41 oldEntries = table->entries; | 34 oldEntries = table->entries; |
| 42 | 35 |
| 43 newTableSz = 2 * oldTableSz + 1; | 36 newTableSz = 2 * oldTableSz + 1; |
| 44 newEntries = VMS__malloc( newTableSz * sizeof(HashEntry *) ); | 37 newEntries = malloc( newTableSz * sizeof(HashEntry *) ); |
| 45 | 38 |
| 46 table->tableSz = newTableSz; | 39 table->tableSz = newTableSz; |
| 47 table->entries = newEntries; | 40 table->entries = newEntries; |
| 48 table->numEntries = 0; //about to add them all back! | 41 table->numEntries = 0; //about to add them all back! |
| 49 | 42 |
| 115 HashEntry* hashEntry; | 108 HashEntry* hashEntry; |
| 116 | 109 |
| 117 hashEntry = getEntryFromTable( key, table ); | 110 hashEntry = getEntryFromTable( key, table ); |
| 118 if( hashEntry == NULL ) | 111 if( hashEntry == NULL ) |
| 119 { hashIdx = hashThisKey( key, table->tableSz ); | 112 { hashIdx = hashThisKey( key, table->tableSz ); |
| 120 hashEntry = (HashEntry*) VMS__malloc( sizeof( HashEntry ) ); | 113 hashEntry = (HashEntry*) malloc( sizeof( HashEntry ) ); |
| 121 if( hashEntry == NULL ) return 0; | 114 if( hashEntry == NULL ) return 0; |
| 122 hashEntry->key = VMS__malloc( sizeof(hashkey_t) ); | 115 hashEntry->key = malloc( sizeof(hashkey_t) ); |
| 123 if( hashEntry->key == NULL ) return 0; | 116 if( hashEntry->key == NULL ) return 0; |
| 124 memcpy( hashEntry->key, key, sizeof(hashkey_t) ); | 117 memcpy( hashEntry->key, key, sizeof(hashkey_t) ); |
| 125 hashEntry->next = (table->entries)[hashIdx]; | 118 hashEntry->next = (table->entries)[hashIdx]; |
| 126 (table->entries)[hashIdx] = hashEntry; | 119 (table->entries)[hashIdx] = hashEntry; |
| 127 table->numEntries += 1; | 120 table->numEntries += 1; |
| 234 void | 227 void |
| 235 freeHashEntryUsing( HashEntry *entry, HashTable *table ) | 228 freeHashEntryUsing( HashEntry *entry, HashTable *table ) |
| 236 { | 229 { |
| 237 if( entry->content != NULL ) | 230 if( entry->content != NULL ) |
| 238 (*(table->freeEntryContentFn))( entry->content ); | 231 (*(table->freeEntryContentFn))( entry->content ); |
| 239 VMS__free( entry->key ); //was VMS__malloc'd above, so free it | 232 free( entry->key ); //was malloc'd above, so free it |
| 240 VMS__free( entry ); | 233 free( entry ); |
| 241 } | 234 } |
| 242 | 235 |
| 243 void | 236 void |
| 244 freeHashEntryButNotContent( HashEntry *entry ) | 237 freeHashEntryButNotContent( HashEntry *entry ) |
| 245 { | 238 { |
| 246 VMS__free( entry->key ); //was VMS__malloc'd above, so free it | 239 free( entry->key ); //was malloc'd above, so free it |
| 247 VMS__free( entry ); | 240 free( entry ); |
| 248 } | 241 } |
| 249 | 242 |
