Reindented server/core/hashtable.c

This commit is contained in:
Johan Wikman
2015-11-30 13:35:23 +02:00
parent 26d1cf0c1f
commit 255a5b53c1
2 changed files with 574 additions and 536 deletions

File diff suppressed because it is too large Load Diff

View File

@ -25,11 +25,11 @@
* @verbatim * @verbatim
* Revision History * Revision History
* *
* Date Who Description * Date Who Description
* 23/06/2013 Mark Riddoch Initial implementation * 23/06/2013 Mark Riddoch Initial implementation
* 23/07/2013 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 * 08/01/2014 Massimiliano Pinto Added function pointers for key/value copy and free
* the routine hashtable_memory_fns() changed accordingly * the routine hashtable_memory_fns() changed accordingly
* *
* @endverbatim * @endverbatim
*/ */
@ -44,21 +44,22 @@
* A NULL value for key indicates an empty entry. * A NULL value for key indicates an empty entry.
* The next pointer is the overflow chain for this hashentry. * The next pointer is the overflow chain for this hashentry.
*/ */
typedef struct hashentry { typedef struct hashentry
void *key; /**< The value of the key or NULL if empty entry */ {
void *value; /**< The value associated with key */ void *key; /**< The value of the key or NULL if empty entry */
struct hashentry *next; /**< The overflow chain */ void *value; /**< The value associated with key */
struct hashentry *next; /**< The overflow chain */
} HASHENTRIES; } HASHENTRIES;
/** /**
* HASHTABLE iterator - used to walk the hashtable in a thread safe * HASHTABLE iterator - used to walk the hashtable in a thread safe
* way * way
*/ */
typedef struct hashiterator { typedef struct hashiterator
struct hashtable {
*table; /**< The hashtable the iterator refers to */ struct hashtable *table; /**< The hashtable the iterator refers to */
int chain; /**< The current chain we are walking */ int chain; /**< The current chain we are walking */
int depth; /**< The current depth down the chain */ int depth; /**< The current depth down the chain */
} HASHITERATOR; } HASHITERATOR;
/** /**
@ -69,67 +70,67 @@ typedef void *(*HASHMEMORYFN)(void *);
/** /**
* The general purpose hashtable struct. * The general purpose hashtable struct.
*/ */
typedef struct hashtable { typedef struct hashtable
{
#if defined(SS_DEBUG) #if defined(SS_DEBUG)
skygw_chk_t ht_chk_top; skygw_chk_t ht_chk_top;
#endif #endif
int hashsize; /**< The number of HASHENTRIES */ int hashsize; /**< The number of HASHENTRIES */
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 kcopyfn; /**< Optional key copy function */ HASHMEMORYFN kcopyfn; /**< Optional key copy function */
HASHMEMORYFN vcopyfn; /**< Optional value copy function */ HASHMEMORYFN vcopyfn; /**< Optional value copy function */
HASHMEMORYFN kfreefn; /**< Optional key free function */ HASHMEMORYFN kfreefn; /**< Optional key free function */
HASHMEMORYFN vfreefn; /**< Optional value 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 */
bool ht_isflat; /**< Indicates whether hashtable is in stack or heap */ bool ht_isflat; /**< Indicates whether hashtable is in stack or heap */
int n_elements; /*< Number of added elements */ int n_elements; /**< Number of added elements */
#if defined(SS_DEBUG) #if defined(SS_DEBUG)
skygw_chk_t ht_chk_tail; skygw_chk_t ht_chk_tail;
#endif #endif
} HASHTABLE; } HASHTABLE;
extern HASHTABLE *hashtable_alloc(int, int (*hashfn)(), int (*cmpfn)()); extern HASHTABLE *hashtable_alloc(int, int (*hashfn)(), int (*cmpfn)());
HASHTABLE *hashtable_alloc_flat(HASHTABLE* target, HASHTABLE *hashtable_alloc_flat(HASHTABLE* target,
int size, int size,
int (*hashfn)(), int (*hashfn)(),
int (*cmpfn)()); int (*cmpfn)());
/**< Allocate a hashtable */ /**< Allocate a hashtable */
extern void hashtable_memory_fns(HASHTABLE *table, extern void hashtable_memory_fns(HASHTABLE *table,
HASHMEMORYFN kcopyfn, HASHMEMORYFN kcopyfn,
HASHMEMORYFN vcopyfn, HASHMEMORYFN vcopyfn,
HASHMEMORYFN kfreefn, HASHMEMORYFN kfreefn,
HASHMEMORYFN vfreefn); HASHMEMORYFN vfreefn);
/**< Provide an interface to control key/value memory /**< Provide an interface to control key/value memory
* manipulation * manipulation
*/ */
extern void hashtable_free(HASHTABLE *); /**< Free a hashtable */ extern void hashtable_free(HASHTABLE *); /**< Free a hashtable */
extern int hashtable_add(HASHTABLE *, void *, void *); /**< Add an entry */ extern int hashtable_add(HASHTABLE *, void *, void *); /**< Add an entry */
extern int hashtable_delete(HASHTABLE *, void *); extern int hashtable_delete(HASHTABLE *, void *);
/**< Delete an entry table */ /**< Delete an entry table */
extern void *hashtable_fetch(HASHTABLE *, void *); extern void *hashtable_fetch(HASHTABLE *, void *);
/**< Fetch the data for a given key */ /**< Fetch the data for a given key */
extern void hashtable_stats(HASHTABLE *); /**< Print statisitics */ extern void hashtable_stats(HASHTABLE *); /**< Print statisitics */
void hashtable_get_stats( void hashtable_get_stats(void* hashtable,
void* hashtable, int* hashsize,
int* hashsize, int* nelems,
int* nelems, int* longest);
int* longest); extern int hashtable_save(HASHTABLE *,
extern int hashtable_save(HASHTABLE *, char *,
char *, int (*keywrite)(int, void*),
int (*keywrite)(int, void*), int (*valuewrite)(int, void*));
int (*valuewrite)(int, void*)); extern int hashtable_load(HASHTABLE *,
extern int hashtable_load(HASHTABLE *, char *,
char *, void *(*keyread)(int),
void *(*keyread)(int), void *(*valueread)(int));
void *(*valueread)(int));
extern HASHITERATOR *hashtable_iterator(HASHTABLE *); extern HASHITERATOR *hashtable_iterator(HASHTABLE *);
/**< Allocate an iterator on the hashtable */ /**< Allocate an iterator on the hashtable */
extern void *hashtable_next(HASHITERATOR *); extern void *hashtable_next(HASHITERATOR *);
/**< Return the key of the hash table iterator */ /**< Return the key of the hash table iterator */
extern void hashtable_iterator_free(HASHITERATOR *); extern void hashtable_iterator_free(HASHITERATOR *);
extern int hashtable_size(HASHTABLE *table); extern int hashtable_size(HASHTABLE *table);
#endif #endif