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
-----------------
Closures could have a lifetime if we had a little bit of support from
the C library. We would generate (from C only):
nbd_set_free_<fn>_<closure> (nbd, free_closure);
which calls free_closure (user_data) as soon as the closure will no
longer be called by the library. This function would be used to
decrement the refcount from Python or remove the global root from
OCaml.
Note this is a family of functions, eg:
nbd_set_free_set_debug_callback_debug_fn
corresponding to the debug_fn arg of nbd_set_debug_callback. Luckily
they can all be generated along with the internal machinery to call
them.
As written above this doesn't quite work. However it could work to
pass an optional free function with the closure. In other words it
would look like:
struct nbd_closure { .cl = my_debug_fn,
.user_data = foo, .free = my_free } cl;
nbd_set_debug_callback (nbd, &cl);
cl->free (cl->user_data) is called if cl->free != NULL when the
closure is no longer used by the library.
This is a bit of a change to the API however.
Rich.
--
Richard Jones, Virtualization Group, Red Hat
http://people.redhat.com/~rjones
Read my programming and virtualization blog:
http://rwmj.wordpress.com
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine. Supports Linux and Windows.
http://people.redhat.com/~rjones/virt-df/