]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
hash: make virHashFree more free-like
authorEric Blake <eblake@redhat.com>
Fri, 18 Feb 2011 21:30:24 +0000 (14:30 -0700)
committerEric Blake <eblake@redhat.com>
Mon, 21 Feb 2011 15:27:02 +0000 (08:27 -0700)
Two-argument free functions are uncommon; match the style elsewhere
by caching the callback at creation.

* src/util/hash.h (virHashCreate, virHashFree): Move deallocator
argument to creation.
* cfg.mk (useless_free_options): Add virHashFree.
* src/util/hash.c (_virHashTable): Track deallocator.
(virHashCreate, virHashFree): Update to new signature.
* src/conf/domain_conf.c (virDomainObjListDeinit)
(virDomainObjListInit, virDomainDiskDefForeachPath)
(virDomainSnapshotObjListDeinit, virDomainSnapshotObjListInit):
Update callers.
* src/conf/nwfilter_params.c (virNWFilterHashTableFree)
(virNWFilterHashTableCreate): Likewise.
* src/conf/nwfilter_conf.c (virNWFilterTriggerVMFilterRebuild):
Likewise.
* src/cpu/cpu_generic.c (genericHashFeatures, genericBaseline):
Likewise.
* src/xen/xm_internal.c (xenXMOpen, xenXMClose): Likewise.
* src/nwfilter/nwfilter_learnipaddr.c (virNWFilterLearnInit)
(virNWFilterLearnShutdown): Likewise.
* src/qemu/qemu_command.c (qemuDomainPCIAddressSetCreate)
(qemuDomainPCIAddressSetFree): Likewise.
* src/qemu/qemu_process.c (qemuProcessWaitForMonitor): Likewise.

cfg.mk
src/conf/domain_conf.c
src/conf/nwfilter_conf.c
src/conf/nwfilter_params.c
src/cpu/cpu_generic.c
src/nwfilter/nwfilter_learnipaddr.c
src/qemu/qemu_command.c
src/qemu/qemu_process.c
src/util/hash.c
src/util/hash.h
src/xen/xm_internal.c

diff --git a/cfg.mk b/cfg.mk
index f870723153bfd775efa767d107f9b80340cbba3c..1bb9cbb72a889c0519cb2d8f4631da7672dbcc3c 100644 (file)
--- a/cfg.mk
+++ b/cfg.mk
@@ -106,6 +106,7 @@ useless_free_options =                              \
   --name=virDomainSoundDefFree                 \
   --name=virDomainVideoDefFree                 \
   --name=virDomainWatchdogDefFree              \
+  --name=virHashFree                           \
   --name=virInterfaceDefFree                   \
   --name=virInterfaceIpDefFree                 \
   --name=virInterfaceObjFree                   \
index e7c340919571f5e18e10c9d3373bac20d63b760a..b97c1f016f85dc2b56cc6c69c9e9330842be37dd 100644 (file)
@@ -391,26 +391,27 @@ VIR_ENUM_IMPL(virDomainTimerMode, VIR_DOMAIN_TIMER_MODE_LAST,
 #define VIR_DOMAIN_XML_WRITE_FLAGS  VIR_DOMAIN_XML_SECURE
 #define VIR_DOMAIN_XML_READ_FLAGS   VIR_DOMAIN_XML_INACTIVE
 
+static void
+virDomainObjListDeallocator(void *payload, const char *name ATTRIBUTE_UNUSED)
+{
+    virDomainObjPtr obj = payload;
+    virDomainObjLock(obj);
+    if (virDomainObjUnref(obj) > 0)
+        virDomainObjUnlock(obj);
+}
+
 int virDomainObjListInit(virDomainObjListPtr doms)
 {
-    doms->objs = virHashCreate(50);
+    doms->objs = virHashCreate(50, virDomainObjListDeallocator);
     if (!doms->objs)
         return -1;
     return 0;
 }
 
 
-static void virDomainObjListDeallocator(void *payload, const char *name ATTRIBUTE_UNUSED)
-{
-    virDomainObjPtr obj = payload;
-    virDomainObjLock(obj);
-    if (virDomainObjUnref(obj) > 0)
-        virDomainObjUnlock(obj);
-}
-
 void virDomainObjListDeinit(virDomainObjListPtr doms)
 {
-    virHashFree(doms->objs, virDomainObjListDeallocator);
+    virHashFree(doms->objs);
 }
 
 
@@ -8751,25 +8752,27 @@ virDomainSnapshotObjPtr virDomainSnapshotAssignDef(virDomainSnapshotObjListPtr s
 }
 
 /* Snapshot Obj List functions */
+static void
+virDomainSnapshotObjListDeallocator(void *payload,
+                                    const char *name ATTRIBUTE_UNUSED)
+{
+    virDomainSnapshotObjPtr obj = payload;
+
+    virDomainSnapshotObjUnref(obj);
+}
+
 int virDomainSnapshotObjListInit(virDomainSnapshotObjListPtr snapshots)
 {
-    snapshots->objs = virHashCreate(50);
+    snapshots->objs = virHashCreate(50, virDomainSnapshotObjListDeallocator);
     if (!snapshots->objs)
         return -1;
     return 0;
 }
 
-static void virDomainSnapshotObjListDeallocator(void *payload,
-                                                const char *name ATTRIBUTE_UNUSED)
-{
-    virDomainSnapshotObjPtr obj = payload;
-
-    virDomainSnapshotObjUnref(obj);
-}
-
-static void virDomainSnapshotObjListDeinit(virDomainSnapshotObjListPtr snapshots)
+static void
+virDomainSnapshotObjListDeinit(virDomainSnapshotObjListPtr snapshots)
 {
-    virHashFree(snapshots->objs, virDomainSnapshotObjListDeallocator);
+    virHashFree(snapshots->objs);
 }
 
 struct virDomainSnapshotNameData {
@@ -9002,7 +9005,7 @@ int virDomainDiskDefForeachPath(virDomainDiskDefPtr disk,
         }
     }
 
-    paths = virHashCreate(5);
+    paths = virHashCreate(5, NULL);
 
     do {
         virStorageFileMetadata meta;
@@ -9070,7 +9073,7 @@ int virDomainDiskDefForeachPath(virDomainDiskDefPtr disk,
     ret = 0;
 
 cleanup:
-    virHashFree(paths, NULL);
+    virHashFree(paths);
     VIR_FREE(nextpath);
 
     return ret;
index e5289eb7775d7b33879f1285f56b8c4f3e98870a..13b5b38af7402324c8cef056cf3b7f88b3130eee 100644 (file)
@@ -2,7 +2,7 @@
  * nwfilter_conf.c: network filter XML processing
  *                  (derived from storage_conf.c)
  *
- * Copyright (C) 2006-2010 Red Hat, Inc.
+ * Copyright (C) 2006-2011 Red Hat, Inc.
  * Copyright (C) 2006-2008 Daniel P. Berrange
  *
  * Copyright (C) 2010 IBM Corporation
@@ -2299,7 +2299,7 @@ virNWFilterTriggerVMFilterRebuild(virConnectPtr conn)
         .conn = conn,
         .err = 0,
         .step = STEP_APPLY_NEW,
-        .skipInterfaces = virHashCreate(0),
+        .skipInterfaces = virHashCreate(0, NULL),
     };
 
     if (!cb.skipInterfaces)
@@ -2330,7 +2330,7 @@ virNWFilterTriggerVMFilterRebuild(virConnectPtr conn)
                                                  &cb);
     }
 
-    virHashFree(cb.skipInterfaces, NULL);
+    virHashFree(cb.skipInterfaces);
 
     return err;
 }
index 23423fa8eabfc5b75045605e6babfa5ec2346c47..cd94b30949f7412da259ef0808bd6b16bf785697 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * nwfilter_params.c: parsing and data maintenance of filter parameters
  *
+ * Copyright (C) 2011 Red Hat, Inc.
  * Copyright (C) 2010 IBM Corporation
  *
  * This library is free software; you can redistribute it and/or
@@ -102,7 +103,7 @@ virNWFilterHashTableFree(virNWFilterHashTablePtr table)
     int i;
     if (!table)
         return;
-    virHashFree(table->hashTable, hashDealloc);
+    virHashFree(table->hashTable);
 
     for (i = 0; i < table->nNames; i++)
         VIR_FREE(table->names[i]);
@@ -119,7 +120,7 @@ virNWFilterHashTableCreate(int n) {
         virReportOOMError();
         return NULL;
     }
-    ret->hashTable = virHashCreate(n);
+    ret->hashTable = virHashCreate(n, hashDealloc);
     if (!ret->hashTable) {
         VIR_FREE(ret);
         return NULL;
index a39f262003f8d88c753b6fced5e46db8d8add3ea..a98b10900a19d849d8bfaa0ac52beb130df08f45 100644 (file)
@@ -2,7 +2,7 @@
  * cpu_generic.c: CPU manipulation driver for architectures which are not
  * handled by their own driver
  *
- * Copyright (C) 2009--2010 Red Hat, Inc.
+ * Copyright (C) 2009-2011 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -39,14 +39,14 @@ genericHashFeatures(virCPUDefPtr cpu)
     virHashTablePtr hash;
     unsigned int i;
 
-    if ((hash = virHashCreate(cpu->nfeatures)) == NULL)
+    if ((hash = virHashCreate(cpu->nfeatures, NULL)) == NULL)
         return NULL;
 
     for (i = 0; i < cpu->nfeatures; i++) {
         if (virHashAddEntry(hash,
                             cpu->features[i].name,
                             cpu->features + i)) {
-            virHashFree(hash, NULL);
+            virHashFree(hash);
             return NULL;
         }
     }
@@ -105,7 +105,7 @@ genericCompare(virCPUDefPtr host,
         ret = VIR_CPU_COMPARE_IDENTICAL;
 
 cleanup:
-    virHashFree(hash, NULL);
+    virHashFree(hash);
     return ret;
 }
 
@@ -178,7 +178,7 @@ genericBaseline(virCPUDefPtr *cpus,
             }
         }
 
-        virHashFree(hash, NULL);
+        virHashFree(hash);
     }
 
     if (VIR_ALLOC_N(cpu->features, count) < 0)
index 02af9188b186adffd94afd54cd3cb8202a9ca985..8bd7b50b6699f45c1c5acace42b8f567d3327d27 100644 (file)
@@ -2,6 +2,7 @@
  * nwfilter_learnipaddr.c: support for learning IP address used by a VM
  *                         on an interface
  *
+ * Copyright (C) 2011 Red Hat, Inc.
  * Copyright (C) 2010 IBM Corp.
  * Copyright (C) 2010 Stefan Berger
  *
@@ -822,7 +823,7 @@ virNWFilterLearnInit(void) {
 
     threadsTerminate = false;
 
-    pendingLearnReq = virHashCreate(0);
+    pendingLearnReq = virHashCreate(0, freeLearnReqEntry);
     if (!pendingLearnReq) {
         return 1;
     }
@@ -844,7 +845,7 @@ virNWFilterLearnInit(void) {
         return 1;
     }
 
-    ifaceLockMap = virHashCreate(0);
+    ifaceLockMap = virHashCreate(0, freeIfaceLock);
     if (!ifaceLockMap) {
         virNWFilterLearnShutdown();
         return 1;
@@ -879,12 +880,12 @@ virNWFilterLearnShutdown(void) {
 
     virNWFilterLearnThreadsTerminate(false);
 
-    virHashFree(pendingLearnReq, freeLearnReqEntry);
+    virHashFree(pendingLearnReq);
     pendingLearnReq = NULL;
 
     virNWFilterHashTableFree(ipAddressMap);
     ipAddressMap = NULL;
 
-    virHashFree(ifaceLockMap, freeIfaceLock);
+    virHashFree(ifaceLockMap);
     ifaceLockMap = NULL;
 }
index 0db284383268707c5c7efc6c60bd7e6ff66a7e4a..a41859ca55de5cdaf7f602866afd3255648696b1 100644 (file)
@@ -745,6 +745,13 @@ cleanup:
 }
 
 
+static void
+qemuDomainPCIAddressSetFreeEntry(void *payload,
+                                 const char *name ATTRIBUTE_UNUSED)
+{
+    VIR_FREE(payload);
+}
+
 qemuDomainPCIAddressSetPtr qemuDomainPCIAddressSetCreate(virDomainDefPtr def)
 {
     qemuDomainPCIAddressSetPtr addrs;
@@ -752,7 +759,7 @@ qemuDomainPCIAddressSetPtr qemuDomainPCIAddressSetCreate(virDomainDefPtr def)
     if (VIR_ALLOC(addrs) < 0)
         goto no_memory;
 
-    if (!(addrs->used = virHashCreate(10)))
+    if (!(addrs->used = virHashCreate(10, qemuDomainPCIAddressSetFreeEntry)))
         goto error;
 
     if (virDomainDeviceInfoIterate(def, qemuCollectPCIAddress, addrs) < 0)
@@ -823,11 +830,6 @@ int qemuDomainPCIAddressEnsureAddr(qemuDomainPCIAddressSetPtr addrs,
     return ret;
 }
 
-static void qemuDomainPCIAddressSetFreeEntry(void *payload, const char *name ATTRIBUTE_UNUSED)
-{
-    VIR_FREE(payload);
-}
-
 
 int qemuDomainPCIAddressReleaseAddr(qemuDomainPCIAddressSetPtr addrs,
                                     virDomainDeviceInfoPtr dev)
@@ -852,7 +854,7 @@ void qemuDomainPCIAddressSetFree(qemuDomainPCIAddressSetPtr addrs)
     if (!addrs)
         return;
 
-    virHashFree(addrs->used, qemuDomainPCIAddressSetFreeEntry);
+    virHashFree(addrs->used);
     VIR_FREE(addrs);
 }
 
index c56027c5e12a0a5b2701855691655f288bfad232..d1e6651e1711c7f326dd29a3188fd7f988f08e11 100644 (file)
@@ -994,7 +994,7 @@ qemuProcessWaitForMonitor(struct qemud_driver* driver,
      * reliable if it's available.
      * Note that the monitor itself can be on a pty, so we still need to try the
      * log output method. */
-    paths = virHashCreate(0);
+    paths = virHashCreate(0, qemuProcessFreePtyPath);
     if (paths == NULL)
         goto cleanup;
 
@@ -1008,7 +1008,7 @@ qemuProcessWaitForMonitor(struct qemud_driver* driver,
         ret = qemuProcessFindCharDevicePTYsMonitor(vm, paths);
 
 cleanup:
-    virHashFree(paths, qemuProcessFreePtyPath);
+    virHashFree(paths);
 
     if (kill(vm->pid, 0) == -1 && errno == ESRCH) {
         /* VM is dead, any other error raised in the interim is probably
index 9497b60ec029073aed730c15e29280ca4883f18a..d7950b9634cf6e966c2815cac58924be673f7bc0 100644 (file)
@@ -55,6 +55,7 @@ struct _virHashTable {
     struct _virHashEntry *table;
     int size;
     int nbElems;
+    virHashDeallocator f;
 };
 
 /*
@@ -80,13 +81,14 @@ virHashComputeKey(virHashTablePtr table, const char *name)
 /**
  * virHashCreate:
  * @size: the size of the hash table
+ * @deallocator: 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)
+virHashCreate(int size, virHashDeallocator deallocator)
 {
     virHashTablePtr table = NULL;
 
@@ -100,6 +102,7 @@ virHashCreate(int size)
 
     table->size = size;
     table->nbElems = 0;
+    table->f = deallocator;
     if (VIR_ALLOC_N(table->table, size) < 0) {
         virReportOOMError();
         VIR_FREE(table);
@@ -203,13 +206,12 @@ virHashGrow(virHashTablePtr table, int size)
 /**
  * virHashFree:
  * @table: the hash table
- * @f:  the deallocator function for items in the hash
  *
  * Free the hash @table and its contents. The userdata is
- * deallocated with @f if provided.
+ * deallocated with function provided at creation time.
  */
 void
-virHashFree(virHashTablePtr table, virHashDeallocator f)
+virHashFree(virHashTablePtr table)
 {
     int i;
     virHashEntryPtr iter;
@@ -228,8 +230,8 @@ virHashFree(virHashTablePtr table, virHashDeallocator f)
             inside_table = 1;
             while (iter) {
                 next = iter->next;
-                if ((f != NULL) && (iter->payload != NULL))
-                    f(iter->payload, iter->name);
+                if ((table->f != NULL) && (iter->payload != NULL))
+                    table->f(iter->payload, iter->name);
                 VIR_FREE(iter->name);
                 iter->payload = NULL;
                 if (!inside_table)
index b0ef4417a390b323ea483cbac144c766b739c8a0..cf08e1a158ad9abb68a2a2e15a8d60a353748966 100644 (file)
@@ -3,7 +3,7 @@
  * Description: This module implements the hash table and allocation and
  *              deallocation of domains and connections
  *
- * Copy: Copyright (C) 2005 Red Hat, Inc.
+ * Copy: Copyright (C) 2005, 2011 Red Hat, Inc.
  *
  * Author: Bjorn Reese <bjorn.reese@systematic.dk>
  *         Daniel Veillard <veillard@redhat.com>
@@ -49,13 +49,14 @@ typedef void (*virHashIterator) (void *payload, const char *name, void *data);
  * Returns 1 if the hash entry is desired, 0 to move
  * to next entry
  */
-typedef int (*virHashSearcher) (const void *payload, const char *name, const void *data);
+typedef int (*virHashSearcher) (const void *payload, const char *name,
+                                const void *data);
 
 /*
  * Constructor and destructor.
  */
-virHashTablePtr virHashCreate(int size);
-void virHashFree(virHashTablePtr table, virHashDeallocator f);
+virHashTablePtr virHashCreate(int size, virHashDeallocator f);
+void virHashFree(virHashTablePtr table);
 int virHashSize(virHashTablePtr table);
 
 /*
index 865805c0591f079f92b5d259bb0cd54001b66183..b09043c92c34bd6e8f39ffec8f827e5b7d3d903b 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * xm_internal.h: helper routines for dealing with inactive domains
  *
- * Copyright (C) 2006-2007, 2009-2010 Red Hat, Inc.
+ * Copyright (C) 2006-2007, 2009-2011 Red Hat, Inc.
  * Copyright (C) 2006 Daniel P. Berrange
  *
  * This library is free software; you can redistribute it and/or
@@ -587,12 +587,12 @@ xenXMOpen (virConnectPtr conn,
 
     priv->configDir = XM_CONFIG_DIR;
 
-    priv->configCache = virHashCreate(50);
+    priv->configCache = virHashCreate(50, xenXMConfigFree);
     if (!priv->configCache)
         return (-1);
-    priv->nameConfigMap = virHashCreate(50);
+    priv->nameConfigMap = virHashCreate(50, NULL);
     if (!priv->nameConfigMap) {
-        virHashFree(priv->configCache, NULL);
+        virHashFree(priv->configCache);
         priv->configCache = NULL;
         return (-1);
     }
@@ -611,8 +611,8 @@ xenXMOpen (virConnectPtr conn,
 int xenXMClose(virConnectPtr conn) {
     xenUnifiedPrivatePtr priv = conn->privateData;
 
-    virHashFree(priv->nameConfigMap, NULL);
-    virHashFree(priv->configCache, xenXMConfigFree);
+    virHashFree(priv->nameConfigMap);
+    virHashFree(priv->configCache);
 
     return (0);
 }