Re: Strange code in include/linux/cpumask.h

From: Nikanth Karthikesan
Date: Thu Mar 26 2009 - 00:15:30 EST


On Thursday 26 March 2009 04:14:46 Rusty Russell wrote:
> On Wednesday 25 March 2009 17:14:30 Nikanth Karthikesan wrote:
> > On Wednesday 25 March 2009 10:41:11 Rusty Russell wrote:
> > > Yes, except that this insists that bitmap be an unsigned long * or
> > > you'll get a warning. Otherwise the macro could be used on anything.
> > > And it needs to be a macro to use it as a static initializer.
> >
> > Ah, got it. Thanks a lot for the explanation. May be a comment could be
> > added to the source.
>
> Well, it's not that unusual a trick in the kernel, but an explanation might
> help.
>

I _assumed_ that kernel developers are good programmers who do not need type
enforcements/warnings. And tricky code for non-performance optimizations wont
be allowed. Yes, this is not very tricky once you are aware that such tricks
are done in the kernel just for type-safety. Anyway lets document it.

Thanks
Nikanth


Add a comment explaining the purpose of the tricky code in the macro
to_cpumask(). The __check_is_bitmap() is used to enforce type-safety.

Signed-off-by: Nikanth Karthikesan <knikanth@xxxxxxx>

---

diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
index 9f31538..32b802b 100644
--- a/include/linux/cpumask.h
+++ b/include/linux/cpumask.h
@@ -279,6 +279,7 @@ static inline void __cpus_shift_left(cpumask_t *dstp,
* expose the definition of 'struct cpumask'.
*
* This does the conversion, and can be used as a constant initializer.
+ * __check_is_bitmap() enforces bitmap to be an 'unsigned long *'.
*/
#define to_cpumask(bitmap) \
((struct cpumask *)(1 ? (bitmap) \

--
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/