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; virHashSteal;
virHashTableSize; virHashTableSize;
virHashUpdateEntry; virHashUpdateEntry;
virHashValueFree;
# util/virhook.h # util/virhook.h
@ -1628,6 +1629,7 @@ virClassIsDerivedFrom;
virClassName; virClassName;
virClassNew; virClassNew;
virObjectFreeCallback; virObjectFreeCallback;
virObjectFreeHashData;
virObjectIsClass; virObjectIsClass;
virObjectLock; virObjectLock;
virObjectLockableNew; 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 void
virNWFilterUnlockIface(const char *ifname) virNWFilterUnlockIface(const char *ifname)
{ {
@ -818,7 +811,7 @@ virNWFilterLearnInit(void)
return -1; return -1;
} }
ifaceLockMap = virHashCreate(0, freeIfaceLock); ifaceLockMap = virHashCreate(0, virHashValueFree);
if (!ifaceLockMap) { if (!ifaceLockMap) {
virNWFilterLearnShutdown(); virNWFilterLearnShutdown();
return -1; 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 virQEMUCapsCachePtr
virQEMUCapsCacheNew(const char *libDir, virQEMUCapsCacheNew(const char *libDir,
const char *cacheDir, const char *cacheDir,
@ -3313,7 +3306,7 @@ virQEMUCapsCacheNew(const char *libDir,
return NULL; return NULL;
} }
if (!(cache->binaries = virHashCreate(10, virQEMUCapsHashDataFree))) if (!(cache->binaries = virHashCreate(10, virObjectFreeHashData)))
goto error; goto error;
if (VIR_STRDUP(cache->libDir, libDir) < 0) if (VIR_STRDUP(cache->libDir, libDir) < 0)
goto error; goto error;

View File

@ -1102,12 +1102,6 @@ qemuCCWAdressIncrement(virDomainDeviceCCWAddressPtr addr)
return 0; return 0;
} }
static void
qemuDomainCCWAddressSetFreeEntry(void *payload,
const void *name ATTRIBUTE_UNUSED)
{
VIR_FREE(payload);
}
int qemuDomainCCWAddressAssign(virDomainDeviceInfoPtr dev, int qemuDomainCCWAddressAssign(virDomainDeviceInfoPtr dev,
qemuDomainCCWAddressSetPtr addrs, qemuDomainCCWAddressSetPtr addrs,
@ -1264,7 +1258,7 @@ qemuDomainCCWAddressSetCreate(void)
if (VIR_ALLOC(addrs) < 0) if (VIR_ALLOC(addrs) < 0)
goto error; goto error;
if (!(addrs->defined = virHashCreate(10, qemuDomainCCWAddressSetFreeEntry))) if (!(addrs->defined = virHashCreate(10, virHashValueFree)))
goto error; goto error;
/* must use cssid = 0xfe (254) for virtio-ccw devices */ /* must use cssid = 0xfe (254) for virtio-ccw devices */

View File

@ -1678,7 +1678,7 @@ qemuMonitorGetBlockInfo(qemuMonitorPtr mon)
return NULL; return NULL;
} }
if (!(table = virHashCreate(32, (virHashDataFree) free))) if (!(table = virHashCreate(32, virHashValueFree)))
return NULL; return NULL;
if (mon->json) if (mon->json)

View File

@ -1871,10 +1871,6 @@ qemuProcessFindCharDevicePTYs(virDomainObjPtr vm,
return 0; return 0;
} }
static void qemuProcessFreePtyPath(void *payload, const void *name ATTRIBUTE_UNUSED)
{
VIR_FREE(payload);
}
static int static int
qemuProcessWaitForMonitor(virQEMUDriverPtr driver, qemuProcessWaitForMonitor(virQEMUDriverPtr driver,
@ -1911,7 +1907,7 @@ qemuProcessWaitForMonitor(virQEMUDriverPtr driver,
* reliable if it's available. * reliable if it's available.
* Note that the monitor itself can be on a pty, so we still need to try the * Note that the monitor itself can be on a pty, so we still need to try the
* log output method. */ * log output method. */
paths = virHashCreate(0, qemuProcessFreePtyPath); paths = virHashCreate(0, virHashValueFree);
if (paths == NULL) if (paths == NULL)
goto cleanup; goto cleanup;

View File

@ -1,7 +1,7 @@
/* /*
* virclosecallbacks.c: Connection close callbacks routines * 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 * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -67,13 +67,6 @@ static int virCloseCallbacksOnceInit(void)
VIR_ONCE_GLOBAL_INIT(virCloseCallbacks) VIR_ONCE_GLOBAL_INIT(virCloseCallbacks)
static void
virCloseCallbacksFreeData(void *payload,
const void *name ATTRIBUTE_UNUSED)
{
VIR_FREE(payload);
}
virCloseCallbacksPtr virCloseCallbacksPtr
virCloseCallbacksNew(void) virCloseCallbacksNew(void)
{ {
@ -85,7 +78,7 @@ virCloseCallbacksNew(void)
if (!(closeCallbacks = virObjectLockableNew(virCloseCallbacksClass))) if (!(closeCallbacks = virObjectLockableNew(virCloseCallbacksClass)))
return NULL; return NULL;
closeCallbacks->list = virHashCreate(5, virCloseCallbacksFreeData); closeCallbacks->list = virHashCreate(5, virHashValueFree);
if (!closeCallbacks->list) { if (!closeCallbacks->list) {
virObjectUnref(closeCallbacks); virObjectUnref(closeCallbacks);
return NULL; return NULL;

View File

@ -3,7 +3,7 @@
* *
* Reference: Your favorite introductory book on algorithms * 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. * Copyright (C) 2000 Bjorn Reese and Daniel Veillard.
* *
* Permission to use, copy, modify, and distribute this software for any * 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 static size_t
virHashComputeKey(const virHashTable *table, const void *name) virHashComputeKey(const virHashTable *table, const void *name)
{ {

View File

@ -3,7 +3,7 @@
* Description: This module implements the hash table and allocation and * Description: This module implements the hash table and allocation and
* deallocation of domains and connections * 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. * Copyright (C) 2000 Bjorn Reese and Daniel Veillard.
* *
* Author: Bjorn Reese <bjorn.reese@systematic.dk> * 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, void *virHashSearch(const virHashTable *table, virHashSearcher iter,
const void *data); 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__ */ #endif /* ! __VIR_HASH_H__ */

View File

@ -1,7 +1,7 @@
/* /*
* virkeyfile.c: "ini"-style configuration file handling * 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 * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * 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) static int virKeyFileParseGroup(virKeyFileParserCtxtPtr ctxt)
{ {
int ret = -1; int ret = -1;
@ -130,7 +125,7 @@ static int virKeyFileParseGroup(virKeyFileParserCtxtPtr ctxt)
NEXT; NEXT;
if (!(ctxt->group = virHashCreate(10, virKeyFileValueFree))) if (!(ctxt->group = virHashCreate(10, virHashValueFree)))
goto cleanup; goto cleanup;
if (virHashAddEntry(ctxt->conf->groups, ctxt->groupname, ctxt->group) < 0) if (virHashAddEntry(ctxt->conf->groups, ctxt->groupname, ctxt->group) < 0)

View File

@ -1,7 +1,7 @@
/* /*
* virobject.c: libvirt reference counted object * 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 * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -390,3 +390,18 @@ void virObjectFreeCallback(void *opaque)
{ {
virObjectUnref(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 * 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 * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -89,6 +89,7 @@ bool virObjectIsClass(void *obj,
ATTRIBUTE_NONNULL(2); ATTRIBUTE_NONNULL(2);
void virObjectFreeCallback(void *opaque); void virObjectFreeCallback(void *opaque);
void virObjectFreeHashData(void *opaque, const void *name);
void *virObjectLockableNew(virClassPtr klass) void *virObjectLockableNew(virClassPtr klass)
ATTRIBUTE_NONNULL(1); 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 * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -1356,8 +1356,8 @@ testQemuMonitorJSONqemuMonitorJSONGetBlockInfo(const void *data)
if (!test) if (!test)
return -1; return -1;
if (!(blockDevices = virHashCreate(32, (virHashDataFree) free)) || if (!(blockDevices = virHashCreate(32, virHashValueFree)) ||
!(expectedBlockDevices = virHashCreate(32, (virHashDataFree) (free)))) !(expectedBlockDevices = virHashCreate(32, virHashValueFree)))
goto cleanup; goto cleanup;
if (VIR_ALLOC(info) < 0) if (VIR_ALLOC(info) < 0)