[libvirt] [PATCH v3 10/16] util: Introduce virObjectLookupHash{Add|Remove}

John Ferlan jferlan at redhat.com
Thu Jun 22 14:02:40 UTC 2017


Add a pair of API's to add/remove the LookupKeys object to/from the
LookupHash object. The caller must check return status and handle
failure properly for the Add. The Remove API callers are all void
functions, so only report the problem and ignore the attempt to
remove if for some reason the passed @tableobj is not as expected.

Signed-off-by: John Ferlan <jferlan at redhat.com>
---
 src/libvirt_private.syms |  2 ++
 src/util/virobject.c     | 80 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virobject.h     |  8 +++++
 3 files changed, 90 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 28244ab..de986e5 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2286,9 +2286,11 @@ virObjectListFree;
 virObjectListFreeCount;
 virObjectLock;
 virObjectLockableNew;
+virObjectLookupHashAdd;
 virObjectLookupHashGetName;
 virObjectLookupHashGetUUID;
 virObjectLookupHashNew;
+virObjectLookupHashRemove;
 virObjectLookupKeysGetName;
 virObjectLookupKeysGetUUID;
 virObjectLookupKeysIsActive;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index 949a93d..9443dda 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -755,6 +755,86 @@ virObjectLookupKeysGetName(void *anyobj)
 
 
 /**
+ * virObjectLookupHashAdd:
+ * @tableobj: LookupHash object with objsUUID/objsName
+ * @obj: The LookupKeys object to insert in the hash table(s)
+ *
+ * Insert @obj into the hash tables found in @tableobj.
+ *
+ * Returns 0 on success, -1 on failure.
+ */
+int
+virObjectLookupHashAdd(void *tableobj,
+                       virObjectLookupKeysPtr obj)
+{
+    virObjectLookupHashPtr hashObj = virObjectGetLookupHashObj(tableobj);
+
+    if (!hashObj) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("tableobj=%p is not a lookup hash object"), tableobj);
+        return -1;
+    }
+
+    if (obj->uuid) {
+        if (virHashAddEntry(hashObj->objsUUID, obj->uuid, obj) < 0)
+            return -1;
+        virObjectRef(obj);
+    }
+
+    if (obj->name) {
+        if (virHashAddEntry(hashObj->objsName, obj->name, obj) < 0) {
+            virHashRemoveEntry(hashObj->objsUUID, obj->uuid);
+            return -1;
+        }
+        virObjectRef(obj);
+    }
+
+    return 0;
+}
+
+
+/**
+ * virObjectLookupHashRemove:
+ * @tableobj: LookupHash object with objsUUID/objsName
+ * @obj: The LookupKeys object to remove from the hash table(s)
+ *
+ * Remove @obj from the hash tables found in @tableobj. The common
+ * function to remove an object from a hash table will also cause
+ * the virObjectUnref to be called via virObjectFreeHashData since
+ * the virHashCreate used that as the Free object element argument.
+ *
+ * Even though this is a void, report the error for a bad @tableobj.
+ */
+void
+virObjectLookupHashRemove(void *tableobj,
+                          virObjectLookupKeysPtr obj)
+{
+    virObjectLookupHashPtr hashObj;
+
+    if (!obj)
+        return;
+
+    if (!(hashObj = virObjectGetLookupHashObj(tableobj))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("tableobj=%p is not a lookup hash object"), tableobj);
+        return;
+    }
+
+    virObjectRef(obj);
+    virObjectUnlock(obj);
+    virObjectLock(tableobj);
+    virObjectLock(obj);
+    if (obj->uuid)
+        virHashRemoveEntry(hashObj->objsUUID, obj->uuid);
+    if (obj->name)
+        virHashRemoveEntry(hashObj->objsName, obj->name);
+    virObjectUnlock(obj);
+    virObjectUnref(obj);
+    virObjectUnlock(tableobj);
+}
+
+
+/**
  * virObjectLookupHashGetUUID
  * @anyobj: Pointer to a LookupHash object
  *
diff --git a/src/util/virobject.h b/src/util/virobject.h
index 7da680a..422c81e 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -179,6 +179,14 @@ virObjectLookupKeysGetUUID(void *anyobj);
 const char *
 virObjectLookupKeysGetName(void *anyobj);
 
+int
+virObjectLookupHashAdd(void *tableobj,
+                       virObjectLookupKeysPtr obj);
+
+void
+virObjectLookupHashRemove(void *tableobj,
+                          virObjectLookupKeysPtr obj);
+
 virHashTablePtr
 virObjectLookupHashGetUUID(void *anyobj);
 
-- 
2.9.4




More information about the libvir-list mailing list