[PATCH 4/6] typesafe: cast_if_type to allow macros functions which take more than one type.

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Linus Torvalds <torvalds@...>
Cc: Andrew Morton <akpm@...>, <linux-kernel@...>, Jeff Garzik <jeff@...>, Tejun Heo <htejun@...>
Date: Sunday, January 20, 2008 - 5:51 am

To create functions which can take two types, but still warn on any
other types, we need a way of casting one type and no others.

To make things more complex, it should correctly handle function args,
NULL, and be usable in initializers.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>

diff -r e6626cc7bdc2 include/linux/compiler-gcc.h
--- a/include/linux/compiler-gcc.h	Sun Jan 20 18:51:51 2008 +1100
+++ b/include/linux/compiler-gcc.h	Sun Jan 20 18:57:14 2008 +1100
@@ -53,3 +53,20 @@
 #define  noinline			__attribute__((noinline))
 #define __attribute_const__		__attribute__((__const__))
 #define __maybe_unused			__attribute__((unused))
+
+/**
+ * cast_if_type - allow an alternate type
+ * @expr: the expression to optionally cast
+ * @oktype: the type to allow.
+ * @desttype: the type to cast to.
+ *
+ * This is used to accept a particular alternate type for an expression:
+ * because any other types will not be cast, they will cause a warning as
+ * normal.
+ *
+ * Note that the unnecessary trinary forces functions to devolve into
+ * function pointers as users expect. */
+#define cast_if_type(expr, oktype, desttype)				\
+  __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):NULL), \
+						     oktype),		\
+			(desttype)(expr), (expr))
diff -r e6626cc7bdc2 include/linux/compiler-intel.h
--- a/include/linux/compiler-intel.h	Sun Jan 20 18:51:51 2008 +1100
+++ b/include/linux/compiler-intel.h	Sun Jan 20 18:57:14 2008 +1100
@@ -29,3 +29,5 @@
 #endif
 
 #define uninitialized_var(x) x
+
+#define cast_if_type(expr, oktype, desttype) ((desttype)(expr))
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 0/6] RFC: Typesafe callbacks, Rusty Russell, (Sun Jan 20, 5:46 am)
Re: [PATCH 0/6] RFC: Typesafe callbacks, Tejun Heo, (Sun Jan 20, 8:56 am)
Re: [PATCH 0/6] RFC: Typesafe callbacks, Tejun Heo, (Sun Jan 20, 9:00 am)
Re: [PATCH 0/6] RFC: Typesafe callbacks, Rusty Russell, (Sun Jan 20, 6:17 pm)
Re: [PATCH 0/6] RFC: Typesafe callbacks, Rusty Russell, (Mon Jan 21, 7:33 am)
Re: [PATCH 0/6] RFC: Typesafe callbacks, Tejun Heo, (Mon Jan 21, 8:38 am)
Re: [PATCH 0/6] RFC: Typesafe callbacks, Rusty Russell, (Mon Jan 21, 7:27 pm)
Re: [PATCH 0/6] RFC: Typesafe callbacks, Andi Kleen, (Tue Jan 22, 12:20 am)
Re: [PATCH 0/6] RFC: Typesafe callbacks, Linus Torvalds, (Mon Jan 21, 7:57 pm)
Re: [PATCH 0/6] RFC: Typesafe callbacks, Rusty Russell, (Tue Jan 22, 3:16 am)
Re: [PATCH 0/6] RFC: Typesafe callbacks, Linus Torvalds, (Tue Jan 22, 11:53 am)
[PATCH 1/6] typesafe: Convert stop_machine and callers, Rusty Russell, (Sun Jan 20, 5:47 am)
[PATCH 2/6] typesafe: kthread_create and kthread_run, Rusty Russell, (Sun Jan 20, 5:48 am)
Re: [PATCH 2/6] typesafe: kthread_create and kthread_run, Jan Engelhardt, (Sun Jan 20, 7:25 am)
Re: [PATCH 2/6] typesafe: kthread_create and kthread_run, Johannes Weiner, (Sun Jan 20, 12:24 pm)
Re: [PATCH 2/6] typesafe: kthread_create and kthread_run, Rusty Russell, (Sun Jan 20, 6:04 pm)
[PATCH 3/6] typesafe: convert kthread users, Rusty Russell, (Sun Jan 20, 5:50 am)
[PATCH 4/6] typesafe: cast_if_type to allow macros functions..., Rusty Russell, (Sun Jan 20, 5:51 am)
[PATCH 5/6] typesafe: request_irq and devm_request_irq, Rusty Russell, (Sun Jan 20, 5:54 am)
[PATCH 6/6] typesafe: timers, Rusty Russell, (Sun Jan 20, 5:57 am)