* [PATCH] Use more gcc extensions in the Linux headers
@ 2007-03-09 5:56 Rusty Russell
2007-03-09 6:22 ` Stephen Rothwell
` (6 more replies)
0 siblings, 7 replies; 29+ messages in thread
From: Rusty Russell @ 2007-03-09 5:56 UTC (permalink / raw)
To: lkml - Kernel Mailing List; +Cc: Linus Torvalds, Andrew Morton
__builtin_types_compatible_p() has been around since gcc 2.95, and we
don't use it anywhere. This patch quietly fixes that.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
diff -r f0ff8138f993 include/linux/kernel.h
--- a/include/linux/kernel.h Fri Mar 09 16:40:25 2007 +1100
+++ b/include/linux/kernel.h Fri Mar 09 16:44:04 2007 +1100
@@ -35,7 +35,9 @@ extern const char linux_proc_banner[];
#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) \
+ + sizeof(typeof(int[1 - 2*!!__builtin_types_compatible_p(typeof(arr), \
+ typeof(&arr[0]))]))*0)
#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
#define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 5:56 [PATCH] Use more gcc extensions in the Linux headers Rusty Russell
@ 2007-03-09 6:22 ` Stephen Rothwell
2007-03-09 6:35 ` Linus Torvalds
` (5 subsequent siblings)
6 siblings, 0 replies; 29+ messages in thread
From: Stephen Rothwell @ 2007-03-09 6:22 UTC (permalink / raw)
To: Rusty Russell; +Cc: lkml - Kernel Mailing List, Linus Torvalds, Andrew Morton
[-- Attachment #1: Type: text/plain, Size: 472 bytes --]
On Fri, 09 Mar 2007 16:56:32 +1100 Rusty Russell <rusty@rustcorp.com.au> wrote:
>
> __builtin_types_compatible_p() has been around since gcc 2.95, and we
> don't use it anywhere. This patch quietly fixes that.
After staring at this for about 2 minutes, how about a commit message like:
Make ARRAY_SIZE complain strangely if passed a pointer instead of an
array.
:-)
--
Cheers,
Stephen Rothwell sfr@canb.auug.org.au
http://www.canb.auug.org.au/~sfr/
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 5:56 [PATCH] Use more gcc extensions in the Linux headers Rusty Russell
2007-03-09 6:22 ` Stephen Rothwell
@ 2007-03-09 6:35 ` Linus Torvalds
2007-03-09 6:50 ` Andrey Panin
` (4 subsequent siblings)
6 siblings, 0 replies; 29+ messages in thread
From: Linus Torvalds @ 2007-03-09 6:35 UTC (permalink / raw)
To: Rusty Russell; +Cc: lkml - Kernel Mailing List, Andrew Morton
On Fri, 9 Mar 2007, Rusty Russell wrote:
>
> __builtin_types_compatible_p() has been around since gcc 2.95, and we
> don't use it anywhere. This patch quietly fixes that.
Whee.
Rusty, that's a work of art.
However, I would suggest that you never show it to anybody ever again. I'm
sure that in fifty years, it will be worth much more. So please keep it
tightly under wraps, to keep people from gouging their eyes out^W^W^W^W^W^W^W
make a killing in the art market.
Please.
Linus
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 5:56 [PATCH] Use more gcc extensions in the Linux headers Rusty Russell
2007-03-09 6:22 ` Stephen Rothwell
2007-03-09 6:35 ` Linus Torvalds
@ 2007-03-09 6:50 ` Andrey Panin
2007-03-09 7:53 ` Christoph Hellwig
2007-03-09 7:52 ` Christoph Hellwig
` (3 subsequent siblings)
6 siblings, 1 reply; 29+ messages in thread
From: Andrey Panin @ 2007-03-09 6:50 UTC (permalink / raw)
To: Rusty Russell; +Cc: lkml - Kernel Mailing List, Linus Torvalds, Andrew Morton
[-- Attachment #1: Type: text/plain, Size: 1438 bytes --]
On 068, 03 09, 2007 at 04:56:32PM +1100, Rusty Russell wrote:
> __builtin_types_compatible_p() has been around since gcc 2.95,
but it's not available in Intel C compiler IIRC :(
> and we don't use it anywhere. This patch quietly fixes that.
>
> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
>
> diff -r f0ff8138f993 include/linux/kernel.h
> --- a/include/linux/kernel.h Fri Mar 09 16:40:25 2007 +1100
> +++ b/include/linux/kernel.h Fri Mar 09 16:44:04 2007 +1100
> @@ -35,7 +35,9 @@ extern const char linux_proc_banner[];
> #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
> #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
>
> -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
> +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) \
> + + sizeof(typeof(int[1 - 2*!!__builtin_types_compatible_p(typeof(arr), \
> + typeof(&arr[0]))]))*0)
> #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
> #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
> #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
>
>
> -
> 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/
>
--
Andrey Panin | Linux and UNIX system administrator
pazke@donpac.ru | PGP key: wwwkeys.pgp.net
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 5:56 [PATCH] Use more gcc extensions in the Linux headers Rusty Russell
` (2 preceding siblings ...)
2007-03-09 6:50 ` Andrey Panin
@ 2007-03-09 7:52 ` Christoph Hellwig
2007-03-09 8:35 ` Jarek Poplawski
2007-03-09 11:27 ` Theodore Ts'o
` (2 subsequent siblings)
6 siblings, 1 reply; 29+ messages in thread
From: Christoph Hellwig @ 2007-03-09 7:52 UTC (permalink / raw)
To: Rusty Russell; +Cc: lkml - Kernel Mailing List, Linus Torvalds, Andrew Morton
On Fri, Mar 09, 2007 at 04:56:32PM +1100, Rusty Russell wrote:
> __builtin_types_compatible_p() has been around since gcc 2.95, and we
> don't use it anywhere. This patch quietly fixes that.
>
> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
>
> diff -r f0ff8138f993 include/linux/kernel.h
> --- a/include/linux/kernel.h Fri Mar 09 16:40:25 2007 +1100
> +++ b/include/linux/kernel.h Fri Mar 09 16:44:04 2007 +1100
> @@ -35,7 +35,9 @@ extern const char linux_proc_banner[];
> #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
> #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
>
> -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
> +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) \
> + + sizeof(typeof(int[1 - 2*!!__builtin_types_compatible_p(typeof(arr), \
> + typeof(&arr[0]))]))*0)
This needs a comment explaning why we're doing this, and maybe a little
explanation of the combination of gcc magic and C trickery used to implement
it to the brave non-uberhacker people trying to understand linux headers.
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 6:50 ` Andrey Panin
@ 2007-03-09 7:53 ` Christoph Hellwig
2007-03-09 9:02 ` Andrey Panin
0 siblings, 1 reply; 29+ messages in thread
From: Christoph Hellwig @ 2007-03-09 7:53 UTC (permalink / raw)
To: Rusty Russell, lkml - Kernel Mailing List, Linus Torvalds,
Andrew Morton
On Fri, Mar 09, 2007 at 09:50:56AM +0300, Andrey Panin wrote:
> On 068, 03 09, 2007 at 04:56:32PM +1100, Rusty Russell wrote:
> > __builtin_types_compatible_p() has been around since gcc 2.95,
>
> but it's not available in Intel C compiler IIRC :(
So what?
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 7:52 ` Christoph Hellwig
@ 2007-03-09 8:35 ` Jarek Poplawski
0 siblings, 0 replies; 29+ messages in thread
From: Jarek Poplawski @ 2007-03-09 8:35 UTC (permalink / raw)
To: Christoph Hellwig, Rusty Russell, lkml - Kernel Mailing List,
Linus Torvalds, Andrew Morton
On 09-03-2007 08:52, Christoph Hellwig wrote:
> On Fri, Mar 09, 2007 at 04:56:32PM +1100, Rusty Russell wrote:
...
>> +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) \
>> + + sizeof(typeof(int[1 - 2*!!__builtin_types_compatible_p(typeof(arr), \
>> + typeof(&arr[0]))]))*0)
>
> This needs a comment explaning why we're doing this, and maybe a little
> explanation of the combination of gcc magic and C trickery used to implement
> it to the brave non-uberhacker people trying to understand linux headers.
...and to deprive them of all the pleasure of contemplating...
Jarek P.
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 7:53 ` Christoph Hellwig
@ 2007-03-09 9:02 ` Andrey Panin
2007-03-09 9:05 ` Christoph Hellwig
0 siblings, 1 reply; 29+ messages in thread
From: Andrey Panin @ 2007-03-09 9:02 UTC (permalink / raw)
To: Christoph Hellwig, Rusty Russell, lkml - Kernel Mailing List,
Linus Torvalds, Andrew Morton
[-- Attachment #1: Type: text/plain, Size: 543 bytes --]
On 068, 03 09, 2007 at 07:53:08AM +0000, Christoph Hellwig wrote:
> On Fri, Mar 09, 2007 at 09:50:56AM +0300, Andrey Panin wrote:
> > On 068, 03 09, 2007 at 04:56:32PM +1100, Rusty Russell wrote:
> > > __builtin_types_compatible_p() has been around since gcc 2.95,
> >
> > but it's not available in Intel C compiler IIRC :(
>
> So what?
Kernel compilation with Intel compiler is (was ?) supported.
This patch will break it.
--
Andrey Panin | Linux and UNIX system administrator
pazke@donpac.ru | PGP key: wwwkeys.pgp.net
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 9:02 ` Andrey Panin
@ 2007-03-09 9:05 ` Christoph Hellwig
2007-03-09 17:47 ` Linus Torvalds
0 siblings, 1 reply; 29+ messages in thread
From: Christoph Hellwig @ 2007-03-09 9:05 UTC (permalink / raw)
To: Christoph Hellwig, Rusty Russell, lkml - Kernel Mailing List,
Linus Torvalds, Andrew Morton
On Fri, Mar 09, 2007 at 12:02:19PM +0300, Andrey Panin wrote:
> Kernel compilation with Intel compiler is (was ?) supported.
> This patch will break it.
It was only put in under the premise that they'll fix whatever breaks,
we're not going to put any maintaince border on us to hack around
broken propritary compilers.
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 5:56 [PATCH] Use more gcc extensions in the Linux headers Rusty Russell
` (3 preceding siblings ...)
2007-03-09 7:52 ` Christoph Hellwig
@ 2007-03-09 11:27 ` Theodore Ts'o
2007-03-09 16:52 ` Andi Kleen
2007-03-09 22:57 ` Rusty Russell
6 siblings, 0 replies; 29+ messages in thread
From: Theodore Ts'o @ 2007-03-09 11:27 UTC (permalink / raw)
To: Rusty Russell; +Cc: lkml - Kernel Mailing List, Linus Torvalds, Andrew Morton
On Fri, Mar 09, 2007 at 04:56:32PM +1100, Rusty Russell wrote:
> __builtin_types_compatible_p() has been around since gcc 2.95, and we
> don't use it anywhere. This patch quietly fixes that.
>
> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Is your clock set correctly? Looks like this mail was sent 23 days
early. :-)
- Ted
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 5:56 [PATCH] Use more gcc extensions in the Linux headers Rusty Russell
` (4 preceding siblings ...)
2007-03-09 11:27 ` Theodore Ts'o
@ 2007-03-09 16:52 ` Andi Kleen
2007-03-09 22:57 ` Rusty Russell
6 siblings, 0 replies; 29+ messages in thread
From: Andi Kleen @ 2007-03-09 16:52 UTC (permalink / raw)
To: Rusty Russell; +Cc: lkml - Kernel Mailing List, Linus Torvalds, Andrew Morton
Rusty Russell <rusty@rustcorp.com.au> writes:
> __builtin_types_compatible_p() has been around since gcc 2.95, and we
> don't use it anywhere. This patch quietly fixes that.
Using BUILD_BUG_ON_ZERO() would have been somewhat cleaner.
-Andi
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 9:05 ` Christoph Hellwig
@ 2007-03-09 17:47 ` Linus Torvalds
2007-03-10 20:36 ` Trent Piepho
0 siblings, 1 reply; 29+ messages in thread
From: Linus Torvalds @ 2007-03-09 17:47 UTC (permalink / raw)
To: Christoph Hellwig
Cc: Rusty Russell, lkml - Kernel Mailing List, Andrew Morton
On Fri, 9 Mar 2007, Christoph Hellwig wrote:
>
> It was only put in under the premise that they'll fix whatever breaks,
> we're not going to put any maintaince border on us to hack around
> broken propritary compilers.
Well, since Rusty's macro was hoddible *anyway*, I don't think I'd apply
it as-is. Breaking icc for something that ugly and not-very-important
simply makes no sense.
There are better ways to do this.
For one, you could (and should!) abstract these kinds of things out,
rather than put them in another macro that really does something totally
different. Then, the macro could have become
#define ARRAY_SIZE (sizeof_expression + 0*error_if_not_array)
which would already be a hell of a lot more readable. But more
importantly, it's also now suddenly much easiler to abstract out for
different compilers.
We *already* support different compilers through <linux/compiler.h>, and
there just isn't any reason for bad code just for bad codes sake!
Linus
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 5:56 [PATCH] Use more gcc extensions in the Linux headers Rusty Russell
` (5 preceding siblings ...)
2007-03-09 16:52 ` Andi Kleen
@ 2007-03-09 22:57 ` Rusty Russell
2007-03-09 23:02 ` Randy Dunlap
` (4 more replies)
6 siblings, 5 replies; 29+ messages in thread
From: Rusty Russell @ 2007-03-09 22:57 UTC (permalink / raw)
To: lkml - Kernel Mailing List; +Cc: Linus Torvalds, Andrew Morton
On Fri, 2007-03-09 at 16:56 +1100, Rusty Russell wrote:
> __builtin_types_compatible_p() has been around since gcc 2.95, and we
> don't use it anywhere. This patch quietly fixes that.
OK, many people complained that it needed a comment. Good point!
==
Add comment to ARRAY_SIZE macro.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
diff -r 933e410f204f include/linux/kernel.h
--- a/include/linux/kernel.h Sat Mar 10 09:55:31 2007 +1100
+++ b/include/linux/kernel.h Sat Mar 10 09:55:53 2007 +1100
@@ -35,6 +35,7 @@ extern const char linux_proc_banner[];
#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
+/* GCC is awesome. */
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) \
+ sizeof(typeof(int[1 - 2*!!__builtin_types_compatible_p(typeof(arr), \
typeof(&arr[0]))]))*0)
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 22:57 ` Rusty Russell
@ 2007-03-09 23:02 ` Randy Dunlap
2007-03-09 23:09 ` Robert P. J. Day
` (3 subsequent siblings)
4 siblings, 0 replies; 29+ messages in thread
From: Randy Dunlap @ 2007-03-09 23:02 UTC (permalink / raw)
To: Rusty Russell; +Cc: lkml - Kernel Mailing List, Linus Torvalds, Andrew Morton
On Sat, 10 Mar 2007 09:57:32 +1100 Rusty Russell wrote:
> On Fri, 2007-03-09 at 16:56 +1100, Rusty Russell wrote:
> > __builtin_types_compatible_p() has been around since gcc 2.95, and we
> > don't use it anywhere. This patch quietly fixes that.
Bah. Just because gcc has a "feature" doesn't mean we should use it
(in this form).
> OK, many people complained that it needed a comment. Good point!
> ==
> Add comment to ARRAY_SIZE macro.
>
> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
>
> diff -r 933e410f204f include/linux/kernel.h
> --- a/include/linux/kernel.h Sat Mar 10 09:55:31 2007 +1100
> +++ b/include/linux/kernel.h Sat Mar 10 09:55:53 2007 +1100
> @@ -35,6 +35,7 @@ extern const char linux_proc_banner[];
> #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
> #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
>
> +/* GCC is awesome. */
> #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) \
> + sizeof(typeof(int[1 - 2*!!__builtin_types_compatible_p(typeof(arr), \
> typeof(&arr[0]))]))*0)
---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 22:57 ` Rusty Russell
2007-03-09 23:02 ` Randy Dunlap
@ 2007-03-09 23:09 ` Robert P. J. Day
2007-03-09 23:12 ` Roland Dreier
` (2 subsequent siblings)
4 siblings, 0 replies; 29+ messages in thread
From: Robert P. J. Day @ 2007-03-09 23:09 UTC (permalink / raw)
To: Rusty Russell; +Cc: lkml - Kernel Mailing List, Linus Torvalds, Andrew Morton
On Sat, 10 Mar 2007, Rusty Russell wrote:
> On Fri, 2007-03-09 at 16:56 +1100, Rusty Russell wrote:
> > __builtin_types_compatible_p() has been around since gcc 2.95, and we
> > don't use it anywhere. This patch quietly fixes that.
>
> OK, many people complained that it needed a comment. Good point!
> ==
> Add comment to ARRAY_SIZE macro.
>
> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
>
> diff -r 933e410f204f include/linux/kernel.h
> --- a/include/linux/kernel.h Sat Mar 10 09:55:31 2007 +1100
> +++ b/include/linux/kernel.h Sat Mar 10 09:55:53 2007 +1100
> @@ -35,6 +35,7 @@ extern const char linux_proc_banner[];
> #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
> #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
>
> +/* GCC is awesome. */
> #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) \
> + sizeof(typeof(int[1 - 2*!!__builtin_types_compatible_p(typeof(arr), \
> typeof(&arr[0]))]))*0)
ah, but is that "universe" kind of awesome, or the "hot dogs" kind of
awesome?
http://youtube.com/watch?v=0rYT0YvQ3hs
rday
--
========================================================================
Robert P. J. Day
Linux Consulting, Training and Annoying Kernel Pedantry
Waterloo, Ontario, CANADA
http://fsdev.net/wiki/index.php?title=Main_Page
========================================================================
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 22:57 ` Rusty Russell
2007-03-09 23:02 ` Randy Dunlap
2007-03-09 23:09 ` Robert P. J. Day
@ 2007-03-09 23:12 ` Roland Dreier
2007-03-10 1:04 ` Jan Engelhardt
2007-03-10 4:03 ` Valdis.Kletnieks
4 siblings, 0 replies; 29+ messages in thread
From: Roland Dreier @ 2007-03-09 23:12 UTC (permalink / raw)
To: Rusty Russell; +Cc: lkml - Kernel Mailing List, Linus Torvalds, Andrew Morton
Perhaps this patch can go into Wesnoth for testing for a while before
we merge it into the kernel?
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 22:57 ` Rusty Russell
` (2 preceding siblings ...)
2007-03-09 23:12 ` Roland Dreier
@ 2007-03-10 1:04 ` Jan Engelhardt
2007-03-11 2:50 ` Rusty Russell
2007-03-10 4:03 ` Valdis.Kletnieks
4 siblings, 1 reply; 29+ messages in thread
From: Jan Engelhardt @ 2007-03-10 1:04 UTC (permalink / raw)
To: Rusty Russell; +Cc: lkml - Kernel Mailing List, Linus Torvalds, Andrew Morton
On Mar 10 2007 09:57, Rusty Russell wrote:
>On Fri, 2007-03-09 at 16:56 +1100, Rusty Russell wrote:
>> __builtin_types_compatible_p() has been around since gcc 2.95, and we
>> don't use it anywhere. This patch quietly fixes that.
>
>OK, many people complained that it needed a comment. Good point!
>==
>Add comment to ARRAY_SIZE macro.
>
>Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
>
>diff -r 933e410f204f include/linux/kernel.h
>--- a/include/linux/kernel.h Sat Mar 10 09:55:31 2007 +1100
>+++ b/include/linux/kernel.h Sat Mar 10 09:55:53 2007 +1100
>@@ -35,6 +35,7 @@ extern const char linux_proc_banner[];
> #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
> #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
>
>+/* GCC is awesome. */
> #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) \
> + sizeof(typeof(int[1 - 2*!!__builtin_types_compatible_p(typeof(arr), \
> typeof(&arr[0]))]))*0)
Getting back at the macro, how would you like to have it merged?
Jan
--
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 22:57 ` Rusty Russell
` (3 preceding siblings ...)
2007-03-10 1:04 ` Jan Engelhardt
@ 2007-03-10 4:03 ` Valdis.Kletnieks
2007-03-10 4:24 ` Randy Dunlap
2007-03-10 5:19 ` Nigel Cunningham
4 siblings, 2 replies; 29+ messages in thread
From: Valdis.Kletnieks @ 2007-03-10 4:03 UTC (permalink / raw)
To: Rusty Russell; +Cc: lkml - Kernel Mailing List, Linus Torvalds, Andrew Morton
[-- Attachment #1: Type: text/plain, Size: 315 bytes --]
On Sat, 10 Mar 2007 09:57:32 +1100, Rusty Russell said:
> +/* GCC is awesome. */
> #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) \
> + sizeof(typeof(int[1 - 2*!!__builtin_types_compatible_p(typeof(arr), \
> typeof(&arr[0]))]))*0)
-/* GCC is awesome. */
+/* GCC leaves me speechless. */
[-- Attachment #2: Type: application/pgp-signature, Size: 226 bytes --]
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-10 4:03 ` Valdis.Kletnieks
@ 2007-03-10 4:24 ` Randy Dunlap
2007-03-11 15:56 ` Valdis.Kletnieks
2007-03-10 5:19 ` Nigel Cunningham
1 sibling, 1 reply; 29+ messages in thread
From: Randy Dunlap @ 2007-03-10 4:24 UTC (permalink / raw)
To: Valdis.Kletnieks
Cc: Rusty Russell, lkml - Kernel Mailing List, Linus Torvalds,
Andrew Morton
On Fri, 09 Mar 2007 23:03:05 -0500 Valdis.Kletnieks@vt.edu wrote:
> On Sat, 10 Mar 2007 09:57:32 +1100, Rusty Russell said:
>
> > +/* GCC is awesome. */
> > #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) \
> > + sizeof(typeof(int[1 - 2*!!__builtin_types_compatible_p(typeof(arr), \
> > typeof(&arr[0]))]))*0)
>
> -/* GCC is awesome. */
> +/* GCC leaves me speechless. */
"awesome" can mean "inspiring awe or admiration or wonder" (amazing)
or it can mean "awful" (as in terrifying). 8)
---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-10 4:03 ` Valdis.Kletnieks
2007-03-10 4:24 ` Randy Dunlap
@ 2007-03-10 5:19 ` Nigel Cunningham
2007-03-10 14:01 ` Jan Engelhardt
1 sibling, 1 reply; 29+ messages in thread
From: Nigel Cunningham @ 2007-03-10 5:19 UTC (permalink / raw)
To: Valdis.Kletnieks
Cc: Rusty Russell, lkml - Kernel Mailing List, Linus Torvalds,
Andrew Morton
Hi.
On Fri, 2007-03-09 at 23:03 -0500, Valdis.Kletnieks@vt.edu wrote:
> On Sat, 10 Mar 2007 09:57:32 +1100, Rusty Russell said:
>
> > +/* GCC is awesome. */
> > #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) \
> > + sizeof(typeof(int[1 - 2*!!__builtin_types_compatible_p(typeof(arr), \
> > typeof(&arr[0]))]))*0)
>
> -/* GCC is awesome. */
> +/* GCC leaves me speechless. */
A speechless Rusty would be horrible. That said, it would be nice if the
comment was something more like the normal Rusty pearl of wisdom. I
understand the first part, but have no idea was + sizeof(typeof(int[....
does...
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-10 5:19 ` Nigel Cunningham
@ 2007-03-10 14:01 ` Jan Engelhardt
2007-03-10 15:18 ` Andreas Schwab
0 siblings, 1 reply; 29+ messages in thread
From: Jan Engelhardt @ 2007-03-10 14:01 UTC (permalink / raw)
To: Nigel Cunningham
Cc: Valdis.Kletnieks, Rusty Russell, lkml - Kernel Mailing List,
Linus Torvalds, Andrew Morton
On Mar 10 2007 16:19, Nigel Cunningham wrote:
>On Fri, 2007-03-09 at 23:03 -0500, Valdis.Kletnieks@vt.edu wrote:
>> On Sat, 10 Mar 2007 09:57:32 +1100, Rusty Russell said:
>>
>> > +/* GCC is awesome. */
>> > #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) \
>> > + sizeof(typeof(int[1 - 2*!!__builtin_types_compatible_p(typeof(arr), \
>> > typeof(&arr[0]))]))*0)
>>
>> -/* GCC is awesome. */
>> +/* GCC leaves me speechless. */
>
>A speechless Rusty would be horrible. That said, it would be nice if the
>comment was something more like the normal Rusty pearl of wisdom. I
>understand the first part, but have no idea was + sizeof(typeof(int[....
>does...
IOCCC entry candidate.
Simple. !!__builtin_types_compatible_p, as it sounds, will return 1 if
both types are compatible. If they are, then '1-2*!!_builtin..' will
produce -1, then - surprisingly - sizeof(typeof(int[-1])) seems strange to
me and should throw the error.
If the types are not compatible, compat_p returns 0. !! will turn it into
0. 2* will turn it into 0. 1-0 is 1. sizeof(typeof(int[1])) is valid, and
*0 will compile it away.
So in case they _ARE_ compatible, we get the compile error, as far as I
can see it. There's a ! too much in the !!_builtin line. Now we know why
such patches are dangerous.
What's more, a test program does not even fail when the types are
incompatible. (Or it's also wrong):
#include <stdio.h>
struct foo {
int x, y, z;
};
struct bar {
const char *fol;
};
#define c(x, y) \
sizeof(typeof(int[1 - \
2*!!__builtin_types_compatible_p(typeof(x), typeof(y))]))
int main(void)
{
printf("%d\n", c(struct foo *, struct bar *));
printf("%d\n", c(int*, int*));
}
Both printf()s throw a compile time error.
Jan
--
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-10 14:01 ` Jan Engelhardt
@ 2007-03-10 15:18 ` Andreas Schwab
2007-03-10 16:53 ` Jan Engelhardt
0 siblings, 1 reply; 29+ messages in thread
From: Andreas Schwab @ 2007-03-10 15:18 UTC (permalink / raw)
To: Jan Engelhardt
Cc: Nigel Cunningham, Valdis.Kletnieks, Rusty Russell,
lkml - Kernel Mailing List, Linus Torvalds, Andrew Morton
Jan Engelhardt <jengelh@linux01.gwdg.de> writes:
> So in case they _ARE_ compatible, we get the compile error, as far as I
> can see it. There's a ! too much in the !!_builtin line.
The error case is when the types are compatible. That means that the
argument is in fact _not_ an array.
Andreas.
--
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-10 15:18 ` Andreas Schwab
@ 2007-03-10 16:53 ` Jan Engelhardt
0 siblings, 0 replies; 29+ messages in thread
From: Jan Engelhardt @ 2007-03-10 16:53 UTC (permalink / raw)
To: Andreas Schwab
Cc: Nigel Cunningham, Valdis.Kletnieks, Rusty Russell,
lkml - Kernel Mailing List, Linus Torvalds, Andrew Morton
On Mar 10 2007 16:18, Andreas Schwab wrote:
>Jan Engelhardt <jengelh@linux01.gwdg.de> writes:
>
>> So in case they _ARE_ compatible, we get the compile error, as far as I
>> can see it. There's a ! too much in the !!_builtin line.
>
>The error case is when the types are compatible. That means that
>the argument is in fact _not_ an array.
That one should go in as a comment for that macro.
Jan
--
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-09 17:47 ` Linus Torvalds
@ 2007-03-10 20:36 ` Trent Piepho
0 siblings, 0 replies; 29+ messages in thread
From: Trent Piepho @ 2007-03-10 20:36 UTC (permalink / raw)
To: Linus Torvalds
Cc: Christoph Hellwig, Rusty Russell, lkml - Kernel Mailing List,
Andrew Morton
On Fri, 9 Mar 2007, Linus Torvalds wrote:
> On Fri, 9 Mar 2007, Christoph Hellwig wrote:
> Well, since Rusty's macro was hoddible *anyway*, I don't think I'd apply
> it as-is. Breaking icc for something that ugly and not-very-important
> simply makes no sense.
>
> There are better ways to do this.
>
> For one, you could (and should!) abstract these kinds of things out,
> rather than put them in another macro that really does something totally
> different. Then, the macro could have become
>
> #define ARRAY_SIZE (sizeof_expression + 0*error_if_not_array)
/* Error if X is a pointer, 0 otherwise */
#define ERROR_IF_POINTER(x) \
sizeof(int[-__builtin_types_compatible_p(typeof(x), typeof(&x[0]))])
/* Warning (div by zero) if x is a pointer, 0 otherwise */
#define WARN_IF_POINTER(x) \
(0/!__builtin_types_compatible_p(typeof(x), typeof(&x[0])))
The gcc docs say __builtin_types_compatible_p returns 1 or 0, so the !!
isn't necessary. And my gcc at least returns 0 for sizeof(int[0]).
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-10 1:04 ` Jan Engelhardt
@ 2007-03-11 2:50 ` Rusty Russell
2007-03-11 2:58 ` Jan Engelhardt
2007-03-11 21:40 ` Valdis.Kletnieks
0 siblings, 2 replies; 29+ messages in thread
From: Rusty Russell @ 2007-03-11 2:50 UTC (permalink / raw)
To: Jan Engelhardt
Cc: lkml - Kernel Mailing List, Linus Torvalds, Andrew Morton,
Andi Kleen
On Sat, 2007-03-10 at 02:04 +0100, Jan Engelhardt wrote:
> Getting back at the macro, how would you like to have it merged?
Well, this is what I sent to Linus and Andrew (many thanks to those who
made appropriately whimsical *or* useful comments):
diff -r 1ccdf46b0f41 include/linux/compiler-gcc.h
--- a/include/linux/compiler-gcc.h Sat Mar 10 09:55:29 2007 +1100
+++ b/include/linux/compiler-gcc.h Sat Mar 10 11:06:35 2007 +1100
@@ -22,6 +22,9 @@
__asm__ ("" : "=r"(__ptr) : "0"(ptr)); \
(typeof(ptr)) (__ptr + (off)); })
+/* &a[0] degrades to a pointer: a different type from an array */
+#define __must_be_array(a) \
+ BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0])))
#define inline inline __attribute__((always_inline))
#define __inline__ __inline__ __attribute__((always_inline))
diff -r 1ccdf46b0f41 include/linux/compiler-intel.h
--- a/include/linux/compiler-intel.h Sat Mar 10 09:55:29 2007 +1100
+++ b/include/linux/compiler-intel.h Sat Mar 10 11:06:25 2007 +1100
@@ -21,4 +21,7 @@
__ptr = (unsigned long) (ptr); \
(typeof(ptr)) (__ptr + (off)); })
+/* Intel ECC compiler doesn't support __builtin_types_compatible_p() */
+#define __must_be_array(a) 0
+
#endif
diff -r 1ccdf46b0f41 include/linux/kernel.h
--- a/include/linux/kernel.h Sat Mar 10 09:55:29 2007 +1100
+++ b/include/linux/kernel.h Sat Mar 10 11:06:16 2007 +1100
@@ -35,7 +35,8 @@ extern const char linux_proc_banner[];
#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
+
#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
#define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-11 2:50 ` Rusty Russell
@ 2007-03-11 2:58 ` Jan Engelhardt
2007-03-11 3:19 ` Rusty Russell
2007-03-11 21:40 ` Valdis.Kletnieks
1 sibling, 1 reply; 29+ messages in thread
From: Jan Engelhardt @ 2007-03-11 2:58 UTC (permalink / raw)
To: Rusty Russell
Cc: lkml - Kernel Mailing List, Linus Torvalds, Andrew Morton,
Andi Kleen
On Mar 11 2007 13:50, Rusty Russell wrote:
>On Sat, 2007-03-10 at 02:04 +0100, Jan Engelhardt wrote:
>> Getting back at the macro, how would you like to have it merged?
>
>Well, this is what I sent to Linus and Andrew (many thanks to those who
>made appropriately whimsical *or* useful comments):
>
>diff -r 1ccdf46b0f41 include/linux/compiler-gcc.h
>--- a/include/linux/compiler-gcc.h Sat Mar 10 09:55:29 2007 +1100
>+++ b/include/linux/compiler-gcc.h Sat Mar 10 11:06:35 2007 +1100
>@@ -22,6 +22,9 @@
> __asm__ ("" : "=r"(__ptr) : "0"(ptr)); \
> (typeof(ptr)) (__ptr + (off)); })
>
>+/* &a[0] degrades to a pointer: a different type from an array */
>+#define __must_be_array(a) \
>+ BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0])))
This looks _much_ nicer! (And BUILD_BUG_ON is also appropriately
commented.)
>
> #define inline inline __attribute__((always_inline))
> #define __inline__ __inline__ __attribute__((always_inline))
>diff -r 1ccdf46b0f41 include/linux/compiler-intel.h
>--- a/include/linux/compiler-intel.h Sat Mar 10 09:55:29 2007 +1100
>+++ b/include/linux/compiler-intel.h Sat Mar 10 11:06:25 2007 +1100
>@@ -21,4 +21,7 @@
> __ptr = (unsigned long) (ptr); \
> (typeof(ptr)) (__ptr + (off)); })
>
>+/* Intel ECC compiler doesn't support __builtin_types_compatible_p() */
>+#define __must_be_array(a) 0
>+
> #endif
>diff -r 1ccdf46b0f41 include/linux/kernel.h
>--- a/include/linux/kernel.h Sat Mar 10 09:55:29 2007 +1100
>+++ b/include/linux/kernel.h Sat Mar 10 11:06:16 2007 +1100
>@@ -35,7 +35,8 @@ extern const char linux_proc_banner[];
> #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
> #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
>
>-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
>+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
>+
> #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
> #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
> #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
80 cols *cough* :)
Jan
--
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-11 2:58 ` Jan Engelhardt
@ 2007-03-11 3:19 ` Rusty Russell
0 siblings, 0 replies; 29+ messages in thread
From: Rusty Russell @ 2007-03-11 3:19 UTC (permalink / raw)
To: Jan Engelhardt
Cc: lkml - Kernel Mailing List, Linus Torvalds, Andrew Morton,
Andi Kleen
On Sun, 2007-03-11 at 03:58 +0100, Jan Engelhardt wrote:
> >-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
> >+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
> >+
> 80 cols *cough* :)
I think your cough added a column?
Rusty.
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-10 4:24 ` Randy Dunlap
@ 2007-03-11 15:56 ` Valdis.Kletnieks
0 siblings, 0 replies; 29+ messages in thread
From: Valdis.Kletnieks @ 2007-03-11 15:56 UTC (permalink / raw)
To: Randy Dunlap
Cc: Rusty Russell, lkml - Kernel Mailing List, Linus Torvalds,
Andrew Morton
[-- Attachment #1: Type: text/plain, Size: 449 bytes --]
On Fri, 09 Mar 2007 20:24:42 PST, Randy Dunlap said:
> On Fri, 09 Mar 2007 23:03:05 -0500 Valdis.Kletnieks@vt.edu wrote:
> > -/* GCC is awesome. */
> > +/* GCC leaves me speechless. */
>
> "awesome" can mean "inspiring awe or admiration or wonder" (amazing)
> or it can mean "awful" (as in terrifying). 8)
And as those who know me well will attest, it takes something well down the
road of either definition to render me actually speechless.. :)
[-- Attachment #2: Type: application/pgp-signature, Size: 226 bytes --]
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH] Use more gcc extensions in the Linux headers
2007-03-11 2:50 ` Rusty Russell
2007-03-11 2:58 ` Jan Engelhardt
@ 2007-03-11 21:40 ` Valdis.Kletnieks
1 sibling, 0 replies; 29+ messages in thread
From: Valdis.Kletnieks @ 2007-03-11 21:40 UTC (permalink / raw)
To: Rusty Russell
Cc: Jan Engelhardt, lkml - Kernel Mailing List, Linus Torvalds,
Andrew Morton, Andi Kleen
[-- Attachment #1: Type: text/plain, Size: 268 bytes --]
On Sun, 11 Mar 2007 13:50:37 +1100, Rusty Russell said:
> Well, this is what I sent to Linus and Andrew (many thanks to those who
> made appropriately whimsical *or* useful comments):
Ahh.. much better - it's now a form that even I can get my brain wrapped around :)
[-- Attachment #2: Type: application/pgp-signature, Size: 226 bytes --]
^ permalink raw reply [flat|nested] 29+ messages in thread
end of thread, other threads:[~2007-03-11 21:41 UTC | newest]
Thread overview: 29+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-03-09 5:56 [PATCH] Use more gcc extensions in the Linux headers Rusty Russell
2007-03-09 6:22 ` Stephen Rothwell
2007-03-09 6:35 ` Linus Torvalds
2007-03-09 6:50 ` Andrey Panin
2007-03-09 7:53 ` Christoph Hellwig
2007-03-09 9:02 ` Andrey Panin
2007-03-09 9:05 ` Christoph Hellwig
2007-03-09 17:47 ` Linus Torvalds
2007-03-10 20:36 ` Trent Piepho
2007-03-09 7:52 ` Christoph Hellwig
2007-03-09 8:35 ` Jarek Poplawski
2007-03-09 11:27 ` Theodore Ts'o
2007-03-09 16:52 ` Andi Kleen
2007-03-09 22:57 ` Rusty Russell
2007-03-09 23:02 ` Randy Dunlap
2007-03-09 23:09 ` Robert P. J. Day
2007-03-09 23:12 ` Roland Dreier
2007-03-10 1:04 ` Jan Engelhardt
2007-03-11 2:50 ` Rusty Russell
2007-03-11 2:58 ` Jan Engelhardt
2007-03-11 3:19 ` Rusty Russell
2007-03-11 21:40 ` Valdis.Kletnieks
2007-03-10 4:03 ` Valdis.Kletnieks
2007-03-10 4:24 ` Randy Dunlap
2007-03-11 15:56 ` Valdis.Kletnieks
2007-03-10 5:19 ` Nigel Cunningham
2007-03-10 14:01 ` Jan Engelhardt
2007-03-10 15:18 ` Andreas Schwab
2007-03-10 16:53 ` Jan Engelhardt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox