diff --git a/server/core/hashtable.c b/server/core/hashtable.c index a014ea91a..72fc05003 100644 --- a/server/core/hashtable.c +++ b/server/core/hashtable.c @@ -49,9 +49,12 @@ * @verbatim * Revision History * - * Date Who Description - * 23/06/13 Mark Riddoch Initial implementation - * 23/07/13 Mark Riddoch Addition of hashtable iterator + * Date Who Description + * 23/06/2013 Mark Riddoch Initial implementation + * 23/07/2013 Mark Riddoch Addition of hashtable iterator + * 08/01/2014 Massimiliano Pinto Added copy and free funtion pointers for keys and values: + * it's possible to copy and free different data types via + * kcopyfn/kfreefn, vcopyfn/vfreefn * * @endverbatim */ @@ -98,8 +101,10 @@ HASHTABLE *rval; rval->hashsize = size; rval->hashfn = hashfn; rval->cmpfn = cmpfn; - rval->copyfn = nullfn; - rval->freefn = nullfn; + rval->kcopyfn = nullfn; + rval->vcopyfn = nullfn; + rval->kfreefn = nullfn; + rval->vfreefn = nullfn; rval->n_readers = 0; rval->writelock = 0; spinlock_init(&rval->spin); @@ -131,8 +136,8 @@ HASHENTRIES *entry, *ptr; while (entry) { ptr = entry->next; - table->freefn(entry->key); - table->freefn(entry->value); + table->kfreefn(entry->key); + table->vfreefn(entry->value); free(entry); entry = ptr; } @@ -144,17 +149,25 @@ HASHENTRIES *entry, *ptr; /** * Provide memory management functions to the hash table. This allows * function pointers to be registered that can make copies of the - * key and value. + * key and value and free them as well. * * @param table The hash table - * @param copyfn The copy function - * @param freefn The free function + * @param kcopyfn The copy function for the key + * @param vcopyfn The copy function for the value + * @param kfreefn The free function for the key + * @param vfreefn The free function for the value */ void -hashtable_memory_fns(HASHTABLE *table, HASHMEMORYFN copyfn, HASHMEMORYFN freefn) +hashtable_memory_fns(HASHTABLE *table, HASHMEMORYFN kcopyfn, HASHMEMORYFN vcopyfn, HASHMEMORYFN kfreefn, HASHMEMORYFN vfreefn) { - table->copyfn = copyfn; - table->freefn = freefn; + if (kcopyfn != NULL) + table->kcopyfn = kcopyfn; + if (vcopyfn != NULL) + table->vcopyfn = vcopyfn; + if (kfreefn != NULL) + table->kfreefn = kfreefn; + if (vfreefn != NULL) + table->vfreefn = vfreefn; } /** @@ -196,8 +209,8 @@ hashtable_add(HASHTABLE *table, void *key, void *value) hashtable_write_unlock(table); return 0; } - ptr->key = table->copyfn(key); - ptr->value = table->copyfn(value); + ptr->key = table->kcopyfn(key); + ptr->value = table->vcopyfn(value); ptr->next = table->entries[hashkey % table->hashsize]; table->entries[hashkey % table->hashsize] = ptr; } @@ -235,8 +248,8 @@ HASHENTRIES *entry, *ptr; { /* We are removing from the first entry */ table->entries[hashkey % table->hashsize] = entry->next; - table->freefn(entry->key); - table->freefn(entry->value); + table->kfreefn(entry->key); + table->vfreefn(entry->value); if (entry->next != NULL) { entry->key = entry->next->key; @@ -258,8 +271,8 @@ HASHENTRIES *entry, *ptr; return 0; /* This should never happen */ } ptr->next = entry->next; - table->freefn(entry->key); - table->freefn(entry->value); + table->kfreefn(entry->key); + table->vfreefn(entry->value); free(entry); } hashtable_write_unlock(table); diff --git a/server/core/users.c b/server/core/users.c index bcb894763..ff0f8fa88 100644 --- a/server/core/users.c +++ b/server/core/users.c @@ -27,8 +27,11 @@ * @verbatim * Revision History * - * Date Who Description - * 23/06/13 Mark Riddoch Initial implementation + * Date Who Description + * 23/06/2013 Mark Riddoch Initial implementation + * 08/01/2014 Massimiliano Pinto In user_alloc now we can pass function pointers for + * copying/freeing keys and values independently via + * hashtable_memory_fns() routine * * @endverbatim */ @@ -64,7 +67,7 @@ USERS *rval; return NULL; } - hashtable_memory_fns(rval->data, (HASHMEMORYFN)strdup, (HASHMEMORYFN)free); + hashtable_memory_fns(rval->data, (HASHMEMORYFN)strdup, (HASHMEMORYFN)strdup, (HASHMEMORYFN)free, (HASHMEMORYFN)free); return rval; } diff --git a/server/include/hashtable.h b/server/include/hashtable.h index ab95b205a..92bc71b8e 100644 --- a/server/include/hashtable.h +++ b/server/include/hashtable.h @@ -25,9 +25,11 @@ * @verbatim * Revision History * - * Date Who Description - * 23/06/13 Mark Riddoch Initial implementation - * 23/07/13 Mark Riddoch Addition of iterator mechanism + * Date Who Description + * 23/06/2013 Mark Riddoch Initial implementation + * 23/07/2013 Mark Riddoch Addition of iterator mechanism + * 08/01/2014 Massimiliano Pinto Added function pointers for key/value copy and free + * the routine hashtable_memory_fns() changed accordingly * * @endverbatim */ @@ -75,8 +77,10 @@ typedef struct hashtable { HASHENTRIES **entries; /**< The entries themselves */ int (*hashfn)(void *); /**< The hash function */ int (*cmpfn)(void *, void *); /**< The key comparison function */ - HASHMEMORYFN copyfn; /**< Optional copy function */ - HASHMEMORYFN freefn; /**< Optional free function */ + HASHMEMORYFN kcopyfn; /**< Optional key copy function */ + HASHMEMORYFN vcopyfn; /**< Optional value copy function */ + HASHMEMORYFN kfreefn; /**< Optional key free function */ + HASHMEMORYFN vfreefn; /**< Optional value free function */ SPINLOCK spin; /**< Internal spinlock for the hashtable */ int n_readers; /**< Number of clients reading the table */ int writelock; /**< The table is locked by a writer */ @@ -87,7 +91,7 @@ typedef struct hashtable { extern HASHTABLE *hashtable_alloc(int, int (*hashfn)(), int (*cmpfn)()); /**< Allocate a hashtable */ -extern void hashtable_memory_fns(HASHTABLE *, HASHMEMORYFN, HASHMEMORYFN); +extern void hashtable_memory_fns(HASHTABLE *, HASHMEMORYFN, HASHMEMORYFN, HASHMEMORYFN, HASHMEMORYFN); /**< Provide an interface to control key/value memory * manipulation */