IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 23, 2004, 1:00:17 PM (22 years ago)
Author:
desonia
Message:

Changed the means of deallocation of memory. ps_Alloc now registers an optional free function to handle any additional processing of a memory object.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sysUtils/psHash.c

    r964 r1073  
    1010 *  @author George Gusciora, MHPCC
    1111 *   
    12  *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2004-06-10 00:09:55 $
     12 *  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-06-23 23:00:15 $
    1414 *
    1515 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1818#include <stdio.h>
    1919#include <string.h>
     20#include <stdbool.h>
    2021#include "psHash.h"
    2122#include "psMemory.h"
     
    2425#include "psAbort.h"
    2526
     27static psHashBucket *hashBucketAlloc(const char *key,void *data,psHashBucket *next);
     28static void hashBucketFree(psHashBucket *bucket);
     29static void *doHashWork(psHash* table, const char* key, void* data, bool remove
     30                           );
     31static void hashFree(psHash *table);
    2632
    2733/******************************************************************************
     
    8793    // Allocate memory for the new hash bucket.
    8894    psHashBucket *bucket = psAlloc(sizeof(psHashBucket));
     95    p_psMemSetDeallocator(bucket,(psFreeFcn)hashBucketFree);
    8996
    9097    // Initialize the bucket.
     
    105112
    106113/******************************************************************************
    107 hashBucketFree(bucket, itemFree): This procedure deallocates the specified
    108 hash bucket.  If "itemFree" is NULL, then we simply free the data with the
    109 standard psFree() function.  If "itemFree" is not NULL, then it must be a
    110 function pointer which takes the hash bucket as a parameter, and frees the
    111 data in that hash bucket.
     114hashBucketFree(bucket): This procedure deallocates the specified
     115hash bucket. 
    112116Inputs:
    113117    bucket: the hash bucket to be freed.
    114     itemFree: a function pointer, possibly NULL.
    115118Return:
    116119    NONE
    117120 *****************************************************************************/
    118 static void hashBucketFree(psHashBucket *bucket,   // bucket to free
    119                            void (*itemFree)(void *item)) // how to free data;
    120 {
    121     if (bucket == NULL)
    122     {
     121static void hashBucketFree(psHashBucket *bucket)
     122{
     123    if (bucket == NULL) {
    123124        return;
    124125    }
     
    126127    // A bucket is actually a linked list of buckets.  We recursively step
    127128    // through that linked list, free each bucket.
    128     if (bucket->next != NULL)
    129     {
    130         hashBucketFree(bucket, itemFree);
    131     }
     129    psFree(bucket->next);
    132130
    133131    psFree(bucket->key);
    134     psMemDecrRefCounter(bucket->data);
    135 
    136     if (itemFree != NULL)
    137     {
    138         itemFree(bucket->data);
    139     } else
    140     {
    141         psFree(bucket->data);
    142     }
    143 
    144     psFree(bucket);
     132
     133    psFree(bucket->data);
    145134}
    146135
     
    159148    // Create the new hash table.
    160149    psHash *table = psAlloc(sizeof(psHash));
     150    p_psMemSetDeallocator(table,(psFreeFcn)hashFree);
    161151
    162152    // Allocate memory for the buckets.
     
    185175Inputs:
    186176    table: a hash table
    187     itemFree: a function pointer, possibly NULL.
    188177Return:
    189178    NONE
    190179 *****************************************************************************/
    191 void psHashFree(psHash *table,  // hash table to be freed
    192                 void (*itemFree)(void *item)) // how to free hashed data; or NULL
     180static void hashFree(psHash *table)
    193181{
    194182    psHashBucket *tmp = NULL;           // Used to step through linked list.
     
    196184    int i = 0;                          // Loop index variable.
    197185
    198     if (table == NULL)
    199     {
     186    if (table == NULL) {
    200187        return;
    201188    }
     
    204191    // NULL, then free the bucket via a function call to hashBucketFree();
    205192
    206     for (i = 0; i < table->nbucket; i++)
    207     {
     193    for (i = 0; i < table->nbucket; i++) {
    208194        // A bucket is composed of a linked list of buckets.  We use the
    209195        // "tmp" and "ptr" pointers to step through that list and free each
     
    214200            while (ptr != NULL) {
    215201                tmp = ptr->next;
    216                 hashBucketFree(ptr, itemFree);
     202                psFree(ptr);
    217203                ptr = tmp;
    218204            }
     
    222208    // Free the bucket structure, then the hash table.
    223209    psFree(table->buckets);
    224     psFree(table);
    225 }
    226 
    227 /******************************************************************************
    228 doHashWork(table, key, data, remove, itemFree): This is an internal
     210}
     211
     212/******************************************************************************
     213doHashWork(table, key, data, remove): This is an internal
    229214procedure which does the bulk of the work in using the hash table.  Depending
    230215upon the input parameters, it will either insert a new key/data into the hash
    231216table, retrieve the data for a specified key, or remove a key/data item.  If
    232 we try to insert a key that already exists in the hash table, then we call
    233 the user-supplied function itemfree (or psFree if that is NULL), to free the
    234 existing data/key item.
     217we try to insert a key that already exists in the hash table, then we deallocate
     218the existing data/key item.
    235219Inputs:
    236220    table: a hash table
     
    238222    data: the data to insert, if not NULL
    239223    remove: set to non-zero if the key/data should be removed from the table.
    240     itemFree: function pointer
    241224Return:
    242225    NONE
     
    246229there is little common code between those functions.
    247230  *****************************************************************************/
    248 static void *doHashWork(psHash     *table,   // table to insert in
    249                         const char *key,     // key to use
    250                         void       *data,    // data to insert, or (if NULL) retrieve/remove
    251                         int remove
    252                             ,          // remove the item from the list?
    253                             void (*itemFree)(void *item)) // how to free hashed data
     231static void *doHashWork(psHash *table, const char *key, void *data, bool remove
     232                           )
    254233{
    255234    long int hash = 1;                  // This will contain an integer value
     
    313292                    }
    314293
    315                     psFree(ptr->key);
    316294                    psFree(ptr);
    317295
    318296                    // By definition, the data associated with that key
    319297                    // must be returned, not freed.
    320                     return psMemDecrRefCounter(data);
     298                    return data;
    321299                }
    322300                optr = ptr;
     
    354332                // the new data was not inserted into the hash table.
    355333
    356                 if (itemFree == NULL) {
    357                     psFree(psMemDecrRefCounter(ptr->data));
    358                 } else {
    359                     itemFree(psMemDecrRefCounter(ptr->data));
    360                 }
     334                psFree(ptr->data);
    361335
    362336                ptr->data = psMemIncrRefCounter(data);
     
    385359    NONE
    386360 *****************************************************************************/
    387 void *psHashInsert(psHash *table,   // table to insert in
    388                    const char *key, // key to use
    389                    void *data,      // data to insert
    390                    void (*itemFree)(void *item)) // how to free hashed data;
     361void *psHashInsert(psHash *table, const char *key, void *data)
    391362{
    392363    if (table == NULL) {
     
    400371    }
    401372
    402     return doHashWork(table, key, data, 0, itemFree);
     373    return doHashWork(table, key, data, 0);
    403374}
    404375
     
    425396
    426397
    427     return doHashWork(table, key, NULL, 0, NULL);
     398    return doHashWork(table, key, NULL, 0);
    428399}
    429400
     
    438409    The data that was associated with that key.
    439410 *****************************************************************************/
    440 void *psHashRemove(psHash *table,   // table to lookup key in
    441                    const char *key, // key to lookup
    442                    void (*itemFree)(void *item)) // how to free hashed data;
     411void *psHashRemove(psHash *table, const char *key)
    443412{
    444413    if (table == NULL) {
     
    449418    }
    450419
    451     return doHashWork(table, key, NULL, 1, itemFree);
    452 }
     420    return doHashWork(table, key, NULL, 1);
     421}
Note: See TracChangeset for help on using the changeset viewer.