hash: add common utility functions

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>
This commit is contained in:
Eric Blake 2014-04-04 17:36:25 -06:00
parent cdc5f3f1a3
commit 09567144d6
13 changed files with 44 additions and 52 deletions

View 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;

View 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;

View 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;

View 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 */

View 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)

View 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;

View 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;

View 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)
{

View 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__ */

View 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)

View 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);
}

View 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);

View 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)