]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
change hash to use flags per entry for free on keys and values
authorAnthony Minessale <anthony.minessale@gmail.com>
Fri, 26 Sep 2008 16:39:46 +0000 (16:39 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Fri, 26 Sep 2008 16:39:46 +0000 (16:39 +0000)
git-svn-id: http://svn.openzap.org/svn/openzap/trunk@566 a93c3328-9c30-0410-af19-c9cd2b2d52af

libs/openzap/src/hashtable.c
libs/openzap/src/include/hashtable.h
libs/openzap/src/include/hashtable_private.h
libs/openzap/src/zap_io.c

index 8e252339a2253573560888058e08c17c831d7c2d..bc5d76475bd26aaca552e6dbe1c20b40880297f1 100644 (file)
@@ -134,7 +134,7 @@ hashtable_count(struct hashtable *h)
 
 /*****************************************************************************/
 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 */
     unsigned int index;
@@ -153,6 +153,7 @@ hashtable_insert(struct hashtable *h, void *k, void *v)
     index = indexFor(h->tablelength,e->h);
     e->k = k;
     e->v = v;
+       e->flags = flags;
     e->next = h->table[index];
     h->table[index] = e;
     return -1;
@@ -200,7 +201,9 @@ hashtable_remove(struct hashtable *h, void *k)
                                        *pE = e->next;
                                        h->entrycount--;
                                        v = e->v;
-                                       freekey(e->k);
+                                       if (e->flags & HASHTABLE_FLAG_FREE_KEY) {
+                                               freekey(e->k);
+                                       }
                                        free(e);
                                        return v;
                                }
@@ -213,7 +216,7 @@ hashtable_remove(struct hashtable *h, void *k)
 /*****************************************************************************/
 /* destroy */
 void
-hashtable_destroy(struct hashtable *h, int free_keys, int free_values)
+hashtable_destroy(struct hashtable *h)
 {
     unsigned int i;
     struct entry *e, *f;
@@ -223,7 +226,7 @@ hashtable_destroy(struct hashtable *h, int free_keys, int free_values)
         {
             e = table[i];
             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);
index 243441c1d47c746ed5979d2ef9c9a529fc6e303a..137e9460e6255bd7f1ff03019441cf353be92c06 100644 (file)
@@ -101,8 +101,15 @@ create_hashtable(unsigned int minsize,
  * 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 
-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)            \
        int fnname (struct hashtable *h, keytype *k, valuetype *v)      \
@@ -167,7 +174,7 @@ hashtable_count(struct hashtable *h);
  */
 
 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_next(struct hashtable_iterator *i);
index 81197e6cb30b1ec339952a5c04281cec06683de3..0a20c214fdbfa59d9a92d1e33077a87dfafc254d 100644 (file)
@@ -12,6 +12,7 @@ struct entry
 {
     void *k, *v;
     unsigned int h;
+       hashtable_flag_t flags;
     struct entry *next;
 };
 
index ae21e578ecc964bf9f4d2719920633d6b474f3ca..9b69349925cd5ab49b825bd113787651b3d054ea 100644 (file)
@@ -2033,7 +2033,7 @@ static zap_status_t load_config(void)
                                                name = buf;
                                        }
                                        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_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)) {
                                zap_log(ZAP_LOG_ERROR, "Interface %s already loaded!\n", interface->name);
                        } 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);
                                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_dso_destroy(&lib);
                } else {
-                       hashtable_insert(globals.module_hash, (void *)mod->name, mod);
+                       hashtable_insert(globals.module_hash, (void *)mod->name, mod, HASHTABLE_FLAG_NONE);
                        count++;
                }
                zap_mutex_unlock(globals.mutex);
@@ -2447,9 +2447,9 @@ zap_status_t zap_global_destroy(void)
        zap_unload_modules();
 
        zap_mutex_lock(globals.mutex);
-       hashtable_destroy(globals.interface_hash, 0, 0);
-       hashtable_destroy(globals.module_hash, 0, 0);
-       hashtable_destroy(globals.span_hash, 0, 0);
+       hashtable_destroy(globals.interface_hash);
+       hashtable_destroy(globals.module_hash);
+       hashtable_destroy(globals.span_hash);
        zap_mutex_unlock(globals.mutex);
        zap_mutex_destroy(&globals.mutex);