Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > Hash_impl
changeset 14:5b89d57e5d10 VMS__malloc_brch
added .brch__VMS__malloc_brch which has purpose of the brch
| author | Me@portablequad |
|---|---|
| date | Sat, 11 Feb 2012 17:55:51 -0800 |
| parents | c934e7d8ab55 |
| children | 64a94ec0e2a4 |
| files | .brch__VMS__malloc_brch .hgeol MurmurHash2.c PrivateHash.c PrivateHash.h |
| diffstat | 5 files changed, 405 insertions(+), 383 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/.brch__VMS__malloc_brch Sat Feb 11 17:55:51 2012 -0800 1.3 @@ -0,0 +1,4 @@ 1.4 +This branch is for the project structure defined Jan 2012.. the #includes reflect this directory structure. 1.5 + 1.6 +More importantly, the MC_shared version of VMS requires a separat malloc implemeted by VMS code.. so this branch has modified the library to use the VMS-specific malloc. 1.7 +
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/.hgeol Sat Feb 11 17:55:51 2012 -0800 2.3 @@ -0,0 +1,14 @@ 2.4 + 2.5 +[patterns] 2.6 +**.py = native 2.7 +**.txt = native 2.8 +**.c = native 2.9 +**.h = native 2.10 +**.cpp = native 2.11 +**.java = native 2.12 +**.class = bin 2.13 +**.jar = bin 2.14 +**.sh = native 2.15 +**.pl = native 2.16 +**.jpg = bin 2.17 +**.gif = bin
3.1 --- a/MurmurHash2.c Thu Feb 09 15:51:22 2012 +0100 3.2 +++ b/MurmurHash2.c Sat Feb 11 17:55:51 2012 -0800 3.3 @@ -1,64 +1,69 @@ 3.4 -//----------------------------------------------------------------------------- 3.5 -// MurmurHash2, by Austin Appleby 3.6 - 3.7 -// Note - This code makes a few assumptions about how your machine behaves - 3.8 - 3.9 -// 1. We can read a 4-byte value from any address without crashing 3.10 -// 2. sizeof(int) == 4 3.11 - 3.12 -// And it has a few limitations - 3.13 - 3.14 -// 1. It will not work incrementally. 3.15 -// 2. It will not produce the same results on little-endian and big-endian 3.16 -// machines. 3.17 - 3.18 -unsigned int MurmurHash2 ( const void * key, int len, unsigned int seed ) 3.19 -{ 3.20 - // 'm' and 'r' are mixing constants generated offline. 3.21 - // They're not really 'magic', they just happen to work well. 3.22 - 3.23 - const unsigned int m = 0x5bd1e995; 3.24 - const int r = 24; 3.25 - 3.26 - // Initialize the hash to a 'random' value 3.27 - 3.28 - unsigned int h = seed ^ len; 3.29 - 3.30 - // Mix 4 bytes at a time into the hash 3.31 - 3.32 - const unsigned char * data = (const unsigned char *)key; 3.33 - 3.34 - while(len >= 4) 3.35 - { 3.36 - unsigned int k = *(unsigned int *)data; 3.37 - 3.38 - k *= m; 3.39 - k ^= k >> r; 3.40 - k *= m; 3.41 - 3.42 - h *= m; 3.43 - h ^= k; 3.44 - 3.45 - data += 4; 3.46 - len -= 4; 3.47 - } 3.48 - 3.49 - // Handle the last few bytes of the input array 3.50 - 3.51 - switch(len) 3.52 - { 3.53 - case 3: h ^= data[2] << 16; 3.54 - case 2: h ^= data[1] << 8; 3.55 - case 1: h ^= data[0]; 3.56 - h *= m; 3.57 - }; 3.58 - 3.59 - // Do a few final mixes of the hash to ensure the last few 3.60 - // bytes are well-incorporated. 3.61 - 3.62 - h ^= h >> 13; 3.63 - h *= m; 3.64 - h ^= h >> 15; 3.65 - 3.66 - return h; 3.67 -} 3.68 + 3.69 +/*This file is sample code pulled off the web -- NOT part of the compiled code 3.70 + * make sure it doesn't get included in makefile 'cause it doesn't compile 3.71 + */ 3.72 + 3.73 +//----------------------------------------------------------------------------- 3.74 +// MurmurHash2, by Austin Appleby 3.75 + 3.76 +// Note - This code makes a few assumptions about how your machine behaves - 3.77 + 3.78 +// 1. We can read a 4-byte value from any address without crashing 3.79 +// 2. sizeof(int) == 4 3.80 + 3.81 +// And it has a few limitations - 3.82 + 3.83 +// 1. It will not work incrementally. 3.84 +// 2. It will not produce the same results on little-endian and big-endian 3.85 +// machines. 3.86 + 3.87 +unsigned int MurmurHash2 ( const void * key, int len, unsigned int seed ) 3.88 +{ 3.89 + // 'm' and 'r' are mixing constants generated offline. 3.90 + // They're not really 'magic', they just happen to work well. 3.91 + 3.92 + const unsigned int m = 0x5bd1e995; 3.93 + const int r = 24; 3.94 + 3.95 + // Initialize the hash to a 'random' value 3.96 + 3.97 + unsigned int h = seed ^ len; 3.98 + 3.99 + // Mix 4 bytes at a time into the hash 3.100 + 3.101 + const unsigned char * data = (const unsigned char *)key; 3.102 + 3.103 + while(len >= 4) 3.104 + { 3.105 + unsigned int k = *(unsigned int *)data; 3.106 + 3.107 + k *= m; 3.108 + k ^= k >> r; 3.109 + k *= m; 3.110 + 3.111 + h *= m; 3.112 + h ^= k; 3.113 + 3.114 + data += 4; 3.115 + len -= 4; 3.116 + } 3.117 + 3.118 + // Handle the last few bytes of the input array 3.119 + 3.120 + switch(len) 3.121 + { 3.122 + case 3: h ^= data[2] << 16; 3.123 + case 2: h ^= data[1] << 8; 3.124 + case 1: h ^= data[0]; 3.125 + h *= m; 3.126 + }; 3.127 + 3.128 + // Do a few final mixes of the hash to ensure the last few 3.129 + // bytes are well-incorporated. 3.130 + 3.131 + h ^= h >> 13; 3.132 + h *= m; 3.133 + h ^= h >> 15; 3.134 + 3.135 + return h; 3.136 +}
4.1 --- a/PrivateHash.c Thu Feb 09 15:51:22 2012 +0100 4.2 +++ b/PrivateHash.c Sat Feb 11 17:55:51 2012 -0800 4.3 @@ -1,249 +1,242 @@ 4.4 -/* 4.5 - * Copyright 2009 OpenSourceStewardshipFoundation.org 4.6 - * Licensed under GNU General Public License version 2 4.7 - * 4.8 - * 4.9 - * Author: seanhalle@yahoo.com 4.10 - */ 4.11 - 4.12 - 4.13 -#include <stdio.h> 4.14 -#include <string.h> 4.15 -#include <errno.h> 4.16 -#include <stdlib.h> 4.17 - 4.18 -#include "PrivateHash.h" 4.19 -#include "../vmalloc.h" 4.20 - 4.21 - 4.22 - HashTable * 4.23 -makeHashTable( int numHashSlots, FreeEntryContentFnPtr freeFn ) 4.24 - { HashTable * retTable; 4.25 - retTable = VMS__malloc( sizeof( HashTable ) ); 4.26 - 4.27 - retTable->freeEntryContentFn = freeFn; 4.28 - 4.29 - retTable->entries = VMS__malloc( numHashSlots * sizeof(HashEntry *) ); 4.30 - retTable->numEntries = 0; 4.31 - retTable->tableSz = numHashSlots; 4.32 - 4.33 - nullOutTablesArray( retTable ); 4.34 - 4.35 - return retTable; 4.36 - } 4.37 - 4.38 - void 4.39 -doubleTableSize( HashTable *table ) 4.40 - { int i, oldTableSz, newTableSz; 4.41 - HashEntry *entry, *nextEntry, **oldEntries, **newEntries; 4.42 - 4.43 - oldTableSz = table->tableSz; 4.44 - oldEntries = table->entries; 4.45 - 4.46 - newTableSz = 2 * oldTableSz + 1; 4.47 - newEntries = VMS__malloc( newTableSz * sizeof(HashEntry *) ); 4.48 - 4.49 - table->tableSz = newTableSz; 4.50 - table->entries = newEntries; 4.51 - table->numEntries = 0; //about to add them all back! 4.52 - 4.53 - // move all the entries from old to new 4.54 - for( i=0; i < oldTableSz; i++ ) 4.55 - { if( oldEntries[i] != NULL ) 4.56 - { entry = oldEntries[i]; 4.57 - while( entry != NULL ) 4.58 - { nextEntry = entry->next; 4.59 - entry->next = NULL; 4.60 - putEntryIntoTable( entry, table ); //does not allocate anything 4.61 - entry = nextEntry; 4.62 - } 4.63 - } 4.64 - } 4.65 - } 4.66 - 4.67 -void 4.68 -nullOutTablesArray( HashTable *table ) 4.69 - { int i, tableSz; 4.70 - tableSz = table->tableSz; 4.71 - HashEntry ** entries = table->entries; 4.72 - for( i = 0; i < tableSz; i++ ) 4.73 - entries[ i ] = NULL; 4.74 - } 4.75 - 4.76 -unsigned int 4.77 -hashThisKey( char* s, int hashSz ) 4.78 - { unsigned int h = 0; 4.79 - unsigned int i; 4.80 - hashkey_t* key = (hashkey_t*)s; 4.81 - 4.82 - for(i=0 ; i<sizeof(hashkey_t); i++ ) 4.83 - h = key->hashable[i] + h*31; 4.84 - return h % hashSz; 4.85 - } 4.86 - 4.87 -/*Need this to be separated out, for use in both getParam and putParam 4.88 - */ 4.89 -HashEntry * 4.90 -getEntryFromTable( char *key, HashTable * table ) 4.91 - { unsigned int 4.92 - hashIndex = hashThisKey( key, table->tableSz ); 4.93 - HashEntry* 4.94 - hashEntry = table->entries[ hashIndex ]; 4.95 - for( ; hashEntry != NULL; hashEntry = hashEntry->next ) 4.96 - { 4.97 - if( memcmp( hashEntry->key, key, sizeof(hashkey_t) ) == 0 ) 4.98 - return hashEntry; 4.99 - } 4.100 - return NULL; 4.101 - } 4.102 - 4.103 -void * 4.104 -getValueFromTable( char *key, HashTable * table ) 4.105 - { HashEntry *entry; 4.106 - entry = getEntryFromTable( key, table ); 4.107 - if( entry == NULL ) return NULL; 4.108 - 4.109 - return entry->content; 4.110 - } 4.111 - 4.112 - 4.113 -/*If key already has a value, clobber the old one and replace it 4.114 - */ 4.115 - int 4.116 -addValueIntoTable( char* key, void *content, HashTable *table ) 4.117 - { unsigned int hashIdx; 4.118 - HashEntry* hashEntry; 4.119 - 4.120 - hashEntry = getEntryFromTable( key, table ); 4.121 - if( hashEntry == NULL ) 4.122 - { hashIdx = hashThisKey( key, table->tableSz ); 4.123 - hashEntry = (HashEntry*) VMS__malloc( sizeof( HashEntry ) ); 4.124 - if( hashEntry == NULL ) return 0; 4.125 - hashEntry->key = VMS__malloc( sizeof(hashkey_t) ); 4.126 - if( hashEntry->key == NULL ) return 0; 4.127 - memcpy( hashEntry->key, key, sizeof(hashkey_t) ); 4.128 - hashEntry->next = (table->entries)[hashIdx]; 4.129 - (table->entries)[hashIdx] = hashEntry; 4.130 - table->numEntries += 1; 4.131 - if( table->tableSz < table->numEntries ) doubleTableSize( table ); 4.132 - } 4.133 - else 4.134 - { (*(table->freeEntryContentFn))( hashEntry->content ); 4.135 - } 4.136 - hashEntry->content = content; 4.137 - return 1; 4.138 - } 4.139 - 4.140 - int 4.141 -putEntryIntoTable( HashEntry *entry, HashTable *table ) 4.142 - { unsigned int hashIdx; 4.143 - HashEntry* testEntry; 4.144 - 4.145 - testEntry = getEntryFromTable( entry->key, table ); 4.146 - if( testEntry == NULL ) 4.147 - { hashIdx = hashThisKey( entry->key, table->tableSz ); 4.148 - entry->next = (table->entries)[hashIdx]; 4.149 - (table->entries)[hashIdx] = entry; 4.150 - table->numEntries += 1; 4.151 - if( table->tableSz < table->numEntries ) doubleTableSize( table ); 4.152 - } 4.153 - else 4.154 - { (*(table->freeEntryContentFn))( testEntry->content ); 4.155 - //being lazy -- will create bug in code that relies on having ptr to 4.156 - // elem given to insert into table! 4.157 - testEntry->content = entry->content; 4.158 - entry->content = NULL; 4.159 - freeHashEntryButNotContent( entry ); 4.160 - } 4.161 - return 1; 4.162 - } 4.163 - 4.164 -/*Better version 4.165 - */ 4.166 - void 4.167 -untested_putEntryIntoTable( HashEntry *entry, HashTable * table ) 4.168 - { HashEntry *testEntry, *prevEntry = NULL; 4.169 - unsigned int 4.170 - hashIndex = hashThisKey( entry->key, table->tableSz ); 4.171 - 4.172 - testEntry = table->entries[ hashIndex ]; 4.173 - for( ; testEntry != NULL; testEntry = testEntry->next ) 4.174 - { if( memcmp( testEntry->key, entry->key, sizeof(hashkey_t)) == 0 ) 4.175 - { if( prevEntry == NULL ) 4.176 - { table->entries[hashIndex] = entry; 4.177 - entry->next = testEntry->next; 4.178 - } 4.179 - else 4.180 - { prevEntry->next = entry; 4.181 - entry->next = testEntry->next; 4.182 - } 4.183 - freeHashEntryUsing( testEntry, table ); //frees content too! 4.184 - return; 4.185 - } 4.186 - } 4.187 - //wasn't found, so insert 4.188 - entry->next = table->entries[hashIndex]; 4.189 - table->entries[hashIndex] = entry; 4.190 - } 4.191 - 4.192 - 4.193 - 4.194 - bool8 4.195 -deleteEntryFromTable( char *key, HashTable *table ) 4.196 - { HashEntry *hashEntry; 4.197 - HashEntry **addrOfHashEntryPtr; 4.198 - unsigned int hashIndex; 4.199 - 4.200 - hashIndex = hashThisKey( key, table->tableSz ); 4.201 - addrOfHashEntryPtr = &( table->entries[ hashIndex ] ); 4.202 - hashEntry = *addrOfHashEntryPtr; 4.203 - while( hashEntry != NULL ) 4.204 - { if( memcmp( hashEntry->key, key, sizeof(hashkey_t) ) == 0 ) 4.205 - { 4.206 - *addrOfHashEntryPtr = hashEntry->next; 4.207 - //TODO: Free the contents of entry? 4.208 - freeHashEntryButNotContent( hashEntry ); 4.209 - table->numEntries -= 1; 4.210 - return TRUE; 4.211 - } 4.212 - addrOfHashEntryPtr = &( hashEntry->next ); 4.213 - hashEntry = *addrOfHashEntryPtr; 4.214 - } 4.215 - return FALSE; 4.216 - } 4.217 - 4.218 -void 4.219 -freeHashTable( HashTable *table ) 4.220 - { int i; 4.221 - HashEntry *hashEntry, *temp, **entries; 4.222 - 4.223 - entries = table->entries; 4.224 - for( i=0; i < table->tableSz; i++ ) 4.225 - { if( entries[i] != NULL ) 4.226 - { hashEntry = entries[i]; 4.227 - while( hashEntry != NULL ) 4.228 - { 4.229 - temp = hashEntry->next; 4.230 - freeHashEntryUsing( hashEntry, table ); 4.231 - hashEntry = temp; 4.232 - } 4.233 - } 4.234 - } 4.235 - } 4.236 - 4.237 -void 4.238 -freeHashEntryUsing( HashEntry *entry, HashTable *table ) 4.239 - { 4.240 - if( entry->content != NULL ) 4.241 - (*(table->freeEntryContentFn))( entry->content ); 4.242 - VMS__free( entry->key ); //was VMS__malloc'd above, so free it 4.243 - VMS__free( entry ); 4.244 - } 4.245 - 4.246 -void 4.247 -freeHashEntryButNotContent( HashEntry *entry ) 4.248 - { 4.249 - VMS__free( entry->key ); //was VMS__malloc'd above, so free it 4.250 - VMS__free( entry ); 4.251 - } 4.252 - 4.253 +/* 4.254 + * Copyright 2009 OpenSourceStewardshipFoundation.org 4.255 + * Licensed under GNU General Public License version 2 4.256 + * 4.257 + * 4.258 + * Author: seanhalle@yahoo.com 4.259 + */ 4.260 + 4.261 +#include "PrivateHash.h" 4.262 + 4.263 + 4.264 + HashTable * 4.265 +makeHashTable( int numHashSlots, FreeEntryContentFnPtr freeFn ) 4.266 + { HashTable * retTable; 4.267 + retTable = VMS__malloc( sizeof( HashTable ) ); 4.268 + 4.269 + retTable->freeEntryContentFn = freeFn; 4.270 + 4.271 + retTable->entries = VMS__malloc( numHashSlots * sizeof(HashEntry *) ); 4.272 + retTable->numEntries = 0; 4.273 + retTable->tableSz = numHashSlots; 4.274 + 4.275 + nullOutTablesArray( retTable ); 4.276 + 4.277 + return retTable; 4.278 + } 4.279 + 4.280 + void 4.281 +doubleTableSize( HashTable *table ) 4.282 + { int i, oldTableSz, newTableSz; 4.283 + HashEntry *entry, *nextEntry, **oldEntries, **newEntries; 4.284 + 4.285 + oldTableSz = table->tableSz; 4.286 + oldEntries = table->entries; 4.287 + 4.288 + newTableSz = 2 * oldTableSz + 1; 4.289 + newEntries = VMS__malloc( newTableSz * sizeof(HashEntry *) ); 4.290 + 4.291 + table->tableSz = newTableSz; 4.292 + table->entries = newEntries; 4.293 + table->numEntries = 0; //about to add them all back! 4.294 + 4.295 + // move all the entries from old to new 4.296 + for( i=0; i < oldTableSz; i++ ) 4.297 + { if( oldEntries[i] != NULL ) 4.298 + { entry = oldEntries[i]; 4.299 + while( entry != NULL ) 4.300 + { nextEntry = entry->next; 4.301 + entry->next = NULL; 4.302 + putEntryIntoTable( entry, table ); //does not allocate anything 4.303 + entry = nextEntry; 4.304 + } 4.305 + } 4.306 + } 4.307 + } 4.308 + 4.309 +void 4.310 +nullOutTablesArray( HashTable *table ) 4.311 + { int i, tableSz; 4.312 + tableSz = table->tableSz; 4.313 + HashEntry ** entries = table->entries; 4.314 + for( i = 0; i < tableSz; i++ ) 4.315 + entries[ i ] = NULL; 4.316 + } 4.317 + 4.318 +unsigned int 4.319 +hashThisKey( char* s, int hashSz ) 4.320 + { unsigned int h = 0; 4.321 + unsigned int i; 4.322 + hashkey_t* key = (hashkey_t*)s; 4.323 + 4.324 + for(i=0 ; i<sizeof(hashkey_t); i++ ) 4.325 + h = key->hashable[i] + h*31; 4.326 + return h % hashSz; 4.327 + } 4.328 + 4.329 +/*Need this to be separated out, for use in both getParam and putParam 4.330 + */ 4.331 +HashEntry * 4.332 +getEntryFromTable( char *key, HashTable * table ) 4.333 + { unsigned int 4.334 + hashIndex = hashThisKey( key, table->tableSz ); 4.335 + HashEntry* 4.336 + hashEntry = table->entries[ hashIndex ]; 4.337 + for( ; hashEntry != NULL; hashEntry = hashEntry->next ) 4.338 + { 4.339 + if( memcmp( hashEntry->key, key, sizeof(hashkey_t) ) == 0 ) 4.340 + return hashEntry; 4.341 + } 4.342 + return NULL; 4.343 + } 4.344 + 4.345 +void * 4.346 +getValueFromTable( char *key, HashTable * table ) 4.347 + { HashEntry *entry; 4.348 + entry = getEntryFromTable( key, table ); 4.349 + if( entry == NULL ) return NULL; 4.350 + 4.351 + return entry->content; 4.352 + } 4.353 + 4.354 + 4.355 +/*If key already has a value, clobber the old one and replace it 4.356 + */ 4.357 + int 4.358 +addValueIntoTable( char* key, void *content, HashTable *table ) 4.359 + { unsigned int hashIdx; 4.360 + HashEntry* hashEntry; 4.361 + 4.362 + hashEntry = getEntryFromTable( key, table ); 4.363 + if( hashEntry == NULL ) 4.364 + { hashIdx = hashThisKey( key, table->tableSz ); 4.365 + hashEntry = (HashEntry*) VMS__malloc( sizeof( HashEntry ) ); 4.366 + if( hashEntry == NULL ) return 0; 4.367 + hashEntry->key = VMS__malloc( sizeof(hashkey_t) ); 4.368 + if( hashEntry->key == NULL ) return 0; 4.369 + memcpy( hashEntry->key, key, sizeof(hashkey_t) ); 4.370 + hashEntry->next = (table->entries)[hashIdx]; 4.371 + (table->entries)[hashIdx] = hashEntry; 4.372 + table->numEntries += 1; 4.373 + if( table->tableSz < table->numEntries ) doubleTableSize( table ); 4.374 + } 4.375 + else 4.376 + { (*(table->freeEntryContentFn))( hashEntry->content ); 4.377 + } 4.378 + hashEntry->content = content; 4.379 + return 1; 4.380 + } 4.381 + 4.382 + int 4.383 +putEntryIntoTable( HashEntry *entry, HashTable *table ) 4.384 + { unsigned int hashIdx; 4.385 + HashEntry* testEntry; 4.386 + 4.387 + testEntry = getEntryFromTable( entry->key, table ); 4.388 + if( testEntry == NULL ) 4.389 + { hashIdx = hashThisKey( entry->key, table->tableSz ); 4.390 + entry->next = (table->entries)[hashIdx]; 4.391 + (table->entries)[hashIdx] = entry; 4.392 + table->numEntries += 1; 4.393 + if( table->tableSz < table->numEntries ) doubleTableSize( table ); 4.394 + } 4.395 + else 4.396 + { (*(table->freeEntryContentFn))( testEntry->content ); 4.397 + //being lazy -- will create bug in code that relies on having ptr to 4.398 + // elem given to insert into table! 4.399 + testEntry->content = entry->content; 4.400 + entry->content = NULL; 4.401 + freeHashEntryButNotContent( entry ); 4.402 + } 4.403 + return 1; 4.404 + } 4.405 + 4.406 +/*Better version 4.407 + */ 4.408 + void 4.409 +untested_putEntryIntoTable( HashEntry *entry, HashTable * table ) 4.410 + { HashEntry *testEntry, *prevEntry = NULL; 4.411 + unsigned int 4.412 + hashIndex = hashThisKey( entry->key, table->tableSz ); 4.413 + 4.414 + testEntry = table->entries[ hashIndex ]; 4.415 + for( ; testEntry != NULL; testEntry = testEntry->next ) 4.416 + { if( memcmp( testEntry->key, entry->key, sizeof(hashkey_t)) == 0 ) 4.417 + { if( prevEntry == NULL ) 4.418 + { table->entries[hashIndex] = entry; 4.419 + entry->next = testEntry->next; 4.420 + } 4.421 + else 4.422 + { prevEntry->next = entry; 4.423 + entry->next = testEntry->next; 4.424 + } 4.425 + freeHashEntryUsing( testEntry, table ); //frees content too! 4.426 + return; 4.427 + } 4.428 + } 4.429 + //wasn't found, so insert 4.430 + entry->next = table->entries[hashIndex]; 4.431 + table->entries[hashIndex] = entry; 4.432 + } 4.433 + 4.434 + 4.435 + 4.436 + bool8 4.437 +deleteEntryFromTable( char *key, HashTable *table ) 4.438 + { HashEntry *hashEntry; 4.439 + HashEntry **addrOfHashEntryPtr; 4.440 + unsigned int hashIndex; 4.441 + 4.442 + hashIndex = hashThisKey( key, table->tableSz ); 4.443 + addrOfHashEntryPtr = &( table->entries[ hashIndex ] ); 4.444 + hashEntry = *addrOfHashEntryPtr; 4.445 + while( hashEntry != NULL ) 4.446 + { if( memcmp( hashEntry->key, key, sizeof(hashkey_t) ) == 0 ) 4.447 + { 4.448 + *addrOfHashEntryPtr = hashEntry->next; 4.449 + //TODO: Free the contents of entry? 4.450 + freeHashEntryButNotContent( hashEntry ); 4.451 + table->numEntries -= 1; 4.452 + return TRUE; 4.453 + } 4.454 + addrOfHashEntryPtr = &( hashEntry->next ); 4.455 + hashEntry = *addrOfHashEntryPtr; 4.456 + } 4.457 + return FALSE; 4.458 + } 4.459 + 4.460 +void 4.461 +freeHashTable( HashTable *table ) 4.462 + { int i; 4.463 + HashEntry *hashEntry, *temp, **entries; 4.464 + 4.465 + entries = table->entries; 4.466 + for( i=0; i < table->tableSz; i++ ) 4.467 + { if( entries[i] != NULL ) 4.468 + { hashEntry = entries[i]; 4.469 + while( hashEntry != NULL ) 4.470 + { 4.471 + temp = hashEntry->next; 4.472 + freeHashEntryUsing( hashEntry, table ); 4.473 + hashEntry = temp; 4.474 + } 4.475 + } 4.476 + } 4.477 + } 4.478 + 4.479 +void 4.480 +freeHashEntryUsing( HashEntry *entry, HashTable *table ) 4.481 + { 4.482 + if( entry->content != NULL ) 4.483 + (*(table->freeEntryContentFn))( entry->content ); 4.484 + VMS__free( entry->key ); //was VMS__malloc'd above, so free it 4.485 + VMS__free( entry ); 4.486 + } 4.487 + 4.488 +void 4.489 +freeHashEntryButNotContent( HashEntry *entry ) 4.490 + { 4.491 + VMS__free( entry->key ); //was VMS__malloc'd above, so free it 4.492 + VMS__free( entry ); 4.493 + } 4.494 +
5.1 --- a/PrivateHash.h Thu Feb 09 15:51:22 2012 +0100 5.2 +++ b/PrivateHash.h Sat Feb 11 17:55:51 2012 -0800 5.3 @@ -1,70 +1,76 @@ 5.4 -/* 5.5 - * Copyright 2009 OpenSourceStewardshipFoundation.org 5.6 - * Licensed under GNU General Public License version 2 5.7 - * 5.8 - * Author: seanhalle@yahoo.com 5.9 - */ 5.10 - 5.11 -#ifndef _PRIVATE_HASH_H 5.12 -#define _PRIVATE_HASH_H 5.13 - 5.14 -#include "../../VMS_Implementations/VMS_impl/VMS_primitive_data_types.h" 5.15 - 5.16 -#define TRUE 1 5.17 -#define FALSE 0 5.18 - 5.19 -union hashkey_t{ 5.20 - char hashable[8]; 5.21 - int parts[2]; 5.22 -}; 5.23 - 5.24 -typedef union hashkey_t hashkey_t; 5.25 - 5.26 -#define DEFAULT_HASHSIZE 1 << 10 5.27 - 5.28 -typedef struct _HashEntry HashEntry; 5.29 - 5.30 -struct _HashEntry 5.31 - { 5.32 - char *key; 5.33 - void *content; 5.34 - HashEntry *next; 5.35 - }; 5.36 - 5.37 -typedef void (*FreeEntryContentFnPtr) ( void * ); 5.38 - 5.39 -typedef struct 5.40 - { int tableSz; 5.41 - int numEntries; 5.42 - HashEntry* *entries; 5.43 - FreeEntryContentFnPtr freeEntryContentFn; 5.44 - } 5.45 -HashTable; 5.46 - 5.47 - 5.48 -//=========================================================================== 5.49 -// Public functions 5.50 -HashTable *makeHashTable( int numHashSlots, FreeEntryContentFnPtr freeFn ); 5.51 - 5.52 -int putEntryIntoTable( HashEntry *entry, HashTable *table); 5.53 -int addValueIntoTable( char* key, void *value, HashTable *table); 5.54 -HashEntry *getEntryFromTable( char *key, HashTable *table ); 5.55 -void *getValueFromTable( char *key, HashTable *table ); 5.56 - 5.57 -bool8 deleteEntryFromTable( char *key, HashTable *table ); 5.58 -bool8 deleteThisEntryFromTable( HashEntry *entry, HashTable *table ); 5.59 -bool8 deleteEntrysValueInTable( char *key, HashTable *table ); 5.60 -bool8 deleteEntryFromTableAndFreeValue( char *key, HashTable *table ); 5.61 -void freeHashTable( HashTable *table ); 5.62 -//char *paramBagToString( ParamBag * bag ) 5.63 - 5.64 -//=========================================================================== 5.65 -// Internal functions 5.66 -void freeHashEntryUsing( HashEntry *entry, HashTable *table ); 5.67 -unsigned int hashThisKey( char *s, int hashSz ); 5.68 -void nullOutTablesArray( HashTable *table ); 5.69 -void doubleTableSize( HashTable *table ); 5.70 -void freeHashEntryButNotContent( HashEntry *entry ); 5.71 - 5.72 -#endif /* _PRIVATE_HASH_H */ 5.73 - 5.74 +/* 5.75 + * Copyright 2009 OpenSourceStewardshipFoundation.org 5.76 + * Licensed under GNU General Public License version 2 5.77 + * 5.78 + * Author: seanhalle@yahoo.com 5.79 + */ 5.80 + 5.81 +#ifndef _PRIVATE_HASH_H 5.82 +#define _PRIVATE_HASH_H 5.83 + 5.84 +#include <stdio.h> 5.85 +#include <string.h> 5.86 +#include <errno.h> 5.87 +#include <stdlib.h> 5.88 + 5.89 +#include "../../VMS_Implementations/VMS_impl/VMS_primitive_data_types.h" 5.90 +#include "../../VMS_Implementations/VMS_impl/vmalloc.h" 5.91 + 5.92 +#define TRUE 1 5.93 +#define FALSE 0 5.94 + 5.95 +union hashkey_t{ 5.96 + char hashable[8]; 5.97 + int parts[2]; 5.98 +}; 5.99 + 5.100 +typedef union hashkey_t hashkey_t; 5.101 + 5.102 +#define DEFAULT_HASHSIZE 1 << 10 5.103 + 5.104 +typedef struct _HashEntry HashEntry; 5.105 + 5.106 +struct _HashEntry 5.107 + { 5.108 + char *key; 5.109 + void *content; 5.110 + HashEntry *next; 5.111 + }; 5.112 + 5.113 +typedef void (*FreeEntryContentFnPtr) ( void * ); 5.114 + 5.115 +typedef struct 5.116 + { int tableSz; 5.117 + int numEntries; 5.118 + HashEntry* *entries; 5.119 + FreeEntryContentFnPtr freeEntryContentFn; 5.120 + } 5.121 +HashTable; 5.122 + 5.123 + 5.124 +//=========================================================================== 5.125 +// Public functions 5.126 +HashTable *makeHashTable( int numHashSlots, FreeEntryContentFnPtr freeFn ); 5.127 + 5.128 +int putEntryIntoTable( HashEntry *entry, HashTable *table); 5.129 +int addValueIntoTable( char* key, void *value, HashTable *table); 5.130 +HashEntry *getEntryFromTable( char *key, HashTable *table ); 5.131 +void *getValueFromTable( char *key, HashTable *table ); 5.132 + 5.133 +bool8 deleteEntryFromTable( char *key, HashTable *table ); 5.134 +bool8 deleteThisEntryFromTable( HashEntry *entry, HashTable *table ); 5.135 +bool8 deleteEntrysValueInTable( char *key, HashTable *table ); 5.136 +bool8 deleteEntryFromTableAndFreeValue( char *key, HashTable *table ); 5.137 +void freeHashTable( HashTable *table ); 5.138 +//char *paramBagToString( ParamBag * bag ) 5.139 + 5.140 +//=========================================================================== 5.141 +// Internal functions 5.142 +void freeHashEntryUsing( HashEntry *entry, HashTable *table ); 5.143 +unsigned int hashThisKey( char *s, int hashSz ); 5.144 +void nullOutTablesArray( HashTable *table ); 5.145 +void doubleTableSize( HashTable *table ); 5.146 +void freeHashEntryButNotContent( HashEntry *entry ); 5.147 + 5.148 +#endif /* _PRIVATE_HASH_H */ 5.149 +
