LWN.net Logo

The timer API: size or type safety?

The timer API: size or type safety?

Posted Dec 7, 2006 16:16 UTC (Thu) by pimlott (guest, #1535)
Parent article: The timer API: size or type safety?

Wow--what Al has done is write a type-safe parametrically polymorphic function in C! The signature of SETUP_TIMER is effectively

    void SETUP_TIMER((*function)(T), T data)
where the T can be any (pointer) type. While you can't write such a signature in C, Al guarantees that the two instances of T are in fact the same with a clever bit of code that has no run-time impact. Lovely!


(Log in to post comments)

The timer API: size or type safety?

Posted Dec 7, 2006 20:34 UTC (Thu) by jzbiciak (✭ supporter ✭, #5246) [Link]

Is typeof() standard C or a GNU extension?

The timer API: size or type safety?

Posted Dec 8, 2006 18:20 UTC (Fri) by giraffedata (subscriber, #1954) [Link]

Is typeof() standard C or a GNU extension?

I'ts GNU C. But it looks like it isn't necessary for this technique.

The timer API: size or type safety?

Posted Dec 8, 2006 19:42 UTC (Fri) by jzbiciak (✭ supporter ✭, #5246) [Link]

Are you certain? Let's look at that closely:

    typeof(*data) *p = data;
    timer->function = (void (*)(void *)) func;
    timer->data = (void *) p;
    (void)(0 && (func(p), 0));

I guess your statement is that the last line, (func(p)) could be rewritten as (func(data)) instead. I can see that.

The timer API: size or type safety?

Posted Jan 3, 2007 19:48 UTC (Wed) by rjbell4 (guest, #35764) [Link]

The reason you wouldn't necessarily do that is that "data" may actually be an expression that has a side effect, so you don't want to reference it twice.

The timer API: size or type safety?

Posted Jan 3, 2007 19:59 UTC (Wed) by jzbiciak (✭ supporter ✭, #5246) [Link]

So, I guess typeof(*data) doesn't evaluate data then? I guess that makes sense.

The timer API: size or type safety?

Posted Jan 3, 2007 20:01 UTC (Wed) by jzbiciak (✭ supporter ✭, #5246) [Link]

Actually, hold on... (0 && func(data)) shouldn't evaluate data a second time under any circumstances anyway.

The timer API: size or type safety?

Posted Dec 8, 2006 11:46 UTC (Fri) by viro (subscriber, #7872) [Link]

Heh... The current version in my tree actually gets you a constant
arithmetic expression. It's built around the following:
#define check_callback_type(f,x) (sizeof((0 ? (f)(x) : (void)0), 0))
Feel free to torture students with it... It actually checks even
more - namely, that f returns void. And since it never evaluates
f or x, we are free to use it in macros without any concerns about
side effects. Can be used in global initializers, too (and yes,
it _is_ valid C99; we get past the constraints since the expression
that appears to be problematic is an argument of sizeof and doesn't
have a variably-modified type).

The timer API: size or type safety?

Posted Dec 8, 2006 17:10 UTC (Fri) by liljencrantz (subscriber, #28458) [Link]

That is so clever it hurts.

Am I correct in thinking that since check_callback_type never evaluates f or x, we also don't need to use typeof anymore, as we only truly reference f or x once? I.e. we can write this as:

check_callback_type( func, data );
timer->function = (void (*)(void *)) func;
timer->data = (void *) data;

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds