[PATCH 0/4] typesafe callbacks - Kernel
This is a discussion on [PATCH 0/4] typesafe callbacks - Kernel ; It's possible to portably make callbacks typesafe, as well as taking the
existing void * arg. This makes the callers more readable and more typesafe.
Done in a way that doesn't break non-gcc: they just don't get a warning.
Been ...
-
[PATCH 0/4] typesafe callbacks
It's possible to portably make callbacks typesafe, as well as taking the
existing void * arg. This makes the callers more readable and more typesafe.
Done in a way that doesn't break non-gcc: they just don't get a warning.
Been in linux-next for about two full cycles now, and I really want to start
using this in my own code.
Thanks,
Rusty.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
-
[PATCH 1/4] typesafe: cast_if_type: allow macros functions which take more than one type.
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. __builtin_choose_expr was
introduced in gcc 3.1 (kernel needs >= 3.2 anyway).
(includes Harvey Harrison 's sparse fix)
Signed-off-by: Rusty Russell
Cc: Harvey Harrison
---
include/linux/compiler-gcc.h | 18 ++++++++++++++++++
include/linux/compiler-intel.h | 2 ++
2 files changed, 20 insertions(+)
diff -r ecbc022961d8 include/linux/compiler-gcc.h
--- a/include/linux/compiler-gcc.h Mon Jul 14 12:30:35 2008 +1000
+++ b/include/linux/compiler-gcc.h Mon Jul 14 20:46:23 2008 +1000
@@ -61,3 +61,21 @@
#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, but means @expr must be a pointer or
+ * integer.
+ */
+#define cast_if_type(expr, oktype, desttype) __builtin_choose_expr( \
+ __builtin_types_compatible_p(typeof(1?(expr)
expr)), oktype), \
+ (desttype)(expr), (expr))
diff -r ecbc022961d8 include/linux/compiler-intel.h
--- a/include/linux/compiler-intel.h Mon Jul 14 12:30:35 2008 +1000
+++ b/include/linux/compiler-intel.h Mon Jul 14 20:46:23 2008 +1000
@@ -29,3 +29,5 @@
#endif
#define uninitialized_var(x) x
+
+#define cast_if_type(expr, oktype, desttype) ((desttype)(expr))
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/