]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
hash: add common utility functions
authorEric Blake <eblake@redhat.com>
Fri, 4 Apr 2014 23:36:25 +0000 (17:36 -0600)
committerEric Blake <eblake@redhat.com>
Mon, 7 Apr 2014 12:14:45 +0000 (06:14 -0600)
I almost wrote a hash value free function that just called
VIR_FREE, then realized I couldn't be the first person to
do that.  Sure enough, it was worth factoring into a common
helper routine.

* src/util/virhash.h (virHashValueFree): New function.
* src/util/virhash.c (virHashValueFree): Implement it.
* src/util/virobject.h (virObjectFreeHashData): New function.
* src/libvirt_private.syms (virhash.h, virobject.h): Export them.
* src/nwfilter/nwfilter_learnipaddr.c (virNWFilterLearnInit): Use
common function.
* src/qemu/qemu_capabilities.c (virQEMUCapsCacheNew): Likewise.
* src/qemu/qemu_command.c (qemuDomainCCWAddressSetCreate):
Likewise.
* src/qemu/qemu_monitor.c (qemuMonitorGetBlockInfo): Likewise.
* src/qemu/qemu_process.c (qemuProcessWaitForMonitor): Likewise.
* src/util/virclosecallbacks.c (virCloseCallbacksNew): Likewise.
* src/util/virkeyfile.c (virKeyFileParseGroup): Likewise.
* tests/qemumonitorjsontest.c
(testQemuMonitorJSONqemuMonitorJSONGetBlockInfo): Likewise.

Signed-off-by: Eric Blake <eblake@redhat.com>
13 files changed:
src/libvirt_private.syms
src/nwfilter/nwfilter_learnipaddr.c
src/qemu/qemu_capabilities.c
src/qemu/qemu_command.c
src/qemu/qemu_monitor.c
src/qemu/qemu_process.c
src/util/virclosecallbacks.c
src/util/virhash.c
src/util/virhash.h
src/util/virkeyfile.c
src/util/virobject.c
src/util/virobject.h
tests/qemumonitorjsontest.c

index 2d12105cb1b5d811df883a90fc7112afd776fc5c..6b68a4395b37119ba0eac2f43a6bc6141ffeec42 100644 (file)
@@ -1290,6 +1290,7 @@ virHashSize;
 virHashSteal;
 virHashTableSize;
 virHashUpdateEntry;
+virHashValueFree;
 
 
 # util/virhook.h
@@ -1628,6 +1629,7 @@ virClassIsDerivedFrom;
 virClassName;
 virClassNew;
 virObjectFreeCallback;
+virObjectFreeHashData;
 virObjectIsClass;
 virObjectLock;
 virObjectLockableNew;
index 0eb5e7e76d6319161c5a729955145613a819dfac..1ffed7838f2234f98352221de7f6c067cad06402 100644 (file)
@@ -188,13 +188,6 @@ virNWFilterLockIface(const char *ifname)
 }
 
 
-static void
-freeIfaceLock(void *payload, const void *name ATTRIBUTE_UNUSED)
-{
-    VIR_FREE(payload);
-}
-
-
 void
 virNWFilterUnlockIface(const char *ifname)
 {
@@ -818,7 +811,7 @@ virNWFilterLearnInit(void)
         return -1;
     }
 
-    ifaceLockMap = virHashCreate(0, freeIfaceLock);
+    ifaceLockMap = virHashCreate(0, virHashValueFree);
     if (!ifaceLockMap) {
         virNWFilterLearnShutdown();
         return -1;
index e1d04ff20b079d705c4a072265e01dd9e74a01e9..2c8ec101c74b745ae5849727fca588d4ff268012 100644 (file)
@@ -3288,13 +3288,6 @@ bool virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps)
 }
 
 
-static void
-virQEMUCapsHashDataFree(void *payload, const void *key ATTRIBUTE_UNUSED)
-{
-    virObjectUnref(payload);
-}
-
-
 virQEMUCapsCachePtr
 virQEMUCapsCacheNew(const char *libDir,
                     const char *cacheDir,
@@ -3313,7 +3306,7 @@ virQEMUCapsCacheNew(const char *libDir,
         return NULL;
     }
 
-    if (!(cache->binaries = virHashCreate(10, virQEMUCapsHashDataFree)))
+    if (!(cache->binaries = virHashCreate(10, virObjectFreeHashData)))
         goto error;
     if (VIR_STRDUP(cache->libDir, libDir) < 0)
         goto error;
index 1723ebfeeb4f904b950468772b02b9327a9c7a60..7b1e9c082f752771860ad08ccec989dd81e035d1 100644 (file)
@@ -1102,12 +1102,6 @@ qemuCCWAdressIncrement(virDomainDeviceCCWAddressPtr addr)
     return 0;
 }
 
-static void
-qemuDomainCCWAddressSetFreeEntry(void *payload,
-                                 const void *name ATTRIBUTE_UNUSED)
-{
-    VIR_FREE(payload);
-}
 
 int qemuDomainCCWAddressAssign(virDomainDeviceInfoPtr dev,
                                qemuDomainCCWAddressSetPtr addrs,
@@ -1264,7 +1258,7 @@ qemuDomainCCWAddressSetCreate(void)
     if (VIR_ALLOC(addrs) < 0)
         goto error;
 
-    if (!(addrs->defined = virHashCreate(10, qemuDomainCCWAddressSetFreeEntry)))
+    if (!(addrs->defined = virHashCreate(10, virHashValueFree)))
         goto error;
 
     /* must use cssid = 0xfe (254) for virtio-ccw devices */
index 205002b4ee9ed184ebccc54f260127713a649c76..5a5a59ba1e3474b138bc9c1428f27d10a07699f5 100644 (file)
@@ -1678,7 +1678,7 @@ qemuMonitorGetBlockInfo(qemuMonitorPtr mon)
         return NULL;
     }
 
-    if (!(table = virHashCreate(32, (virHashDataFree) free)))
+    if (!(table = virHashCreate(32, virHashValueFree)))
         return NULL;
 
     if (mon->json)
index ca9e15cb038e7a343e508d6d3c10dff472bab1a6..0afad9d0785ed7d707ef3554cb7556867647bca7 100644 (file)
@@ -1871,10 +1871,6 @@ qemuProcessFindCharDevicePTYs(virDomainObjPtr vm,
     return 0;
 }
 
-static void qemuProcessFreePtyPath(void *payload, const void *name ATTRIBUTE_UNUSED)
-{
-    VIR_FREE(payload);
-}
 
 static int
 qemuProcessWaitForMonitor(virQEMUDriverPtr driver,
@@ -1911,7 +1907,7 @@ qemuProcessWaitForMonitor(virQEMUDriverPtr 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, qemuProcessFreePtyPath);
+    paths = virHashCreate(0, virHashValueFree);
     if (paths == NULL)
         goto cleanup;
 
index d62fd896bad76b723cad876a9d1b6e22e79e50dd..4f261728c437accd8761a2f2706b84a57e483957 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * virclosecallbacks.c: Connection close callbacks routines
  *
- * Copyright (C) 2013 Red Hat, Inc.
+ * Copyright (C) 2013-2014 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
@@ -67,13 +67,6 @@ static int virCloseCallbacksOnceInit(void)
 VIR_ONCE_GLOBAL_INIT(virCloseCallbacks)
 
 
-static void
-virCloseCallbacksFreeData(void *payload,
-                          const void *name ATTRIBUTE_UNUSED)
-{
-    VIR_FREE(payload);
-}
-
 virCloseCallbacksPtr
 virCloseCallbacksNew(void)
 {
@@ -85,7 +78,7 @@ virCloseCallbacksNew(void)
     if (!(closeCallbacks = virObjectLockableNew(virCloseCallbacksClass)))
         return NULL;
 
-    closeCallbacks->list = virHashCreate(5, virCloseCallbacksFreeData);
+    closeCallbacks->list = virHashCreate(5, virHashValueFree);
     if (!closeCallbacks->list) {
         virObjectUnref(closeCallbacks);
         return NULL;
index 9ef355444f55323f7370391f7c837e01eee89fe5..e3c1880fbfe00dec1518335213c7721c32d3cf41 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Reference: Your favorite introductory book on algorithms
  *
- * Copyright (C) 2005-2013 Red Hat, Inc.
+ * Copyright (C) 2005-2014 Red Hat, Inc.
  * Copyright (C) 2000 Bjorn Reese and Daniel Veillard.
  *
  * Permission to use, copy, modify, and distribute this software for any
@@ -99,6 +99,13 @@ static void virHashStrFree(void *name)
 }
 
 
+void
+virHashValueFree(void *value, const void *name ATTRIBUTE_UNUSED)
+{
+    VIR_FREE(value);
+}
+
+
 static size_t
 virHashComputeKey(const virHashTable *table, const void *name)
 {
index 4de9a143e272e667dce54d05e49359d891e24905..a1371373c93a6bfd8ce412f6ba24d7ab13f5c7c5 100644 (file)
@@ -3,7 +3,7 @@
  * Description: This module implements the hash table and allocation and
  *              deallocation of domains and connections
  *
- * Copyright (C) 2005-2013 Red Hat, Inc.
+ * Copyright (C) 2005-2014 Red Hat, Inc.
  * Copyright (C) 2000 Bjorn Reese and Daniel Veillard.
  *
  * Author: Bjorn Reese <bjorn.reese@systematic.dk>
@@ -184,4 +184,7 @@ ssize_t virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void
 void *virHashSearch(const virHashTable *table, virHashSearcher iter,
                     const void *data);
 
+/* Convenience for when VIR_FREE(value) is sufficient as a data freer.  */
+void virHashValueFree(void *value, const void *name);
+
 #endif                          /* ! __VIR_HASH_H__ */
index 742fb371fa4f3ea2a5fd237922febd2fec5c1d20..da064256e522e8a4012348c97f9a2c2ae1e9cbfb 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * virkeyfile.c: "ini"-style configuration file handling
  *
- * Copyright (C) 2012-2013 Red Hat, Inc.
+ * Copyright (C) 2012-2014 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
@@ -103,11 +103,6 @@ virKeyFileErrorHelper(const char *file, const char *func, size_t line,
 }
 
 
-static void virKeyFileValueFree(void *value, const void *name ATTRIBUTE_UNUSED)
-{
-    VIR_FREE(value);
-}
-
 static int virKeyFileParseGroup(virKeyFileParserCtxtPtr ctxt)
 {
     int ret = -1;
@@ -130,7 +125,7 @@ static int virKeyFileParseGroup(virKeyFileParserCtxtPtr ctxt)
 
     NEXT;
 
-    if (!(ctxt->group = virHashCreate(10, virKeyFileValueFree)))
+    if (!(ctxt->group = virHashCreate(10, virHashValueFree)))
         goto cleanup;
 
     if (virHashAddEntry(ctxt->conf->groups, ctxt->groupname, ctxt->group) < 0)
index c8bc19305ea14deae5e9d883a5ad0ea8ecb28a13..6cb84b4a90476adb196a5c9d24983800dec8625d 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * virobject.c: libvirt reference counted object
  *
- * Copyright (C) 2012-2013 Red Hat, Inc.
+ * Copyright (C) 2012-2014 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
@@ -390,3 +390,18 @@ void virObjectFreeCallback(void *opaque)
 {
     virObjectUnref(opaque);
 }
+
+
+/**
+ * virObjectFreeHashData:
+ * @opaque: a pointer to a virObject instance
+ * @name: ignored, name of the hash key being deleted
+ *
+ * Provides identical functionality to virObjectUnref,
+ * but with the signature matching the virHashDataFree
+ * typedef.
+ */
+void virObjectFreeHashData(void *opaque, const void *name ATTRIBUTE_UNUSED)
+{
+    virObjectUnref(opaque);
+}
index d571b5cedc18860283c3048ad857eded3d4f4d43..ad1f0c1d2c92a2c15c54c1f22509c53f84ddce79 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * virobject.h: libvirt reference counted object
  *
- * Copyright (C) 2012-2013 Red Hat, Inc.
+ * Copyright (C) 2012-2014 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
@@ -89,6 +89,7 @@ bool virObjectIsClass(void *obj,
     ATTRIBUTE_NONNULL(2);
 
 void virObjectFreeCallback(void *opaque);
+void virObjectFreeHashData(void *opaque, const void *name);
 
 void *virObjectLockableNew(virClassPtr klass)
     ATTRIBUTE_NONNULL(1);
index f80d03eeaba6deed10802b56d16e0b6d95ed946e..47d7481c204b1baae410238f838dddaf98f3c192 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011-2013 Red Hat, Inc.
+ * Copyright (C) 2011-2014 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
@@ -1356,8 +1356,8 @@ testQemuMonitorJSONqemuMonitorJSONGetBlockInfo(const void *data)
     if (!test)
         return -1;
 
-    if (!(blockDevices = virHashCreate(32, (virHashDataFree) free)) ||
-        !(expectedBlockDevices = virHashCreate(32, (virHashDataFree) (free))))
+    if (!(blockDevices = virHashCreate(32, virHashValueFree)) ||
+        !(expectedBlockDevices = virHashCreate(32, virHashValueFree)))
         goto cleanup;
 
     if (VIR_ALLOC(info) < 0)