[Libguestfs] [libnbd PATCH v4 4/4] internal: Refactor layout of replies in sbuf

Eric Blake eblake at redhat.com
Mon Jun 12 13:00:14 UTC 2023


On Thu, Jun 08, 2023 at 09:17:38PM -0500, Eric Blake wrote:
> In order to more easily add a third reply type with an even larger
> header, but where the payload will look the same for both structured
> and extended replies, it is nicer if simple and structured replies are
> nested inside the same layer of sbuf.reply.hdr.  Doing this also lets
> us add an alias for accessing the cookie directly without picking
> which header type we have on hand.
>
[...]
> 
> Although we do not use union payload with simple replies, a later
> patch does use payload for two out of the three possible headers, and
> it does not hurt for sbuf to call out storage for more than we use on
> a particular state machine sequence.  The commit is largely
> mechanical, and there should be no semantic change.
> 
> Signed-off-by: Eric Blake <eblake at redhat.com>
> ---
>  lib/internal.h                  | 16 +++++--
>  generator/states-reply.c        | 35 +++++++-------
>  generator/states-reply-chunk.c  | 84 ++++++++++++++++-----------------
>  generator/states-reply-simple.c |  4 +-
>  4 files changed, 76 insertions(+), 63 deletions(-)

Here's what I'm leaning towards squashing in, now that patch 2/4
landed as 2e34ceb8.

diff --git a/generator/states-reply.c b/generator/states-reply.c
index af5f6135..0d200513 100644
--- a/generator/states-reply.c
+++ b/generator/states-reply.c
@@ -19,6 +19,11 @@
 #include <assert.h>
 #include <stddef.h>

+#define ASSERT_MEMBER_ALIAS(type, member_a, member_b) \
+  STATIC_ASSERT (offsetof (type, member_a) == offsetof (type, member_b) && \
+                 sizeof ((type *)NULL)->member_a == \
+                 sizeof ((type *)NULL)->member_b, member_alias)
+
 /* State machine for receiving reply messages from the server.
  *
  * Note that we never block while in this sub-group. If there is
@@ -70,12 +75,10 @@  REPLY.START:
    * structs (an intentional design decision in the NBD spec when
    * structured replies were added).
    */
-  STATIC_ASSERT (offsetof (union reply_header, simple.cookie) ==
-                 offsetof (union reply_header, cookie),
-                 _cookie_aliasing_simple);
-  STATIC_ASSERT (offsetof (union reply_header, structured.cookie) ==
-                 offsetof (union reply_header, cookie),
-                 _cookie_aliasing_structured);
+  ASSERT_MEMBER_ALIAS (union reply_header, simple.magic, magic);
+  ASSERT_MEMBER_ALIAS (union reply_header, simple.cookie, cookie);
+  ASSERT_MEMBER_ALIAS (union reply_header, structured.magic, magic);
+  ASSERT_MEMBER_ALIAS (union reply_header, structured.cookie, cookie);
   assert (h->reply_cmd == NULL);
   assert (h->rlen == 0);


-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org


More information about the Libguestfs mailing list