mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-07 17:28:15 +00: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:
parent
d6d4c08daf
commit
7c1a4bc775
@ -56,11 +56,6 @@ struct _virHashTable {
|
|||||||
size_t size;
|
size_t size;
|
||||||
size_t nbElems;
|
size_t nbElems;
|
||||||
virHashDataFree dataFree;
|
virHashDataFree dataFree;
|
||||||
virHashKeyCode keyCode;
|
|
||||||
virHashKeyEqual keyEqual;
|
|
||||||
virHashKeyCopy keyCopy;
|
|
||||||
virHashKeyPrintHuman keyPrint;
|
|
||||||
virHashKeyFree keyFree;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _virHashAtomic {
|
struct _virHashAtomic {
|
||||||
@ -81,40 +76,10 @@ static int virHashAtomicOnceInit(void)
|
|||||||
|
|
||||||
VIR_ONCE_GLOBAL_INIT(virHashAtomic);
|
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
|
static size_t
|
||||||
virHashComputeKey(const virHashTable *table, const char *name)
|
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;
|
return value % table->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,11 +103,6 @@ virHashNew(virHashDataFree dataFree)
|
|||||||
table->size = 32;
|
table->size = 32;
|
||||||
table->nbElems = 0;
|
table->nbElems = 0;
|
||||||
table->dataFree = dataFree;
|
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);
|
table->table = g_new0(virHashEntryPtr, table->size);
|
||||||
|
|
||||||
@ -260,8 +220,7 @@ virHashFree(virHashTablePtr table)
|
|||||||
|
|
||||||
if (table->dataFree)
|
if (table->dataFree)
|
||||||
table->dataFree(iter->payload);
|
table->dataFree(iter->payload);
|
||||||
if (table->keyFree)
|
g_free(iter->name);
|
||||||
table->keyFree(iter->name);
|
|
||||||
VIR_FREE(iter);
|
VIR_FREE(iter);
|
||||||
iter = next;
|
iter = next;
|
||||||
}
|
}
|
||||||
@ -287,20 +246,15 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const char *name,
|
|||||||
|
|
||||||
/* Check for duplicate entry */
|
/* Check for duplicate entry */
|
||||||
for (entry = table->table[key]; entry; entry = entry->next) {
|
for (entry = table->table[key]; entry; entry = entry->next) {
|
||||||
if (table->keyEqual(entry->name, name)) {
|
if (STREQ(entry->name, name)) {
|
||||||
if (is_update) {
|
if (is_update) {
|
||||||
if (table->dataFree)
|
if (table->dataFree)
|
||||||
table->dataFree(entry->payload);
|
table->dataFree(entry->payload);
|
||||||
entry->payload = userdata;
|
entry->payload = userdata;
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
g_autofree char *keystr = NULL;
|
|
||||||
|
|
||||||
if (table->keyPrint)
|
|
||||||
keystr = table->keyPrint(name);
|
|
||||||
|
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
_("Duplicate hash table key '%s'"), NULLSTR(keystr));
|
_("Duplicate hash table key '%s'"), name);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -309,7 +263,7 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const char *name,
|
|||||||
}
|
}
|
||||||
|
|
||||||
entry = g_new0(virHashEntry, 1);
|
entry = g_new0(virHashEntry, 1);
|
||||||
entry->name = table->keyCopy(name);
|
entry->name = g_strdup(name);
|
||||||
entry->payload = userdata;
|
entry->payload = userdata;
|
||||||
|
|
||||||
if (last)
|
if (last)
|
||||||
@ -388,7 +342,7 @@ virHashGetEntry(const virHashTable *table,
|
|||||||
|
|
||||||
key = virHashComputeKey(table, name);
|
key = virHashComputeKey(table, name);
|
||||||
for (entry = table->table[key]; entry; entry = entry->next) {
|
for (entry = table->table[key]; entry; entry = entry->next) {
|
||||||
if (table->keyEqual(entry->name, name))
|
if (STREQ(entry->name, name))
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -510,11 +464,10 @@ virHashRemoveEntry(virHashTablePtr table, const char *name)
|
|||||||
|
|
||||||
nextptr = table->table + virHashComputeKey(table, name);
|
nextptr = table->table + virHashComputeKey(table, name);
|
||||||
for (entry = *nextptr; entry; entry = entry->next) {
|
for (entry = *nextptr; entry; entry = entry->next) {
|
||||||
if (table->keyEqual(entry->name, name)) {
|
if (STREQ(entry->name, name)) {
|
||||||
if (table->dataFree)
|
if (table->dataFree)
|
||||||
table->dataFree(entry->payload);
|
table->dataFree(entry->payload);
|
||||||
if (table->keyFree)
|
g_free(entry->name);
|
||||||
table->keyFree(entry->name);
|
|
||||||
*nextptr = entry->next;
|
*nextptr = entry->next;
|
||||||
VIR_FREE(entry);
|
VIR_FREE(entry);
|
||||||
table->nbElems--;
|
table->nbElems--;
|
||||||
@ -601,8 +554,7 @@ virHashRemoveSet(virHashTablePtr table,
|
|||||||
count++;
|
count++;
|
||||||
if (table->dataFree)
|
if (table->dataFree)
|
||||||
table->dataFree(entry->payload);
|
table->dataFree(entry->payload);
|
||||||
if (table->keyFree)
|
g_free(entry->name);
|
||||||
table->keyFree(entry->name);
|
|
||||||
*nextptr = entry->next;
|
*nextptr = entry->next;
|
||||||
VIR_FREE(entry);
|
VIR_FREE(entry);
|
||||||
table->nbElems--;
|
table->nbElems--;
|
||||||
@ -669,7 +621,7 @@ void *virHashSearch(const virHashTable *ctable,
|
|||||||
for (entry = table->table[i]; entry; entry = entry->next) {
|
for (entry = table->table[i]; entry; entry = entry->next) {
|
||||||
if (iter(entry->payload, entry->name, data)) {
|
if (iter(entry->payload, entry->name, data)) {
|
||||||
if (name)
|
if (name)
|
||||||
*name = table->keyCopy(entry->name);
|
*name = g_strdup(entry->name);
|
||||||
return entry->payload;
|
return entry->payload;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,60 +54,6 @@ typedef int (*virHashIterator) (void *payload, const char *name, void *data);
|
|||||||
typedef int (*virHashSearcher) (const void *payload, const char *name,
|
typedef int (*virHashSearcher) (const void *payload, const char *name,
|
||||||
const void *data);
|
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.
|
* Constructor and destructor.
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user