[libvirt] [PATCH v2 8/8] util: Add virObjectPoolableDef* accessor API's

John Ferlan jferlan at redhat.com
Fri Jun 2 10:17:22 UTC 2017


Add new API's for object management:

    virObjectPoolableDefGetDef

        - Just return the obj->def from the object.

    virObjectPoolableDefSetDef

        - If the new @def is non-NULL, replace the previous obj->def with
          the new @def argument calling the obj->defFreeFunc on the previous
          obj->def.  If the @def is NULL, then just clear the obj->def
          leaving the caller to handle removal of the previous obj->def.

    virObjectPoolableDefGetNewDef

        - Just return the obj->newDef from the object.

    virObjectPoolableDefSetNewDef

        - If the new @newDef is non-NULL, replace the previous obj->newDef
          with the new @newDef argument calling the obj->defFreeFunc on
          the previous obj->newDef.  If the @newDef is NULL, then just clear
          the obj->newDef leaving the caller to handle removal of the
          previous obj->newDef.

    virObjectPoolableDefSwapNewDef

       - Manage swapping the obj->newDef into obj->def by first calling
         the obj->defFreeFunc on the current obj->def and then stealing
         the obj->newDef into obj->def.

Signed-off-by: John Ferlan <jferlan at redhat.com>
---
 src/libvirt_private.syms |   5 ++
 src/util/virobject.c     | 143 +++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virobject.h     |  17 ++++++
 3 files changed, 165 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 1896f24..afc04c5 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2271,7 +2271,12 @@ virObjectListFreeCount;
 virObjectLock;
 virObjectLockableNew;
 virObjectNew;
+virObjectPoolableDefGetDef;
+virObjectPoolableDefGetNewDef;
 virObjectPoolableDefNew;
+virObjectPoolableDefSetDef;
+virObjectPoolableDefSetNewDef;
+virObjectPoolableDefStealNewDef;
 virObjectPoolableHashElementGetPrimaryKey;
 virObjectPoolableHashElementGetSecondaryKey;
 virObjectPoolableHashElementNew;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index 0aed790..831cde2 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -502,6 +502,18 @@ virObjectGetPoolableHashElementObj(void *anyobj)
 }
 
 
+static virObjectPoolableDefPtr
+virObjectGetPoolableDefObj(void *anyobj)
+{
+    if (virObjectIsClass(anyobj, virObjectPoolableDefClass))
+        return anyobj;
+
+    VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectPoolableDefClass);
+
+    return NULL;
+}
+
+
 /**
  * virObjectLock:
  * @anyobj: any instance of virObjectLockablePtr
@@ -691,3 +703,134 @@ virObjectPoolableHashElementGetSecondaryKey(void *anyobj)
 
     return obj->secondaryKey;
 }
+
+
+/**
+ * virObjectPoolableDefGetDef
+ * @anyobj: Pointer to a locked PoolableDef object
+ *
+ * Returns: Pointer to the object @def or NULL on failure
+ */
+void *
+virObjectPoolableDefGetDef(void *anyobj)
+{
+    virObjectPoolableDefPtr obj = virObjectGetPoolableDefObj(anyobj);
+
+    if (!obj)
+        return NULL;
+
+    return obj->def;
+}
+
+
+/**
+ * virObjectPoolableDefSetDef
+ * @anyobj: Pointer to a locked PoolableDef object
+ * @def: Opaque object definition to replace in the object
+ *
+ * Set the @def of the object - this may be NULL if clearing the @def
+ * from the object in which case it is up to the caller to handle managing
+ * the previous obj->def. If @def is not NULL, this includes a call to the
+ * defFreeFunc prior to the set.
+ *
+ * Returns 0 on success, -1 on failure
+ */
+int
+virObjectPoolableDefSetDef(void *anyobj,
+                           void *def)
+{
+    virObjectPoolableDefPtr obj = virObjectGetPoolableDefObj(anyobj);
+
+    if (!obj)
+        return -1;
+
+    if (def) {
+        obj->defFreeFunc(obj->def);
+        obj->def = def;
+    } else {
+        obj->def = NULL;
+    }
+
+    return 0;
+}
+
+
+/**
+ * virObjectPoolableDefGetNewDef
+ * @anyobj: Pointer to a locked PoolableDef object
+ *
+ * Returns: Pointer to the object 'newDef' or NULL on failure
+ */
+void *
+virObjectPoolableDefGetNewDef(void *anyobj)
+{
+    virObjectPoolableDefPtr obj = virObjectGetPoolableDefObj(anyobj);
+
+    if (!obj)
+        return NULL;
+
+    return obj->newDef;
+}
+
+
+/**
+ * virObjectPoolableDefSetNewDef
+ * @anyobj: Pointer to a locked PoolableDef object
+ * @newDef: Opaque 'newDef' to replace in the object, may be NULL
+ *
+ * Set the 'newDef' of the object - this may be NULL if clearing the newDef
+ * from the object in which case it is up to the caller to handle managing
+ * the previous newDef. If @newDef is not NULL, this includes a call to the
+ * defFreeFunc prior to the set.
+ *
+ * Returns 0 on success, -1 on failure
+ */
+int
+virObjectPoolableDefSetNewDef(void *anyobj,
+                              void *newDef)
+{
+    virObjectPoolableDefPtr obj = virObjectGetPoolableDefObj(anyobj);
+
+    if (!obj)
+        return -1;
+
+    if (newDef) {
+        obj->defFreeFunc(obj->newDef);
+        obj->newDef = newDef;
+    } else {
+        obj->newDef = NULL;
+    }
+
+    return 0;
+}
+
+
+/**
+ * virObjectPoolableDefSwapNewDef
+ * @anyobj: Pointer to a locked PoolableDef object
+ *
+ * Manage swapping the obj->newDef into obj->def in one pass.
+ * When called, the previous obj->def will be free'd and the
+ * existing defDef will take it's place.
+ *
+ * Returns 0 on success, -1 on failure
+ */
+int
+virObjectPoolableDefStealNewDef(void *anyobj)
+{
+    virObjectPoolableDefPtr obj = virObjectGetPoolableDefObj(anyobj);
+
+    if (!obj)
+        return -1;
+
+    if (!obj->newDef) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Unable to steal NULL newDef"));
+        return -1;
+    }
+
+    obj->defFreeFunc(obj->def);
+    VIR_STEAL_PTR(obj->def, obj->newDef);
+
+    return 0;
+}
diff --git a/src/util/virobject.h b/src/util/virobject.h
index 085ed43..dd6412e 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -170,4 +170,21 @@ virObjectPoolableHashElementGetPrimaryKey(void *anyobj);
 const char *
 virObjectPoolableHashElementGetSecondaryKey(void *anyobj);
 
+void *
+virObjectPoolableDefGetDef(void *anyobj);
+
+int
+virObjectPoolableDefSetDef(void *anyobj,
+                           void *def);
+
+void *
+virObjectPoolableDefGetNewDef(void *anyobj);
+
+int
+virObjectPoolableDefSetNewDef(void *anyobj,
+                              void *newDef);
+
+int
+virObjectPoolableDefStealNewDef(void *anyobj);
+
 #endif /* __VIR_OBJECT_H */
-- 
2.9.4




More information about the libvir-list mailing list