[libvirt] [PATCH] qemu: improve compile-time check of qemuBlockjobState mapping

Eric Blake posted 1 patch 5 years, 3 months ago
Test syntax-check passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20190123194507.11168-1-eblake@redhat.com
src/qemu/qemu_blockjob.h | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
[libvirt] [PATCH] qemu: improve compile-time check of qemuBlockjobState mapping
Posted by Eric Blake 5 years, 3 months ago
Asserting the value we set four lines earlier in qemuBlockjobState
doesn't buy us any safety (if the public header adds a value, we end
up skipping that value without the compiler warning us of our gap);
what we really want is to assert that the value auto-assigned by the
compiler matches the actual last value in the public headers (as was
done below for qemuBlockJobType).  Add useful comments while at it.

Signed-off-by: Eric Blake <eblake@redhat.com>
---

The spelling VIR_DOMAIN_BLOCK_JOB_LAST vs. VIR_DOMAIN_BLOCK_JOB_TYPE_LAST
took me on a wild goose chase; the least I can do is make the code nicer
for the next reader.

src/qemu/qemu_blockjob.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/qemu/qemu_blockjob.h b/src/qemu/qemu_blockjob.h
index ed63f2c709..c7325c6daf 100644
--- a/src/qemu/qemu_blockjob.h
+++ b/src/qemu/qemu_blockjob.h
@@ -29,15 +29,17 @@
  * This enum has to map all known block job states from enum virDomainBlockJobType
  * to the same values. All internal blockjobs can be mapped after and don't
  * need to have stable values.
  */
 typedef enum {
+    /* Mapped to public enum */
     QEMU_BLOCKJOB_STATE_COMPLETED = VIR_DOMAIN_BLOCK_JOB_COMPLETED,
     QEMU_BLOCKJOB_STATE_FAILED = VIR_DOMAIN_BLOCK_JOB_FAILED,
     QEMU_BLOCKJOB_STATE_CANCELLED = VIR_DOMAIN_BLOCK_JOB_CANCELED,
     QEMU_BLOCKJOB_STATE_READY = VIR_DOMAIN_BLOCK_JOB_READY,
-    QEMU_BLOCKJOB_STATE_NEW = VIR_DOMAIN_BLOCK_JOB_LAST,
+    /* Additional enum values local to qemu */
+    QEMU_BLOCKJOB_STATE_NEW,
     QEMU_BLOCKJOB_STATE_RUNNING,
     QEMU_BLOCKJOB_STATE_LAST
 } qemuBlockjobState;
 verify((int)QEMU_BLOCKJOB_STATE_NEW == VIR_DOMAIN_BLOCK_JOB_LAST);

@@ -45,15 +47,17 @@ verify((int)QEMU_BLOCKJOB_STATE_NEW == VIR_DOMAIN_BLOCK_JOB_LAST);
  * This enum has to map all known block job types from enum virDomainBlockJobType
  * to the same values. All internal blockjobs can be mapped after and don't
  * need to have stable values.
  */
 typedef enum {
+    /* Mapped to public enum */
     QEMU_BLOCKJOB_TYPE_NONE = VIR_DOMAIN_BLOCK_JOB_TYPE_UNKNOWN,
     QEMU_BLOCKJOB_TYPE_PULL = VIR_DOMAIN_BLOCK_JOB_TYPE_PULL,
     QEMU_BLOCKJOB_TYPE_COPY = VIR_DOMAIN_BLOCK_JOB_TYPE_COPY,
     QEMU_BLOCKJOB_TYPE_COMMIT = VIR_DOMAIN_BLOCK_JOB_TYPE_COMMIT,
     QEMU_BLOCKJOB_TYPE_ACTIVE_COMMIT = VIR_DOMAIN_BLOCK_JOB_TYPE_ACTIVE_COMMIT,
+    /* Additional enum values local to qemu */
     QEMU_BLOCKJOB_TYPE_INTERNAL,
     QEMU_BLOCKJOB_TYPE_LAST
 } qemuBlockJobType;
 verify((int)QEMU_BLOCKJOB_TYPE_INTERNAL == VIR_DOMAIN_BLOCK_JOB_TYPE_LAST);

-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] qemu: improve compile-time check of qemuBlockjobState mapping
Posted by Peter Krempa 5 years, 3 months ago
On Wed, Jan 23, 2019 at 13:45:07 -0600, Eric Blake wrote:
> Asserting the value we set four lines earlier in qemuBlockjobState
> doesn't buy us any safety (if the public header adds a value, we end
> up skipping that value without the compiler warning us of our gap);
> what we really want is to assert that the value auto-assigned by the
> compiler matches the actual last value in the public headers (as was
> done below for qemuBlockJobType).  Add useful comments while at it.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---

ACK
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list