comparison PrivateHash.c @ 34:049a8d8917c5

changed headers and PR_WL__malloc plus PR_int__malloc to PR__malloc & elim _WL versions
author Sean Halle <seanhalle@yahoo.com>
date Tue, 23 Jul 2013 07:38:54 -0700
parents 1d42a512f482
children dc1e44b0d702
comparison
equal deleted inserted replaced
18:f7dd8d2bf886 20:036970865f00
5 * 5 *
6 * Author: seanhalle@yahoo.com 6 * Author: seanhalle@yahoo.com
7 */ 7 */
8 8
9 #include "PrivateHash.h" 9 #include "PrivateHash.h"
10 10 #include "PR__common_includes/Services_offered_by_PR/Memory_Handling/vmalloc__wrapper_library.h"
11 11
12 HashTable * 12 HashTable *
13 makeHashTable( int numHashSlots, FreeEntryContentFnPtr freeFn ) 13 makeHashTable( int numHashSlots, FreeEntryContentFnPtr freeFn )
14 { HashTable * retTable; 14 { HashTable * retTable;
15 retTable = PR_int__malloc( sizeof( HashTable ) ); 15 retTable = PR__malloc( sizeof( HashTable ) );
16 16
17 retTable->freeEntryContentFn = freeFn; 17 retTable->freeEntryContentFn = freeFn;
18 18
19 retTable->entries = PR_int__malloc( numHashSlots * sizeof(HashEntry *) ); 19 retTable->entries = PR__malloc( numHashSlots * sizeof(HashEntry *) );
20 retTable->numEntries = 0;
21 retTable->tableSz = numHashSlots;
22
23 nullOutTablesArray( retTable );
24
25 return retTable;
26 }
27
28 HashTable *
29 makeHashTable_WL( int numHashSlots, FreeEntryContentFnPtr freeFn )
30 { HashTable * retTable;
31 retTable = PR_WL__malloc( sizeof( HashTable ) );
32
33 retTable->freeEntryContentFn = freeFn;
34
35 retTable->entries = PR_int__malloc( numHashSlots * sizeof(HashEntry *) );
36 retTable->numEntries = 0; 20 retTable->numEntries = 0;
37 retTable->tableSz = numHashSlots; 21 retTable->tableSz = numHashSlots;
38 22
39 nullOutTablesArray( retTable ); 23 nullOutTablesArray( retTable );
40 24
48 32
49 oldTableSz = table->tableSz; 33 oldTableSz = table->tableSz;
50 oldEntries = table->entries; 34 oldEntries = table->entries;
51 35
52 newTableSz = 2 * oldTableSz; 36 newTableSz = 2 * oldTableSz;
53 newEntries = PR_int__malloc( newTableSz * sizeof(HashEntry *) ); 37 newEntries = PR__malloc( newTableSz * sizeof(HashEntry *) );
54 38
55 table->tableSz = newTableSz; 39 table->tableSz = newTableSz;
56 table->entries = newEntries; 40 table->entries = newEntries;
57 table->numEntries = 0; //about to add them all back! 41 table->numEntries = 0; //about to add them all back!
58 42
95 makeHashEntry( char * key ) 79 makeHashEntry( char * key )
96 { HashEntry *hashEntry; 80 { HashEntry *hashEntry;
97 81
98 int32 len = strlen(key); 82 int32 len = strlen(key);
99 83
100 hashEntry = (HashEntry*) PR_int__malloc( sizeof( HashEntry ) ); 84 hashEntry = (HashEntry*) PR__malloc( sizeof( HashEntry ) );
101 if( hashEntry == NULL ) return NULL; 85 if( hashEntry == NULL ) return NULL;
102 86
103 hashEntry->key = PR_int__malloc( len + 1 ); 87 hashEntry->key = PR__malloc( len + 1 );
104 if( hashEntry->key == NULL ) return NULL; 88 if( hashEntry->key == NULL ) return NULL;
105 memcpy( hashEntry->key, key, len + 1 ); 89 memcpy( hashEntry->key, key, len + 1 );
106 hashEntry->next = NULL; 90 hashEntry->next = NULL;
107 91
108 return hashEntry; 92 return hashEntry;
260 void 244 void
261 freeHashEntryUsing( HashEntry *entry, HashTable *table ) 245 freeHashEntryUsing( HashEntry *entry, HashTable *table )
262 { 246 {
263 if( entry->content != NULL ) 247 if( entry->content != NULL )
264 (*(table->freeEntryContentFn))( entry->content ); 248 (*(table->freeEntryContentFn))( entry->content );
265 PR_int__free( entry->key ); //was PR_int__malloc'd above, so free it 249 PR__free( entry->key ); //was PR__malloc'd above, so free it
266 PR_int__free( entry ); 250 PR__free( entry );
251 }
252
253 /* obsolete -- delete
254
255 HashTable *
256 makeHashTable_WL( int numHashSlots, FreeEntryContentFnPtr freeFn )
257 { HashTable * retTable;
258 retTable = PR__malloc( sizeof( HashTable ) );
259
260 retTable->freeEntryContentFn = freeFn;
261
262 retTable->entries = PR__malloc( numHashSlots * sizeof(HashEntry *) );
263 retTable->numEntries = 0;
264 retTable->tableSz = numHashSlots;
265
266 nullOutTablesArray( retTable );
267
268 return retTable;
267 } 269 }
268 270
269 void 271 void
270 freeHashEntryUsing_WL( HashEntry *entry, HashTable *table ) 272 freeHashEntryUsing_WL( HashEntry *entry, HashTable *table )
271 { 273 {
272 if( entry->content != NULL ) 274 if( entry->content != NULL )
273 (*(table->freeEntryContentFn))( entry->content ); 275 (*(table->freeEntryContentFn))( entry->content );
274 PR_WL__free( entry->key ); //was PR_int__malloc'd above, so free it 276 PR__free( entry->key ); //was PR__malloc'd above, so free it
275 PR_WL__free( entry ); 277 PR__free( entry );
276 } 278 }
277
278 void 279 void
279 freeHashTable_WL( HashTable *table ) 280 freeHashTable_WL( HashTable *table )
280 { int i; 281 { int i;
281 HashEntry *hashEntry, *temp, **entries; 282 HashEntry *hashEntry, *temp, **entries;
282 283
291 hashEntry = temp; 292 hashEntry = temp;
292 } 293 }
293 } 294 }
294 } 295 }
295 } 296 }
296 297 */
297 298
298 void 299 void
299 freeHashEntryButNotContent( HashEntry *entry ) 300 freeHashEntryButNotContent( HashEntry *entry )
300 { 301 {
301 PR_int__free( entry->key ); //was PR_int__malloc'd above, so free it 302 PR__free( entry->key ); //was PR__malloc'd above, so free it
302 PR_int__free( entry ); 303 PR__free( entry );
303 }
304
305 void
306 freeHashEntryButNotContent_WL( HashEntry *entry )
307 {
308 PR_WL__free( entry->key ); //was PR_int__malloc'd above, so free it
309 PR_WL__free( entry );
310 } 304 }
311 305
312 306
313 307
314 308
323 HashTable * 317 HashTable *
324 makeHashTable32( int powerOf2OfSz, FreeEntryContentFnPtr freeFn ) 318 makeHashTable32( int powerOf2OfSz, FreeEntryContentFnPtr freeFn )
325 { HashTable * retTable; 319 { HashTable * retTable;
326 int32 numHashSlots; 320 int32 numHashSlots;
327 321
328 retTable = PR_int__malloc( sizeof( HashTable ) ); 322 retTable = PR__malloc( sizeof( HashTable ) );
329 323
330 numHashSlots = 1 << powerOf2OfSz; 324 numHashSlots = 1 << powerOf2OfSz;
331 retTable->hashMask = 0xffffffff >> 32 - powerOf2OfSz; 325 retTable->hashMask = 0xffffffff >> 32 - powerOf2OfSz;
332 retTable->prevHash = (int32)rand(); 326 retTable->prevHash = (int32)rand();
333 327
334 retTable->freeEntryContentFn = freeFn; 328 retTable->freeEntryContentFn = freeFn;
335 329
336 retTable->entries = PR_int__malloc( numHashSlots * sizeof(HashEntry *)); 330 retTable->entries = PR__malloc( numHashSlots * sizeof(HashEntry *));
337 retTable->numEntries = 0; 331 retTable->numEntries = 0;
338 retTable->tableSz = numHashSlots; 332 retTable->tableSz = numHashSlots;
339 333
340 nullOutTablesArray( retTable ); 334 nullOutTablesArray( retTable );
341 335
350 344
351 HashEntry * 345 HashEntry *
352 makeHashEntry32( uint32 * key ) 346 makeHashEntry32( uint32 * key )
353 { HashEntry *hashEntry; 347 { HashEntry *hashEntry;
354 348
355 hashEntry = (HashEntry*) PR_int__malloc( sizeof( HashEntry ) ); 349 hashEntry = (HashEntry*) PR__malloc( sizeof( HashEntry ) );
356 if( hashEntry == NULL ) return NULL; 350 if( hashEntry == NULL ) return NULL;
357 hashEntry->key = PR_int__malloc( sizeOfKey(key) ); 351 hashEntry->key = PR__malloc( sizeOfKey(key) );
358 if( hashEntry->key == NULL ) return NULL; 352 if( hashEntry->key == NULL ) return NULL;
359 memcpy( hashEntry->key, key, sizeOfKey(key) ); 353 memcpy( hashEntry->key, key, sizeOfKey(key) );
360 hashEntry->next = NULL; 354 hashEntry->next = NULL;
361 355
362 return hashEntry; 356 return hashEntry;
398 392
399 oldTableSz = table->tableSz; 393 oldTableSz = table->tableSz;
400 oldEntries = table->entries; 394 oldEntries = table->entries;
401 395
402 newTableSz = 2 * oldTableSz; 396 newTableSz = 2 * oldTableSz;
403 newEntries = PR_int__malloc( newTableSz * sizeof(HashEntry *) ); 397 newEntries = PR__malloc( newTableSz * sizeof(HashEntry *) );
404 398
405 table->tableSz = newTableSz; 399 table->tableSz = newTableSz;
406 table->entries = newEntries; 400 table->entries = newEntries;
407 table->numEntries = 0; //about to add them all back! 401 table->numEntries = 0; //about to add them all back!
408 402