/* #define DEBUG_GROW */
-#define virHashIterationError(ret) \
- do { \
- VIR_ERROR(_("Hash operation not allowed during iteration")); \
- return ret; \
- } while (0)
-
/*
* A single entry in the hash table
*/
uint32_t seed;
size_t size;
size_t nbElems;
- /* True iff we are iterating over hash entries. */
- bool iterating;
- /* Pointer to the current entry during iteration. */
- virHashEntryPtr current;
virHashDataFree dataFree;
virHashKeyCode keyCode;
virHashKeyEqual keyEqual;
if ((table == NULL) || (name == NULL))
return -1;
- if (table->iterating)
- virHashIterationError(-1);
-
key = virHashComputeKey(table, name);
/* Check for duplicate entry */
nextptr = table->table + virHashComputeKey(table, name);
for (entry = *nextptr; entry; entry = entry->next) {
if (table->keyEqual(entry->name, name)) {
- if (table->iterating && table->current != entry)
- virHashIterationError(-1);
-
if (table->dataFree)
table->dataFree(entry->payload, entry->name);
if (table->keyFree)
if (table == NULL || iter == NULL)
return -1;
- if (table->iterating)
- virHashIterationError(-1);
-
- table->iterating = true;
- table->current = NULL;
for (i = 0; i < table->size; i++) {
virHashEntryPtr entry = table->table[i];
while (entry) {
virHashEntryPtr next = entry->next;
- table->current = entry;
ret = iter(entry->payload, entry->name, data);
- table->current = NULL;
if (ret < 0)
goto cleanup;
ret = 0;
cleanup:
- table->iterating = false;
return ret;
}
if (table == NULL || iter == NULL)
return -1;
- if (table->iterating)
- virHashIterationError(-1);
-
- table->iterating = true;
- table->current = NULL;
for (i = 0; i < table->size; i++) {
virHashEntryPtr *nextptr = table->table + i;
}
}
}
- table->iterating = false;
return count;
}
if (table == NULL || iter == NULL)
return NULL;
- if (table->iterating)
- virHashIterationError(NULL);
-
- table->iterating = true;
- table->current = NULL;
for (i = 0; i < table->size; i++) {
virHashEntryPtr entry;
for (entry = table->table[i]; entry; entry = entry->next) {
if (iter(entry->payload, entry->name, data)) {
- table->iterating = false;
if (name)
*name = table->keyCopy(entry->name);
return entry->payload;
}
}
}
- table->iterating = false;
return NULL;
}
}
-const int testHashCountRemoveForEachForbidden = ARRAY_CARDINALITY(uuids);
-
-static int
-testHashRemoveForEachForbidden(void *payload ATTRIBUTE_UNUSED,
- const void *name,
- void *data)
-{
- virHashTablePtr hash = data;
- size_t i;
-
- for (i = 0; i < ARRAY_CARDINALITY(uuids_subset); i++) {
- if (STREQ(uuids_subset[i], name)) {
- int next = (i + 1) % ARRAY_CARDINALITY(uuids_subset);
-
- if (virHashRemoveEntry(hash, uuids_subset[next]) == 0) {
- VIR_TEST_VERBOSE(
- "\nentry \"%s\" should not be allowed to be removed",
- uuids_subset[next]);
- }
- break;
- }
- }
- return 0;
-}
-
-
static int
testHashRemoveForEach(const void *data)
{
}
-static int
-testHashIter(void *payload ATTRIBUTE_UNUSED,
- const void *name ATTRIBUTE_UNUSED,
- void *data ATTRIBUTE_UNUSED)
-{
- return 0;
-}
-
-static int
-testHashForEachIter(void *payload ATTRIBUTE_UNUSED,
- const void *name ATTRIBUTE_UNUSED,
- void *data)
-{
- virHashTablePtr hash = data;
-
- if (virHashAddEntry(hash, uuids_new[0], NULL) == 0)
- VIR_TEST_VERBOSE("\nadding entries in ForEach should be forbidden");
-
- if (virHashUpdateEntry(hash, uuids_new[0], NULL) == 0)
- VIR_TEST_VERBOSE("\nupdating entries in ForEach should be forbidden");
-
- if (virHashSteal(hash, uuids_new[0]) != NULL)
- VIR_TEST_VERBOSE("\nstealing entries in ForEach should be forbidden");
-
- if (virHashSteal(hash, uuids_new[0]) != NULL)
- VIR_TEST_VERBOSE("\nstealing entries in ForEach should be forbidden");
-
- if (virHashForEach(hash, testHashIter, NULL) >= 0)
- VIR_TEST_VERBOSE("\niterating through hash in ForEach"
- " should be forbidden");
- return 0;
-}
-
-static int
-testHashForEach(const void *data ATTRIBUTE_UNUSED)
-{
- virHashTablePtr hash;
- int ret = -1;
-
- if (!(hash = testHashInit(0)))
- return -1;
-
- if (virHashForEach(hash, testHashForEachIter, hash)) {
- VIR_TEST_VERBOSE("\nvirHashForEach didn't go through all entries");
- goto cleanup;
- }
-
- ret = 0;
-
- cleanup:
- virHashFree(hash);
- return ret;
-}
-
-
static int
testHashRemoveSetIter(const void *payload ATTRIBUTE_UNUSED,
const void *name,
DO_TEST("Remove", Remove);
DO_TEST_DATA("Remove in ForEach", RemoveForEach, Some);
DO_TEST_DATA("Remove in ForEach", RemoveForEach, All);
- DO_TEST_DATA("Remove in ForEach", RemoveForEach, Forbidden);
DO_TEST("Steal", Steal);
- DO_TEST("Forbidden ops in ForEach", ForEach);
DO_TEST("RemoveSet", RemoveSet);
DO_TEST("Search", Search);
DO_TEST("GetItems", GetItems);