[Libguestfs] [PATCH libnbd v2 02/10] lib: Add macros to check if a callback is "null" or not, and set it to null.

Richard W.M. Jones rjones at redhat.com
Thu Aug 15 09:56:13 UTC 2019


We have defined the concept of a "null callback" meaning one where the
.callback field = NULL.  The first two new macros just test this
property, and the third one sets a callback to null.

This change is neutral refactoring.
---
 generator/generator                 |  4 ++--
 generator/states-reply-simple.c     |  2 +-
 generator/states-reply-structured.c | 11 ++++++-----
 generator/states-reply.c            |  2 +-
 generator/states.c                  |  2 +-
 lib/debug.c                         |  2 +-
 lib/internal.h                      |  9 +++++++--
 7 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/generator/generator b/generator/generator
index ca97910..98c99e0 100755
--- a/generator/generator
+++ b/generator/generator
@@ -3576,7 +3576,7 @@ let generate_lib_api_c () =
          let value = match errcode with
            | Some value -> value
            | None -> assert false in
-         pr "  if (%s_callback.callback == NULL) {\n" cbname;
+         pr "  if (CALLBACK_IS_NULL (%s_callback)) {\n" cbname;
          pr "    set_error (EFAULT, \"%%s cannot be NULL\", \"%s\");\n" cbname;
          pr "    ret = %s;\n" value;
          pr "    goto out;\n";
@@ -3689,7 +3689,7 @@ let generate_lib_api_c () =
     List.iter (
       function
       | OClosure { cbname } ->
-         pr ", %s_callback.callback ? \"<fun>\" : \"NULL\"" cbname
+         pr ", CALLBACK_IS_NULL (%s_callback) ? \"<fun>\" : \"NULL\"" cbname
       | OFlags (n, _) -> pr ", %s" n
     ) optargs;
     pr ");\n"
diff --git a/generator/states-reply-simple.c b/generator/states-reply-simple.c
index 2f83e6f..5e5b631 100644
--- a/generator/states-reply-simple.c
+++ b/generator/states-reply-simple.c
@@ -60,7 +60,7 @@
   case 0:
     /* guaranteed by START */
     assert (cmd);
-    if (cmd->cb.fn.chunk.callback) {
+    if (CALLBACK_IS_NOT_NULL (cmd->cb.fn.chunk)) {
       int error = 0;
 
       assert (cmd->error == 0);
diff --git a/generator/states-reply-structured.c b/generator/states-reply-structured.c
index a1641d4..85f0775 100644
--- a/generator/states-reply-structured.c
+++ b/generator/states-reply-structured.c
@@ -157,7 +157,7 @@
       set_error (0, "invalid length in NBD_REPLY_TYPE_BLOCK_STATUS");
       return 0;
     }
-    if (cmd->cb.fn.extent.callback == NULL) {
+    if (CALLBACK_IS_NULL (cmd->cb.fn.extent)) {
       SET_NEXT_STATE (%.DEAD);
       set_error (0, "not expecting NBD_REPLY_TYPE_BLOCK_STATUS here");
       return 0;
@@ -293,7 +293,8 @@
                    offset, cmd->offset, cmd->count);
         return 0;
       }
-      if (cmd->type == NBD_CMD_READ && cmd->cb.fn.chunk.callback) {
+      if (cmd->type == NBD_CMD_READ &&
+          CALLBACK_IS_NOT_NULL (cmd->cb.fn.chunk)) {
         int scratch = error;
 
         /* Different from successful reads: inform the callback about the
@@ -385,7 +386,7 @@
     offset = be64toh (h->sbuf.sr.payload.offset_data.offset);
 
     assert (cmd); /* guaranteed by CHECK */
-    if (cmd->cb.fn.chunk.callback) {
+    if (CALLBACK_IS_NOT_NULL (cmd->cb.fn.chunk)) {
       int error = cmd->error;
 
       if (CALL_CALLBACK (cmd->cb.fn.chunk, cmd->data + (offset - cmd->offset),
@@ -446,7 +447,7 @@
      * them as an extension, and this works even when length == 0.
      */
     memset (cmd->data + offset, 0, length);
-    if (cmd->cb.fn.chunk.callback) {
+    if (CALLBACK_IS_NOT_NULL (cmd->cb.fn.chunk)) {
       int error = cmd->error;
 
       if (CALL_CALLBACK (cmd->cb.fn.chunk,
@@ -479,7 +480,7 @@
 
     assert (cmd); /* guaranteed by CHECK */
     assert (cmd->type == NBD_CMD_BLOCK_STATUS);
-    assert (cmd->cb.fn.extent.callback);
+    assert (CALLBACK_IS_NOT_NULL (cmd->cb.fn.extent));
     assert (h->bs_entries);
     assert (length >= 12);
 
diff --git a/generator/states-reply.c b/generator/states-reply.c
index 575a6d1..14bb010 100644
--- a/generator/states-reply.c
+++ b/generator/states-reply.c
@@ -168,7 +168,7 @@ save_reply_state (struct nbd_handle *h)
   retire = cmd->type == NBD_CMD_DISC;
 
   /* Notify the user */
-  if (cmd->cb.completion.callback) {
+  if (CALLBACK_IS_NOT_NULL (cmd->cb.completion)) {
     int error = cmd->error;
     int r;
 
diff --git a/generator/states.c b/generator/states.c
index cfa9957..32bf975 100644
--- a/generator/states.c
+++ b/generator/states.c
@@ -121,7 +121,7 @@ void abort_commands (struct nbd_handle *h,
     bool retire = cmd->type == NBD_CMD_DISC;
 
     next = cmd->next;
-    if (cmd->cb.completion.callback) {
+    if (CALLBACK_IS_NOT_NULL (cmd->cb.completion)) {
       int error = cmd->error ? cmd->error : ENOTCONN;
       int r;
 
diff --git a/lib/debug.c b/lib/debug.c
index eec2051..90d2f71 100644
--- a/lib/debug.c
+++ b/lib/debug.c
@@ -82,7 +82,7 @@ nbd_internal_debug (struct nbd_handle *h, const char *fs, ...)
   if (r == -1)
     goto out;
 
-  if (h->debug_callback.callback)
+  if (CALLBACK_IS_NOT_NULL (h->debug_callback))
     /* ignore return value */
     CALL_CALLBACK (h->debug_callback, context, msg);
   else
diff --git a/lib/internal.h b/lib/internal.h
index dc3676a..a18d581 100644
--- a/lib/internal.h
+++ b/lib/internal.h
@@ -273,6 +273,11 @@ struct command {
   uint32_t error; /* Local errno value */
 };
 
+/* Test if a callback is "null" or not, and set it to null. */
+#define CALLBACK_IS_NULL(cb)     ((cb).callback == NULL)
+#define CALLBACK_IS_NOT_NULL(cb) (! CALLBACK_IS_NULL ((cb)))
+#define SET_CALLBACK_TO_NULL(cb) ((cb).callback = NULL)
+
 /* Call a callback. */
 #define CALL_CALLBACK(cb, ...) \
   (cb).callback ((cb).user_data, ##__VA_ARGS__)
@@ -286,9 +291,9 @@ struct command {
 #define FREE_CALLBACK(cb)                                               \
   do {                                                                  \
     nbd_completion_callback *_cb = (nbd_completion_callback *)&(cb);    \
-    if (_cb->callback != NULL && _cb->free != NULL)                     \
+    if (CALLBACK_IS_NOT_NULL (cb) && _cb->free != NULL)                 \
       _cb->free (_cb->user_data);                                       \
-    _cb->callback = NULL;                                               \
+    SET_CALLBACK_TO_NULL (cb);                                          \
   } while (0)
 
 /* aio.c */
-- 
2.22.0




More information about the Libguestfs mailing list