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

Eric Blake eblake at redhat.com
Thu Aug 15 11:46:35 UTC 2019


On 8/15/19 4:56 AM, Richard W.M. Jones wrote:
> 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;

Slightly longer, but gives us a bit more freedom for future refactorings
(if everything goes through the macros rather than direct assignments,
then only the macro needs tweaking).


> +++ 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)))

I don't know if I would have done this macro, but it doesn't hurt.

> +#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 */
> 

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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libguestfs/attachments/20190815/413f1049/attachment.sig>


More information about the Libguestfs mailing list