Hi all, /* * Check at compile time that something is of a particular type. * Always evaluates to 1 so you may use it easily in comparisons. */ #define typecheck(type,x) \ ({ type __dummy; \ typeof(x) __dummy2; \ (void)(&__dummy == &__dummy2); \ 1; \ }) #define typecheck_fn(type,function) \ ({ typeof(type) __tmp = function; \ (void)__tmp; \ }) Can anyone help me, explain the above code typecheck. How does (void)(&__dummy == &__dummy2) evaluates to 1 I appreciate any explain. -- Regards, Sri.
Can anyone help me, explain the above code typecheck. How does (void)(&__dummy == &__dummy2) evaluates to 1
Its not this comparison getting evaluated to 1, but last expression "1; \" which is forcibly returning 1 in every case. Since it is just compile time warning and should not effect the program execution, this macro is always returning 1 at run time. To me it just valuable for coding style where you do some action only if type matches, but in real sense you take action anyways. Rajat On Mon, Jan 31, 2011 at 10:33 PM, Sri Ram Vemulpali <sri.ram.gmu06@gmail.com> wrote:
Hi all,
/* * Check at compile time that something is of a particular type. * Always evaluates to 1 so you may use it easily in comparisons. */ #define typecheck(type,x) \ ({ type __dummy; \ typeof(x) __dummy2; \ (void)(&__dummy == &__dummy2); \ 1; \ })
#define typecheck_fn(type,function) \ ({ typeof(type) __tmp = function; \ (void)__tmp; \ })
Can anyone help me, explain the above code typecheck. How does (void)(&__dummy == &__dummy2) evaluates to 1
I appreciate any explain.
-- Regards, Sri.
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
On Mon, Jan 31, 2011 at 9:03 AM, Sri Ram Vemulpali <sri.ram.gmu06@gmail.com> wrote:
Hi all,
/* * Check at compile time that something is of a particular type. * Always evaluates to 1 so you may use it easily in comparisons. */ #define typecheck(type,x) \ ({ type __dummy; \ typeof(x) __dummy2; \ (void)(&__dummy == &__dummy2); \ 1; \ })
#define typecheck_fn(type,function) \ ({ typeof(type) __tmp = function; \ (void)__tmp; \ })
Can anyone help me, explain the above code typecheck. How does (void)(&__dummy == &__dummy2) evaluates to 1
Infact I think it will never return 1, since the addresses of __dummy1 and __dummy2 have to be different (off by 4 or 8). As pointed out it is the next line that always returns 1. The purpose of this line is to throw away warnings like "Incompatible pointer comparison" or something like that (haven't tried :-)) incase there is a mismatch. -- Thanks - Manish
Thanks for all explanation. It really helped to understand. Sri On Mon, Jan 31, 2011 at 1:03 PM, Manish Katiyar <mkatiyar@gmail.com> wrote:
On Mon, Jan 31, 2011 at 9:03 AM, Sri Ram Vemulpali <sri.ram.gmu06@gmail.com> wrote:
Hi all,
/* * Check at compile time that something is of a particular type. * Always evaluates to 1 so you may use it easily in comparisons. */ #define typecheck(type,x) \ ({ type __dummy; \ typeof(x) __dummy2; \ (void)(&__dummy == &__dummy2); \ 1; \ })
#define typecheck_fn(type,function) \ ({ typeof(type) __tmp = function; \ (void)__tmp; \ })
Can anyone help me, explain the above code typecheck. How does (void)(&__dummy == &__dummy2) evaluates to 1
Infact I think it will never return 1, since the addresses of __dummy1 and __dummy2 have to be different (off by 4 or 8). As pointed out it is the next line that always returns 1. The purpose of this line is to throw away warnings like "Incompatible pointer comparison" or something like that (haven't tried :-)) incase there is a mismatch.
-- Thanks - Manish
-- Regards, Sri.
Hi Sri, On Mon, Jan 31, 2011 at 9:03 AM, Sri Ram Vemulpali <sri.ram.gmu06@gmail.com> wrote:
Hi all,
/* * Check at compile time that something is of a particular type. * Always evaluates to 1 so you may use it easily in comparisons. */ #define typecheck(type,x) \ ({ type __dummy; \ typeof(x) __dummy2; \ (void)(&__dummy == &__dummy2); \ 1; \ })
#define typecheck_fn(type,function) \ ({ typeof(type) __tmp = function; \ (void)__tmp; \ })
Can anyone help me, explain the above code typecheck. How does (void)(&__dummy == &__dummy2) evaluates to 1
I appreciate any explain.
If dummy and dummy2 are of different types, then when you try and do a pointer comparison (&dummy == &dummy2) it will produce a compiler warning/error. The actual comparison will always fail, but it doesn't matter since the results aren't used. typecheck always returns 1. Dave Hylands
If dummy and dummy2 are of different types, then when you try and do a pointer comparison (&dummy == &dummy2) it will produce a compiler warning/error.
O--ooohhh... I came across this ages ago when I was trying to unravel the jiffies wraparound macros and I've been scratching my head ever since trying to figure out what this line _really_ did. A bit arcane, isn't it? Thanks a lot Dave and everyone for explaining. Cheers Julie
participants (5)
-
Dave Hylands -
julie Sullivan -
Manish Katiyar -
Rajat Sharma -
Sri Ram Vemulpali