Allow hash tables to use generic pointers as keys

Relax the restriction that the hash table key must be a string
by allowing an arbitrary hash code generator + comparison func
to be provided

* util/hash.c, util/hash.h: Allow any pointer as a key
* internal.h: Include stdbool.h as standard.
* conf/domain_conf.c, conf/domain_conf.c,
  conf/nwfilter_params.c, nwfilter/nwfilter_gentech_driver.c,
  nwfilter/nwfilter_gentech_driver.h, nwfilter/nwfilter_learnipaddr.c,
  qemu/qemu_command.c, qemu/qemu_driver.c,
  qemu/qemu_process.c, uml/uml_driver.c,
  xen/xm_internal.c: s/char */void */ in hash callbacks
This commit is contained in:
Daniel P. Berrange 2011-02-22 15:11:59 +00:00
parent 6952708ca4
commit 16ba2aafc4
13 changed files with 181 additions and 77 deletions

View File

@ -392,7 +392,7 @@ VIR_ENUM_IMPL(virDomainTimerMode, VIR_DOMAIN_TIMER_MODE_LAST,
#define VIR_DOMAIN_XML_READ_FLAGS VIR_DOMAIN_XML_INACTIVE
static void
virDomainObjListDataFree(void *payload, const char *name ATTRIBUTE_UNUSED)
virDomainObjListDataFree(void *payload, const void *name ATTRIBUTE_UNUSED)
{
virDomainObjPtr obj = payload;
virDomainObjLock(obj);
@ -416,7 +416,7 @@ void virDomainObjListDeinit(virDomainObjListPtr doms)
static int virDomainObjListSearchID(const void *payload,
const char *name ATTRIBUTE_UNUSED,
const void *name ATTRIBUTE_UNUSED,
const void *data)
{
virDomainObjPtr obj = (virDomainObjPtr)payload;
@ -457,7 +457,7 @@ virDomainObjPtr virDomainFindByUUID(const virDomainObjListPtr doms,
}
static int virDomainObjListSearchName(const void *payload,
const char *name ATTRIBUTE_UNUSED,
const void *name ATTRIBUTE_UNUSED,
const void *data)
{
virDomainObjPtr obj = (virDomainObjPtr)payload;
@ -8435,7 +8435,7 @@ void virDomainObjUnlock(virDomainObjPtr obj)
}
static void virDomainObjListCountActive(void *payload, const char *name ATTRIBUTE_UNUSED, void *data)
static void virDomainObjListCountActive(void *payload, const void *name ATTRIBUTE_UNUSED, void *data)
{
virDomainObjPtr obj = payload;
int *count = data;
@ -8445,7 +8445,7 @@ static void virDomainObjListCountActive(void *payload, const char *name ATTRIBUT
virDomainObjUnlock(obj);
}
static void virDomainObjListCountInactive(void *payload, const char *name ATTRIBUTE_UNUSED, void *data)
static void virDomainObjListCountInactive(void *payload, const void *name ATTRIBUTE_UNUSED, void *data)
{
virDomainObjPtr obj = payload;
int *count = data;
@ -8471,7 +8471,7 @@ struct virDomainIDData {
int *ids;
};
static void virDomainObjListCopyActiveIDs(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque)
static void virDomainObjListCopyActiveIDs(void *payload, const void *name ATTRIBUTE_UNUSED, void *opaque)
{
virDomainObjPtr obj = payload;
struct virDomainIDData *data = opaque;
@ -8497,7 +8497,7 @@ struct virDomainNameData {
char **const names;
};
static void virDomainObjListCopyInactiveNames(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque)
static void virDomainObjListCopyInactiveNames(void *payload, const void *name ATTRIBUTE_UNUSED, void *opaque)
{
virDomainObjPtr obj = payload;
struct virDomainNameData *data = opaque;
@ -8754,7 +8754,7 @@ virDomainSnapshotObjPtr virDomainSnapshotAssignDef(virDomainSnapshotObjListPtr s
/* Snapshot Obj List functions */
static void
virDomainSnapshotObjListDataFree(void *payload,
const char *name ATTRIBUTE_UNUSED)
const void *name ATTRIBUTE_UNUSED)
{
virDomainSnapshotObjPtr obj = payload;
@ -8783,7 +8783,7 @@ struct virDomainSnapshotNameData {
};
static void virDomainSnapshotObjListCopyNames(void *payload,
const char *name ATTRIBUTE_UNUSED,
const void *name ATTRIBUTE_UNUSED,
void *opaque)
{
virDomainSnapshotObjPtr obj = payload;
@ -8821,7 +8821,7 @@ cleanup:
}
static void virDomainSnapshotObjListCount(void *payload ATTRIBUTE_UNUSED,
const char *name ATTRIBUTE_UNUSED,
const void *name ATTRIBUTE_UNUSED,
void *data)
{
int *count = data;
@ -8839,7 +8839,7 @@ int virDomainSnapshotObjListNum(virDomainSnapshotObjListPtr snapshots)
}
static int virDomainSnapshotObjListSearchName(const void *payload,
const char *name ATTRIBUTE_UNUSED,
const void *name ATTRIBUTE_UNUSED,
const void *data)
{
virDomainSnapshotObjPtr obj = (virDomainSnapshotObjPtr)payload;
@ -8869,7 +8869,7 @@ struct snapshot_has_children {
};
static void virDomainSnapshotCountChildren(void *payload,
const char *name ATTRIBUTE_UNUSED,
const void *name ATTRIBUTE_UNUSED,
void *data)
{
virDomainSnapshotObjPtr obj = payload;

View File

@ -35,7 +35,7 @@
#define VIR_FROM_THIS VIR_FROM_NWFILTER
static void
hashDataFree(void *payload, const char *name ATTRIBUTE_UNUSED)
hashDataFree(void *payload, const void *name ATTRIBUTE_UNUSED)
{
VIR_FREE(payload);
}
@ -157,7 +157,7 @@ struct addToTableStruct {
static void
addToTable(void *payload, const char *name, void *data)
addToTable(void *payload, const void *name, void *data)
{
struct addToTableStruct *atts = (struct addToTableStruct *)data;
char *val;
@ -172,10 +172,10 @@ addToTable(void *payload, const char *name, void *data)
return;
}
if (virNWFilterHashTablePut(atts->target, name, val, 1) != 0) {
if (virNWFilterHashTablePut(atts->target, (const char *)name, val, 1) != 0) {
virNWFilterReportError(VIR_ERR_INTERNAL_ERROR,
_("Could not put variable '%s' into hashmap"),
name);
(const char *)name);
atts->errOccurred = 1;
VIR_FREE(val);
}
@ -265,13 +265,13 @@ struct formatterParam {
static void
_formatParameterAttrs(void *payload, const char *name, void *data)
_formatParameterAttrs(void *payload, const void *name, void *data)
{
struct formatterParam *fp = (struct formatterParam *)data;
virBufferVSprintf(fp->buf, "%s<parameter name='%s' value='%s'/>\n",
fp->indent,
name,
(const char *)name,
(char *)payload);
}

View File

@ -1967,7 +1967,7 @@ struct lxcAutostartData {
};
static void
lxcAutostartDomain(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque)
lxcAutostartDomain(void *payload, const void *name ATTRIBUTE_UNUSED, void *opaque)
{
virDomainObjPtr vm = payload;
const struct lxcAutostartData *data = opaque;
@ -2014,7 +2014,7 @@ lxcAutostartConfigs(lxc_driver_t *driver) {
}
static void
lxcReconnectVM(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque)
lxcReconnectVM(void *payload, const void *name ATTRIBUTE_UNUSED, void *opaque)
{
virDomainObjPtr vm = payload;
lxc_driver_t *driver = opaque;

View File

@ -1012,7 +1012,7 @@ virNWFilterTeardownFilter(const virDomainNetDefPtr net)
void
virNWFilterDomainFWUpdateCB(void *payload,
const char *name ATTRIBUTE_UNUSED,
const void *name ATTRIBUTE_UNUSED,
void *data)
{
virDomainObjPtr obj = payload;

View File

@ -64,7 +64,7 @@ virNWFilterHashTablePtr virNWFilterCreateVarHashmap(char *macaddr,
char *ipaddr);
void virNWFilterDomainFWUpdateCB(void *payload,
const char *name ATTRIBUTE_UNUSED,
const void *name,
void *data);
#endif

View File

@ -189,7 +189,7 @@ virNWFilterLockIface(const char *ifname) {
static void
freeIfaceLock(void *payload, const char *name ATTRIBUTE_UNUSED) {
freeIfaceLock(void *payload, const void *name ATTRIBUTE_UNUSED) {
VIR_FREE(payload);
}
@ -287,7 +287,7 @@ virNWFilterLookupLearnReq(int ifindex) {
static void
freeLearnReqEntry(void *payload, const char *name ATTRIBUTE_UNUSED) {
freeLearnReqEntry(void *payload, const void *name ATTRIBUTE_UNUSED) {
virNWFilterIPAddrLearnReqFree(payload);
}

View File

@ -748,7 +748,7 @@ cleanup:
static void
qemuDomainPCIAddressSetFreeEntry(void *payload,
const char *name ATTRIBUTE_UNUSED)
const void *name ATTRIBUTE_UNUSED)
{
VIR_FREE(payload);
}

View File

@ -132,7 +132,7 @@ struct qemuAutostartData {
};
static void
qemuAutostartDomain(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque)
qemuAutostartDomain(void *payload, const void *name ATTRIBUTE_UNUSED, void *opaque)
{
virDomainObjPtr vm = payload;
struct qemuAutostartData *data = opaque;
@ -275,7 +275,7 @@ err_exit:
}
static void qemuDomainSnapshotLoad(void *payload,
const char *name ATTRIBUTE_UNUSED,
const void *name ATTRIBUTE_UNUSED,
void *data)
{
virDomainObjPtr vm = (virDomainObjPtr)payload;
@ -6519,7 +6519,7 @@ struct snap_remove {
};
static void qemuDomainSnapshotDiscardChildren(void *payload,
const char *name ATTRIBUTE_UNUSED,
const void *name ATTRIBUTE_UNUSED,
void *data)
{
virDomainSnapshotObjPtr snap = payload;
@ -6550,7 +6550,7 @@ struct snap_reparent {
static void
qemuDomainSnapshotReparentChildren(void *payload,
const char *name ATTRIBUTE_UNUSED,
const void *name ATTRIBUTE_UNUSED,
void *data)
{
virDomainSnapshotObjPtr snap = payload;

View File

@ -949,7 +949,7 @@ qemuProcessFindCharDevicePTYs(virDomainObjPtr vm,
return 0;
}
static void qemuProcessFreePtyPath(void *payload, const char *name ATTRIBUTE_UNUSED)
static void qemuProcessFreePtyPath(void *payload, const void *name ATTRIBUTE_UNUSED)
{
VIR_FREE(payload);
}
@ -1783,7 +1783,7 @@ struct qemuProcessReconnectData {
* and re-reserve the security labels in use
*/
static void
qemuProcessReconnect(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque)
qemuProcessReconnect(void *payload, const void *name ATTRIBUTE_UNUSED, void *opaque)
{
virDomainObjPtr obj = payload;
struct qemuProcessReconnectData *data = opaque;

View File

@ -149,7 +149,7 @@ struct umlAutostartData {
};
static void
umlAutostartDomain(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque)
umlAutostartDomain(void *payload, const void *name ATTRIBUTE_UNUSED, void *opaque)
{
virDomainObjPtr vm = payload;
const struct umlAutostartData *data = opaque;
@ -508,7 +508,7 @@ umlActive(void) {
}
static void
umlShutdownOneVM(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque)
umlShutdownOneVM(void *payload, const void *name ATTRIBUTE_UNUSED, void *opaque)
{
virDomainObjPtr dom = payload;
struct uml_driver *driver = opaque;

View File

@ -42,7 +42,7 @@ typedef struct _virHashEntry virHashEntry;
typedef virHashEntry *virHashEntryPtr;
struct _virHashEntry {
struct _virHashEntry *next;
char *name;
void *name;
void *payload;
int valid;
};
@ -55,39 +55,70 @@ struct _virHashTable {
int size;
int nbElems;
virHashDataFree dataFree;
virHashKeyCode keyCode;
virHashKeyEqual keyEqual;
virHashKeyCopy keyCopy;
virHashKeyFree keyFree;
};
/*
* virHashComputeKey:
* Calculate the hash key
*/
static unsigned long
virHashComputeKey(virHashTablePtr table, const char *name)
static unsigned long virHashStrCode(const void *name)
{
const char *str = name;
unsigned long value = 0L;
char ch;
if (name != NULL) {
value += 30 * (*name);
while ((ch = *name++) != 0) {
if (str != NULL) {
value += 30 * (*str);
while ((ch = *str++) != 0) {
value =
value ^ ((value << 5) + (value >> 3) + (unsigned long) ch);
}
}
return value;
}
static bool virHashStrEqual(const void *namea, const void *nameb)
{
return STREQ(namea, nameb);
}
static void *virHashStrCopy(const void *name)
{
return strdup(name);
}
static void virHashStrFree(void *name)
{
VIR_FREE(name);
}
static unsigned long
virHashComputeKey(virHashTablePtr table, const void *name)
{
unsigned long value = table->keyCode(name);
return (value % table->size);
}
/**
* virHashCreate:
* virHashCreateFull:
* @size: the size of the hash table
* @dataFree: function to call on each entry during virHashFree
* @dataFree: callback to free data
* @keyCode: callback to compute hash code
* @keyEqual: callback to compare hash keys
* @keyCopy: callback to copy hash keys
* @keyFree: callback to free keys
*
* Create a new virHashTablePtr.
*
* Returns the newly created object, or NULL if an error occured.
*/
virHashTablePtr
virHashCreate(int size, virHashDataFree dataFree)
virHashTablePtr virHashCreateFull(int size,
virHashDataFree dataFree,
virHashKeyCode keyCode,
virHashKeyEqual keyEqual,
virHashKeyCopy keyCopy,
virHashKeyFree keyFree)
{
virHashTablePtr table = NULL;
@ -102,6 +133,11 @@ virHashCreate(int size, virHashDataFree dataFree)
table->size = size;
table->nbElems = 0;
table->dataFree = dataFree;
table->keyCode = keyCode;
table->keyEqual = keyEqual;
table->keyCopy = keyCopy;
table->keyFree = keyFree;
if (VIR_ALLOC_N(table->table, size) < 0) {
virReportOOMError();
VIR_FREE(table);
@ -111,6 +147,26 @@ virHashCreate(int size, virHashDataFree dataFree)
return table;
}
/**
* virHashCreate:
* @size: the size of the hash table
* @dataFree: callback to free data
*
* Create a new virHashTablePtr.
*
* Returns the newly created object, or NULL if an error occured.
*/
virHashTablePtr virHashCreate(int size, virHashDataFree dataFree)
{
return virHashCreateFull(size,
dataFree,
virHashStrCode,
virHashStrEqual,
virHashStrCopy,
virHashStrFree);
}
/**
* virHashGrow:
* @table: the hash table
@ -231,7 +287,8 @@ virHashFree(virHashTablePtr table)
next = iter->next;
if ((table->dataFree != NULL) && (iter->payload != NULL))
table->dataFree(iter->payload, iter->name);
VIR_FREE(iter->name);
if (table->keyFree)
table->keyFree(iter->name);
iter->payload = NULL;
if (!inside_table)
VIR_FREE(iter);
@ -269,13 +326,13 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const void *name,
} else {
for (insert = &(table->table[key]); insert->next != NULL;
insert = insert->next) {
if (STREQ(insert->name, name)) {
if (table->keyEqual(insert->name, name)) {
found = true;
break;
}
len++;
}
if (STREQ(insert->name, name))
if (table->keyEqual(insert->name, name))
found = true;
}
@ -299,7 +356,7 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const void *name,
}
}
new_name = strdup(name);
new_name = table->keyCopy(name);
if (new_name == NULL) {
virReportOOMError();
if (insert != NULL)
@ -334,7 +391,7 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const void *name,
* Returns 0 the addition succeeded and -1 in case of error.
*/
int
virHashAddEntry(virHashTablePtr table, const char *name, void *userdata)
virHashAddEntry(virHashTablePtr table, const void *name, void *userdata)
{
return virHashAddOrUpdateEntry(table, name, userdata, false);
}
@ -352,7 +409,7 @@ virHashAddEntry(virHashTablePtr table, const char *name, void *userdata)
* Returns 0 the addition succeeded and -1 in case of error.
*/
int
virHashUpdateEntry(virHashTablePtr table, const char *name,
virHashUpdateEntry(virHashTablePtr table, const void *name,
void *userdata)
{
return virHashAddOrUpdateEntry(table, name, userdata, true);
@ -363,12 +420,12 @@ virHashUpdateEntry(virHashTablePtr table, const char *name,
* @table: the hash table
* @name: the name of the userdata
*
* Find the userdata specified by the (@name, @name2, @name3) tuple.
* Find the userdata specified by @name
*
* Returns the a pointer to the userdata
*/
void *
virHashLookup(virHashTablePtr table, const char *name)
virHashLookup(virHashTablePtr table, const void *name)
{
unsigned long key;
virHashEntryPtr entry;
@ -381,7 +438,7 @@ virHashLookup(virHashTablePtr table, const char *name)
if (table->table[key].valid == 0)
return (NULL);
for (entry = &(table->table[key]); entry != NULL; entry = entry->next) {
if (STREQ(entry->name, name))
if (table->keyEqual(entry->name, name))
return (entry->payload);
}
return (NULL);
@ -398,7 +455,7 @@ virHashLookup(virHashTablePtr table, const char *name)
*
* Returns the a pointer to the userdata
*/
void *virHashSteal(virHashTablePtr table, const char *name)
void *virHashSteal(virHashTablePtr table, const void *name)
{
void *data = virHashLookup(table, name);
if (data) {
@ -440,7 +497,7 @@ virHashSize(virHashTablePtr table)
* Returns 0 if the removal succeeded and -1 in case of error or not found.
*/
int
virHashRemoveEntry(virHashTablePtr table, const char *name)
virHashRemoveEntry(virHashTablePtr table, const void *name)
{
unsigned long key;
virHashEntryPtr entry;
@ -455,11 +512,12 @@ virHashRemoveEntry(virHashTablePtr table, const char *name)
} else {
for (entry = &(table->table[key]); entry != NULL;
entry = entry->next) {
if (STREQ(entry->name, name)) {
if (table->keyEqual(entry->name, name)) {
if (table->dataFree && (entry->payload != NULL))
table->dataFree(entry->payload, entry->name);
entry->payload = NULL;
VIR_FREE(entry->name);
if (table->keyFree)
table->keyFree(entry->name);
if (prev) {
prev->next = entry->next;
VIR_FREE(entry);
@ -544,7 +602,8 @@ int virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void *da
count++;
if (table->dataFree)
table->dataFree(entry->payload, entry->name);
VIR_FREE(entry->name);
if (table->keyFree)
table->keyFree(entry->name);
table->nbElems--;
if (prev) {
prev->next = entry->next;

View File

@ -29,33 +29,78 @@ typedef virHashTable *virHashTablePtr;
*
* Callback to free data from a hash.
*/
typedef void (*virHashDataFree) (void *payload, const char *name);
typedef void (*virHashDataFree) (void *payload, const void *name);
/**
* virHashIterator:
* @payload: the data in the hash
* @name: the name associated
* @name: the hash key
* @data: user supplied data blob
*
* Callback to process a hash entry during iteration
*/
typedef void (*virHashIterator) (void *payload, const char *name, void *data);
typedef void (*virHashIterator) (void *payload, const void *name, void *data);
/**
* virHashSearcher
* virHashSearcher:
* @payload: the data in the hash
* @name: the name associated
* @name: the hash key
* @data: user supplied data blob
*
* Callback to identify hash entry desired
* Returns 1 if the hash entry is desired, 0 to move
* to next entry
*/
typedef int (*virHashSearcher) (const void *payload, const char *name,
typedef int (*virHashSearcher) (const void *payload, const void *name,
const void *data);
/**
* virHashKeyCode:
* @name: the hash key
*
* Compute the hash code corresponding to the key @name
*
* Returns the hash code
*/
typedef unsigned long (*virHashKeyCode)(const void *name);
/**
* virHashKeyEqual:
* @namea: the first hash key
* @nameb: the second hash key
*
* Compare two hash keys for equality
*
* Returns true if the keys are equal, false otherwise
*/
typedef bool (*virHashKeyEqual)(const void *namea, const void *nameb);
/**
* virHashKeyCopy:
* @name: the hash key
*
* Create a copy of the hash key, duplicating
* memory allocation where applicable
*
* Returns a newly allocated copy of @name
*/
typedef void *(*virHashKeyCopy)(const void *name);
/**
* virHashKeyFree:
* @name: the hash key
*
* Free any memory associated with the hash
* key @name
*/
typedef void (*virHashKeyFree)(void *name);
/*
* Constructor and destructor.
*/
virHashTablePtr virHashCreate(int size, virHashDataFree dataFree);
virHashTablePtr virHashCreate(int size,
virHashDataFree dataFree);
virHashTablePtr virHashCreateFull(int size,
virHashDataFree dataFree,
virHashKeyCode keyCode,
virHashKeyEqual keyEqual,
virHashKeyCopy keyCopy,
virHashKeyFree keyFree);
void virHashFree(virHashTablePtr table);
int virHashSize(virHashTablePtr table);
@ -63,26 +108,26 @@ int virHashSize(virHashTablePtr table);
* Add a new entry to the hash table.
*/
int virHashAddEntry(virHashTablePtr table,
const char *name, void *userdata);
const void *name, void *userdata);
int virHashUpdateEntry(virHashTablePtr table,
const char *name,
const void *name,
void *userdata);
/*
* Remove an entry from the hash table.
*/
int virHashRemoveEntry(virHashTablePtr table,
const char *name);
const void *name);
/*
* Retrieve the userdata.
*/
void *virHashLookup(virHashTablePtr table, const char *name);
void *virHashLookup(virHashTablePtr table, const void *name);
/*
* Retrieve & remove the userdata.
*/
void *virHashSteal(virHashTablePtr table, const char *name);
void *virHashSteal(virHashTablePtr table, const void *name);
/*

View File

@ -139,7 +139,7 @@ static int xenInotifyActive(virConnectPtr conn)
/* Release memory associated with a cached config object */
static void xenXMConfigFree(void *payload, const char *key ATTRIBUTE_UNUSED) {
static void xenXMConfigFree(void *payload, const void *key ATTRIBUTE_UNUSED) {
xenXMConfCachePtr entry = (xenXMConfCachePtr)payload;
virDomainDefFree(entry->def);
VIR_FREE(entry);
@ -151,7 +151,7 @@ struct xenXMConfigReaperData {
};
/* Remove any configs which were not refreshed recently */
static int xenXMConfigReaper(const void *payload, const char *key ATTRIBUTE_UNUSED, const void *data) {
static int xenXMConfigReaper(const void *payload, const void *key ATTRIBUTE_UNUSED, const void *data) {
const struct xenXMConfigReaperData *args = data;
xenXMConfCachePtr entry = (xenXMConfCachePtr)payload;
@ -938,7 +938,7 @@ cleanup:
/*
* Hash table iterator to search for a domain based on UUID
*/
static int xenXMDomainSearchForUUID(const void *payload, const char *name ATTRIBUTE_UNUSED, const void *data) {
static int xenXMDomainSearchForUUID(const void *payload, const void *name ATTRIBUTE_UNUSED, const void *data) {
const unsigned char *wantuuid = (const unsigned char *)data;
const xenXMConfCachePtr entry = (const xenXMConfCachePtr)payload;
@ -1235,7 +1235,7 @@ struct xenXMListIteratorContext {
char ** names;
};
static void xenXMListIterator(void *payload ATTRIBUTE_UNUSED, const char *name, void *data) {
static void xenXMListIterator(void *payload ATTRIBUTE_UNUSED, const void *name, void *data) {
struct xenXMListIteratorContext *ctx = data;
virDomainPtr dom = NULL;