mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-02-23 03:42:19 +00:00
Convert various virHash functions to use size_t / uint32
In preparation for conversion over to use the Murmurhash3 algorithm, convert various virHash APIs to use size_t or uint32 for their return values/parameters, instead of the variable size 'unsigned long' or 'int' types
This commit is contained in:
parent
e95ef67b35
commit
9f2bf8fd03
@ -1636,9 +1636,9 @@ cleanup:
|
||||
}
|
||||
|
||||
|
||||
static unsigned long virCgroupPidCode(const void *name)
|
||||
static uint32_t virCgroupPidCode(const void *name)
|
||||
{
|
||||
return (unsigned long)name;
|
||||
return (uint32_t)(intptr_t)name;
|
||||
}
|
||||
static bool virCgroupPidEqual(const void *namea, const void *nameb)
|
||||
{
|
||||
|
@ -57,8 +57,8 @@ struct _virHashEntry {
|
||||
*/
|
||||
struct _virHashTable {
|
||||
virHashEntryPtr *table;
|
||||
int size;
|
||||
int nbElems;
|
||||
size_t size;
|
||||
size_t nbElems;
|
||||
/* True iff we are iterating over hash entries. */
|
||||
bool iterating;
|
||||
/* Pointer to the current entry during iteration. */
|
||||
@ -70,17 +70,17 @@ struct _virHashTable {
|
||||
virHashKeyFree keyFree;
|
||||
};
|
||||
|
||||
static unsigned long virHashStrCode(const void *name)
|
||||
static uint32_t virHashStrCode(const void *name)
|
||||
{
|
||||
const char *str = name;
|
||||
unsigned long value = 0L;
|
||||
uint32_t value = 0L;
|
||||
char ch;
|
||||
|
||||
if (str != NULL) {
|
||||
value += 30 * (*str);
|
||||
while ((ch = *str++) != 0) {
|
||||
value =
|
||||
value ^ ((value << 5) + (value >> 3) + (unsigned long) ch);
|
||||
value ^ ((value << 5) + (value >> 3) + (uint32_t) ch);
|
||||
}
|
||||
}
|
||||
return value;
|
||||
@ -102,10 +102,10 @@ static void virHashStrFree(void *name)
|
||||
}
|
||||
|
||||
|
||||
static unsigned long
|
||||
static size_t
|
||||
virHashComputeKey(virHashTablePtr table, const void *name)
|
||||
{
|
||||
unsigned long value = table->keyCode(name);
|
||||
uint32_t value = table->keyCode(name);
|
||||
return (value % table->size);
|
||||
}
|
||||
|
||||
@ -122,7 +122,7 @@ virHashComputeKey(virHashTablePtr table, const void *name)
|
||||
*
|
||||
* Returns the newly created object, or NULL if an error occurred.
|
||||
*/
|
||||
virHashTablePtr virHashCreateFull(int size,
|
||||
virHashTablePtr virHashCreateFull(ssize_t size,
|
||||
virHashDataFree dataFree,
|
||||
virHashKeyCode keyCode,
|
||||
virHashKeyEqual keyEqual,
|
||||
@ -166,7 +166,7 @@ virHashTablePtr virHashCreateFull(int size,
|
||||
*
|
||||
* Returns the newly created object, or NULL if an error occurred.
|
||||
*/
|
||||
virHashTablePtr virHashCreate(int size, virHashDataFree dataFree)
|
||||
virHashTablePtr virHashCreate(ssize_t size, virHashDataFree dataFree)
|
||||
{
|
||||
return virHashCreateFull(size,
|
||||
dataFree,
|
||||
@ -186,13 +186,13 @@ virHashTablePtr virHashCreate(int size, virHashDataFree dataFree)
|
||||
* Returns 0 in case of success, -1 in case of failure
|
||||
*/
|
||||
static int
|
||||
virHashGrow(virHashTablePtr table, int size)
|
||||
virHashGrow(virHashTablePtr table, size_t size)
|
||||
{
|
||||
int oldsize, i;
|
||||
size_t oldsize, i;
|
||||
virHashEntryPtr *oldtable;
|
||||
|
||||
#ifdef DEBUG_GROW
|
||||
unsigned long nbElem = 0;
|
||||
size_t nbElem = 0;
|
||||
#endif
|
||||
|
||||
if (table == NULL)
|
||||
@ -218,7 +218,7 @@ virHashGrow(virHashTablePtr table, int size)
|
||||
virHashEntryPtr iter = oldtable[i];
|
||||
while (iter) {
|
||||
virHashEntryPtr next = iter->next;
|
||||
unsigned long key = virHashComputeKey(table, iter->name);
|
||||
size_t key = virHashComputeKey(table, iter->name);
|
||||
|
||||
iter->next = table->table[key];
|
||||
table->table[key] = iter;
|
||||
@ -250,7 +250,7 @@ virHashGrow(virHashTablePtr table, int size)
|
||||
void
|
||||
virHashFree(virHashTablePtr table)
|
||||
{
|
||||
int i;
|
||||
size_t i;
|
||||
|
||||
if (table == NULL)
|
||||
return;
|
||||
@ -278,7 +278,7 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const void *name,
|
||||
void *userdata,
|
||||
bool is_update)
|
||||
{
|
||||
unsigned long key, len = 0;
|
||||
size_t key, len = 0;
|
||||
virHashEntryPtr entry;
|
||||
char *new_name;
|
||||
|
||||
@ -372,7 +372,7 @@ virHashUpdateEntry(virHashTablePtr table, const void *name,
|
||||
void *
|
||||
virHashLookup(virHashTablePtr table, const void *name)
|
||||
{
|
||||
unsigned long key;
|
||||
size_t key;
|
||||
virHashEntryPtr entry;
|
||||
|
||||
if (!table || !name)
|
||||
@ -419,7 +419,7 @@ void *virHashSteal(virHashTablePtr table, const void *name)
|
||||
* Returns the number of elements in the hash table or
|
||||
* -1 in case of error
|
||||
*/
|
||||
int
|
||||
ssize_t
|
||||
virHashSize(virHashTablePtr table)
|
||||
{
|
||||
if (table == NULL)
|
||||
@ -436,7 +436,7 @@ virHashSize(virHashTablePtr table)
|
||||
* Returns the number of keys in the hash table or
|
||||
* -1 in case of error
|
||||
*/
|
||||
int
|
||||
ssize_t
|
||||
virHashTableSize(virHashTablePtr table)
|
||||
{
|
||||
if (table == NULL)
|
||||
@ -499,9 +499,10 @@ virHashRemoveEntry(virHashTablePtr table, const void *name)
|
||||
*
|
||||
* Returns number of items iterated over upon completion, -1 on failure
|
||||
*/
|
||||
int virHashForEach(virHashTablePtr table, virHashIterator iter, void *data)
|
||||
ssize_t
|
||||
virHashForEach(virHashTablePtr table, virHashIterator iter, void *data)
|
||||
{
|
||||
int i, count = 0;
|
||||
size_t i, count = 0;
|
||||
|
||||
if (table == NULL || iter == NULL)
|
||||
return (-1);
|
||||
@ -542,11 +543,12 @@ int virHashForEach(virHashTablePtr table, virHashIterator iter, void *data)
|
||||
*
|
||||
* Returns number of items removed on success, -1 on failure
|
||||
*/
|
||||
int virHashRemoveSet(virHashTablePtr table,
|
||||
virHashSearcher iter,
|
||||
const void *data)
|
||||
ssize_t
|
||||
virHashRemoveSet(virHashTablePtr table,
|
||||
virHashSearcher iter,
|
||||
const void *data)
|
||||
{
|
||||
int i, count = 0;
|
||||
size_t i, count = 0;
|
||||
|
||||
if (table == NULL || iter == NULL)
|
||||
return (-1);
|
||||
@ -595,7 +597,7 @@ void *virHashSearch(virHashTablePtr table,
|
||||
virHashSearcher iter,
|
||||
const void *data)
|
||||
{
|
||||
int i;
|
||||
size_t i;
|
||||
|
||||
if (table == NULL || iter == NULL)
|
||||
return (NULL);
|
||||
@ -622,7 +624,7 @@ void *virHashSearch(virHashTablePtr table,
|
||||
struct getKeysIter
|
||||
{
|
||||
virHashKeyValuePair *sortArray;
|
||||
unsigned arrayIdx;
|
||||
size_t arrayIdx;
|
||||
};
|
||||
|
||||
static void virHashGetKeysIterator(void *payload,
|
||||
@ -641,7 +643,7 @@ typedef int (*qsort_comp)(const void *, const void *);
|
||||
virHashKeyValuePairPtr virHashGetItems(virHashTablePtr table,
|
||||
virHashKeyComparator compar)
|
||||
{
|
||||
int numElems = virHashSize(table);
|
||||
ssize_t numElems = virHashSize(table);
|
||||
struct getKeysIter iter = {
|
||||
.arrayIdx = 0,
|
||||
.sortArray = NULL,
|
||||
|
@ -60,7 +60,7 @@ typedef int (*virHashSearcher) (const void *payload, const void *name,
|
||||
*
|
||||
* Returns the hash code
|
||||
*/
|
||||
typedef unsigned long (*virHashKeyCode)(const void *name);
|
||||
typedef uint32_t (*virHashKeyCode)(const void *name);
|
||||
/**
|
||||
* virHashKeyEqual:
|
||||
* @namea: the first hash key
|
||||
@ -93,17 +93,17 @@ typedef void (*virHashKeyFree)(void *name);
|
||||
/*
|
||||
* Constructor and destructor.
|
||||
*/
|
||||
virHashTablePtr virHashCreate(int size,
|
||||
virHashTablePtr virHashCreate(ssize_t size,
|
||||
virHashDataFree dataFree);
|
||||
virHashTablePtr virHashCreateFull(int size,
|
||||
virHashTablePtr virHashCreateFull(ssize_t size,
|
||||
virHashDataFree dataFree,
|
||||
virHashKeyCode keyCode,
|
||||
virHashKeyEqual keyEqual,
|
||||
virHashKeyCopy keyCopy,
|
||||
virHashKeyFree keyFree);
|
||||
void virHashFree(virHashTablePtr table);
|
||||
int virHashSize(virHashTablePtr table);
|
||||
int virHashTableSize(virHashTablePtr table);
|
||||
ssize_t virHashSize(virHashTablePtr table);
|
||||
ssize_t virHashTableSize(virHashTablePtr table);
|
||||
|
||||
/*
|
||||
* Add a new entry to the hash table.
|
||||
@ -168,8 +168,8 @@ bool virHashEqual(const virHashTablePtr table1,
|
||||
/*
|
||||
* Iterators
|
||||
*/
|
||||
int virHashForEach(virHashTablePtr table, virHashIterator iter, void *data);
|
||||
int virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void *data);
|
||||
ssize_t virHashForEach(virHashTablePtr table, virHashIterator iter, void *data);
|
||||
ssize_t virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void *data);
|
||||
void *virHashSearch(virHashTablePtr table, virHashSearcher iter, const void *data);
|
||||
|
||||
#endif /* ! __VIR_HASH_H__ */
|
||||
|
@ -24,7 +24,7 @@ static virHashTablePtr
|
||||
testHashInit(int size)
|
||||
{
|
||||
virHashTablePtr hash;
|
||||
int i;
|
||||
ssize_t i;
|
||||
|
||||
if (!(hash = virHashCreate(size, NULL)))
|
||||
return NULL;
|
||||
@ -33,14 +33,14 @@ testHashInit(int size)
|
||||
* collision list in the same order as in the uuids array
|
||||
*/
|
||||
for (i = ARRAY_CARDINALITY(uuids) - 1; i >= 0; i--) {
|
||||
int oldsize = virHashTableSize(hash);
|
||||
ssize_t oldsize = virHashTableSize(hash);
|
||||
if (virHashAddEntry(hash, uuids[i], (void *) uuids[i]) < 0) {
|
||||
virHashFree(hash);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (virHashTableSize(hash) != oldsize && virTestGetDebug()) {
|
||||
fprintf(stderr, "\nhash grown from %d to %d",
|
||||
fprintf(stderr, "\nhash grown from %zd to %zd",
|
||||
oldsize, virHashTableSize(hash));
|
||||
}
|
||||
}
|
||||
@ -70,19 +70,19 @@ testHashCheckForEachCount(void *payload ATTRIBUTE_UNUSED,
|
||||
}
|
||||
|
||||
static int
|
||||
testHashCheckCount(virHashTablePtr hash, int count)
|
||||
testHashCheckCount(virHashTablePtr hash, size_t count)
|
||||
{
|
||||
int iter_count = 0;
|
||||
ssize_t iter_count = 0;
|
||||
|
||||
if (virHashSize(hash) != count) {
|
||||
testError("\nhash contains %d instead of %d elements\n",
|
||||
testError("\nhash contains %zd instead of %zu elements\n",
|
||||
virHashSize(hash), count);
|
||||
return -1;
|
||||
}
|
||||
|
||||
iter_count = virHashForEach(hash, testHashCheckForEachCount, NULL);
|
||||
if (count != iter_count) {
|
||||
testError("\nhash claims to have %d elements but iteration finds %d\n",
|
||||
testError("\nhash claims to have %zu elements but iteration finds %zd\n",
|
||||
count, iter_count);
|
||||
return -1;
|
||||
}
|
||||
@ -93,7 +93,7 @@ testHashCheckCount(virHashTablePtr hash, int count)
|
||||
|
||||
struct testInfo {
|
||||
void *data;
|
||||
int count;
|
||||
size_t count;
|
||||
};
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user