1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-02-03 17:47:15 +03:00

util: virhash: Remove key handling callbacks

Since we use virHashTable for string-keyed values only, we can remove
all the callbacks which allowed universal keys.

Code which wishes to use non-string keys should use glib's GHashTable.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
Peter Krempa 2020-10-21 13:52:01 +02:00
parent d6d4c08daf
commit 7c1a4bc775
2 changed files with 10 additions and 112 deletions

View File

@ -56,11 +56,6 @@ struct _virHashTable {
size_t size;
size_t nbElems;
virHashDataFree dataFree;
virHashKeyCode keyCode;
virHashKeyEqual keyEqual;
virHashKeyCopy keyCopy;
virHashKeyPrintHuman keyPrint;
virHashKeyFree keyFree;
};
struct _virHashAtomic {
@ -81,40 +76,10 @@ static int virHashAtomicOnceInit(void)
VIR_ONCE_GLOBAL_INIT(virHashAtomic);
static uint32_t virHashStrCode(const char *name, uint32_t seed)
{
return virHashCodeGen(name, strlen(name), seed);
}
static bool virHashStrEqual(const char *namea, const char *nameb)
{
return STREQ(namea, nameb);
}
static char *virHashStrCopy(const char *name)
{
return g_strdup(name);
}
static char *
virHashStrPrintHuman(const char *name)
{
return g_strdup(name);
}
static void virHashStrFree(char *name)
{
VIR_FREE(name);
}
static size_t
virHashComputeKey(const virHashTable *table, const char *name)
{
uint32_t value = table->keyCode(name, table->seed);
uint32_t value = virHashCodeGen(name, strlen(name), table->seed);
return value % table->size;
}
@ -138,11 +103,6 @@ virHashNew(virHashDataFree dataFree)
table->size = 32;
table->nbElems = 0;
table->dataFree = dataFree;
table->keyCode = virHashStrCode;
table->keyEqual = virHashStrEqual;
table->keyCopy = virHashStrCopy;
table->keyPrint = virHashStrPrintHuman;
table->keyFree = virHashStrFree;
table->table = g_new0(virHashEntryPtr, table->size);
@ -260,8 +220,7 @@ virHashFree(virHashTablePtr table)
if (table->dataFree)
table->dataFree(iter->payload);
if (table->keyFree)
table->keyFree(iter->name);
g_free(iter->name);
VIR_FREE(iter);
iter = next;
}
@ -287,20 +246,15 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const char *name,
/* Check for duplicate entry */
for (entry = table->table[key]; entry; entry = entry->next) {
if (table->keyEqual(entry->name, name)) {
if (STREQ(entry->name, name)) {
if (is_update) {
if (table->dataFree)
table->dataFree(entry->payload);
entry->payload = userdata;
return 0;
} else {
g_autofree char *keystr = NULL;
if (table->keyPrint)
keystr = table->keyPrint(name);
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Duplicate hash table key '%s'"), NULLSTR(keystr));
_("Duplicate hash table key '%s'"), name);
return -1;
}
}
@ -309,7 +263,7 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const char *name,
}
entry = g_new0(virHashEntry, 1);
entry->name = table->keyCopy(name);
entry->name = g_strdup(name);
entry->payload = userdata;
if (last)
@ -388,7 +342,7 @@ virHashGetEntry(const virHashTable *table,
key = virHashComputeKey(table, name);
for (entry = table->table[key]; entry; entry = entry->next) {
if (table->keyEqual(entry->name, name))
if (STREQ(entry->name, name))
return entry;
}
@ -510,11 +464,10 @@ virHashRemoveEntry(virHashTablePtr table, const char *name)
nextptr = table->table + virHashComputeKey(table, name);
for (entry = *nextptr; entry; entry = entry->next) {
if (table->keyEqual(entry->name, name)) {
if (STREQ(entry->name, name)) {
if (table->dataFree)
table->dataFree(entry->payload);
if (table->keyFree)
table->keyFree(entry->name);
g_free(entry->name);
*nextptr = entry->next;
VIR_FREE(entry);
table->nbElems--;
@ -601,8 +554,7 @@ virHashRemoveSet(virHashTablePtr table,
count++;
if (table->dataFree)
table->dataFree(entry->payload);
if (table->keyFree)
table->keyFree(entry->name);
g_free(entry->name);
*nextptr = entry->next;
VIR_FREE(entry);
table->nbElems--;
@ -669,7 +621,7 @@ void *virHashSearch(const virHashTable *ctable,
for (entry = table->table[i]; entry; entry = entry->next) {
if (iter(entry->payload, entry->name, data)) {
if (name)
*name = table->keyCopy(entry->name);
*name = g_strdup(entry->name);
return entry->payload;
}
}

View File

@ -54,60 +54,6 @@ typedef int (*virHashIterator) (void *payload, const char *name, void *data);
typedef int (*virHashSearcher) (const void *payload, const char *name,
const void *data);
/**
* virHashKeyCode:
* @name: the hash key
* @seed: random seed
*
* Compute the hash code corresponding to the key @name, using
* @seed to perturb the hashing algorithm
*
* Returns the hash code
*/
typedef uint32_t (*virHashKeyCode)(const char *name,
uint32_t seed);
/**
* 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 char *namea, const char *nameb);
/**
* virHashKeyCopy:
* @name: the hash key
*
* Create a copy of the hash key, duplicating
* memory allocation where applicable
*
* Returns a copy of @name which will eventually be passed to the
* 'virHashKeyFree' callback at the end of its lifetime.
*/
typedef char *(*virHashKeyCopy)(const char *name);
/**
* virHashKeyPrintHuman:
* @name: the hash key
*
* Get a human readable version of the key for error messages. Caller
* will free the returned string.
*
* Returns a string representation of the key for use in error messages. Caller
* promises to always free the returned string.
*/
typedef char *(*virHashKeyPrintHuman) (const char *name);
/**
* virHashKeyFree:
* @name: the hash key
*
* Free any memory associated with the hash
* key @name
*/
typedef void (*virHashKeyFree)(char *name);
/*
* Constructor and destructor.
*/