[Swan-dev] typing libevent callback functions

D. Hugh Redelmeier hugh at mimosa.com
Thu Jan 29 04:17:45 EET 2015


| From: Antony Antony <antony at phenome.org>

| On Sun, Jan 25, 2015 at 04:10:12PM -0500, D. Hugh Redelmeier wrote:
| > Getting callback functions right is tricky.  One tool that can detect
| > some problems is the C type system.
| 
| I see some appeal this idea, however, I don't have a clear picture of 
| what could potentially go wrong otherwise.

It depends on how strict the C compiler is.  All C compilers that I
know of are fairly strict in having all declarations and definitions
of a function match.  Not always with pointer arguments.

More important is the documentation it provides.


|  Any 
| references for further reading?

Not that I know of.

| Having said that, this is a minor change lets do it.

More of an addition than a change.  And minor.

| >   typedef void event_callback_routine(evutil_socket_t fd, short events, void *arg);

| > Here's a list of the callback functions used with pluto_event_new:
| >     kernel_process_msg_cb
| >     kernel_process_queue_cb
| >     comm_handle_cb
| >     handle_helper_answer_cb
| >     timer_event_cb
| 
| do we need one typedef per function? I am guessing not.

No, one declaration of the functino per function.

| Please take a look at bdf978aa5b441fec27.

If a static cb function didn't need a separate declaration (i.e. was
not referenced before its definition) you can put the new definition
anywhere in the file (not inside something).  I tend to put it
immediately before the definition so that the two are visually linked.

I would not change the function prototype of pluto_event_new: I'd
leave it using
	event_callback_fn cb
because that matches event_new's signature.

This should be clear from the example of the crypto continuation
function typedef.


More information about the Swan-dev mailing list