[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [Libguestfs] [libnbd] More thoughts on callbacks and more

On Mon, Jul 22, 2019 at 10:08:25AM +0100, Richard W.M. Jones wrote:
> On Sat, Jul 20, 2019 at 07:38:45AM +0100, Richard W.M. Jones wrote:
> > More thoughts on callbacks, etc. following on from:
> > https://www.redhat.com/archives/libguestfs/2019-July/thread.html#00184
> > 
> > Closure lifetimes
> > -----------------

Here's a possibly better idea which still changes the API a bit but
not as invasively.

We overload the callback so that it can either be a callback function
or a "free function".  They are distinguished by an extra flag
argument passed to the callback:

  extern int nbd_set_debug_callback (
        struct nbd_handle *h,
        int (*debug_fn) (int valid_flag,   // <-- note
                         void *user_data,
	                 const char *context, const char *msg),
        void *user_data);

The extra ‘valid_flag’ contains one or both of LIBNBD_CALLBACK_VALID
and LIBNBD_CALLBACK_FREE.  Callback code would look like:

  int my_debug_fn (int valid_flag, void *user_data,
                   const char *context, const char *msg)
    if (valid_flag & LIBNBD_CALLBACK_VALID) {
      // This is the normal callback as before.
      printf ("debug msg = %s\n", msg);

    if (valid_flag & LIBNBD_CALLBACK_FREE) {
      // In this case only user_data is valid.  The other parameters
      // may be unspecified (unless VALID was also set).
      free (user_data);


Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-builder quickly builds VMs from scratch

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]