]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Remove deallocator parameter from hash functions
authorDaniel P. Berrange <berrange@redhat.com>
Tue, 22 Feb 2011 15:11:59 +0000 (15:11 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Fri, 25 Feb 2011 13:00:46 +0000 (13:00 +0000)
Since the deallocator is passed into the constructor of
a hash table it is not desirable to pass it into each
function again. Remove it from all functions, but provide
a virHashSteal to allow a item to be removed from a hash
table without deleteing it.

* src/util/hash.c, src/util/hash.h: Remove deallocator
  param from all functions. Add virHashSteal
* src/libvirt_private.syms: Add virHashSteal
* src/conf/domain_conf.c, src/conf/nwfilter_params.c,
  src/nwfilter/nwfilter_learnipaddr.c,
  src/qemu/qemu_command.c, src/xen/xm_internal.c: Update
  for changed hash API

src/conf/domain_conf.c
src/conf/nwfilter_params.c
src/libvirt_private.syms
src/nwfilter/nwfilter_learnipaddr.c
src/qemu/qemu_command.c
src/util/hash.c
src/util/hash.h
src/xen/xm_internal.c

index b97c1f016f85dc2b56cc6c69c9e9330842be37dd..8d69237e5fb5f93d5fb1b858653dd38647ae7399 100644 (file)
@@ -392,7 +392,7 @@ VIR_ENUM_IMPL(virDomainTimerMode, VIR_DOMAIN_TIMER_MODE_LAST,
 #define VIR_DOMAIN_XML_READ_FLAGS   VIR_DOMAIN_XML_INACTIVE
 
 static void
-virDomainObjListDeallocator(void *payload, const char *name ATTRIBUTE_UNUSED)
+virDomainObjListDataFree(void *payload, const char *name ATTRIBUTE_UNUSED)
 {
     virDomainObjPtr obj = payload;
     virDomainObjLock(obj);
@@ -402,7 +402,7 @@ virDomainObjListDeallocator(void *payload, const char *name ATTRIBUTE_UNUSED)
 
 int virDomainObjListInit(virDomainObjListPtr doms)
 {
-    doms->objs = virHashCreate(50, virDomainObjListDeallocator);
+    doms->objs = virHashCreate(50, virDomainObjListDataFree);
     if (!doms->objs)
         return -1;
     return 0;
@@ -1143,7 +1143,7 @@ void virDomainRemoveInactive(virDomainObjListPtr doms,
 
     virDomainObjUnlock(dom);
 
-    virHashRemoveEntry(doms->objs, uuidstr, virDomainObjListDeallocator);
+    virHashRemoveEntry(doms->objs, uuidstr);
 }
 
 
@@ -8753,8 +8753,8 @@ virDomainSnapshotObjPtr virDomainSnapshotAssignDef(virDomainSnapshotObjListPtr s
 
 /* Snapshot Obj List functions */
 static void
-virDomainSnapshotObjListDeallocator(void *payload,
-                                    const char *name ATTRIBUTE_UNUSED)
+virDomainSnapshotObjListDataFree(void *payload,
+                                 const char *name ATTRIBUTE_UNUSED)
 {
     virDomainSnapshotObjPtr obj = payload;
 
@@ -8763,7 +8763,7 @@ virDomainSnapshotObjListDeallocator(void *payload,
 
 int virDomainSnapshotObjListInit(virDomainSnapshotObjListPtr snapshots)
 {
-    snapshots->objs = virHashCreate(50, virDomainSnapshotObjListDeallocator);
+    snapshots->objs = virHashCreate(50, virDomainSnapshotObjListDataFree);
     if (!snapshots->objs)
         return -1;
     return 0;
@@ -8860,8 +8860,7 @@ virDomainSnapshotObjPtr virDomainSnapshotFindByName(const virDomainSnapshotObjLi
 void virDomainSnapshotObjListRemove(virDomainSnapshotObjListPtr snapshots,
                                     virDomainSnapshotObjPtr snapshot)
 {
-    virHashRemoveEntry(snapshots->objs, snapshot->def->name,
-                       virDomainSnapshotObjListDeallocator);
+    virHashRemoveEntry(snapshots->objs, snapshot->def->name);
 }
 
 struct snapshot_has_children {
index cd94b30949f7412da259ef0808bd6b16bf785697..ad64c8c24847870a4684d192ee95bfa0558fae14 100644 (file)
@@ -35,7 +35,7 @@
 #define VIR_FROM_THIS VIR_FROM_NWFILTER
 
 static void
-hashDealloc(void *payload, const char *name ATTRIBUTE_UNUSED)
+hashDataFree(void *payload, const char *name ATTRIBUTE_UNUSED)
 {
     VIR_FREE(payload);
 }
@@ -80,7 +80,7 @@ virNWFilterHashTablePut(virNWFilterHashTablePtr table,
             return 1;
         }
     } else {
-        if (virHashUpdateEntry(table->hashTable, name, val, hashDealloc) != 0) {
+        if (virHashUpdateEntry(table->hashTable, name, val) != 0) {
             return 1;
         }
     }
@@ -120,7 +120,7 @@ virNWFilterHashTableCreate(int n) {
         virReportOOMError();
         return NULL;
     }
-    ret->hashTable = virHashCreate(n, hashDealloc);
+    ret->hashTable = virHashCreate(n, hashDataFree);
     if (!ret->hashTable) {
         VIR_FREE(ret);
         return NULL;
@@ -134,7 +134,7 @@ virNWFilterHashTableRemoveEntry(virNWFilterHashTablePtr ht,
                                 const char *entry)
 {
     int i;
-    int rc = virHashRemoveEntry(ht->hashTable, entry, hashDealloc);
+    int rc = virHashRemoveEntry(ht->hashTable, entry);
 
     if (rc == 0) {
         for (i = 0; i < ht->nNames; i++) {
index 66917ca70abeec243e77b33ef846b702f45c650a..55109f2f1f5e7575e55ca272ebb42129dad2b1f9 100644 (file)
@@ -413,6 +413,7 @@ virHashRemoveEntry;
 virHashRemoveSet;
 virHashSearch;
 virHashSize;
+virHashSteal;
 
 
 # hooks.h
index 8bd7b50b6699f45c1c5acace42b8f567d3327d27..8f6263a5bf8a6505c820ce2a4af4d116e00e08ce 100644 (file)
@@ -207,7 +207,7 @@ virNWFilterUnlockIface(const char *ifname) {
 
         ifaceLock->refctr--;
         if (ifaceLock->refctr == 0)
-            virHashRemoveEntry(ifaceLockMap, ifname, freeIfaceLock);
+            virHashRemoveEntry(ifaceLockMap, ifname);
     }
 
     virMutexUnlock(&ifaceMapLock);
@@ -301,10 +301,7 @@ virNWFilterDeregisterLearnReq(int ifindex) {
 
     virMutexLock(&pendingLearnReqLock);
 
-    res = virHashLookup(pendingLearnReq, ifindex_str);
-
-    if (res)
-        virHashRemoveEntry(pendingLearnReq, ifindex_str, NULL);
+    res = virHashSteal(pendingLearnReq, ifindex_str);
 
     virMutexUnlock(&pendingLearnReqLock);
 
index ddd218f308bf5a86aa9ff2aef1e9ea833b1f4a9a..4bf80d01e7af26f5e8506707e0b181d8518e6eee 100644 (file)
@@ -842,7 +842,7 @@ int qemuDomainPCIAddressReleaseAddr(qemuDomainPCIAddressSetPtr addrs,
     if (!addr)
         return -1;
 
-    ret = virHashRemoveEntry(addrs->used, addr, qemuDomainPCIAddressSetFreeEntry);
+    ret = virHashRemoveEntry(addrs->used, addr);
 
     VIR_FREE(addr);
 
index 3ab73dda510d5caf2a5b91cc8396939b666aec76..c3c8b66b1555f7d39cd8cffacfbb0adf6b3095e5 100644 (file)
@@ -54,7 +54,7 @@ struct _virHashTable {
     struct _virHashEntry *table;
     int size;
     int nbElems;
-    virHashDeallocator f;
+    virHashDataFree dataFree;
 };
 
 /*
@@ -80,14 +80,14 @@ virHashComputeKey(virHashTablePtr table, const char *name)
 /**
  * virHashCreate:
  * @size: the size of the hash table
- * @deallocator: function to call on each entry during virHashFree
+ * @dataFree: function to call on each entry during virHashFree
  *
  * Create a new virHashTablePtr.
  *
  * Returns the newly created object, or NULL if an error occured.
  */
 virHashTablePtr
-virHashCreate(int size, virHashDeallocator deallocator)
+virHashCreate(int size, virHashDataFree dataFree)
 {
     virHashTablePtr table = NULL;
 
@@ -101,7 +101,7 @@ virHashCreate(int size, virHashDeallocator deallocator)
 
     table->size = size;
     table->nbElems = 0;
-    table->f = deallocator;
+    table->dataFree = dataFree;
     if (VIR_ALLOC_N(table->table, size) < 0) {
         virReportOOMError();
         VIR_FREE(table);
@@ -229,8 +229,8 @@ virHashFree(virHashTablePtr table)
             inside_table = 1;
             while (iter) {
                 next = iter->next;
-                if ((table->f != NULL) && (iter->payload != NULL))
-                    table->f(iter->payload, iter->name);
+                if ((table->dataFree != NULL) && (iter->payload != NULL))
+                    table->dataFree(iter->payload, iter->name);
                 VIR_FREE(iter->name);
                 iter->payload = NULL;
                 if (!inside_table)
@@ -246,8 +246,8 @@ virHashFree(virHashTablePtr table)
 }
 
 static int
-virHashAddOrUpdateEntry(virHashTablePtr table, const char *name,
-                        void *userdata, virHashDeallocator f,
+virHashAddOrUpdateEntry(virHashTablePtr table, const void *name,
+                        void *userdata,
                         bool is_update)
 {
     unsigned long key, len = 0;
@@ -281,8 +281,8 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const char *name,
 
     if (found) {
         if (is_update) {
-            if (f)
-                f(insert->payload, insert->name);
+            if (table->dataFree)
+                table->dataFree(insert->payload, insert->name);
             insert->payload = userdata;
             return (0);
         } else {
@@ -336,7 +336,7 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const char *name,
 int
 virHashAddEntry(virHashTablePtr table, const char *name, void *userdata)
 {
-    return virHashAddOrUpdateEntry(table, name, userdata, NULL, false);
+    return virHashAddOrUpdateEntry(table, name, userdata, false);
 }
 
 /**
@@ -344,7 +344,6 @@ virHashAddEntry(virHashTablePtr table, const char *name, void *userdata)
  * @table: the hash table
  * @name: the name of the userdata
  * @userdata: a pointer to the userdata
- * @f: the deallocator function for replaced item (if any)
  *
  * Add the @userdata to the hash @table. This can later be retrieved
  * by using @name. Existing entry for this tuple
@@ -354,9 +353,9 @@ virHashAddEntry(virHashTablePtr table, const char *name, void *userdata)
  */
 int
 virHashUpdateEntry(virHashTablePtr table, const char *name,
-                   void *userdata, virHashDeallocator f)
+                   void *userdata)
 {
-    return virHashAddOrUpdateEntry(table, name, userdata, f, true);
+    return virHashAddOrUpdateEntry(table, name, userdata, true);
 }
 
 /**
@@ -388,6 +387,30 @@ virHashLookup(virHashTablePtr table, const char *name)
     return (NULL);
 }
 
+
+/**
+ * virHashSteal:
+ * @table: the hash table
+ * @name: the name of the userdata
+ *
+ * Find the userdata specified by @name
+ * and remove it from the hash without freeing it.
+ *
+ * Returns the a pointer to the userdata
+ */
+void *virHashSteal(virHashTablePtr table, const char *name)
+{
+    void *data = virHashLookup(table, name);
+    if (data) {
+        virHashDataFree dataFree = table->dataFree;
+        table->dataFree = NULL;
+        virHashRemoveEntry(table, name);
+        table->dataFree = dataFree;
+    }
+    return data;
+}
+
+
 /**
  * virHashSize:
  * @table: the hash table
@@ -409,7 +432,6 @@ virHashSize(virHashTablePtr table)
  * virHashRemoveEntry:
  * @table: the hash table
  * @name: the name of the userdata
- * @f: the deallocator function for removed item (if any)
  *
  * Find the userdata specified by the @name and remove
  * it from the hash @table. Existing userdata for this tuple will be removed
@@ -418,8 +440,7 @@ virHashSize(virHashTablePtr table)
  * Returns 0 if the removal succeeded and -1 in case of error or not found.
  */
 int
-virHashRemoveEntry(virHashTablePtr table, const char *name,
-                   virHashDeallocator f)
+virHashRemoveEntry(virHashTablePtr table, const char *name)
 {
     unsigned long key;
     virHashEntryPtr entry;
@@ -435,8 +456,8 @@ virHashRemoveEntry(virHashTablePtr table, const char *name,
         for (entry = &(table->table[key]); entry != NULL;
              entry = entry->next) {
             if (STREQ(entry->name, name)) {
-                if ((f != NULL) && (entry->payload != NULL))
-                    f(entry->payload, entry->name);
+                if (table->dataFree && (entry->payload != NULL))
+                    table->dataFree(entry->payload, entry->name);
                 entry->payload = NULL;
                 VIR_FREE(entry->name);
                 if (prev) {
@@ -508,7 +529,7 @@ int virHashForEach(virHashTablePtr table, virHashIterator iter, void *data) {
  *
  * Returns number of items removed on success, -1 on failure
  */
-int virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, virHashDeallocator f, const void *data) {
+int virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void *data) {
     int i, count = 0;
 
     if (table == NULL || iter == NULL)
@@ -521,7 +542,8 @@ int virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, virHashDealloc
         while (entry && entry->valid) {
             if (iter(entry->payload, entry->name, data)) {
                 count++;
-                f(entry->payload, entry->name);
+                if (table->dataFree)
+                    table->dataFree(entry->payload, entry->name);
                 VIR_FREE(entry->name);
                 table->nbElems--;
                 if (prev) {
index cf08e1a158ad9abb68a2a2e15a8d60a353748966..1147f46cf9fe96c3771cf6be4e58dfaaf1a67773 100644 (file)
@@ -23,13 +23,13 @@ typedef virHashTable *virHashTablePtr;
  */
 
 /**
- * virHashDeallocator:
+ * virHashDataFree:
  * @payload:  the data in the hash
  * @name:  the name associated
  *
  * Callback to free data from a hash.
  */
-typedef void (*virHashDeallocator) (void *payload, const char *name);
+typedef void (*virHashDataFree) (void *payload, const char *name);
 /**
  * virHashIterator:
  * @payload: the data in the hash
@@ -55,7 +55,7 @@ typedef int (*virHashSearcher) (const void *payload, const char *name,
 /*
  * Constructor and destructor.
  */
-virHashTablePtr virHashCreate(int size, virHashDeallocator f);
+virHashTablePtr virHashCreate(int size, virHashDataFree dataFree);
 void virHashFree(virHashTablePtr table);
 int virHashSize(virHashTablePtr table);
 
@@ -66,25 +66,30 @@ int virHashAddEntry(virHashTablePtr table,
                     const char *name, void *userdata);
 int virHashUpdateEntry(virHashTablePtr table,
                        const char *name,
-                       void *userdata, virHashDeallocator f);
+                       void *userdata);
 
 /*
  * Remove an entry from the hash table.
  */
 int virHashRemoveEntry(virHashTablePtr table,
-                       const char *name, virHashDeallocator f);
+                       const char *name);
 
 /*
  * Retrieve the userdata.
  */
 void *virHashLookup(virHashTablePtr table, const char *name);
 
+/*
+ * Retrieve & remove the userdata.
+ */
+void *virHashSteal(virHashTablePtr table, const char *name);
+
 
 /*
  * Iterators
  */
 int virHashForEach(virHashTablePtr table, virHashIterator iter, void *data);
-int virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, virHashDeallocator f, const void *data);
+int virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void *data);
 void *virHashSearch(virHashTablePtr table, virHashSearcher iter, const void *data);
 
 #endif                          /* ! __VIR_HASH_H__ */
index 27cc387e863213ab7c17b924710592a032004f3f..62e10687f3d9878d7e8a6e647eda070d836490e9 100644 (file)
@@ -161,7 +161,7 @@ static int xenXMConfigReaper(const void *payload, const char *key ATTRIBUTE_UNUS
         const char *olddomname = entry->def->name;
         char *nameowner = (char *)virHashLookup(args->priv->nameConfigMap, olddomname);
         if (nameowner && STREQ(nameowner, key)) {
-            virHashRemoveEntry(args->priv->nameConfigMap, olddomname, NULL);
+            virHashRemoveEntry(args->priv->nameConfigMap, olddomname);
         }
         return (1);
     }
@@ -216,8 +216,8 @@ xenXMConfigCacheRemoveFile(virConnectPtr conn,
         return 0;
     }
 
-    virHashRemoveEntry(priv->nameConfigMap, entry->def->name, NULL);
-    virHashRemoveEntry(priv->configCache, filename, xenXMConfigFree);
+    virHashRemoveEntry(priv->nameConfigMap, entry->def->name);
+    virHashRemoveEntry(priv->configCache, filename);
     VIR_DEBUG("Removed %s %s", entry->def->name, filename);
     return 0;
 }
@@ -268,7 +268,7 @@ xenXMConfigCacheAddFile(virConnectPtr conn, const char *filename)
             re-acquire it later - just in case it was renamed */
         nameowner = (char *)virHashLookup(priv->nameConfigMap, entry->def->name);
         if (nameowner && STREQ(nameowner, filename)) {
-            virHashRemoveEntry(priv->nameConfigMap, entry->def->name, NULL);
+            virHashRemoveEntry(priv->nameConfigMap, entry->def->name);
         }
 
         /* Clear existing config entry which needs refresh */
@@ -287,7 +287,7 @@ xenXMConfigCacheAddFile(virConnectPtr conn, const char *filename)
     if (!(entry->def = xenXMConfigReadFile(conn, entry->filename))) {
         VIR_DEBUG("Failed to read %s", entry->filename);
         if (!newborn)
-            virHashRemoveEntry(priv->configCache, filename, NULL);
+            virHashSteal(priv->configCache, filename);
         VIR_FREE(entry);
         return -1;
     }
@@ -309,7 +309,7 @@ xenXMConfigCacheAddFile(virConnectPtr conn, const char *filename)
         */
     if (!virHashLookup(priv->nameConfigMap, entry->def->name)) {
         if (virHashAddEntry(priv->nameConfigMap, entry->def->name, entry->filename) < 0) {
-            virHashRemoveEntry(priv->configCache, filename, NULL);
+            virHashSteal(priv->configCache, filename);
             virDomainDefFree(entry->def);
             VIR_FREE(entry);
         }
@@ -412,7 +412,7 @@ int xenXMConfigCacheRefresh (virConnectPtr conn) {
        then the config is no longer on disk */
     args.now = now;
     args.priv = priv;
-    virHashRemoveSet(priv->configCache, xenXMConfigReaper, xenXMConfigFree, &args);
+    virHashRemoveSet(priv->configCache, xenXMConfigReaper, &args);
     ret = 0;
 
     closedir(dh);
@@ -1114,14 +1114,14 @@ virDomainPtr xenXMDomainDefineXML(virConnectPtr conn, const char *xml) {
         }
 
         /* Remove the name -> filename mapping */
-        if (virHashRemoveEntry(priv->nameConfigMap, def->name, NULL) < 0) {
+        if (virHashRemoveEntry(priv->nameConfigMap, def->name) < 0) {
             xenXMError(VIR_ERR_INTERNAL_ERROR,
                        "%s", _("failed to remove old domain from config map"));
             goto error;
         }
 
         /* Remove the config record itself */
-        if (virHashRemoveEntry(priv->configCache, oldfilename, xenXMConfigFree) < 0) {
+        if (virHashRemoveEntry(priv->configCache, oldfilename) < 0) {
             xenXMError(VIR_ERR_INTERNAL_ERROR,
                        "%s", _("failed to remove old domain from config map"));
             goto error;
@@ -1164,7 +1164,7 @@ virDomainPtr xenXMDomainDefineXML(virConnectPtr conn, const char *xml) {
     }
 
     if (virHashAddEntry(priv->nameConfigMap, def->name, entry->filename) < 0) {
-        virHashRemoveEntry(priv->configCache, filename, NULL);
+        virHashSteal(priv->configCache, filename);
         xenXMError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("unable to store config file handle"));
         goto error;
@@ -1213,11 +1213,11 @@ int xenXMDomainUndefine(virDomainPtr domain) {
         goto cleanup;
 
     /* Remove the name -> filename mapping */
-    if (virHashRemoveEntry(priv->nameConfigMap, domain->name, NULL) < 0)
+    if (virHashRemoveEntry(priv->nameConfigMap, domain->name) < 0)
         goto cleanup;
 
     /* Remove the config record itself */
-    if (virHashRemoveEntry(priv->configCache, entry->filename, xenXMConfigFree) < 0)
+    if (virHashRemoveEntry(priv->configCache, entry->filename) < 0)
         goto cleanup;
 
     ret = 0;