]> xenbits.xensource.com Git - libvirt.git/commitdiff
conf: Introduce virDomainObjListAddObjLocked
authorJohn Ferlan <jferlan@redhat.com>
Mon, 26 Mar 2018 22:18:56 +0000 (18:18 -0400)
committerJohn Ferlan <jferlan@redhat.com>
Fri, 6 Apr 2018 18:14:37 +0000 (14:14 -0400)
Create a common helper to add an object to the locked domain
objlist hash tables and use it.

Signed-off-by: John Ferlan <jferlan@redhat.com>
src/conf/virdomainobjlist.c

index 259657918cab8a5421aff41651e0ead61b32a687..7022abe0949c12faf03a174b54af74af85313c66 100644 (file)
@@ -220,6 +220,42 @@ virDomainObjPtr virDomainObjListFindByName(virDomainObjListPtr doms,
 }
 
 
+/**
+ * @doms: Domain object list pointer
+ * @vm: Domain object to be added
+ *
+ * Upon entry @vm should have at least 1 ref and be locked.
+ *
+ * Add the @vm into the @doms->objs and @doms->objsName hash
+ * tables.
+ *
+ * Returns 0 on success with 2 references and locked
+ *        -1 on failure with 1 reference and locked
+ */
+static int
+virDomainObjListAddObjLocked(virDomainObjListPtr doms,
+                             virDomainObjPtr vm)
+{
+    char uuidstr[VIR_UUID_STRING_BUFLEN];
+
+    virUUIDFormat(vm->def->uuid, uuidstr);
+    if (virHashAddEntry(doms->objs, uuidstr, vm) < 0)
+        return -1;
+
+    if (virHashAddEntry(doms->objsName, vm->def->name, vm) < 0) {
+        virObjectRef(vm);
+        virHashRemoveEntry(doms->objs, uuidstr);
+        return -1;
+    }
+
+    /* Since domain is in two hash tables, increment the
+     * reference counter */
+    virObjectRef(vm);
+
+    return 0;
+}
+
+
 /*
  * virDomainObjListAddLocked:
  *
@@ -294,22 +330,10 @@ virDomainObjListAddLocked(virDomainObjListPtr doms,
             goto cleanup;
         vm->def = def;
 
-        virUUIDFormat(def->uuid, uuidstr);
-        if (virHashAddEntry(doms->objs, uuidstr, vm) < 0) {
-            virDomainObjEndAPI(&vm);
-            return NULL;
-        }
-
-        if (virHashAddEntry(doms->objsName, def->name, vm) < 0) {
-            virObjectRef(vm);
-            virHashRemoveEntry(doms->objs, uuidstr);
+        if (virDomainObjListAddObjLocked(doms, vm) < 0) {
             virDomainObjEndAPI(&vm);
             return NULL;
         }
-
-        /* Since domain is in two hash tables, increment the
-         * reference counter */
-        virObjectRef(vm);
     }
  cleanup:
     return vm;
@@ -532,19 +556,9 @@ virDomainObjListLoadStatus(virDomainObjListPtr doms,
         goto error;
     }
 
-    if (virHashAddEntry(doms->objs, uuidstr, obj) < 0)
+    if (virDomainObjListAddObjLocked(doms, obj) < 0)
         goto error;
 
-    if (virHashAddEntry(doms->objsName, obj->def->name, obj) < 0) {
-        virObjectRef(obj);
-        virHashRemoveEntry(doms->objs, uuidstr);
-        goto error;
-    }
-
-    /* Since domain is in two hash tables, increment the
-     * reference counter */
-    virObjectRef(obj);
-
     if (notify)
         (*notify)(obj, 1, opaque);