[PATCH 1/4] virInterfaceObjListAssignDef: Transfer definition ownership

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Upon successful return from virInterfaceObjListAssignDef() the
virInterfaceObj is the owner of secret definition. To make this
ownership transfer even more visible, lets pass the definition as
a double pointer and use g_steal_pointer().

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 src/conf/virinterfaceobj.c | 25 +++++++++++++++++++------
 src/conf/virinterfaceobj.h |  2 +-
 src/test/test_driver.c     |  6 ++----
 3 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/src/conf/virinterfaceobj.c b/src/conf/virinterfaceobj.c
index c5dfa6c7f5..daac74e88c 100644
--- a/src/conf/virinterfaceobj.c
+++ b/src/conf/virinterfaceobj.c
@@ -377,9 +377,8 @@ virInterfaceObjListCloneCb(void *payload,
         goto error;
     VIR_FREE(xml);
 
-    if (!(obj = virInterfaceObjListAssignDef(data->dest, backup)))
+    if (!(obj = virInterfaceObjListAssignDef(data->dest, &backup)))
         goto error;
-    backup = NULL;
     virInterfaceObjEndAPI(&obj);
 
     virObjectUnlock(srcObj);
@@ -420,25 +419,39 @@ virInterfaceObjListClone(virInterfaceObjList *interfaces)
 }
 
 
+/**
+ * virInterfaceObjListAssignDef:
+ * @interfaces: virInterface object list
+ * @def: new definition
+ *
+ * Assigns new definition to either an existing or newly created
+ * virInterface object. Upon successful return the virInterface
+ * object is the owner of @def and callers should use
+ * virInterfaceObjGetDef() if they need to access the definition
+ * as @def is set to NULL.
+ *
+ * Returns: a virInterface object instance on success, or
+ *          NULL on error.
+ */
 virInterfaceObj *
 virInterfaceObjListAssignDef(virInterfaceObjList *interfaces,
-                             virInterfaceDef *def)
+                             virInterfaceDef **def)
 {
     virInterfaceObj *obj;
 
     virObjectRWLockWrite(interfaces);
-    if ((obj = virInterfaceObjListFindByNameLocked(interfaces, def->name))) {
+    if ((obj = virInterfaceObjListFindByNameLocked(interfaces, (*def)->name))) {
         virInterfaceDefFree(obj->def);
     } else {
         if (!(obj = virInterfaceObjNew()))
             goto error;
 
-        if (virHashAddEntry(interfaces->objsName, def->name, obj) < 0)
+        if (virHashAddEntry(interfaces->objsName, (*def)->name, obj) < 0)
             goto error;
         virObjectRef(obj);
     }
 
-    obj->def = def;
+    obj->def = g_steal_pointer(def);
     virObjectRWUnlock(interfaces);
 
     return obj;
diff --git a/src/conf/virinterfaceobj.h b/src/conf/virinterfaceobj.h
index d60faa95f2..5927484167 100644
--- a/src/conf/virinterfaceobj.h
+++ b/src/conf/virinterfaceobj.h
@@ -59,7 +59,7 @@ virInterfaceObjListClone(virInterfaceObjList *interfaces);
 
 virInterfaceObj *
 virInterfaceObjListAssignDef(virInterfaceObjList *interfaces,
-                             virInterfaceDef *def);
+                             virInterfaceDef **def);
 
 void
 virInterfaceObjListRemove(virInterfaceObjList *interfaces,
diff --git a/src/test/test_driver.c b/src/test/test_driver.c
index e45748c5fc..92c0462170 100644
--- a/src/test/test_driver.c
+++ b/src/test/test_driver.c
@@ -1141,9 +1141,8 @@ testParseInterfaces(testDriver *privconn,
         if (!def)
             return -1;
 
-        if (!(obj = virInterfaceObjListAssignDef(privconn->ifaces, def)))
+        if (!(obj = virInterfaceObjListAssignDef(privconn->ifaces, &def)))
             return -1;
-        def = NULL;
 
         virInterfaceObjSetActive(obj, true);
         virInterfaceObjEndAPI(&obj);
@@ -6203,9 +6202,8 @@ testInterfaceDefineXML(virConnectPtr conn,
     if ((def = virInterfaceDefParseString(xmlStr, flags)) == NULL)
         goto cleanup;
 
-    if ((obj = virInterfaceObjListAssignDef(privconn->ifaces, def)) == NULL)
+    if ((obj = virInterfaceObjListAssignDef(privconn->ifaces, &def)) == NULL)
         goto cleanup;
-    def = NULL;
     objdef = virInterfaceObjGetDef(obj);
 
     ret = virGetInterface(conn, objdef->name, objdef->mac);
-- 
2.32.0




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux