hash: make virHashFree more free-like

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.
This commit is contained in:
Eric Blake 2011-02-18 14:30:24 -07:00
parent 6e9f3dfa0c
commit 03ba07cb73
11 changed files with 75 additions and 64 deletions

1
cfg.mk
View File

@ -106,6 +106,7 @@ useless_free_options = \
--name=virDomainSoundDefFree \ --name=virDomainSoundDefFree \
--name=virDomainVideoDefFree \ --name=virDomainVideoDefFree \
--name=virDomainWatchdogDefFree \ --name=virDomainWatchdogDefFree \
--name=virHashFree \
--name=virInterfaceDefFree \ --name=virInterfaceDefFree \
--name=virInterfaceIpDefFree \ --name=virInterfaceIpDefFree \
--name=virInterfaceObjFree \ --name=virInterfaceObjFree \

View File

@ -391,16 +391,8 @@ VIR_ENUM_IMPL(virDomainTimerMode, VIR_DOMAIN_TIMER_MODE_LAST,
#define VIR_DOMAIN_XML_WRITE_FLAGS VIR_DOMAIN_XML_SECURE #define VIR_DOMAIN_XML_WRITE_FLAGS VIR_DOMAIN_XML_SECURE
#define VIR_DOMAIN_XML_READ_FLAGS VIR_DOMAIN_XML_INACTIVE #define VIR_DOMAIN_XML_READ_FLAGS VIR_DOMAIN_XML_INACTIVE
int virDomainObjListInit(virDomainObjListPtr doms) static void
{ virDomainObjListDeallocator(void *payload, const char *name ATTRIBUTE_UNUSED)
doms->objs = virHashCreate(50);
if (!doms->objs)
return -1;
return 0;
}
static void virDomainObjListDeallocator(void *payload, const char *name ATTRIBUTE_UNUSED)
{ {
virDomainObjPtr obj = payload; virDomainObjPtr obj = payload;
virDomainObjLock(obj); virDomainObjLock(obj);
@ -408,9 +400,18 @@ static void virDomainObjListDeallocator(void *payload, const char *name ATTRIBUT
virDomainObjUnlock(obj); virDomainObjUnlock(obj);
} }
int virDomainObjListInit(virDomainObjListPtr doms)
{
doms->objs = virHashCreate(50, virDomainObjListDeallocator);
if (!doms->objs)
return -1;
return 0;
}
void virDomainObjListDeinit(virDomainObjListPtr doms) void virDomainObjListDeinit(virDomainObjListPtr doms)
{ {
virHashFree(doms->objs, virDomainObjListDeallocator); virHashFree(doms->objs);
} }
@ -8751,25 +8752,27 @@ virDomainSnapshotObjPtr virDomainSnapshotAssignDef(virDomainSnapshotObjListPtr s
} }
/* Snapshot Obj List functions */ /* Snapshot Obj List functions */
int virDomainSnapshotObjListInit(virDomainSnapshotObjListPtr snapshots) static void
{ virDomainSnapshotObjListDeallocator(void *payload,
snapshots->objs = virHashCreate(50); const char *name ATTRIBUTE_UNUSED)
if (!snapshots->objs)
return -1;
return 0;
}
static void virDomainSnapshotObjListDeallocator(void *payload,
const char *name ATTRIBUTE_UNUSED)
{ {
virDomainSnapshotObjPtr obj = payload; virDomainSnapshotObjPtr obj = payload;
virDomainSnapshotObjUnref(obj); virDomainSnapshotObjUnref(obj);
} }
static void virDomainSnapshotObjListDeinit(virDomainSnapshotObjListPtr snapshots) int virDomainSnapshotObjListInit(virDomainSnapshotObjListPtr snapshots)
{ {
virHashFree(snapshots->objs, virDomainSnapshotObjListDeallocator); snapshots->objs = virHashCreate(50, virDomainSnapshotObjListDeallocator);
if (!snapshots->objs)
return -1;
return 0;
}
static void
virDomainSnapshotObjListDeinit(virDomainSnapshotObjListPtr snapshots)
{
virHashFree(snapshots->objs);
} }
struct virDomainSnapshotNameData { struct virDomainSnapshotNameData {
@ -9002,7 +9005,7 @@ int virDomainDiskDefForeachPath(virDomainDiskDefPtr disk,
} }
} }
paths = virHashCreate(5); paths = virHashCreate(5, NULL);
do { do {
virStorageFileMetadata meta; virStorageFileMetadata meta;
@ -9070,7 +9073,7 @@ int virDomainDiskDefForeachPath(virDomainDiskDefPtr disk,
ret = 0; ret = 0;
cleanup: cleanup:
virHashFree(paths, NULL); virHashFree(paths);
VIR_FREE(nextpath); VIR_FREE(nextpath);
return ret; return ret;

View File

@ -2,7 +2,7 @@
* nwfilter_conf.c: network filter XML processing * nwfilter_conf.c: network filter XML processing
* (derived from storage_conf.c) * (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) 2006-2008 Daniel P. Berrange
* *
* Copyright (C) 2010 IBM Corporation * Copyright (C) 2010 IBM Corporation
@ -2299,7 +2299,7 @@ virNWFilterTriggerVMFilterRebuild(virConnectPtr conn)
.conn = conn, .conn = conn,
.err = 0, .err = 0,
.step = STEP_APPLY_NEW, .step = STEP_APPLY_NEW,
.skipInterfaces = virHashCreate(0), .skipInterfaces = virHashCreate(0, NULL),
}; };
if (!cb.skipInterfaces) if (!cb.skipInterfaces)
@ -2330,7 +2330,7 @@ virNWFilterTriggerVMFilterRebuild(virConnectPtr conn)
&cb); &cb);
} }
virHashFree(cb.skipInterfaces, NULL); virHashFree(cb.skipInterfaces);
return err; return err;
} }

View File

@ -1,6 +1,7 @@
/* /*
* nwfilter_params.c: parsing and data maintenance of filter parameters * nwfilter_params.c: parsing and data maintenance of filter parameters
* *
* Copyright (C) 2011 Red Hat, Inc.
* Copyright (C) 2010 IBM Corporation * Copyright (C) 2010 IBM Corporation
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@ -102,7 +103,7 @@ virNWFilterHashTableFree(virNWFilterHashTablePtr table)
int i; int i;
if (!table) if (!table)
return; return;
virHashFree(table->hashTable, hashDealloc); virHashFree(table->hashTable);
for (i = 0; i < table->nNames; i++) for (i = 0; i < table->nNames; i++)
VIR_FREE(table->names[i]); VIR_FREE(table->names[i]);
@ -119,7 +120,7 @@ virNWFilterHashTableCreate(int n) {
virReportOOMError(); virReportOOMError();
return NULL; return NULL;
} }
ret->hashTable = virHashCreate(n); ret->hashTable = virHashCreate(n, hashDealloc);
if (!ret->hashTable) { if (!ret->hashTable) {
VIR_FREE(ret); VIR_FREE(ret);
return NULL; return NULL;

View File

@ -2,7 +2,7 @@
* cpu_generic.c: CPU manipulation driver for architectures which are not * cpu_generic.c: CPU manipulation driver for architectures which are not
* handled by their own driver * 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 * 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
@ -39,14 +39,14 @@ genericHashFeatures(virCPUDefPtr cpu)
virHashTablePtr hash; virHashTablePtr hash;
unsigned int i; unsigned int i;
if ((hash = virHashCreate(cpu->nfeatures)) == NULL) if ((hash = virHashCreate(cpu->nfeatures, NULL)) == NULL)
return NULL; return NULL;
for (i = 0; i < cpu->nfeatures; i++) { for (i = 0; i < cpu->nfeatures; i++) {
if (virHashAddEntry(hash, if (virHashAddEntry(hash,
cpu->features[i].name, cpu->features[i].name,
cpu->features + i)) { cpu->features + i)) {
virHashFree(hash, NULL); virHashFree(hash);
return NULL; return NULL;
} }
} }
@ -105,7 +105,7 @@ genericCompare(virCPUDefPtr host,
ret = VIR_CPU_COMPARE_IDENTICAL; ret = VIR_CPU_COMPARE_IDENTICAL;
cleanup: cleanup:
virHashFree(hash, NULL); virHashFree(hash);
return ret; return ret;
} }
@ -178,7 +178,7 @@ genericBaseline(virCPUDefPtr *cpus,
} }
} }
virHashFree(hash, NULL); virHashFree(hash);
} }
if (VIR_ALLOC_N(cpu->features, count) < 0) if (VIR_ALLOC_N(cpu->features, count) < 0)

View File

@ -2,6 +2,7 @@
* nwfilter_learnipaddr.c: support for learning IP address used by a VM * nwfilter_learnipaddr.c: support for learning IP address used by a VM
* on an interface * on an interface
* *
* Copyright (C) 2011 Red Hat, Inc.
* Copyright (C) 2010 IBM Corp. * Copyright (C) 2010 IBM Corp.
* Copyright (C) 2010 Stefan Berger * Copyright (C) 2010 Stefan Berger
* *
@ -822,7 +823,7 @@ virNWFilterLearnInit(void) {
threadsTerminate = false; threadsTerminate = false;
pendingLearnReq = virHashCreate(0); pendingLearnReq = virHashCreate(0, freeLearnReqEntry);
if (!pendingLearnReq) { if (!pendingLearnReq) {
return 1; return 1;
} }
@ -844,7 +845,7 @@ virNWFilterLearnInit(void) {
return 1; return 1;
} }
ifaceLockMap = virHashCreate(0); ifaceLockMap = virHashCreate(0, freeIfaceLock);
if (!ifaceLockMap) { if (!ifaceLockMap) {
virNWFilterLearnShutdown(); virNWFilterLearnShutdown();
return 1; return 1;
@ -879,12 +880,12 @@ virNWFilterLearnShutdown(void) {
virNWFilterLearnThreadsTerminate(false); virNWFilterLearnThreadsTerminate(false);
virHashFree(pendingLearnReq, freeLearnReqEntry); virHashFree(pendingLearnReq);
pendingLearnReq = NULL; pendingLearnReq = NULL;
virNWFilterHashTableFree(ipAddressMap); virNWFilterHashTableFree(ipAddressMap);
ipAddressMap = NULL; ipAddressMap = NULL;
virHashFree(ifaceLockMap, freeIfaceLock); virHashFree(ifaceLockMap);
ifaceLockMap = NULL; ifaceLockMap = NULL;
} }

View File

@ -745,6 +745,13 @@ cleanup:
} }
static void
qemuDomainPCIAddressSetFreeEntry(void *payload,
const char *name ATTRIBUTE_UNUSED)
{
VIR_FREE(payload);
}
qemuDomainPCIAddressSetPtr qemuDomainPCIAddressSetCreate(virDomainDefPtr def) qemuDomainPCIAddressSetPtr qemuDomainPCIAddressSetCreate(virDomainDefPtr def)
{ {
qemuDomainPCIAddressSetPtr addrs; qemuDomainPCIAddressSetPtr addrs;
@ -752,7 +759,7 @@ qemuDomainPCIAddressSetPtr qemuDomainPCIAddressSetCreate(virDomainDefPtr def)
if (VIR_ALLOC(addrs) < 0) if (VIR_ALLOC(addrs) < 0)
goto no_memory; goto no_memory;
if (!(addrs->used = virHashCreate(10))) if (!(addrs->used = virHashCreate(10, qemuDomainPCIAddressSetFreeEntry)))
goto error; goto error;
if (virDomainDeviceInfoIterate(def, qemuCollectPCIAddress, addrs) < 0) if (virDomainDeviceInfoIterate(def, qemuCollectPCIAddress, addrs) < 0)
@ -823,11 +830,6 @@ int qemuDomainPCIAddressEnsureAddr(qemuDomainPCIAddressSetPtr addrs,
return ret; return ret;
} }
static void qemuDomainPCIAddressSetFreeEntry(void *payload, const char *name ATTRIBUTE_UNUSED)
{
VIR_FREE(payload);
}
int qemuDomainPCIAddressReleaseAddr(qemuDomainPCIAddressSetPtr addrs, int qemuDomainPCIAddressReleaseAddr(qemuDomainPCIAddressSetPtr addrs,
virDomainDeviceInfoPtr dev) virDomainDeviceInfoPtr dev)
@ -852,7 +854,7 @@ void qemuDomainPCIAddressSetFree(qemuDomainPCIAddressSetPtr addrs)
if (!addrs) if (!addrs)
return; return;
virHashFree(addrs->used, qemuDomainPCIAddressSetFreeEntry); virHashFree(addrs->used);
VIR_FREE(addrs); VIR_FREE(addrs);
} }

View File

@ -994,7 +994,7 @@ qemuProcessWaitForMonitor(struct qemud_driver* 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); paths = virHashCreate(0, qemuProcessFreePtyPath);
if (paths == NULL) if (paths == NULL)
goto cleanup; goto cleanup;
@ -1008,7 +1008,7 @@ qemuProcessWaitForMonitor(struct qemud_driver* driver,
ret = qemuProcessFindCharDevicePTYsMonitor(vm, paths); ret = qemuProcessFindCharDevicePTYsMonitor(vm, paths);
cleanup: cleanup:
virHashFree(paths, qemuProcessFreePtyPath); virHashFree(paths);
if (kill(vm->pid, 0) == -1 && errno == ESRCH) { if (kill(vm->pid, 0) == -1 && errno == ESRCH) {
/* VM is dead, any other error raised in the interim is probably /* VM is dead, any other error raised in the interim is probably

View File

@ -55,6 +55,7 @@ struct _virHashTable {
struct _virHashEntry *table; struct _virHashEntry *table;
int size; int size;
int nbElems; int nbElems;
virHashDeallocator f;
}; };
/* /*
@ -80,13 +81,14 @@ virHashComputeKey(virHashTablePtr table, const char *name)
/** /**
* virHashCreate: * virHashCreate:
* @size: the size of the hash table * @size: the size of the hash table
* @deallocator: function to call on each entry during virHashFree
* *
* Create a new virHashTablePtr. * Create a new virHashTablePtr.
* *
* Returns the newly created object, or NULL if an error occured. * Returns the newly created object, or NULL if an error occured.
*/ */
virHashTablePtr virHashTablePtr
virHashCreate(int size) virHashCreate(int size, virHashDeallocator deallocator)
{ {
virHashTablePtr table = NULL; virHashTablePtr table = NULL;
@ -100,6 +102,7 @@ virHashCreate(int size)
table->size = size; table->size = size;
table->nbElems = 0; table->nbElems = 0;
table->f = deallocator;
if (VIR_ALLOC_N(table->table, size) < 0) { if (VIR_ALLOC_N(table->table, size) < 0) {
virReportOOMError(); virReportOOMError();
VIR_FREE(table); VIR_FREE(table);
@ -203,13 +206,12 @@ virHashGrow(virHashTablePtr table, int size)
/** /**
* virHashFree: * virHashFree:
* @table: the hash table * @table: the hash table
* @f: the deallocator function for items in the hash
* *
* Free the hash @table and its contents. The userdata is * Free the hash @table and its contents. The userdata is
* deallocated with @f if provided. * deallocated with function provided at creation time.
*/ */
void void
virHashFree(virHashTablePtr table, virHashDeallocator f) virHashFree(virHashTablePtr table)
{ {
int i; int i;
virHashEntryPtr iter; virHashEntryPtr iter;
@ -228,8 +230,8 @@ virHashFree(virHashTablePtr table, virHashDeallocator f)
inside_table = 1; inside_table = 1;
while (iter) { while (iter) {
next = iter->next; next = iter->next;
if ((f != NULL) && (iter->payload != NULL)) if ((table->f != NULL) && (iter->payload != NULL))
f(iter->payload, iter->name); table->f(iter->payload, iter->name);
VIR_FREE(iter->name); VIR_FREE(iter->name);
iter->payload = NULL; iter->payload = NULL;
if (!inside_table) if (!inside_table)

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
* *
* Copy: Copyright (C) 2005 Red Hat, Inc. * Copy: Copyright (C) 2005, 2011 Red Hat, Inc.
* *
* Author: Bjorn Reese <bjorn.reese@systematic.dk> * Author: Bjorn Reese <bjorn.reese@systematic.dk>
* Daniel Veillard <veillard@redhat.com> * 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 * Returns 1 if the hash entry is desired, 0 to move
* to next entry * 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. * Constructor and destructor.
*/ */
virHashTablePtr virHashCreate(int size); virHashTablePtr virHashCreate(int size, virHashDeallocator f);
void virHashFree(virHashTablePtr table, virHashDeallocator f); void virHashFree(virHashTablePtr table);
int virHashSize(virHashTablePtr table); int virHashSize(virHashTablePtr table);
/* /*

View File

@ -1,7 +1,7 @@
/* /*
* xm_internal.h: helper routines for dealing with inactive domains * 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 * Copyright (C) 2006 Daniel P. Berrange
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@ -587,12 +587,12 @@ xenXMOpen (virConnectPtr conn,
priv->configDir = XM_CONFIG_DIR; priv->configDir = XM_CONFIG_DIR;
priv->configCache = virHashCreate(50); priv->configCache = virHashCreate(50, xenXMConfigFree);
if (!priv->configCache) if (!priv->configCache)
return (-1); return (-1);
priv->nameConfigMap = virHashCreate(50); priv->nameConfigMap = virHashCreate(50, NULL);
if (!priv->nameConfigMap) { if (!priv->nameConfigMap) {
virHashFree(priv->configCache, NULL); virHashFree(priv->configCache);
priv->configCache = NULL; priv->configCache = NULL;
return (-1); return (-1);
} }
@ -611,8 +611,8 @@ xenXMOpen (virConnectPtr conn,
int xenXMClose(virConnectPtr conn) { int xenXMClose(virConnectPtr conn) {
xenUnifiedPrivatePtr priv = conn->privateData; xenUnifiedPrivatePtr priv = conn->privateData;
virHashFree(priv->nameConfigMap, NULL); virHashFree(priv->nameConfigMap);
virHashFree(priv->configCache, xenXMConfigFree); virHashFree(priv->configCache);
return (0); return (0);
} }