change hash to use flags per entry for free on keys and values

git-svn-id: http://svn.openzap.org/svn/openzap/trunk@566 a93c3328-9c30-0410-af19-c9cd2b2d52af
This commit is contained in:
Anthony Minessale 2008-09-26 16:39:46 +00:00
parent cf7125c118
commit dfc4c3533e
4 changed files with 23 additions and 12 deletions

View File

@ -134,7 +134,7 @@ hashtable_count(struct hashtable *h)
/*****************************************************************************/ /*****************************************************************************/
int int
hashtable_insert(struct hashtable *h, void *k, void *v) hashtable_insert(struct hashtable *h, void *k, void *v, hashtable_flag_t flags)
{ {
/* This method allows duplicate keys - but they shouldn't be used */ /* This method allows duplicate keys - but they shouldn't be used */
unsigned int index; unsigned int index;
@ -153,6 +153,7 @@ hashtable_insert(struct hashtable *h, void *k, void *v)
index = indexFor(h->tablelength,e->h); index = indexFor(h->tablelength,e->h);
e->k = k; e->k = k;
e->v = v; e->v = v;
e->flags = flags;
e->next = h->table[index]; e->next = h->table[index];
h->table[index] = e; h->table[index] = e;
return -1; return -1;
@ -200,7 +201,9 @@ hashtable_remove(struct hashtable *h, void *k)
*pE = e->next; *pE = e->next;
h->entrycount--; h->entrycount--;
v = e->v; v = e->v;
freekey(e->k); if (e->flags & HASHTABLE_FLAG_FREE_KEY) {
freekey(e->k);
}
free(e); free(e);
return v; return v;
} }
@ -213,7 +216,7 @@ hashtable_remove(struct hashtable *h, void *k)
/*****************************************************************************/ /*****************************************************************************/
/* destroy */ /* destroy */
void void
hashtable_destroy(struct hashtable *h, int free_keys, int free_values) hashtable_destroy(struct hashtable *h)
{ {
unsigned int i; unsigned int i;
struct entry *e, *f; struct entry *e, *f;
@ -223,7 +226,7 @@ hashtable_destroy(struct hashtable *h, int free_keys, int free_values)
{ {
e = table[i]; e = table[i];
while (NULL != e) while (NULL != e)
{ f = e; e = e->next; if (free_keys) freekey(f->k); if (free_values) free(f->v); free(f); } { f = e; e = e->next; if (f->flags & HASHTABLE_FLAG_FREE_KEY) freekey(f->k); if (f->flags & HASHTABLE_FLAG_FREE_VALUE) free(f->v); free(f); }
} }
free(h->table); free(h->table);

View File

@ -101,8 +101,15 @@ create_hashtable(unsigned int minsize,
* If in doubt, remove before insert. * If in doubt, remove before insert.
*/ */
typedef enum {
HASHTABLE_FLAG_NONE = 0,
HASHTABLE_FLAG_FREE_KEY = (1 << 0),
HASHTABLE_FLAG_FREE_VALUE = (1 << 1)
} hashtable_flag_t;
int int
hashtable_insert(struct hashtable *h, void *k, void *v); hashtable_insert(struct hashtable *h, void *k, void *v, hashtable_flag_t flags);
#define DEFINE_HASHTABLE_INSERT(fnname, keytype, valuetype) \ #define DEFINE_HASHTABLE_INSERT(fnname, keytype, valuetype) \
int fnname (struct hashtable *h, keytype *k, valuetype *v) \ int fnname (struct hashtable *h, keytype *k, valuetype *v) \
@ -167,7 +174,7 @@ hashtable_count(struct hashtable *h);
*/ */
void void
hashtable_destroy(struct hashtable *h, int free_keys, int free_values); hashtable_destroy(struct hashtable *h);
struct hashtable_iterator *hashtable_first(struct hashtable *h); struct hashtable_iterator *hashtable_first(struct hashtable *h);
struct hashtable_iterator *hashtable_next(struct hashtable_iterator *i); struct hashtable_iterator *hashtable_next(struct hashtable_iterator *i);

View File

@ -12,6 +12,7 @@ struct entry
{ {
void *k, *v; void *k, *v;
unsigned int h; unsigned int h;
hashtable_flag_t flags;
struct entry *next; struct entry *next;
}; };

View File

@ -2033,7 +2033,7 @@ static zap_status_t load_config(void)
name = buf; name = buf;
} }
span->name = strdup(name); span->name = strdup(name);
hashtable_insert(globals.span_hash, (void *)span->name, span); hashtable_insert(globals.span_hash, (void *)span->name, span, HASHTABLE_FLAG_NONE);
zap_mutex_unlock(globals.mutex); zap_mutex_unlock(globals.mutex);
zap_log(ZAP_LOG_DEBUG, "created span %d (%s) of type %s\n", span->span_id, span->name, type); zap_log(ZAP_LOG_DEBUG, "created span %d (%s) of type %s\n", span->span_id, span->name, type);
@ -2212,7 +2212,7 @@ int zap_load_module(const char *name)
if (hashtable_search(globals.interface_hash, (void *)interface->name)) { if (hashtable_search(globals.interface_hash, (void *)interface->name)) {
zap_log(ZAP_LOG_ERROR, "Interface %s already loaded!\n", interface->name); zap_log(ZAP_LOG_ERROR, "Interface %s already loaded!\n", interface->name);
} else { } else {
hashtable_insert(globals.interface_hash, (void *)interface->name, interface); hashtable_insert(globals.interface_hash, (void *)interface->name, interface, HASHTABLE_FLAG_NONE);
process_module_config(interface); process_module_config(interface);
x++; x++;
} }
@ -2245,7 +2245,7 @@ int zap_load_module(const char *name)
zap_log(ZAP_LOG_ERROR, "Module %s already loaded!\n", mod->name); zap_log(ZAP_LOG_ERROR, "Module %s already loaded!\n", mod->name);
zap_dso_destroy(&lib); zap_dso_destroy(&lib);
} else { } else {
hashtable_insert(globals.module_hash, (void *)mod->name, mod); hashtable_insert(globals.module_hash, (void *)mod->name, mod, HASHTABLE_FLAG_NONE);
count++; count++;
} }
zap_mutex_unlock(globals.mutex); zap_mutex_unlock(globals.mutex);
@ -2447,9 +2447,9 @@ zap_status_t zap_global_destroy(void)
zap_unload_modules(); zap_unload_modules();
zap_mutex_lock(globals.mutex); zap_mutex_lock(globals.mutex);
hashtable_destroy(globals.interface_hash, 0, 0); hashtable_destroy(globals.interface_hash);
hashtable_destroy(globals.module_hash, 0, 0); hashtable_destroy(globals.module_hash);
hashtable_destroy(globals.span_hash, 0, 0); hashtable_destroy(globals.span_hash);
zap_mutex_unlock(globals.mutex); zap_mutex_unlock(globals.mutex);
zap_mutex_destroy(&globals.mutex); zap_mutex_destroy(&globals.mutex);