[Libguestfs] [PATCH libnbd 4/4] lib: Add CALL_CALLBACK macro.

Richard W.M. Jones rjones at redhat.com
Tue Aug 13 22:37:01 UTC 2019


Another simple internal macro, this time encapsulating calling a
callback.
---
 generator/states-reply-simple.c     |  8 ++++----
 generator/states-reply-structured.c | 31 ++++++++++++++---------------
 generator/states-reply.c            |  2 +-
 generator/states.c                  |  2 +-
 lib/debug.c                         |  2 +-
 lib/internal.h                      |  4 ++++
 6 files changed, 26 insertions(+), 23 deletions(-)

diff --git a/generator/states-reply-simple.c b/generator/states-reply-simple.c
index 8905367..8e3d7f1 100644
--- a/generator/states-reply-simple.c
+++ b/generator/states-reply-simple.c
@@ -64,10 +64,10 @@
       int error = 0;
 
       assert (cmd->error == 0);
-      if (cmd->cb.fn.chunk.callback (cmd->cb.fn.chunk.user_data,
-                                     cmd->data, cmd->count,
-                                     cmd->offset, LIBNBD_READ_DATA,
-                                     &error) == -1)
+      if (CALL_CALLBACK (cmd->cb.fn.chunk,
+                         cmd->data, cmd->count,
+                         cmd->offset, LIBNBD_READ_DATA,
+                         &error) == -1)
         cmd->error = error ? error : EPROTO;
       FREE_CALLBACK (cmd->cb.fn.chunk);
     }
diff --git a/generator/states-reply-structured.c b/generator/states-reply-structured.c
index 62ae3ad..2e327ce 100644
--- a/generator/states-reply-structured.c
+++ b/generator/states-reply-structured.c
@@ -301,10 +301,10 @@
          * current error rather than any earlier one. If the callback fails
          * without setting errno, then use the server's error below.
          */
-        if (cmd->cb.fn.chunk.callback (cmd->cb.fn.chunk.user_data,
-                                       cmd->data + (offset - cmd->offset),
-                                       0, offset, LIBNBD_READ_ERROR,
-                                       &scratch) == -1)
+        if (CALL_CALLBACK (cmd->cb.fn.chunk,
+                           cmd->data + (offset - cmd->offset),
+                           0, offset, LIBNBD_READ_ERROR,
+                           &scratch) == -1)
           if (cmd->error == 0)
             cmd->error = scratch;
         if (flags & NBD_REPLY_FLAG_DONE)
@@ -392,10 +392,9 @@
       int error = cmd->error;
       uint16_t flags = be16toh (h->sbuf.sr.structured_reply.flags);
 
-      if (cmd->cb.fn.chunk.callback (cmd->cb.fn.chunk.user_data,
-                                     cmd->data + (offset - cmd->offset),
-                                     length - sizeof offset, offset,
-                                     LIBNBD_READ_DATA, &error) == -1)
+      if (CALL_CALLBACK (cmd->cb.fn.chunk, cmd->data + (offset - cmd->offset),
+                         length - sizeof offset, offset,
+                         LIBNBD_READ_DATA, &error) == -1)
         if (cmd->error == 0)
           cmd->error = error ? error : EPROTO;
       if (flags & NBD_REPLY_FLAG_DONE)
@@ -457,10 +456,10 @@
       int error = cmd->error;
       uint16_t flags = be16toh (h->sbuf.sr.structured_reply.flags);
 
-      if (cmd->cb.fn.chunk.callback (cmd->cb.fn.chunk.user_data,
-                                     cmd->data + offset, length,
-                                     cmd->offset + offset,
-                                     LIBNBD_READ_HOLE, &error) == -1)
+      if (CALL_CALLBACK (cmd->cb.fn.chunk,
+                         cmd->data + offset, length,
+                         cmd->offset + offset,
+                         LIBNBD_READ_HOLE, &error) == -1)
         if (cmd->error == 0)
           cmd->error = error ? error : EPROTO;
       if (flags & NBD_REPLY_FLAG_DONE)
@@ -512,10 +511,10 @@
       int error = cmd->error;
       uint16_t flags = be16toh (h->sbuf.sr.structured_reply.flags);
 
-      if (cmd->cb.fn.extent.callback (cmd->cb.fn.extent.user_data,
-                                      meta_context->name, cmd->offset,
-                                      &h->bs_entries[1], (length-4) / 4,
-                                      &error) == -1)
+      if (CALL_CALLBACK (cmd->cb.fn.extent,
+                         meta_context->name, cmd->offset,
+                         &h->bs_entries[1], (length-4) / 4,
+                         &error) == -1)
         if (cmd->error == 0)
           cmd->error = error ? error : EPROTO;
       if (flags & NBD_REPLY_FLAG_DONE)
diff --git a/generator/states-reply.c b/generator/states-reply.c
index b8bf0ce..41dcf8d 100644
--- a/generator/states-reply.c
+++ b/generator/states-reply.c
@@ -173,7 +173,7 @@ save_reply_state (struct nbd_handle *h)
     int r;
 
     assert (cmd->type != NBD_CMD_DISC);
-    r = cmd->cb.completion.callback (cmd->cb.completion.user_data, &error);
+    r = CALL_CALLBACK (cmd->cb.completion, &error);
     FREE_CALLBACK (cmd->cb.completion);
     switch (r) {
     case -1:
diff --git a/generator/states.c b/generator/states.c
index 98c10b3..263f60e 100644
--- a/generator/states.c
+++ b/generator/states.c
@@ -126,7 +126,7 @@ void abort_commands (struct nbd_handle *h,
       int r;
 
       assert (cmd->type != NBD_CMD_DISC);
-      r = cmd->cb.completion.callback (cmd->cb.completion.user_data, &error);
+      r = CALL_CALLBACK (cmd->cb.completion, &error);
       FREE_CALLBACK (cmd->cb.completion);
       switch (r) {
       case -1:
diff --git a/lib/debug.c b/lib/debug.c
index e1ec675..eec2051 100644
--- a/lib/debug.c
+++ b/lib/debug.c
@@ -84,7 +84,7 @@ nbd_internal_debug (struct nbd_handle *h, const char *fs, ...)
 
   if (h->debug_callback.callback)
     /* ignore return value */
-    h->debug_callback.callback (h->debug_callback.user_data, context, msg);
+    CALL_CALLBACK (h->debug_callback, context, msg);
   else
     fprintf (stderr, "libnbd: debug: %s: %s: %s\n",
              h->hname, context ? : "unknown", msg);
diff --git a/lib/internal.h b/lib/internal.h
index 305158e..dc3676a 100644
--- a/lib/internal.h
+++ b/lib/internal.h
@@ -273,6 +273,10 @@ struct command {
   uint32_t error; /* Local errno value */
 };
 
+/* Call a callback. */
+#define CALL_CALLBACK(cb, ...) \
+  (cb).callback ((cb).user_data, ##__VA_ARGS__)
+
 /* Free a callback.
  *
  * Note this works for any type of callback because the basic layout
-- 
2.22.0




More information about the Libguestfs mailing list