New routines for key/value copy and free in hash tables
With the routines for key/value copy and free it is possible to duplicate or free different data types in the hash tables. The user_alloc() routine is the first example.
This commit is contained in:
@ -49,9 +49,12 @@
|
|||||||
* @verbatim
|
* @verbatim
|
||||||
* Revision History
|
* Revision History
|
||||||
*
|
*
|
||||||
* Date Who Description
|
* Date Who Description
|
||||||
* 23/06/13 Mark Riddoch Initial implementation
|
* 23/06/2013 Mark Riddoch Initial implementation
|
||||||
* 23/07/13 Mark Riddoch Addition of hashtable iterator
|
* 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
|
* @endverbatim
|
||||||
*/
|
*/
|
||||||
@ -98,8 +101,10 @@ HASHTABLE *rval;
|
|||||||
rval->hashsize = size;
|
rval->hashsize = size;
|
||||||
rval->hashfn = hashfn;
|
rval->hashfn = hashfn;
|
||||||
rval->cmpfn = cmpfn;
|
rval->cmpfn = cmpfn;
|
||||||
rval->copyfn = nullfn;
|
rval->kcopyfn = nullfn;
|
||||||
rval->freefn = nullfn;
|
rval->vcopyfn = nullfn;
|
||||||
|
rval->kfreefn = nullfn;
|
||||||
|
rval->vfreefn = nullfn;
|
||||||
rval->n_readers = 0;
|
rval->n_readers = 0;
|
||||||
rval->writelock = 0;
|
rval->writelock = 0;
|
||||||
spinlock_init(&rval->spin);
|
spinlock_init(&rval->spin);
|
||||||
@ -131,8 +136,8 @@ HASHENTRIES *entry, *ptr;
|
|||||||
while (entry)
|
while (entry)
|
||||||
{
|
{
|
||||||
ptr = entry->next;
|
ptr = entry->next;
|
||||||
table->freefn(entry->key);
|
table->kfreefn(entry->key);
|
||||||
table->freefn(entry->value);
|
table->vfreefn(entry->value);
|
||||||
free(entry);
|
free(entry);
|
||||||
entry = ptr;
|
entry = ptr;
|
||||||
}
|
}
|
||||||
@ -144,17 +149,25 @@ HASHENTRIES *entry, *ptr;
|
|||||||
/**
|
/**
|
||||||
* Provide memory management functions to the hash table. This allows
|
* Provide memory management functions to the hash table. This allows
|
||||||
* function pointers to be registered that can make copies of the
|
* 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 table The hash table
|
||||||
* @param copyfn The copy function
|
* @param kcopyfn The copy function for the key
|
||||||
* @param freefn The free function
|
* @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
|
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;
|
if (kcopyfn != NULL)
|
||||||
table->freefn = freefn;
|
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);
|
hashtable_write_unlock(table);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
ptr->key = table->copyfn(key);
|
ptr->key = table->kcopyfn(key);
|
||||||
ptr->value = table->copyfn(value);
|
ptr->value = table->vcopyfn(value);
|
||||||
ptr->next = table->entries[hashkey % table->hashsize];
|
ptr->next = table->entries[hashkey % table->hashsize];
|
||||||
table->entries[hashkey % table->hashsize] = ptr;
|
table->entries[hashkey % table->hashsize] = ptr;
|
||||||
}
|
}
|
||||||
@ -235,8 +248,8 @@ HASHENTRIES *entry, *ptr;
|
|||||||
{
|
{
|
||||||
/* We are removing from the first entry */
|
/* We are removing from the first entry */
|
||||||
table->entries[hashkey % table->hashsize] = entry->next;
|
table->entries[hashkey % table->hashsize] = entry->next;
|
||||||
table->freefn(entry->key);
|
table->kfreefn(entry->key);
|
||||||
table->freefn(entry->value);
|
table->vfreefn(entry->value);
|
||||||
|
|
||||||
if (entry->next != NULL) {
|
if (entry->next != NULL) {
|
||||||
entry->key = entry->next->key;
|
entry->key = entry->next->key;
|
||||||
@ -258,8 +271,8 @@ HASHENTRIES *entry, *ptr;
|
|||||||
return 0; /* This should never happen */
|
return 0; /* This should never happen */
|
||||||
}
|
}
|
||||||
ptr->next = entry->next;
|
ptr->next = entry->next;
|
||||||
table->freefn(entry->key);
|
table->kfreefn(entry->key);
|
||||||
table->freefn(entry->value);
|
table->vfreefn(entry->value);
|
||||||
free(entry);
|
free(entry);
|
||||||
}
|
}
|
||||||
hashtable_write_unlock(table);
|
hashtable_write_unlock(table);
|
||||||
|
|||||||
@ -27,8 +27,11 @@
|
|||||||
* @verbatim
|
* @verbatim
|
||||||
* Revision History
|
* Revision History
|
||||||
*
|
*
|
||||||
* Date Who Description
|
* Date Who Description
|
||||||
* 23/06/13 Mark Riddoch Initial implementation
|
* 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
|
* @endverbatim
|
||||||
*/
|
*/
|
||||||
@ -64,7 +67,7 @@ USERS *rval;
|
|||||||
return NULL;
|
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;
|
return rval;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,9 +25,11 @@
|
|||||||
* @verbatim
|
* @verbatim
|
||||||
* Revision History
|
* Revision History
|
||||||
*
|
*
|
||||||
* Date Who Description
|
* Date Who Description
|
||||||
* 23/06/13 Mark Riddoch Initial implementation
|
* 23/06/2013 Mark Riddoch Initial implementation
|
||||||
* 23/07/13 Mark Riddoch Addition of iterator mechanism
|
* 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
|
* @endverbatim
|
||||||
*/
|
*/
|
||||||
@ -75,8 +77,10 @@ typedef struct hashtable {
|
|||||||
HASHENTRIES **entries; /**< The entries themselves */
|
HASHENTRIES **entries; /**< The entries themselves */
|
||||||
int (*hashfn)(void *); /**< The hash function */
|
int (*hashfn)(void *); /**< The hash function */
|
||||||
int (*cmpfn)(void *, void *); /**< The key comparison function */
|
int (*cmpfn)(void *, void *); /**< The key comparison function */
|
||||||
HASHMEMORYFN copyfn; /**< Optional copy function */
|
HASHMEMORYFN kcopyfn; /**< Optional key copy function */
|
||||||
HASHMEMORYFN freefn; /**< Optional free 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 */
|
SPINLOCK spin; /**< Internal spinlock for the hashtable */
|
||||||
int n_readers; /**< Number of clients reading the table */
|
int n_readers; /**< Number of clients reading the table */
|
||||||
int writelock; /**< The table is locked by a writer */
|
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)());
|
extern HASHTABLE *hashtable_alloc(int, int (*hashfn)(), int (*cmpfn)());
|
||||||
/**< Allocate a hashtable */
|
/**< 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
|
/**< Provide an interface to control key/value memory
|
||||||
* manipulation
|
* manipulation
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user