From 82197ea6d794144e51b72f97df2315a65134b385 Mon Sep 17 00:00:00 2001
From: Andrea Bolognani <abologna@redhat.com>
Date: Thu, 25 Jun 2020 18:37:27 +0200
Subject: [libvirt PATCH 1/2] conf: Move id->{value,isSet} handling further
 down the stack

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
---
 src/conf/domain_addr.c | 61 ++++++++++++++++++++++++------------------
 1 file changed, 35 insertions(+), 26 deletions(-)

diff --git a/src/conf/domain_addr.c b/src/conf/domain_addr.c
index 90ed31ef4e..493b155129 100644
--- a/src/conf/domain_addr.c
+++ b/src/conf/domain_addr.c
@@ -33,20 +33,27 @@ VIR_LOG_INIT("conf.domain_addr");
 
 static int
 virDomainZPCIAddressReserveId(virHashTablePtr set,
-                              unsigned int id,
+                              virZPCIDeviceAddressID *id,
                               const char *name)
 {
-    if (virHashLookup(set, &id)) {
+    if (!id->isSet) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("No zPCI %s to reserve"),
+                       name);
+        return -1;
+    }
+
+    if (virHashLookup(set, &id->value)) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("zPCI %s %o is already reserved"),
-                       name, id);
+                       name, id->value);
         return -1;
     }
 
-    if (virHashAddEntry(set, &id, (void*)1) < 0) {
+    if (virHashAddEntry(set, &id->value, (void*)1) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Failed to reserve %s %o"),
-                       name, id);
+                       name, id->value);
         return -1;
     }
 
@@ -58,7 +65,7 @@ static int
 virDomainZPCIAddressReserveUid(virHashTablePtr set,
                                virZPCIDeviceAddressPtr addr)
 {
-    return virDomainZPCIAddressReserveId(set, addr->uid.value, "uid");
+    return virDomainZPCIAddressReserveId(set, &addr->uid, "uid");
 }
 
 
@@ -66,17 +73,20 @@ static int
 virDomainZPCIAddressReserveFid(virHashTablePtr set,
                                virZPCIDeviceAddressPtr addr)
 {
-    return virDomainZPCIAddressReserveId(set, addr->fid.value, "fid");
+    return virDomainZPCIAddressReserveId(set, &addr->fid, "fid");
 }
 
 
 static int
 virDomainZPCIAddressAssignId(virHashTablePtr set,
-                             unsigned int *id,
+                             virZPCIDeviceAddressID *id,
                              unsigned int min,
                              unsigned int max,
                              const char *name)
 {
+    if (id->isSet)
+        return 0;
+
     while (virHashLookup(set, &min)) {
         if (min == max) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -86,7 +96,9 @@ virDomainZPCIAddressAssignId(virHashTablePtr set,
         }
         ++min;
     }
-    *id = min;
+
+    id->value = min;
+    id->isSet = true;
 
     return 0;
 }
@@ -96,7 +108,7 @@ static int
 virDomainZPCIAddressAssignUid(virHashTablePtr set,
                               virZPCIDeviceAddressPtr addr)
 {
-    return virDomainZPCIAddressAssignId(set, &addr->uid.value, 1,
+    return virDomainZPCIAddressAssignId(set, &addr->uid, 1,
                                         VIR_DOMAIN_DEVICE_ZPCI_MAX_UID, "uid");
 }
 
@@ -105,23 +117,27 @@ static int
 virDomainZPCIAddressAssignFid(virHashTablePtr set,
                               virZPCIDeviceAddressPtr addr)
 {
-    return virDomainZPCIAddressAssignId(set, &addr->fid.value, 0,
+    return virDomainZPCIAddressAssignId(set, &addr->fid, 0,
                                         VIR_DOMAIN_DEVICE_ZPCI_MAX_FID, "fid");
 }
 
 
 static void
 virDomainZPCIAddressReleaseId(virHashTablePtr set,
-                              unsigned int *id,
+                              virZPCIDeviceAddressID *id,
                               const char *name)
 {
-    if (virHashRemoveEntry(set, id) < 0) {
+    if (!id->isSet)
+        return;
+
+    if (virHashRemoveEntry(set, &id->value) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Release %s %o failed"),
-                       name, *id);
+                       name, id->value);
     }
 
-    *id = 0;
+    id->value = 0;
+    id->isSet = false;
 }
 
 
@@ -129,8 +145,7 @@ static void
 virDomainZPCIAddressReleaseUid(virHashTablePtr set,
                                virZPCIDeviceAddressPtr addr)
 {
-    if (addr->uid.isSet)
-        virDomainZPCIAddressReleaseId(set, &addr->uid.value, "uid");
+    virDomainZPCIAddressReleaseId(set, &addr->uid, "uid");
 }
 
 
@@ -138,8 +153,7 @@ static void
 virDomainZPCIAddressReleaseFid(virHashTablePtr set,
                                virZPCIDeviceAddressPtr addr)
 {
-    if (addr->fid.isSet)
-        virDomainZPCIAddressReleaseId(set, &addr->fid.value, "fid");
+    virDomainZPCIAddressReleaseId(set, &addr->fid, "fid");
 }
 
 
@@ -159,12 +173,10 @@ static int
 virDomainZPCIAddressReserveAddr(virDomainZPCIAddressIdsPtr zpciIds,
                                 virZPCIDeviceAddressPtr addr)
 {
-    if (!addr->uid.isSet &&
-        virDomainZPCIAddressAssignUid(zpciIds->uids, addr) < 0)
+    if (virDomainZPCIAddressAssignUid(zpciIds->uids, addr) < 0)
         return -1;
 
-    if (!addr->fid.isSet &&
-        virDomainZPCIAddressAssignFid(zpciIds->fids, addr) < 0)
+    if (virDomainZPCIAddressAssignFid(zpciIds->fids, addr) < 0)
         return -1;
 
     if (virDomainZPCIAddressReserveUid(zpciIds->uids, addr) < 0)
@@ -175,9 +187,6 @@ virDomainZPCIAddressReserveAddr(virDomainZPCIAddressIdsPtr zpciIds,
         return -1;
     }
 
-    addr->uid.isSet = true;
-    addr->fid.isSet = true;
-
     return 0;
 }
 
-- 
2.25.4

