Re: [PATCH 5/5] typesafe: TIMER_INITIALIZER and setup_timer

From: Rusty Russell
Date: Sun Mar 09 2008 - 21:08:58 EST


On Thursday 06 March 2008 21:40:35 Al Viro wrote:
> extern void decay_to_pointer(void const volatile *);
> #define check_callback_type(f,x) (sizeof((0 ? decay_to_pointer(x),(f)(x) :
> (void)0), 0)) #define __setup_timer(timer, func, arg) \
> do { \
> struct timer_list *__timer = (timer); \
> __timer->function = (void (*)(unsigned long))(func); \
> __timer->data = (unsigned long)(arg); \
> (void)check_callback_type(func, arg); \
> } while(0)

Thanks for that snippet; it's much more complete than the last one I found.
I especially like the handling of const- and volatile-taking callback fns.
My version is uglier, pasted below.

There are three problems I see here. First, the lack of warning for "void
intfn(int); DECLARE_TIMER(t, intfn, 0);", but that's relatively minor.
Second, the change of all the users is just churn, with cast_if_type() it can
be done over the next couple of years, if ever.

Worst, I can't see a way to apply your technique in general, for
non-void-returning functions (eg. interrupt handlers).

Here's the typesafe_cb () helper function from ccan (there are also
typesafe_cb_preargs and postargs for callbacks with extra args):

/* Doesn't handle const volatile, but can be added. */
#define typesafe_cb(rettype, fn, arg) \
cast_if_type(cast_if_type(cast_if_type((fn), \
rettype (*)(const typeof(arg)), \
rettype (*)(void *)), \
rettype (*)(volatile typeof(arg)), \
rettype (*)(void *)), \
rettype (*)(typeof(arg)), \
rettype (*)(void *))

> PS: apologies for missing your previous mail; ETOOBIGMBOX ;-/

That's fine, happens to all of us. I was a little disappointed to return from
holiday to discover that either your work or mine hadn't been merged tho :)

Cheers,
Rusty.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/