#define VIR_DOMAIN_XML_READ_FLAGS VIR_DOMAIN_XML_INACTIVE
static void
-virDomainObjListDeallocator(void *payload, const char *name ATTRIBUTE_UNUSED)
+virDomainObjListDataFree(void *payload, const char *name ATTRIBUTE_UNUSED)
{
virDomainObjPtr obj = payload;
virDomainObjLock(obj);
int virDomainObjListInit(virDomainObjListPtr doms)
{
- doms->objs = virHashCreate(50, virDomainObjListDeallocator);
+ doms->objs = virHashCreate(50, virDomainObjListDataFree);
if (!doms->objs)
return -1;
return 0;
virDomainObjUnlock(dom);
- virHashRemoveEntry(doms->objs, uuidstr, virDomainObjListDeallocator);
+ virHashRemoveEntry(doms->objs, uuidstr);
}
/* Snapshot Obj List functions */
static void
-virDomainSnapshotObjListDeallocator(void *payload,
- const char *name ATTRIBUTE_UNUSED)
+virDomainSnapshotObjListDataFree(void *payload,
+ const char *name ATTRIBUTE_UNUSED)
{
virDomainSnapshotObjPtr obj = payload;
int virDomainSnapshotObjListInit(virDomainSnapshotObjListPtr snapshots)
{
- snapshots->objs = virHashCreate(50, virDomainSnapshotObjListDeallocator);
+ snapshots->objs = virHashCreate(50, virDomainSnapshotObjListDataFree);
if (!snapshots->objs)
return -1;
return 0;
void virDomainSnapshotObjListRemove(virDomainSnapshotObjListPtr snapshots,
virDomainSnapshotObjPtr snapshot)
{
- virHashRemoveEntry(snapshots->objs, snapshot->def->name,
- virDomainSnapshotObjListDeallocator);
+ virHashRemoveEntry(snapshots->objs, snapshot->def->name);
}
struct snapshot_has_children {
#define VIR_FROM_THIS VIR_FROM_NWFILTER
static void
-hashDealloc(void *payload, const char *name ATTRIBUTE_UNUSED)
+hashDataFree(void *payload, const char *name ATTRIBUTE_UNUSED)
{
VIR_FREE(payload);
}
return 1;
}
} else {
- if (virHashUpdateEntry(table->hashTable, name, val, hashDealloc) != 0) {
+ if (virHashUpdateEntry(table->hashTable, name, val) != 0) {
return 1;
}
}
virReportOOMError();
return NULL;
}
- ret->hashTable = virHashCreate(n, hashDealloc);
+ ret->hashTable = virHashCreate(n, hashDataFree);
if (!ret->hashTable) {
VIR_FREE(ret);
return NULL;
const char *entry)
{
int i;
- int rc = virHashRemoveEntry(ht->hashTable, entry, hashDealloc);
+ int rc = virHashRemoveEntry(ht->hashTable, entry);
if (rc == 0) {
for (i = 0; i < ht->nNames; i++) {
virHashRemoveSet;
virHashSearch;
virHashSize;
+virHashSteal;
# hooks.h
ifaceLock->refctr--;
if (ifaceLock->refctr == 0)
- virHashRemoveEntry(ifaceLockMap, ifname, freeIfaceLock);
+ virHashRemoveEntry(ifaceLockMap, ifname);
}
virMutexUnlock(&ifaceMapLock);
virMutexLock(&pendingLearnReqLock);
- res = virHashLookup(pendingLearnReq, ifindex_str);
-
- if (res)
- virHashRemoveEntry(pendingLearnReq, ifindex_str, NULL);
+ res = virHashSteal(pendingLearnReq, ifindex_str);
virMutexUnlock(&pendingLearnReqLock);
if (!addr)
return -1;
- ret = virHashRemoveEntry(addrs->used, addr, qemuDomainPCIAddressSetFreeEntry);
+ ret = virHashRemoveEntry(addrs->used, addr);
VIR_FREE(addr);
struct _virHashEntry *table;
int size;
int nbElems;
- virHashDeallocator f;
+ virHashDataFree dataFree;
};
/*
/**
* virHashCreate:
* @size: the size of the hash table
- * @deallocator: function to call on each entry during virHashFree
+ * @dataFree: function to call on each entry during virHashFree
*
* Create a new virHashTablePtr.
*
* Returns the newly created object, or NULL if an error occured.
*/
virHashTablePtr
-virHashCreate(int size, virHashDeallocator deallocator)
+virHashCreate(int size, virHashDataFree dataFree)
{
virHashTablePtr table = NULL;
table->size = size;
table->nbElems = 0;
- table->f = deallocator;
+ table->dataFree = dataFree;
if (VIR_ALLOC_N(table->table, size) < 0) {
virReportOOMError();
VIR_FREE(table);
inside_table = 1;
while (iter) {
next = iter->next;
- if ((table->f != NULL) && (iter->payload != NULL))
- table->f(iter->payload, iter->name);
+ if ((table->dataFree != NULL) && (iter->payload != NULL))
+ table->dataFree(iter->payload, iter->name);
VIR_FREE(iter->name);
iter->payload = NULL;
if (!inside_table)
}
static int
-virHashAddOrUpdateEntry(virHashTablePtr table, const char *name,
- void *userdata, virHashDeallocator f,
+virHashAddOrUpdateEntry(virHashTablePtr table, const void *name,
+ void *userdata,
bool is_update)
{
unsigned long key, len = 0;
if (found) {
if (is_update) {
- if (f)
- f(insert->payload, insert->name);
+ if (table->dataFree)
+ table->dataFree(insert->payload, insert->name);
insert->payload = userdata;
return (0);
} else {
int
virHashAddEntry(virHashTablePtr table, const char *name, void *userdata)
{
- return virHashAddOrUpdateEntry(table, name, userdata, NULL, false);
+ return virHashAddOrUpdateEntry(table, name, userdata, false);
}
/**
* @table: the hash table
* @name: the name of the userdata
* @userdata: a pointer to the userdata
- * @f: the deallocator function for replaced item (if any)
*
* Add the @userdata to the hash @table. This can later be retrieved
* by using @name. Existing entry for this tuple
*/
int
virHashUpdateEntry(virHashTablePtr table, const char *name,
- void *userdata, virHashDeallocator f)
+ void *userdata)
{
- return virHashAddOrUpdateEntry(table, name, userdata, f, true);
+ return virHashAddOrUpdateEntry(table, name, userdata, true);
}
/**
return (NULL);
}
+
+/**
+ * virHashSteal:
+ * @table: the hash table
+ * @name: the name of the userdata
+ *
+ * Find the userdata specified by @name
+ * and remove it from the hash without freeing it.
+ *
+ * Returns the a pointer to the userdata
+ */
+void *virHashSteal(virHashTablePtr table, const char *name)
+{
+ void *data = virHashLookup(table, name);
+ if (data) {
+ virHashDataFree dataFree = table->dataFree;
+ table->dataFree = NULL;
+ virHashRemoveEntry(table, name);
+ table->dataFree = dataFree;
+ }
+ return data;
+}
+
+
/**
* virHashSize:
* @table: the hash table
* virHashRemoveEntry:
* @table: the hash table
* @name: the name of the userdata
- * @f: the deallocator function for removed item (if any)
*
* Find the userdata specified by the @name and remove
* it from the hash @table. Existing userdata for this tuple will be removed
* Returns 0 if the removal succeeded and -1 in case of error or not found.
*/
int
-virHashRemoveEntry(virHashTablePtr table, const char *name,
- virHashDeallocator f)
+virHashRemoveEntry(virHashTablePtr table, const char *name)
{
unsigned long key;
virHashEntryPtr entry;
for (entry = &(table->table[key]); entry != NULL;
entry = entry->next) {
if (STREQ(entry->name, name)) {
- if ((f != NULL) && (entry->payload != NULL))
- f(entry->payload, entry->name);
+ if (table->dataFree && (entry->payload != NULL))
+ table->dataFree(entry->payload, entry->name);
entry->payload = NULL;
VIR_FREE(entry->name);
if (prev) {
*
* Returns number of items removed on success, -1 on failure
*/
-int virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, virHashDeallocator f, const void *data) {
+int virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void *data) {
int i, count = 0;
if (table == NULL || iter == NULL)
while (entry && entry->valid) {
if (iter(entry->payload, entry->name, data)) {
count++;
- f(entry->payload, entry->name);
+ if (table->dataFree)
+ table->dataFree(entry->payload, entry->name);
VIR_FREE(entry->name);
table->nbElems--;
if (prev) {
*/
/**
- * virHashDeallocator:
+ * virHashDataFree:
* @payload: the data in the hash
* @name: the name associated
*
* Callback to free data from a hash.
*/
-typedef void (*virHashDeallocator) (void *payload, const char *name);
+typedef void (*virHashDataFree) (void *payload, const char *name);
/**
* virHashIterator:
* @payload: the data in the hash
/*
* Constructor and destructor.
*/
-virHashTablePtr virHashCreate(int size, virHashDeallocator f);
+virHashTablePtr virHashCreate(int size, virHashDataFree dataFree);
void virHashFree(virHashTablePtr table);
int virHashSize(virHashTablePtr table);
const char *name, void *userdata);
int virHashUpdateEntry(virHashTablePtr table,
const char *name,
- void *userdata, virHashDeallocator f);
+ void *userdata);
/*
* Remove an entry from the hash table.
*/
int virHashRemoveEntry(virHashTablePtr table,
- const char *name, virHashDeallocator f);
+ const char *name);
/*
* Retrieve the userdata.
*/
void *virHashLookup(virHashTablePtr table, const char *name);
+/*
+ * Retrieve & remove the userdata.
+ */
+void *virHashSteal(virHashTablePtr table, const char *name);
+
/*
* Iterators
*/
int virHashForEach(virHashTablePtr table, virHashIterator iter, void *data);
-int virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, virHashDeallocator f, const void *data);
+int virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void *data);
void *virHashSearch(virHashTablePtr table, virHashSearcher iter, const void *data);
#endif /* ! __VIR_HASH_H__ */
const char *olddomname = entry->def->name;
char *nameowner = (char *)virHashLookup(args->priv->nameConfigMap, olddomname);
if (nameowner && STREQ(nameowner, key)) {
- virHashRemoveEntry(args->priv->nameConfigMap, olddomname, NULL);
+ virHashRemoveEntry(args->priv->nameConfigMap, olddomname);
}
return (1);
}
return 0;
}
- virHashRemoveEntry(priv->nameConfigMap, entry->def->name, NULL);
- virHashRemoveEntry(priv->configCache, filename, xenXMConfigFree);
+ virHashRemoveEntry(priv->nameConfigMap, entry->def->name);
+ virHashRemoveEntry(priv->configCache, filename);
VIR_DEBUG("Removed %s %s", entry->def->name, filename);
return 0;
}
re-acquire it later - just in case it was renamed */
nameowner = (char *)virHashLookup(priv->nameConfigMap, entry->def->name);
if (nameowner && STREQ(nameowner, filename)) {
- virHashRemoveEntry(priv->nameConfigMap, entry->def->name, NULL);
+ virHashRemoveEntry(priv->nameConfigMap, entry->def->name);
}
/* Clear existing config entry which needs refresh */
if (!(entry->def = xenXMConfigReadFile(conn, entry->filename))) {
VIR_DEBUG("Failed to read %s", entry->filename);
if (!newborn)
- virHashRemoveEntry(priv->configCache, filename, NULL);
+ virHashSteal(priv->configCache, filename);
VIR_FREE(entry);
return -1;
}
*/
if (!virHashLookup(priv->nameConfigMap, entry->def->name)) {
if (virHashAddEntry(priv->nameConfigMap, entry->def->name, entry->filename) < 0) {
- virHashRemoveEntry(priv->configCache, filename, NULL);
+ virHashSteal(priv->configCache, filename);
virDomainDefFree(entry->def);
VIR_FREE(entry);
}
then the config is no longer on disk */
args.now = now;
args.priv = priv;
- virHashRemoveSet(priv->configCache, xenXMConfigReaper, xenXMConfigFree, &args);
+ virHashRemoveSet(priv->configCache, xenXMConfigReaper, &args);
ret = 0;
closedir(dh);
}
/* Remove the name -> filename mapping */
- if (virHashRemoveEntry(priv->nameConfigMap, def->name, NULL) < 0) {
+ if (virHashRemoveEntry(priv->nameConfigMap, def->name) < 0) {
xenXMError(VIR_ERR_INTERNAL_ERROR,
"%s", _("failed to remove old domain from config map"));
goto error;
}
/* Remove the config record itself */
- if (virHashRemoveEntry(priv->configCache, oldfilename, xenXMConfigFree) < 0) {
+ if (virHashRemoveEntry(priv->configCache, oldfilename) < 0) {
xenXMError(VIR_ERR_INTERNAL_ERROR,
"%s", _("failed to remove old domain from config map"));
goto error;
}
if (virHashAddEntry(priv->nameConfigMap, def->name, entry->filename) < 0) {
- virHashRemoveEntry(priv->configCache, filename, NULL);
+ virHashSteal(priv->configCache, filename);
xenXMError(VIR_ERR_INTERNAL_ERROR,
"%s", _("unable to store config file handle"));
goto error;
goto cleanup;
/* Remove the name -> filename mapping */
- if (virHashRemoveEntry(priv->nameConfigMap, domain->name, NULL) < 0)
+ if (virHashRemoveEntry(priv->nameConfigMap, domain->name) < 0)
goto cleanup;
/* Remove the config record itself */
- if (virHashRemoveEntry(priv->configCache, entry->filename, xenXMConfigFree) < 0)
+ if (virHashRemoveEntry(priv->configCache, entry->filename) < 0)
goto cleanup;
ret = 0;